Revision: 1328
Author: mathiasbr
Date: 2006-08-20 23:54:50 -0700 (Sun, 20 Aug 2006)
ViewCVS: http://svn.sourceforge.net/spring-rich-c/?rev=1328&view=rev
Log Message:
-----------
formatted text field caused a race condition when the field has a not null
constraint and the guarded button is pressed after filling a value into this
field
Modified Paths:
--------------
trunk/spring-richclient/support/src/main/java/org/springframework/binding/value/swing/FormattedTextFieldAdapter.java
Modified:
trunk/spring-richclient/support/src/main/java/org/springframework/binding/value/swing/FormattedTextFieldAdapter.java
===================================================================
---
trunk/spring-richclient/support/src/main/java/org/springframework/binding/value/swing/FormattedTextFieldAdapter.java
2006-08-21 06:38:03 UTC (rev 1327)
+++
trunk/spring-richclient/support/src/main/java/org/springframework/binding/value/swing/FormattedTextFieldAdapter.java
2006-08-21 06:54:50 UTC (rev 1328)
@@ -15,6 +15,8 @@
*/
package org.springframework.binding.value.swing;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
@@ -27,22 +29,23 @@
import org.springframework.binding.value.support.AbstractValueModelAdapter;
/**
- * Sets the value of the value model associated with a formatted text field
when
- * the text field changes according to the value commit policy.
+ * Sets the value of the value model associated with a formatted text field
when the text field changes according to the
+ * value commit policy.
*
- * This setter will also update the formatted text field value when the
- * underlying value model value changes.
+ * This setter will also update the formatted text field value when the
underlying value model value changes.
*
* @author Oliver Hutchison
* @author Keith Donald
*/
public class FormattedTextFieldAdapter extends AbstractValueModelAdapter
implements PropertyChangeListener,
- DocumentListener {
+ DocumentListener, FocusListener {
private final JFormattedTextField component;
private boolean settingValue;
+ private boolean ignoreValue;
+
public FormattedTextFieldAdapter(JFormattedTextField component, ValueModel
valueModel,
ValueCommitPolicy commitPolicy) {
super(valueModel);
@@ -52,6 +55,20 @@
if (commitPolicy == ValueCommitPolicy.AS_YOU_TYPE) {
component.getDocument().addDocumentListener(this);
}
+
+ // mathiasbr: register focus listener to avoid a race condition.
+ // If the formatted text field lost its focus the value will be
replaced.
+ // This results into two events: the first one sets the value to null
+ // the second one sets the value to its value before it was set to
null.
+ // If the focus is moved to the button which is bound to the validation
+ // and the field has a constraint (like required) the button will be
disabled
+ // while it is pressed. Although the button will be enabled when the
second
+ // event with the previous value is set again but the button will lost
its
+ // armed and pressed state when it is disabled.
+ // The result is that a user has to click the button twice
+ // before the button fires the actionPerformed event
+ component.addFocusListener(this);
+
initalizeAdaptedValue();
}
@@ -59,8 +76,7 @@
settingValue = true;
try {
component.setValue(value);
- }
- finally {
+ } finally {
settingValue = false;
}
}
@@ -79,7 +95,10 @@
}
public void removeUpdate(DocumentEvent e) {
- tryToCommitEdit();
+ if (!ignoreValue)
+ tryToCommitEdit();
+ else
+ ignoreValue = false;
}
public void changedUpdate(DocumentEvent e) {
@@ -90,10 +109,17 @@
if (!settingValue) {
try {
component.commitEdit();
- }
- catch (ParseException e) {
+ } catch (ParseException e) {
// ignore
}
}
}
+
+ public void focusGained(FocusEvent e) {
+ ignoreValue = false;
+ }
+
+ public void focusLost(FocusEvent e) {
+ ignoreValue = true;
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs