if (userOriginated && isReadOnly()) {
return false;
}
- if (Objects.equals(value, getValue())) {
+ if (!isDifferentValue(value)) {
return false;
}
doSetValue(value);
return true;
}
+ /**
+ * Called when a new value is set to determine whether the provided new
+ * value is considered to be a change compared to the current value. This is
+ * used to determine whether {@link #doSetValue(Object)} should be called
+ * and a value change event fired.
+ *
+ * @param newValue
+ * the new value candidate to check, may be <code>null</code>
+ *
+ * @return <code>true</code> if the provided value is considered to be
+ * different and a value change event should be fired;
+ * <code>false</code> if the values are considered to be the same
+ * and no value change should be fired
+ */
+ protected boolean isDifferentValue(T newValue) {
+ return !Objects.equals(newValue, getValue());
+ }
+
/**
* Sets the value of this field. May do sanitization or throw
* {@code IllegalArgumentException} if the value is invalid. Typically saves
public class AbstractFieldTest extends EasyMockSupport {
+ private final class IdentityTextField extends TextField {
+ @Override
+ protected boolean isDifferentValue(String newValue) {
+ // Checks for identity instead of equality
+ return newValue != getValue();
+ }
+ }
+
class TextField extends AbstractField<String> {
String value = "";
assertSame("event source connector", source, e.getSource());
assertEquals("event from user", userOriginated, e.isUserOriginated());
}
+
+ @Test
+ public void identityField_realChange() {
+ TextField identityField = new IdentityTextField();
+
+ identityField.addValueChangeListener(l);
+
+ // Expect event to both listeners for actual change
+ l.valueChange(EasyMock.capture(capture));
+
+ replayAll();
+
+ identityField.setValue("value");
+
+ verifyAll();
+ }
+
+ @Test
+ public void identityField_onlyIdentityChange() {
+ TextField identityField = new IdentityTextField();
+ identityField.setValue("value");
+
+ identityField.addValueChangeListener(l);
+
+ // Expect event to both listeners for actual change
+ l.valueChange(EasyMock.capture(capture));
+
+ replayAll();
+
+ String sameValueDifferentIdentity = new String("value");
+ identityField.setValue(sameValueDifferentIdentity);
+
+ verifyAll();
+ }
+
+ @Test
+ public void identityField_noChange() {
+ TextField identityField = new IdentityTextField();
+ identityField.setValue("value");
+
+ identityField.addValueChangeListener(l);
+
+ // Expect no event for identical value
+ replayAll();
+
+ identityField.setValue(identityField.getValue());
+
+ verifyAll();
+ }
}