org.fxmisc.undo.UndoManager Java Examples
The following examples show how to use
org.fxmisc.undo.UndoManager.
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 |
@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 #2
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #3
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #4
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #5
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #6
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
/** * 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 #7
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #8
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #9
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #10
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 6 votes |
@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 #11
Source File: UndoUtils.java From RichTextFX with BSD 2-Clause "Simplified" License | 6 votes |
/** * 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 {@code preventMergeDelay}. * <p><b>Note</b>: that <u>only styling changes</u> may occur <u>during suspension</u> of the undo manager. */ public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextSuspendableUndoManager( GenericStyledArea<PS, SEG, S> area, Duration preventMergeDelay, SuspendableYes suspendUndo) { RichTextChange.skipStyleComparison( true ); return new MultiChangeUndoManagerImpl<> ( new UnlimitedChangeQueue<>(), TextChange::invert, applyMultiRichTextChange(area), TextChange::mergeWith, TextChange::isIdentity, area.multiRichChanges().conditionOn(suspendUndo), preventMergeDelay ); }
Example #12
Source File: BaseCodeArea.java From jmonkeybuilder with Apache License 2.0 | 6 votes |
/** * Reload the content. * * @param content the content. * @param clearHistory true if need to clear history. */ @FxThread public void reloadContent(@NotNull final String content, final boolean clearHistory) { final String currentContent = getText(); if (!StringUtils.equals(currentContent, content)) { if (content.isEmpty()) { try { clear(); } catch (final IllegalStateException e) { //FIXME it's a bug in the richfxeditor library e.printStackTrace(); } } else { replaceText(0, currentContent.length(), content); } } if (clearHistory) { final UndoManager undoManager = getUndoManager(); undoManager.forgetHistory(); } }
Example #13
Source File: UndoUtils.java From RichTextFX with BSD 2-Clause "Simplified" License | 5 votes |
/** * 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: UndoUtils.java From RichTextFX with BSD 2-Clause "Simplified" License | 5 votes |
/** * Constructs an UndoManager with no history */ public static UndoManager noOpUndoManager() { return new UndoManager() { private final Val<Boolean> alwaysFalse = Val.constant(false); @Override public boolean undo() { return false; } @Override public boolean redo() { return false; } @Override public Val<Boolean> undoAvailableProperty() { return alwaysFalse; } @Override public boolean isUndoAvailable() { return false; } @Override public Val<Boolean> redoAvailableProperty() { return alwaysFalse; } @Override public boolean isRedoAvailable() { return false; } @Override public boolean isPerformingAction() { return false; } @Override public boolean isAtMarkedPosition() { return false; } // not sure whether these may throw NPEs at some point @Override public Val nextUndoProperty() { return null; } @Override public Val nextRedoProperty() { return null; } @Override public ObservableBooleanValue performingActionProperty() { return null; } @Override public UndoPosition getCurrentPosition() { return null; } @Override public ObservableBooleanValue atMarkedPositionProperty() { return null; } // ignore these @Override public void preventMerge() { } @Override public void forgetHistory() { } @Override public void close() { } }; }
Example #15
Source File: UndoUtils.java From RichTextFX with BSD 2-Clause "Simplified" License | 5 votes |
/** * 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 #16
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #17
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #18
Source File: BaseCodeArea.java From jmonkeybuilder with Apache License 2.0 | 5 votes |
/** * Load the content. * * @param content the content. */ @FxThread public void loadContent(@NotNull final String content) { appendText(content); final UndoManager undoManager = getUndoManager(); undoManager.forgetHistory(); }
Example #19
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #20
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #21
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 |
@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 #23
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #24
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #25
Source File: UndoManagerTest.java From UndoFX with BSD 2-Clause "Simplified" License | 5 votes |
@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 #26
Source File: FileEditor.java From markdown-writer-fx with BSD 2-Clause "Simplified" License | 4 votes |
private void activated() { if( tab.getTabPane() == null || !tab.isSelected()) return; // tab is already closed or no longer active if (tab.getContent() != null) { reload(); updatePreviewType(); markdownEditorPane.requestFocus(); return; } // load file and create UI when the tab becomes visible the first time markdownEditorPane = new MarkdownEditorPane(); markdownPreviewPane = new MarkdownPreviewPane(); markdownEditorPane.pathProperty().bind(path); load(); // clear undo history after first load markdownEditorPane.getUndoManager().forgetHistory(); // bind preview to editor markdownPreviewPane.pathProperty().bind(pathProperty()); markdownPreviewPane.markdownTextProperty().bind(markdownEditorPane.markdownTextProperty()); markdownPreviewPane.markdownASTProperty().bind(markdownEditorPane.markdownASTProperty()); markdownPreviewPane.editorSelectionProperty().bind(markdownEditorPane.selectionProperty()); markdownPreviewPane.scrollYProperty().bind(markdownEditorPane.scrollYProperty()); // bind properties readOnly.bind(markdownEditorPane.readOnlyProperty()); // bind the editor undo manager to the properties UndoManager<?> undoManager = markdownEditorPane.getUndoManager(); modified.bind(Bindings.not(undoManager.atMarkedPositionProperty())); canUndo.bind(undoManager.undoAvailableProperty()); canRedo.bind(undoManager.redoAvailableProperty()); splitPane = new SplitPane(markdownEditorPane.getNode()); if (getPreviewType() != MarkdownPreviewPane.Type.None) splitPane.getItems().add(markdownPreviewPane.getNode()); tab.setContent(splitPane); updatePreviewType(); markdownEditorPane.requestFocus(); // update 'editor' property editor.set(markdownEditorPane); }
Example #27
Source File: GenericStyledArea.java From RichTextFX with BSD 2-Clause "Simplified" License | 4 votes |
/** * @param undoManager may be null in which case a no op undo manager will be set. */ @Override public void setUndoManager(UndoManager undoManager) { this.undoManager.close(); this.undoManager = undoManager != null ? undoManager : UndoUtils.noOpUndoManager(); }
Example #28
Source File: UndoUtils.java From RichTextFX with BSD 2-Clause "Simplified" License | 4 votes |
/** * Constructs an UndoManager with an unlimited history: * if {@link GenericStyledArea#isPreserveStyle() the area's preserveStyle flag is true}, the returned UndoManager * can undo/redo multiple {@link RichTextChange}s; otherwise, it can undo/redo multiple {@link PlainTextChange}s. */ public static <PS, SEG, S> UndoManager defaultUndoManager(GenericStyledArea<PS, SEG, S> area) { return area.isPreserveStyle() ? richTextUndoManager(area) : plainTextUndoManager(area); }
Example #29
Source File: MarkdownEditorPane.java From markdown-writer-fx with BSD 2-Clause "Simplified" License | 4 votes |
public UndoManager<?> getUndoManager() { return textArea.getUndoManager(); }
Example #30
Source File: UndoUtils.java From RichTextFX with BSD 2-Clause "Simplified" License | 2 votes |
/** * 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); }