org.fxmisc.undo.UndoManagerFactory Java Examples

The following examples show how to use org.fxmisc.undo.UndoManagerFactory. 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: LyricsTextArea.java    From Quelea with GNU General Public License v3.0 6 votes vote down vote up
public LyricsTextArea() {
    textArea = new InlineCssTextArea();
    ContextMenu contextMenu = new ContextMenu();
    Clipboard systemClipboard = Clipboard.getSystemClipboard();
    MenuItem paste = new MenuItem(LabelGrabber.INSTANCE.getLabel("paste.label"));
    contextMenu.setOnShown(e -> {
        paste.setDisable(!systemClipboard.hasContent(DataFormat.PLAIN_TEXT));
    });

    paste.setOnAction(e -> {
        String clipboardText = systemClipboard.getString();
        textArea.insertText(textArea.getCaretPosition(), clipboardText);
    });

    contextMenu.getItems().add(paste);
    textArea.setContextMenu(contextMenu);
    textArea.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
        Platform.runLater(this::refreshStyle);
    });

    textArea.setStyle("-fx-font-family: monospace; -fx-font-size: 10pt;");
    textArea.setUndoManager(UndoManagerFactory.zeroHistorySingleChangeUM(textArea.richChanges()));
    getChildren().add(new VirtualizedScrollPane<>(textArea));
    textArea.getStyleClass().add("text-area");
}
 
Example #2
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 #3
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 #4
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testMark() {
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.fixedSizeHistorySingleChangeUM(
            changes, c -> c, changes::push, 4);

    assertTrue(um.atMarkedPositionProperty().get());
    changes.push(1);
    assertFalse(um.atMarkedPositionProperty().get());
    changes.push(2);
    um.mark();
    assertTrue(um.atMarkedPositionProperty().get());
    changes.push(3);
    changes.push(4);
    assertFalse(um.atMarkedPositionProperty().get());
    um.undo();
    um.undo();
    assertTrue(um.atMarkedPositionProperty().get());
    changes.push(3);
    changes.push(4);
    changes.push(5); // overflow
    changes.push(6);
    assertFalse(um.atMarkedPositionProperty().get());
}
 
Example #5
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 #6
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 #7
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 #8
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Tests that isAtMarkedPosition() forces atMarkedPositionProperty()
 * become valid.
 */
@Test
public void testAtMarkedPositionRevalidation() {
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.zeroHistorySingleChangeUM(changes);

    um.atMarkedPositionProperty().get(); // atMarkedPositionProperty is now valid

    // we are going to expect two invalidations
    CountDownLatch latch = new CountDownLatch(2);
    um.atMarkedPositionProperty().addListener(observable -> latch.countDown());

    changes.push(1); // atMarkedPositionProperty has been invalidated
    assertEquals(1, latch.getCount());

    um.isAtMarkedPosition(); // we want to test whether this caused revalidation of atMarkedPositionProperty

    changes.push(2); // should have caused invalidation of atMarkedPositionProperty
    assertEquals(0, latch.getCount());
}
 
Example #9
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 #10
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 #11
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 #12
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an UndoManager that can undo/redo {@link PlainTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@code preventMergeDelay}
 */
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
        GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory, Duration preventMergeDelay) {
    return factory.createMultiChangeUM(area.multiPlainChanges(),
            TextChange::invert,
            applyMultiPlainTextChange(area),
            TextChange::mergeWith,
            TextChange::isIdentity,
            preventMergeDelay);
}
 
Example #13
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an UndoManager that can undo/redo {@link RichTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@code preventMergeDelay}
 */
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
        GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory, Duration preventMergeDelay) {
    return factory.createMultiChangeUM(area.multiRichChanges(),
            TextChange::invert,
            applyMultiRichTextChange(area),
            TextChange::mergeWith,
            TextChange::isIdentity,
            preventMergeDelay);
}
 
Example #14
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());
}
 
Example #15
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());
}
 
Example #16
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 #17
Source File: UndoManagerTest.java    From UndoFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void zeroHistoryUndoManagerMark() {
    EventSource<Integer> changes = new EventSource<>();
    UndoManager<?> um = UndoManagerFactory.zeroHistorySingleChangeUM(changes);

    assertTrue(um.atMarkedPositionProperty().get());
    changes.push(1);
    assertFalse(um.atMarkedPositionProperty().get());
    changes.push(2);
    um.mark();
    assertTrue(um.atMarkedPositionProperty().get());
    changes.push(3);
    changes.push(4);
    assertFalse(um.atMarkedPositionProperty().get());
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
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 #23
Source File: PreviewStyledTextArea.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
PreviewStyledTextArea() {
	setEditable(false);
	setFocusTraversable(false);
	getStyleClass().add("padding");
	setUndoManager(UndoManagerFactory.zeroHistorySingleChangeUM(richChanges()));

	updateFont();

	optionsListener = e -> updateFont();
	WeakInvalidationListener weakOptionsListener = new WeakInvalidationListener(optionsListener);
	Options.fontFamilyProperty().addListener(weakOptionsListener);
	Options.fontSizeProperty().addListener(weakOptionsListener);
}
 
Example #24
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Returns an UndoManager with an unlimited history that can undo/redo {@link RichTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@link #DEFAULT_PREVENT_MERGE_DELAY}
 */
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
        GenericStyledArea<PS, SEG, S> area) {
    return richTextUndoManager(area, UndoManagerFactory.unlimitedHistoryFactory());
}
 
Example #25
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Returns an UndoManager that can undo/redo {@link RichTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@code preventMergeDelay}
 */
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
        GenericStyledArea<PS, SEG, S> area, Duration preventMergeDelay) {
    return richTextUndoManager(area, UndoManagerFactory.unlimitedHistoryFactory(), preventMergeDelay);
}
 
Example #26
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Returns an UndoManager that can undo/redo {@link RichTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@link #DEFAULT_PREVENT_MERGE_DELAY}
 */
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
        GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory) {
    return richTextUndoManager(area, factory, DEFAULT_PREVENT_MERGE_DELAY);
}
 
Example #27
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Returns an UndoManager that can undo/redo {@link PlainTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@code preventMergeDelay}
 */
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
        GenericStyledArea<PS, SEG, S> area, Duration preventMergeDelay) {
    return plainTextUndoManager(area, UndoManagerFactory.unlimitedHistoryFactory(), preventMergeDelay);
}
 
Example #28
Source File: UndoUtils.java    From RichTextFX with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Returns an UndoManager that can undo/redo {@link PlainTextChange}s. New changes
 * emitted from the stream will not be merged with the previous change
 * after {@link #DEFAULT_PREVENT_MERGE_DELAY}
 */
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
        GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory) {
    return plainTextUndoManager(area, factory, DEFAULT_PREVENT_MERGE_DELAY);
}