/******************************************************************************* * Copyright (c) 2016 itemis AG and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Alexander Nyßen (itemis AG) - initial API and implementation * *******************************************************************************/ package org.eclipse.gef.common.beans.property; import org.eclipse.gef.common.beans.binding.ListExpressionHelperEx; import javafx.beans.InvalidationListener; import javafx.beans.property.SimpleListProperty; import javafx.beans.value.ChangeListener; import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener.Change; import javafx.collections.ObservableList; /** * A replacement for {@link SimpleListProperty} to fix the following JavaFX * issue: * <ul> * <li>Change notifications are fired even when the observed value did not * change.(https://bugs.openjdk.java.net/browse/JDK-8089169)</li> * </ul> * * @author anyssen * * @param <E> * The element type of the {@link SimpleListProperty}. * */ public class SimpleListPropertyEx<E> extends SimpleListProperty<E> { private ListExpressionHelperEx<E> helper = null; /** * Creates a new unnamed {@link SimpleListPropertyEx}. */ public SimpleListPropertyEx() { super(); } /** * Constructs a new {@link SimpleListPropertyEx} for the given bean and with * the given name. * * @param bean * The bean this property is related to. * @param name * The name of the property. */ public SimpleListPropertyEx(Object bean, String name) { super(bean, name); } /** * Constructs a new {@link SimpleListPropertyEx} for the given bean and with * the given name and initial value. * * @param bean * The bean this property is related to. * @param name * The name of the property. * @param initialValue * The initial value of the property */ public SimpleListPropertyEx(Object bean, String name, ObservableList<E> initialValue) { super(bean, name, initialValue); } /** * Constructs a new unnamed {@link SimpleListPropertyEx} that is not related * to a bean, with the given initial value. * * @param initialValue * The initial value of the property */ public SimpleListPropertyEx(ObservableList<E> initialValue) { super(initialValue); } @Override public void addListener( ChangeListener<? super ObservableList<E>> listener) { if (helper == null) { helper = new ListExpressionHelperEx<>(this); } helper.addListener(listener); } @Override public void addListener(InvalidationListener listener) { if (helper == null) { helper = new ListExpressionHelperEx<>(this); } helper.addListener(listener); } @Override public void addListener(ListChangeListener<? super E> listener) { if (helper == null) { helper = new ListExpressionHelperEx<>(this); } helper.addListener(listener); } @Override protected void fireValueChangedEvent() { if (helper != null) { helper.fireValueChangedEvent(); } } @Override protected void fireValueChangedEvent(Change<? extends E> change) { if (helper != null) { helper.fireValueChangedEvent(change); } } @Override public int hashCode() { // XXX: As we rely on equality to remove a binding again, we have to // ensure the hash code is the same for a pair of given properties. // We fall back to the very easiest case here (and use a constant). return 0; } @Override public void removeListener( ChangeListener<? super ObservableList<E>> listener) { if (helper != null) { helper.removeListener(listener); } } @Override public void removeListener(InvalidationListener listener) { if (helper != null) { helper.removeListener(listener); } } @Override public void removeListener(ListChangeListener<? super E> listener) { if (helper != null) { helper.removeListener(listener); } } // TODO: overwrite sort(Comparator) and replaceAll(UnaryOperator) as well, // as soon as we drop JavaSE-1.7 support. }