Java Code Examples for javafx.collections.MapChangeListener#Change

The following examples show how to use javafx.collections.MapChangeListener#Change . 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: Connection.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a position change listener (PCL) which {@link #refresh()
 * refreshes} this {@link Connection} upon anchor position changes
 * corresponding to the given {@link AnchorKey}.
 *
 * @param anchorKey
 *            The {@link AnchorKey} for which a position change will trigger
 *            a {@link #refresh()} with the returned PCL.
 * @return A position change listener to {@link #refresh() refresh} this
 *         {@link Connection} when the position for the given
 *         {@link AnchorKey} changes.
 */
protected MapChangeListener<? super AnchorKey, ? super Point> createPCL(
		final AnchorKey anchorKey) {
	return new MapChangeListener<AnchorKey, Point>() {
		@Override
		public void onChanged(
				MapChangeListener.Change<? extends AnchorKey, ? extends Point> change) {
			// if (inRefresh) {
			// return;
			// }
			if (change.getKey().equals(anchorKey)) {
				if (change.wasAdded() && change.wasRemoved()) {
					Point newPoint = FX2Geometry
							.toPoint(getCurve().localToParent(Geometry2FX
									.toFXPoint(change.getValueAdded())));
					if (!points.get(getAnchorIndex(anchorKey))
							.equals(newPoint)) {
						points.set(getAnchorIndex(anchorKey), newPoint);
						refresh();
					}
				}
			}
		}
	};
}
 
Example 2
Source File: Listeners.java    From SynchronizeFX with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onChanged(final MapChangeListener.Change<? extends Object, ? extends Object> change) {
    final ObservableMap<?, ?> map = change.getMap();
    if (disabledFor.containsKey(map)) {
        return;
    }
    try {
        final UUID mapId = objectRegistry.getIdOrFail(map);
        final Object key = change.getKey();
        if (change.wasAdded()) {
            final Object value = change.getValueAdded();
            final List<Command> commands = creator.putToMap(mapId, key, value);
            registerListenersOnEverything(key);
            if (value != null) {
                registerListenersOnEverything(value);
            }
            distributeCommands(commands);
        } else {
            distributeCommands(creator.removeFromMap(mapId, key));
        }
    } catch (final SynchronizeFXException e) {
        topology.onError(e);
    }
}
 
Example 3
Source File: ReadOnlyMapPropertyBaseEx.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void fireValueChangedEvent(
		MapChangeListener.Change<? extends K, ? extends V> change) {
	if (helper != null) {
		helper.fireValueChangedEvent(change);
	}
}
 
Example 4
Source File: MapExpressionHelperEx.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Fires notifications to all attached {@link InvalidationListener
 * InvalidationListeners}, and {@link MapChangeListener MapChangeListeners}.
 *
 * @param change
 *            The change that needs to be propagated.
 */
@Override
public void fireValueChangedEvent(
		MapChangeListener.Change<? extends K, ? extends V> change) {
	if (change != null) {
		notifyInvalidationListeners();
		// XXX: We do not notify change listeners here, as the identity of
		// the observed value did not change (see
		// https://bugs.openjdk.java.net/browse/JDK-8089169)
		notifyMapChangeListeners(
				new AtomicChange<>(observableValue, change));
	}
}
 
Example 5
Source File: MapListenerHelperEx.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Notifies all attached {@link InvalidationListener}s and
 * {@link MapChangeListener}s about the change.
 *
 * @param change
 *            The change to notify listeners about.
 */
public void fireValueChangedEvent(
		MapChangeListener.Change<? extends K, ? extends V> change) {
	notifyInvalidationListeners();
	if (change != null) {
		notifyMapChangeListeners(change);
	}
}
 
Example 6
Source File: EdgePart.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void onChanged(MapChangeListener.Change<? extends String, ? extends Object> change) {
	if (ZestProperties.ROUTER__E.equals(change.getKey())) {
		// if the router changed, re-attach the visual (so we attach to
		// a different anchor)
		for (Entry<IVisualPart<? extends Node>, String> anchoragesByRole : getAnchoragesUnmodifiable()
				.entries()) {
			doDetachFromAnchorageVisual(anchoragesByRole.getKey(), anchoragesByRole.getValue());
			doAttachToAnchorageVisual(anchoragesByRole.getKey(), anchoragesByRole.getValue());
		}
	}
	refreshVisual();
}
 
Example 7
Source File: EventStreams.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <K, V> EventStream<MapChangeListener.Change<? extends K, ? extends V>> changesOf(ObservableMap<K, V> map) {
    return new EventStreamBase<MapChangeListener.Change<? extends K, ? extends V>>() {
        @Override
        protected Subscription observeInputs() {
            MapChangeListener<K, V> listener = c -> emit(c);
            map.addListener(listener);
            return () -> map.removeListener(listener);
        }
    };
}
 
Example 8
Source File: MeshGeneratorJobManager.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private synchronized void handleMeshListChange(final MapChangeListener.Change<? extends ShapeKey<T>, ? extends Pair<MeshView, Node>> change)
{
	final ShapeKey<T> key = change.getKey();
	assert change.wasAdded() != change.wasRemoved() : "Mesh is only supposed to be added or removed at any time but not replaced: " + key;

	if (change.wasAdded())
	{
		assert tasks.containsKey(key) : "Mesh was rendered but its task does not exist: " + key;
		final long tag = tasks.get(key).tag;
		final Runnable onMeshAdded = () -> {
			if (!managers.isShutdown())
				managers.submit(withErrorPrinting(() -> onMeshAdded(key, tag)));
		};

		if (change.getValueAdded().getA() != null || change.getValueAdded().getB() != null)
		{
			// add to the queue, call onMeshAdded() when complete
			final MeshWorkerPriority priority = tasks.get(key).priority;

			meshViewUpdateQueue.addToQueue(
					key,
					change.getValueAdded(),
					meshesAndBlocksGroups,
					onMeshAdded,
					priority
			);
		}
		else
		{
			// nothing to add, invoke the callback immediately
			onMeshAdded.run();
		}
	}

	if (change.wasRemoved() && (change.getValueRemoved().getA() != null || change.getValueRemoved().getB() != null))
	{
		// try to remove the request from the queue in case the mesh has not been added to the scene yet
		if (!meshViewUpdateQueue.removeFromQueue(key))
		{
			// was not in the queue, remove it from the scene
			InvokeOnJavaFXApplicationThread.invoke(() -> {
				meshesAndBlocksGroups.getA().getChildren().remove(change.getValueRemoved().getA());
				meshesAndBlocksGroups.getB().getChildren().remove(change.getValueRemoved().getB());
			});
		}
	}
}
 
Example 9
Source File: DynamicAnchorSnippet.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Scene createScene() {
	BorderPane root = new BorderPane();
	Scene scene = new Scene(root, 400, 400);

	r1 = new Rectangle(50, 50);
	r1.setFill(Color.RED);
	r1.relocate(100, 100);
	r2 = new Rectangle(50, 50);
	r2.setFill(Color.BLUE);
	r2.relocate(200, 200);
	final Line l = new Line();
	l.setStroke(Color.BLACK);

	DynamicAnchor startAnchor = new DynamicAnchor(r1);
	DynamicAnchor endAnchor = new DynamicAnchor(r2);
	final AnchorKey startKey = new AnchorKey(l, "start");
	final AnchorKey endKey = new AnchorKey(l, "end");

	// update start and end point in case provided position values change
	MapChangeListener<AnchorKey, Point> changeListener = new MapChangeListener<AnchorKey, Point>() {

		@Override
		public void onChanged(
				MapChangeListener.Change<? extends AnchorKey, ? extends Point> change) {
			if (change.getKey().equals(startKey)) {
				l.setStartX(change.getMap().get(startKey).x);
				l.setStartY(change.getMap().get(startKey).y);
			}
			if (change.getKey().equals(endKey)) {
				l.setEndX(change.getMap().get(endKey).x);
				l.setEndY(change.getMap().get(endKey).y);
			}
		}
	};

	startAnchor.positionsUnmodifiableProperty().addListener(changeListener);
	endAnchor.positionsUnmodifiableProperty().addListener(changeListener);

	Point r1Center = new Point(
			r1.getLayoutBounds().getMinX() + r1.getLayoutX()
					+ r1.getWidth() / 2,
			r1.getLayoutBounds().getMinY() + r1.getLayoutY()
					+ r1.getHeight() / 2);
	Point r2Center = new Point(
			r2.getLayoutBounds().getMinX() + r2.getLayoutX()
					+ r2.getWidth() / 2,
			r2.getLayoutBounds().getMinY() + r2.getLayoutY()
					+ r2.getHeight() / 2);

	// use static values for dynamic anchor reference points
	startAnchor.getComputationParameter(startKey,
			AnchoredReferencePoint.class).set(r2Center);
	startAnchor.attach(startKey);
	endAnchor.getComputationParameter(endKey,
			AnchoredReferencePoint.class).set(r1Center);
	endAnchor.attach(endKey);

	Group g = new Group(r1, r2, l);
	root.getChildren().add(g);

	return scene;
}
 
Example 10
Source File: NodePart.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void onChanged(MapChangeListener.Change<? extends String, ? extends Object> change) {
	refreshVisual();
}
 
Example 11
Source File: AbstractLabelPart.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void onChanged(MapChangeListener.Change<? extends String, ? extends Object> change) {
	refreshVisual();
}
 
Example 12
Source File: MapPropertyExTests.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void onChanged(
		MapChangeListener.Change<? extends K, ? extends V> change) {
	if (keyQueue.size() <= 0) {
		fail("Received unexpected change " + change);
	}

	assertEquals(source, change.getMap());

	// check key
	K expectedKey = keyQueue.pollLast();
	assertEquals(expectedKey, change.getKey());

	// check added values
	V expectedAddedValue = addedValueQueue.pollLast();
	assertEquals(expectedAddedValue, change.getValueAdded());
	if (expectedAddedValue != null) {
		assertTrue(change.wasAdded());
	} else {
		assertFalse(change.wasAdded());
	}

	// check removed values
	V expectedRemovedValue = removedValueQueue.pollLast();
	assertEquals(expectedRemovedValue, change.getValueRemoved());
	if (expectedRemovedValue != null) {
		assertTrue(change.wasRemoved());
	} else {
		assertFalse(change.wasRemoved());
	}

	// check string representation
	if (expectedAddedValue == null && expectedRemovedValue != null) {
		assertEquals("Removed " + expectedRemovedValue + " for key "
				+ expectedKey + ".", change.toString());
	} else if (expectedAddedValue != null
			&& expectedRemovedValue == null) {
		assertEquals("Added " + expectedAddedValue + " for key "
				+ expectedKey + ".", change.toString());
	} else {
		assertEquals("Replaced " + expectedRemovedValue + " by "
				+ expectedAddedValue + " for key " + expectedKey + ".",
				change.toString());
	}
}
 
Example 13
Source File: MapListenerHelperEx.java    From gef with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link MapListenerHelperEx.AtomicChange} for the passed
 * in source, based on the data provided in the passed-in change.
 * <p>
 * This is basically used to allow properties wrapping an
 * {@link ObservableMap} to re-fire change events of their wrapped
 * {@link ObservableMap} with themselves as source.
 *
 * @param source
 *            The new source {@link ObservableMap}.
 * @param change
 *            The change to infer a new change from. It is expected that
 *            the change is in initial state. In either case it will be
 *            reset to initial state.
 */
public AtomicChange(ObservableMap<K, V> source,
		MapChangeListener.Change<? extends K, ? extends V> change) {
	super(source);

	this.key = change.getKey();
	this.addedValue = change.getValueAdded();
	this.removedValue = change.getValueRemoved();
}