Java Code Examples for org.fxmisc.undo.UndoManagerFactory#unlimitedHistoryMultiChangeUM()

The following examples show how to use org.fxmisc.undo.UndoManagerFactory#unlimitedHistoryMultiChangeUM() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testMultiChangeUndoInvertsTheChangesAndReversesTheList() {
    EventSource<List<Integer>> changes = new EventSource<>();
    Var<List<Integer>> lastChange = Var.newSimpleVar(null);
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes, i -> -i, i -> { lastChange.setValue(i); changes.push(i); });

    changes.push(list(3, 7));
    assertNull(lastChange.getValue());

    um.undo();
    assertEquals(list(-7, -3), lastChange.getValue());

    um.redo();
    assertEquals(list(3, 7), lastChange.getValue());
}
 
Example 2
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testPushedNonIdentityMultiChangeIsStored() {
    SimpleObjectProperty<List<Integer>> lastAppliedValue = new SimpleObjectProperty<>();
    EventSource<List<Integer>> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes,
            i -> -i,    // invert
            i -> { lastAppliedValue.set(i); changes.push(i); }, // apply redo and re-emit value so expected change is received
            (a, b) -> Optional.of(a + b), // merge adds two changes together
            i -> i == 0); // identity change = 0

    changes.push(list(4, 5));
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertEquals(list(-5, -4), lastAppliedValue.get());
    assertFalse(um.isUndoAvailable());
}
 
Example 3
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testPushedIdentityMultiChangeIsNotStored() {
    SimpleObjectProperty<List<Integer>> lastAppliedValue = new SimpleObjectProperty<>();
    EventSource<List<Integer>> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes,
            i -> -i,    // invert
            i -> { lastAppliedValue.set(i); changes.push(i); }, // apply redo and re-emit value so expected change is received
            (a, b) -> Optional.of(a + b), // merge adds two changes together
            i -> i == 0); // identity change = 0

    // force lastAppliedValue to store non-zero value
    changes.push(list(4, 8));
    um.undo();

    // test that pushed identity change is not stored
    changes.push(list(0, 0));
    assertFalse(um.isUndoAvailable());
    assertEquals(list(-8, -4), lastAppliedValue.get());
}
 
Example 4
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testMergeResultingInNonIdentityMultiChangeStoresMergeAndPreventsNextMerge() {
    SimpleObjectProperty<List<Integer>> lastAppliedValue = new SimpleObjectProperty<>();
    EventSource<List<Integer>> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes,
            i -> -i,    // invert
            i -> { lastAppliedValue.set(i); changes.push(i); }, // apply change and re-emit value so expected change is received
            (a, b) -> Optional.of(a + b), // merge adds two changes together
            i -> i == 0); // identity change = 0

    changes.push(list(1, 4));
    changes.push(list(2, 5));
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertFalse(um.isUndoAvailable());
    assertEquals(list(-9, -3), lastAppliedValue.get());

    um.redo(); // redo to test whether merge occurs on next push
    changes.push(list(5, 8));
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertEquals(list(-8, -5), lastAppliedValue.get());
}
 
Example 5
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testPositionInvalidAfterMultiChangeMerge() {
    EventSource<List<Integer>> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes, c -> -c, changes::push, (c1, c2) -> Optional.of(c1 + c2));

    changes.push(list(1));
    UndoManager.UndoPosition pos = um.getCurrentPosition();
    changes.push(list(1));
    assertFalse(pos.isValid());
}
 
Example 6
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testRedoUnavailableAfterMultiChangeAnnihilation() {
    EventSource<List<Integer>> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes, c -> -c, changes::push, (c1, c2) -> Optional.of(c1 + c2), c -> c == 0);

    changes.push(list(1, 2, 3));
    changes.push(list(-1, -2, -3));
    assertFalse(um.isRedoAvailable());
}
 
Example 7
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testMergeResultingInIdentityMultiChangeAnnihilatesBothAndPreventsNextMerge() {
    SimpleObjectProperty<List<Integer>> lastAppliedValue = new SimpleObjectProperty<>();
    EventSource<List<Integer>> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistoryMultiChangeUM(
            changes,
            i -> -i,    // invert
            i -> { lastAppliedValue.set(i); changes.push(i); }, // apply redo and re-emit value so expected change is received
            (a, b) -> Optional.of(a + b), // merge adds two changes together
            i -> i == 0); // identity change = 0

    // have at least one change stored
    changes.push(list(6, 9));
    // prevent next merge from occurring
    um.preventMerge();

    // now push the identity-resulting merge changes
    changes.push(list(-3, -4));   // change A
    changes.push(list(3, 4));    // change B

    // changes should annihilate; neither are stored
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertFalse(um.isUndoAvailable());
    assertEquals(list(-9, -6), lastAppliedValue.get());

    um.redo(); // redo to test whether merge occurs on next push
    changes.push(list(3, 4));
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertTrue(um.isUndoAvailable());
    assertEquals(list(-4, -3), lastAppliedValue.get());
}