Java Code Examples for org.reactfx.value.Var#newSimpleVar()
The following examples show how to use
org.reactfx.value.Var#newSimpleVar() .
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 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 |
@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 3
Source File: ListMapTest.java From ReactFX with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testDynamicMap() { LiveList<String> strings = new LiveArrayList<>("1", "22", "333"); Var<Function<String, Integer>> fn = Var.newSimpleVar(String::length); SuspendableList<Integer> ints = strings.mapDynamic(fn).suspendable(); assertEquals(2, ints.get(1).intValue()); ints.observeChanges(ch -> { for(ListModification<?> mod: ch) { assertEquals(Arrays.asList(1, 2, 3), mod.getRemoved()); assertEquals(Arrays.asList(1, 16, 9), mod.getAddedSubList()); } }); ints.suspendWhile(() -> { strings.set(1, "4444"); fn.setValue(s -> s.length() * s.length()); }); }
Example 4
Source File: ValAsListTest.java From ReactFX with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testNullToValChange() { Var<String> src = Var.newSimpleVar(null); LiveList<String> list = src.asList(); assertEquals(0, list.size()); List<ListModification<? extends String>> mods = new ArrayList<>(); list.observeModifications(mods::add); src.setValue("foo"); assertEquals(1, mods.size()); ListModification<? extends String> mod = mods.get(0); assertEquals(0, mod.getRemovedSize()); assertEquals(Collections.singletonList("foo"), mod.getAddedSubList()); assertEquals(1, list.size()); }
Example 5
Source File: ValAsListTest.java From ReactFX with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testValToNullChange() { Var<String> src = Var.newSimpleVar("foo"); LiveList<String> list = src.asList(); assertEquals(1, list.size()); List<ListModification<? extends String>> mods = new ArrayList<>(); list.observeModifications(mods::add); src.setValue(null); assertEquals(1, mods.size()); ListModification<? extends String> mod = mods.get(0); assertEquals(Collections.singletonList("foo"), mod.getRemoved()); assertEquals(0, mod.getAddedSize()); assertEquals(0, list.size()); }
Example 6
Source File: ValAsListTest.java From ReactFX with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testValToValChange() { Var<String> src = Var.newSimpleVar("foo"); LiveList<String> list = src.asList(); assertEquals(1, list.size()); List<ListModification<? extends String>> mods = new ArrayList<>(); list.observeModifications(mods::add); src.setValue("bar"); assertEquals(1, mods.size()); ListModification<? extends String> mod = mods.get(0); assertEquals(Collections.singletonList("foo"), mod.getRemoved()); assertEquals(Collections.singletonList("bar"), mod.getAddedSubList()); assertEquals(1, list.size()); }
Example 7
Source File: ListRangeReductionTest.java From ReactFX with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void test() { LiveList<Integer> list = new LiveArrayList<>(1, 2, 4); Var<IndexRange> range = Var.newSimpleVar(new IndexRange(0, 0)); Val<Integer> rangeSum = list.reduceRange(range, (a, b) -> a + b); assertNull(rangeSum.getValue()); List<Integer> observed = new ArrayList<>(); rangeSum.values().subscribe(sum -> { observed.add(sum); if(sum == null) { range.setValue(new IndexRange(0, 2)); } else if(sum == 3) { list.addAll(1, Arrays.asList(8, 16)); } else if(sum == 9) { range.setValue(new IndexRange(2, 4)); } }); assertEquals(Arrays.asList(null, 3, 9, 18), observed); }
Example 8
Source File: ListReductionTest.java From ReactFX with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testWhenBound() { ObservableList<Integer> list = FXCollections.observableArrayList(1, 1, 1, 1, 1); Val<Integer> sum = LiveList.reduce(list, (a, b) -> a + b); Var<Integer> lastObserved = Var.newSimpleVar(sum.getValue()); assertEquals(5, lastObserved.getValue().intValue()); sum.addListener((obs, oldVal, newVal) -> { assertEquals(lastObserved.getValue(), oldVal); lastObserved.setValue(newVal); }); list.addAll(2, Arrays.asList(2, 2)); assertEquals(9, lastObserved.getValue().intValue()); list.subList(3, 6).clear(); assertEquals(5, lastObserved.getValue().intValue()); }
Example 9
Source File: ParagraphBox.java From RichTextFX with BSD 2-Clause "Simplified" License | 5 votes |
ParagraphBox(Paragraph<PS, SEG, S> par, BiConsumer<TextFlow, PS> applyParagraphStyle, Function<StyledSegment<SEG, S>, Node> nodeFactory) { this.getStyleClass().add("paragraph-box"); this.text = new ParagraphText<>(par, nodeFactory); applyParagraphStyle.accept(this.text, par.getParagraphStyle()); // start at -1 so that the first time it is displayed, the caret at pos 0 is not // accidentally removed from its parent and moved to this node's ParagraphText // before this node gets updated to its real index and therefore removes // caret from the SceneGraph completely this.index = Var.newSimpleVar(-1); getChildren().add(text); graphic = Val.combine( graphicFactory, this.index, (f, i) -> f != null && i > -1 ? f.apply(i) : null); graphic.addListener((obs, oldG, newG) -> { if(oldG != null) { getChildren().remove(oldG); } if(newG != null) { getChildren().add(newG); } }); graphicOffset.addListener(obs -> requestLayout()); }
Example 10
Source File: ListReductionTest.java From ReactFX with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void testMultipleModificationsWhenBound() { SuspendableList<Integer> list = new LiveArrayList<>(1, 1, 1, 1, 1).suspendable(); Val<Integer> sum = list.reduce((a, b) -> a + b); Var<Integer> lastObserved = Var.newSimpleVar(null); sum.observeChanges((obs, oldVal, newVal) -> lastObserved.setValue(newVal)); list.suspendWhile(() -> { list.addAll(0, Arrays.asList(3, 2)); list.remove(4, 6); list.addAll(4, Arrays.asList(8, 15)); }); assertEquals(31, lastObserved.getValue().intValue()); }
Example 11
Source File: ListReductionTest.java From ReactFX with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void testRecursion() { SuspendableList<Integer> list = new LiveArrayList<>(1, 1, 1, 1, 1).suspendable(); Val<Integer> sum = list.reduce((a, b) -> a + b); Var<Integer> lastObserved = Var.newSimpleVar(null); Random random = new Random(0xcafebabe); sum.addListener(obs -> { Integer newVal = sum.getValue(); if(newVal < 1000) { list.suspendWhile(() -> { // remove 4 items int i = random.nextInt(list.size() - 3); list.subList(i, i + 3).clear(); list.remove(random.nextInt(list.size())); // insert 5 items list.addAll(random.nextInt(list.size()), Arrays.asList( random.nextInt(12), random.nextInt(12), random.nextInt(12))); list.add(random.nextInt(list.size()), random.nextInt(12)); list.add(random.nextInt(list.size()), random.nextInt(12)); }); } }); sum.observeChanges((obs, oldVal, newVal) -> lastObserved.setValue(newVal)); list.set(2, 0); assertThat(lastObserved.getValue().intValue(), greaterThanOrEqualTo(1000)); }
Example 12
Source File: AnimatedValDemo.java From ReactFX with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void start(Stage primaryStage) { Circle circle = new Circle(30.0); Pane canvas = new Pane(circle); // animate circle position Var<Point2D> center = Var.newSimpleVar(new Point2D(W/2, H/2)); Val<Point2D> animCenter = center.animate( (p1, p2) -> Duration.ofMillis((long) p1.distance(p2)), (p1, p2, frac) -> p1.multiply(1.0-frac).add(p2.multiply(frac))); circle.centerXProperty().bind(animCenter.map(Point2D::getX)); circle.centerYProperty().bind(animCenter.map(Point2D::getY)); // animate circle color Var<Color> color = Var.newSimpleVar(Color.BLUE); Val<Color> animColor = Val.animate(color, Duration.ofMillis(500)); circle.fillProperty().bind(animColor); // on click, move to random position and transition to random color Random random = new Random(); circle.setOnMouseClicked(click -> { center.setValue(new Point2D( random.nextInt(W), random.nextInt(H))); color.setValue(Color.rgb( random.nextInt(240), random.nextInt(240), random.nextInt(240))); }); primaryStage.setScene(new Scene(canvas, W, H)); primaryStage.show(); }
Example 13
Source File: LiveViolationRecord.java From pmd-designer with BSD 2-Clause "Simplified" License | 4 votes |
public LiveViolationRecord(@Nullable TextRange range, @Nullable String message, boolean exactRange) { this.range = Var.newSimpleVar(range); this.message = Var.newSimpleVar(message); this.exactRange = Var.newSimpleVar(exactRange); }
Example 14
Source File: SearchableTreeView.java From pmd-designer with BSD 2-Clause "Simplified" License | 4 votes |
/** * Textfield for the search query. */ private void popSearchField() { TextField textField = new TextField(); textField.setPrefWidth(150); textField.setPromptText("Search tree"); ControlUtil.makeTextFieldShowPromptEvenIfFocused(textField); Label label = new Label(); label.getStyleClass().addAll("hint-label"); label.setTooltip(new Tooltip("Go to next result with F3")); StackPane pane = new StackPane(); pane.getStyleClass().addAll("search-popup"); pane.getStylesheets().addAll(DesignerUtil.getCss("designer").toString()); StackPane.setAlignment(textField, Pos.TOP_RIGHT); StackPane.setAlignment(label, Pos.BOTTOM_RIGHT); pane.getChildren().addAll(textField, label); Val<String> query = Val.wrap(textField.textProperty()) .filter(StringUtils::isNotBlank).map(String::trim) .filter(it -> it.length() >= MIN_QUERY_LENGTH); Var<Integer> numResults = Var.newSimpleVar(0); Subscription subscription = bindSearchQuery(query.conditionOnShowing(pane), numResults, textField); label.textProperty().bind( numResults.map(n -> n == 0 ? "no match" : n == 1 ? "1 match" : n + " matches") ); label.visibleProperty().bind(query.map(Objects::nonNull)); Popup popup = new Popup(); popup.getContent().addAll(pane); popup.setAutoHide(true); popup.setHideOnEscape(true); Bounds bounds = localToScreen(getBoundsInLocal()); popup.show(this, bounds.getMaxX() - textField.getPrefWidth() - 1, bounds.getMinY()); popup.setOnHidden(e -> { openSearchField = null; subscription.unsubscribe(); }); // release resources // Hide popup when ENTER or ESCAPE is pressed EventStreams.eventsOf(popup, KeyEvent.KEY_RELEASED) .filter(it -> it.getCode() == KeyCode.ENTER || it.getCode() == KeyCode.ESCAPE) .subscribeForOne(e -> { popup.hide(); e.consume(); }); textField.requestFocus(); openSearchField = textField; }
Example 15
Source File: DefaultEventStreamTest.java From ReactFX with BSD 2-Clause "Simplified" License | 3 votes |
@Test public void testAutoEmittingStream() { List<Integer> emitted = new ArrayList<>(); Var<Integer> source = Var.newSimpleVar(1); EventStream<Integer> stream = source.values().withDefaultEvent(0); stream.subscribe(emitted::add); assertEquals(Arrays.asList(1), emitted); source.setValue(2); assertEquals(Arrays.asList(1, 2), emitted); }