From: Johannes Dahlström
+ * If the data source implements + * {@link com.vaadin.data.Property.ValueChangeNotifier} and/or + * {@link com.vaadin.data.Property.ReadOnlyStatusChangeNotifier}, the field + * registers itself as a listener and updates itself according to the events + * it receives. To avoid memory leaks caused by references to a field no + * longer in use, the listener registrations are removed on + * {@link AbstractField#detach() detach} and re-added on + * {@link AbstractField#attach() attach}. + *
+ * + *
* Note: before 6.5 we actually called discard() method in the beginning of
* the method. This was removed to simplify implementation, avoid excess
* calls to backing property and to avoid odd value change events that were
@@ -1120,8 +1136,14 @@ public abstract class AbstractField extends AbstractComponent implements Field,
if (actionManager != null) {
actionManager.setViewer(getWindow());
}
- // No-op if listeners already registered
- addPropertyListeners();
+
+ if (!isListeningToPropertyEvents) {
+ addPropertyListeners();
+ if (!isModified() && isReadThrough()) {
+ // Update value from data source
+ discard();
+ }
+ }
}
@Override
@@ -1334,8 +1356,13 @@ public abstract class AbstractField extends AbstractComponent implements Field,
}
}
+ /**
+ * Registers this as an event listener for events sent by the data source
+ * (if any). Does nothing if
+ * isListeningToPropertyEvents == true
.
+ */
private void addPropertyListeners() {
- if (!isListening) {
+ if (!isListeningToPropertyEvents) {
if (dataSource instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) dataSource).addListener(this);
}
@@ -1343,12 +1370,16 @@ public abstract class AbstractField extends AbstractComponent implements Field,
((Property.ReadOnlyStatusChangeNotifier) dataSource)
.addListener(this);
}
- isListening = true;
+ isListeningToPropertyEvents = true;
}
}
+ /**
+ * Stops listening to events sent by the data source (if any). Does nothing
+ * if isListeningToPropertyEvents == false
.
+ */
private void removePropertyListeners() {
- if (isListening) {
+ if (isListeningToPropertyEvents) {
if (dataSource instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) dataSource)
.removeListener(this);
@@ -1357,7 +1388,7 @@ public abstract class AbstractField extends AbstractComponent implements Field,
((Property.ReadOnlyStatusChangeNotifier) dataSource)
.removeListener(this);
}
- isListening = false;
+ isListeningToPropertyEvents = false;
}
}
}
\ No newline at end of file