diff --git a/modules/javafx.base/src/main/java/com/sun/javafx/binding/BidirectionalBinding.java b/modules/javafx.base/src/main/java/com/sun/javafx/binding/BidirectionalBinding.java index 84ee0494fe3..38eb63e8a3e 100644 --- a/modules/javafx.base/src/main/java/com/sun/javafx/binding/BidirectionalBinding.java +++ b/modules/javafx.base/src/main/java/com/sun/javafx/binding/BidirectionalBinding.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,9 +25,10 @@ package com.sun.javafx.binding; +import javafx.beans.InvalidationListener; +import javafx.beans.Observable; import javafx.beans.WeakListener; import javafx.beans.property.*; -import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.util.StringConverter; @@ -36,7 +37,7 @@ import java.text.ParseException; import java.util.Objects; -public abstract class BidirectionalBinding implements ChangeListener, WeakListener { +public abstract class BidirectionalBinding implements InvalidationListener, WeakListener { private static void checkParameters(Object property1, Object property2) { Objects.requireNonNull(property1, "Both properties must be specified."); @@ -97,10 +98,10 @@ public static void unbind(Object property1, Object property2) { checkParameters(property1, property2); final BidirectionalBinding binding = new UntypedGenericBidirectionalBinding(property1, property2); if (property1 instanceof ObservableValue) { - ((ObservableValue) property1).removeListener(binding); + ((ObservableValue)property1).removeListener(binding); } if (property2 instanceof ObservableValue) { - ((ObservableValue) property2).removeListener(binding); + ((ObservableValue)property2).removeListener(binding); } } @@ -139,7 +140,7 @@ public static BidirectionalBinding bindNumber(DoubleProperty property1, Property private static BidirectionalBinding bindNumberObject(Property property1, Property property2) { checkParameters(property1, property2); - final BidirectionalBinding binding = new TypedNumberBidirectionalBinding(property2, property1); + final BidirectionalBinding binding = new TypedNumberBidirectionalBinding<>(property2, property1); property1.setValue(property2.getValue()); property1.addListener(binding); @@ -150,7 +151,7 @@ private static BidirectionalBinding bindNumberObject(Property private static BidirectionalBinding bindNumber(Property property1, Property property2) { checkParameters(property1, property2); - final BidirectionalBinding binding = new TypedNumberBidirectionalBinding(property1, property2); + final BidirectionalBinding binding = new TypedNumberBidirectionalBinding<>(property1, property2); property1.setValue((T)property2.getValue()); property1.addListener(binding); @@ -208,15 +209,17 @@ public boolean equals(Object obj) { return false; } - private static class BidirectionalBooleanBinding extends BidirectionalBinding { + private static class BidirectionalBooleanBinding extends BidirectionalBinding { private final WeakReference propertyRef1; private final WeakReference propertyRef2; - private boolean updating = false; + private boolean oldValue; + private boolean updating; private BidirectionalBooleanBinding(BooleanProperty property1, BooleanProperty property2) { super(property1, property2); - propertyRef1 = new WeakReference(property1); - propertyRef2 = new WeakReference(property2); + oldValue = property1.get(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -230,7 +233,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Boolean oldValue, Boolean newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final BooleanProperty property1 = propertyRef1.get(); final BooleanProperty property2 = propertyRef2.get(); @@ -245,9 +248,13 @@ public void changed(ObservableValue sourceProperty, Boolean o try { updating = true; if (property1 == sourceProperty) { + boolean newValue = property1.get(); property2.set(newValue); + oldValue = newValue; } else { + boolean newValue = property2.get(); property1.set(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { @@ -275,15 +282,17 @@ public void changed(ObservableValue sourceProperty, Boolean o } } - private static class BidirectionalDoubleBinding extends BidirectionalBinding { + private static class BidirectionalDoubleBinding extends BidirectionalBinding { private final WeakReference propertyRef1; private final WeakReference propertyRef2; + private double oldValue; private boolean updating = false; private BidirectionalDoubleBinding(DoubleProperty property1, DoubleProperty property2) { super(property1, property2); - propertyRef1 = new WeakReference(property1); - propertyRef2 = new WeakReference(property2); + oldValue = property1.get(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -297,7 +306,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Number oldValue, Number newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final DoubleProperty property1 = propertyRef1.get(); final DoubleProperty property2 = propertyRef2.get(); @@ -312,16 +321,20 @@ public void changed(ObservableValue sourceProperty, Number old try { updating = true; if (property1 == sourceProperty) { - property2.set(newValue.doubleValue()); + double newValue = property1.get(); + property2.set(newValue); + oldValue = newValue; } else { - property1.set(newValue.doubleValue()); + double newValue = property2.get(); + property1.set(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { if (property1 == sourceProperty) { - property1.set(oldValue.doubleValue()); + property1.set(oldValue); } else { - property2.set(oldValue.doubleValue()); + property2.set(oldValue); } } catch (Exception e2) { e2.addSuppressed(e); @@ -342,15 +355,17 @@ public void changed(ObservableValue sourceProperty, Number old } } - private static class BidirectionalFloatBinding extends BidirectionalBinding { + private static class BidirectionalFloatBinding extends BidirectionalBinding { private final WeakReference propertyRef1; private final WeakReference propertyRef2; + private float oldValue; private boolean updating = false; private BidirectionalFloatBinding(FloatProperty property1, FloatProperty property2) { super(property1, property2); - propertyRef1 = new WeakReference(property1); - propertyRef2 = new WeakReference(property2); + oldValue = property1.get(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -364,7 +379,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Number oldValue, Number newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final FloatProperty property1 = propertyRef1.get(); final FloatProperty property2 = propertyRef2.get(); @@ -379,16 +394,20 @@ public void changed(ObservableValue sourceProperty, Number old try { updating = true; if (property1 == sourceProperty) { - property2.set(newValue.floatValue()); + float newValue = property1.get(); + property2.set(newValue); + oldValue = newValue; } else { - property1.set(newValue.floatValue()); + float newValue = property2.get(); + property1.set(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { if (property1 == sourceProperty) { - property1.set(oldValue.floatValue()); + property1.set(oldValue); } else { - property2.set(oldValue.floatValue()); + property2.set(oldValue); } } catch (Exception e2) { e2.addSuppressed(e); @@ -409,15 +428,17 @@ public void changed(ObservableValue sourceProperty, Number old } } - private static class BidirectionalIntegerBinding extends BidirectionalBinding{ + private static class BidirectionalIntegerBinding extends BidirectionalBinding { private final WeakReference propertyRef1; private final WeakReference propertyRef2; + private int oldValue; private boolean updating = false; private BidirectionalIntegerBinding(IntegerProperty property1, IntegerProperty property2) { super(property1, property2); - propertyRef1 = new WeakReference(property1); - propertyRef2 = new WeakReference(property2); + oldValue = property1.get(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -431,7 +452,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Number oldValue, Number newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final IntegerProperty property1 = propertyRef1.get(); final IntegerProperty property2 = propertyRef2.get(); @@ -446,16 +467,20 @@ public void changed(ObservableValue sourceProperty, Number old try { updating = true; if (property1 == sourceProperty) { - property2.set(newValue.intValue()); + int newValue = property1.get(); + property2.set(newValue); + oldValue = newValue; } else { - property1.set(newValue.intValue()); + int newValue = property2.get(); + property1.set(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { if (property1 == sourceProperty) { - property1.set(oldValue.intValue()); + property1.set(oldValue); } else { - property2.set(oldValue.intValue()); + property2.set(oldValue); } } catch (Exception e2) { e2.addSuppressed(e); @@ -476,15 +501,17 @@ public void changed(ObservableValue sourceProperty, Number old } } - private static class BidirectionalLongBinding extends BidirectionalBinding { + private static class BidirectionalLongBinding extends BidirectionalBinding { private final WeakReference propertyRef1; private final WeakReference propertyRef2; + private long oldValue; private boolean updating = false; private BidirectionalLongBinding(LongProperty property1, LongProperty property2) { super(property1, property2); - propertyRef1 = new WeakReference(property1); - propertyRef2 = new WeakReference(property2); + oldValue = property1.get(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -498,7 +525,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Number oldValue, Number newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final LongProperty property1 = propertyRef1.get(); final LongProperty property2 = propertyRef2.get(); @@ -513,16 +540,20 @@ public void changed(ObservableValue sourceProperty, Number old try { updating = true; if (property1 == sourceProperty) { - property2.set(newValue.longValue()); + long newValue = property1.get(); + property2.set(newValue); + oldValue = newValue; } else { - property1.set(newValue.longValue()); + long newValue = property2.get(); + property1.set(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { if (property1 == sourceProperty) { - property1.set(oldValue.longValue()); + property1.set(oldValue); } else { - property2.set(oldValue.longValue()); + property2.set(oldValue); } } catch (Exception e2) { e2.addSuppressed(e); @@ -543,15 +574,17 @@ public void changed(ObservableValue sourceProperty, Number old } } - private static class TypedGenericBidirectionalBinding extends BidirectionalBinding { + private static class TypedGenericBidirectionalBinding extends BidirectionalBinding { private final WeakReference> propertyRef1; private final WeakReference> propertyRef2; + private T oldValue; private boolean updating = false; private TypedGenericBidirectionalBinding(Property property1, Property property2) { super(property1, property2); - propertyRef1 = new WeakReference>(property1); - propertyRef2 = new WeakReference>(property2); + oldValue = property1.getValue(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -565,7 +598,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, T oldValue, T newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final Property property1 = propertyRef1.get(); final Property property2 = propertyRef2.get(); @@ -580,9 +613,13 @@ public void changed(ObservableValue sourceProperty, T oldValue, T n try { updating = true; if (property1 == sourceProperty) { + T newValue = property1.getValue(); property2.setValue(newValue); + oldValue = newValue; } else { + T newValue = property2.getValue(); property1.setValue(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { @@ -610,15 +647,17 @@ public void changed(ObservableValue sourceProperty, T oldValue, T n } } - private static class TypedNumberBidirectionalBinding extends BidirectionalBinding { + private static class TypedNumberBidirectionalBinding extends BidirectionalBinding { private final WeakReference> propertyRef1; private final WeakReference> propertyRef2; + private T oldValue; private boolean updating = false; private TypedNumberBidirectionalBinding(Property property1, Property property2) { super(property1, property2); - propertyRef1 = new WeakReference>(property1); - propertyRef2 = new WeakReference>(property2); + oldValue = property1.getValue(); + propertyRef1 = new WeakReference<>(property1); + propertyRef2 = new WeakReference<>(property2); } @Override @@ -632,7 +671,7 @@ protected Property getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Number oldValue, Number newValue) { + public void invalidated(Observable sourceProperty) { if (!updating) { final Property property1 = propertyRef1.get(); final Property property2 = propertyRef2.get(); @@ -647,9 +686,13 @@ public void changed(ObservableValue sourceProperty, Number old try { updating = true; if (property1 == sourceProperty) { + T newValue = property1.getValue(); property2.setValue(newValue); + oldValue = newValue; } else { - property1.setValue((T)newValue); + T newValue = (T)property2.getValue(); + property1.setValue(newValue); + oldValue = newValue; } } catch (RuntimeException e) { try { @@ -677,8 +720,7 @@ public void changed(ObservableValue sourceProperty, Number old } } - private static class UntypedGenericBidirectionalBinding extends BidirectionalBinding { - + private static class UntypedGenericBidirectionalBinding extends BidirectionalBinding { private final Object property1; private final Object property2; @@ -699,21 +741,20 @@ protected Object getProperty2() { } @Override - public void changed(ObservableValue sourceProperty, Object oldValue, Object newValue) { + public void invalidated(Observable sourceProperty) { throw new RuntimeException("Should not reach here"); } } - public abstract static class StringConversionBidirectionalBinding extends BidirectionalBinding { - + public abstract static class StringConversionBidirectionalBinding extends BidirectionalBinding { private final WeakReference> stringPropertyRef; private final WeakReference> otherPropertyRef; private boolean updating; public StringConversionBidirectionalBinding(Property stringProperty, Property otherProperty) { super(stringProperty, otherProperty); - stringPropertyRef = new WeakReference>(stringProperty); - otherPropertyRef = new WeakReference>(otherProperty); + stringPropertyRef = new WeakReference<>(stringProperty); + otherPropertyRef = new WeakReference<>(otherProperty); } protected abstract String toString(T value); @@ -731,7 +772,7 @@ protected Object getProperty2() { } @Override - public void changed(ObservableValue observable, Object oldValue, Object newValue) { + public void invalidated(Observable observable) { if (!updating) { final Property property1 = stringPropertyRef.get(); final Property property2 = otherPropertyRef.get(); @@ -769,7 +810,6 @@ public void changed(ObservableValue observable, Object oldValu } private static class StringFormatBidirectionalBinding extends StringConversionBidirectionalBinding { - private final Format format; @SuppressWarnings("unchecked") @@ -790,7 +830,6 @@ protected Object fromString(String value) throws ParseException { } private static class StringConverterBidirectionalBinding extends StringConversionBidirectionalBinding { - private final StringConverter converter; public StringConverterBidirectionalBinding(Property stringProperty, Property otherProperty, StringConverter converter) { diff --git a/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingTest.java b/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingTest.java index c19195d3d6a..6a65998b4ed 100644 --- a/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingTest.java +++ b/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingTest.java @@ -176,7 +176,7 @@ public void testChaining() { } private int getListenerCount(ObservableValue v) { - return ExpressionHelperUtility.getChangeListeners(v).size(); + return ExpressionHelperUtility.getInvalidationListeners(v).size(); } @Test diff --git a/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingWithConversionTest.java b/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingWithConversionTest.java index e75c2907dc2..a93006249d1 100644 --- a/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingWithConversionTest.java +++ b/modules/javafx.base/src/test/java/test/com/sun/javafx/binding/BidirectionalBindingWithConversionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -281,13 +281,13 @@ public int getListenerCount() { } @Override - public void addListener(ChangeListener listener) { + public void addListener(InvalidationListener listener) { super.addListener(listener); listenerCount++; } @Override - public void removeListener(ChangeListener listener) { + public void removeListener(InvalidationListener listener) { super.removeListener(listener); listenerCount--; } @@ -303,13 +303,13 @@ public int getListenerCount() { } @Override - public void addListener(ChangeListener listener) { + public void addListener(InvalidationListener listener) { super.addListener(listener); listenerCount++; } @Override - public void removeListener(ChangeListener listener) { + public void removeListener(InvalidationListener listener) { super.removeListener(listener); listenerCount--; }