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

The following examples show how to use org.fxmisc.undo.UndoManagerFactory#unlimitedHistorySingleChangeUM() . 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 testSingleChangeUndoInvertsTheChange() {
    EventSource<Integer> changes = new EventSource<>();
    Var<Integer> lastAction = Var.newSimpleVar(null);
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            changes, i -> -i, i -> { lastAction.setValue(i); changes.push(i); });

    changes.push(3);
    changes.push(7);
    assertNull(lastAction.getValue());

    um.undo();
    assertEquals(-7, lastAction.getValue().intValue());

    um.undo();
    assertEquals(-3, lastAction.getValue().intValue());

    um.redo();
    assertEquals(3, lastAction.getValue().intValue());

    um.redo();
    assertEquals(7, lastAction.getValue().intValue());
}
 
Example 2
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testPushedNonIdentitySingleChangeIsStored() {
    SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            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(4);
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertEquals(-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 testPushedIdentitySingleChangeIsNotStored() {
    SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            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

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

    // test that pushed identity change is not stored
    changes.push(0);
    assertFalse(um.isUndoAvailable());
    assertEquals(-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 testMergeResultingInNonIdentitySingleChangeStoresMergeAndPreventsNextMerge() {
    SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            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(1);
    changes.push(2);
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertFalse(um.isUndoAvailable());
    assertEquals(-3, lastAppliedValue.get());

    um.redo(); // redo to test whether merge occurs on next push
    changes.push(5);
    assertTrue(um.isUndoAvailable());
    um.undo();
    assertEquals(-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 testPositionValidAfterAddingAChange() {
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(changes, c -> c, changes::push);

    changes.push(1);
    UndoPosition pos = um.getCurrentPosition();
    changes.push(1);
    assertTrue(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 testPositionInvalidAfterSingleChangeMerge() {
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            changes, c -> -c, changes::push, (c1, c2) -> Optional.of(c1 + c2));

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

    changes.push(1);
    changes.push(-1);
    assertFalse(um.isRedoAvailable());
}
 
Example 8
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testFailFastWhenExpectedChangeNotReceived() {
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            changes, i -> -i, i -> {});

    changes.push(1);

    um.undo(); // should throw because the undone change is not received back
}
 
Example 9
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testMergeResultingInIdentitySingleChangeAnnihilatesBothAndPreventsNextMerge() {
    SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.unlimitedHistorySingleChangeUM(
            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

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

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

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

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