org.reactfx.EventSource Java Examples

The following examples show how to use org.reactfx.EventSource. 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 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 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 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 #6
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 #7
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 #8
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 #9
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 #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 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 #12
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 #13
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 #14
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 #15
Source File: SimplePopups.java    From pmd-designer with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Show a transient popup with a message, to let the user know an action
 * was performed.
 *
 * @param owner Node next to which the popup will be shown
 * @return
 */
public static EventStream<?> showActionFeedback(@NonNull Node owner,
                                                @Nullable Node graphic,
                                                @NonNull String message,
                                                double offsetX,
                                                boolean stick,
                                                String... cssClasses) {

    Popup popup = new Popup();
    Label label = new Label(message, graphic);
    StackPane pane = new StackPane();

    DesignerUtil.addCustomStyleSheets(pane, "designer");
    pane.getStyleClass().addAll("action-feedback");
    pane.getStyleClass().addAll(cssClasses);

    pane.getChildren().addAll(label);
    popup.getContent().addAll(pane);

    Animation fadeTransition = stick ? fadeInAnimation(pane) : bounceFadeAnimation(pane);
    EventSource<?> closeTick = new EventSource<>();
    if (stick) {
        pane.setOnMouseClicked(evt -> {
            popup.hide();
            closeTick.push(null);
        });
    } else {
        fadeTransition.setOnFinished(e -> {
            popup.hide();
            closeTick.push(null);
        });
    }

    popup.setOnShowing(e -> fadeTransition.play());

    Bounds screenBounds = owner.localToScreen(owner.getBoundsInLocal());
    popup.show(owner, screenBounds.getMaxX() + offsetX, screenBounds.getMinY());
    return closeTick;
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: AstTreeView.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public AstTreeView(@NamedArg("designerRoot") DesignerRoot root) {
    designerRoot = root;
    baseSelectionEvents = new EventSource<>();
    suppressibleSelectionEvents = baseSelectionEvents.suppressible();

    getStyleClass().addAll("ast-tree-view");

    initNodeSelectionHandling(root, suppressibleSelectionEvents, false);

    // this needs to be done even if the selection originates from this node
    EventStreams.changesOf(getSelectionModel().selectedItemProperty())
                .subscribe(item -> highlightFocusNodeParents((ASTTreeItem) item.getOldValue(), (ASTTreeItem) item.getNewValue()));

    // push a node selection event whenever...
    //  * The selection changes
    EventStreams.valuesOf(getSelectionModel().selectedItemProperty())
                .filterMap(Objects::nonNull, TreeItem::getValue)
                .map(NodeSelectionEvent::of)
                .subscribe(baseSelectionEvents::push);

    //  * the currently selected cell is explicitly clicked
    setCellFactory(tv -> new ASTTreeCell(getDesignerRoot(), n -> {
        ASTTreeItem selectedTreeItem = (ASTTreeItem) getSelectionModel().getSelectedItem();

        // only push an event if the node was already selected
        if (selectedTreeItem != null && selectedTreeItem.getValue() != null
            && selectedTreeItem.getValue().equals(n)) {
            baseSelectionEvents.push(NodeSelectionEvent.of(n));
        }
    }));


    EventStreams.valuesOf(additionalStyleClasses)
                .repeatOn(EventStreams.valuesOf(rootProperty()))
                .subscribe(fun -> {
                    TreeItem<Node> rootNode = getRoot();
                    if (rootNode != null && fun != null) {
                        ((ASTTreeItem) rootNode).foreach(it -> ((ASTTreeItem) it).setStyleClasses(fun.apply(it.getValue())));
                    }
                });

}