From 812d41be428701e66fcafa3d20182fb11afc5f99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Wed, 11 Apr 2012 12:07:29 +0000 Subject: [PATCH] #6155 Javadoc/comments; refresh field value from data source on attach svn changeset:23476/svn branch:6.8 --- src/com/vaadin/ui/AbstractField.java | 47 +++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index f3939de6e1..e1d3270225 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -140,9 +140,14 @@ public abstract class AbstractField extends AbstractComponent implements Field, private boolean valueWasModifiedByDataSourceDuringCommit; /** - * Whether this field currently listens to Property events. + * Whether this field is currently registered as listening to events from + * its data source. + * + * @see #setPropertyDataSource(Property) + * @see #addPropertyListeners() + * @see #removePropertyListeners() */ - private boolean isListening = false; + private boolean isListeningToPropertyEvents = false; /* Component basics */ @@ -582,6 +587,17 @@ public abstract class AbstractField extends AbstractComponent implements Field, *

* *

+ * 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 -- 2.39.5