| Data binding, validation and verification [message #1181194]
||Mon, 11 November 2013 07:42
| Baruch Youssin
Registered: April 2013
I am new to Java and Eclipse and am trying to do a simple task: create a text control to input an integer, bind it to an Integer and drop silently all inappropriate characters that user may enter.|
As this is a simple and a standard task (I am sure thousands of developers have already faced it), I am looking for a simple solution without having to reinvent anything.
Data binding framework appears to have all the tools for the task: binding, validators and converters.
However, I have not been able to get it to do the entire job.
I was thinking of converter to report incorrect input to the model (e.g., as a null converted value), and the model in such case to restore the last good value to the control.
This would achieve some other nice things such as removing leading zeroes and limiting the number of digits in a simple way.
I have not been able to get this working; the reason is that while a change in one of the two bound properties effects a change in the other one, the change back is blocked.
(The private field
blocks it; it is set to true in the method
org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.doSetValue(..) and set to false in this method after the updating is done.)
Well, this is done obviously to prevent infinite loops of two properties updating one another without end.
However, if the model can change back the widget but not vice versa then there would be no infinite loop.
I have found a snippet
in the list of snippets
that shows how to make a text field silently drop all characters other than digits; it does not use the data binding framework but rather creates a listener to the Verify event that does the job in a usual way.
Thus, the solution that appears to be recommended, is to have two things separate:
1. The Text control drops wrong characters by itself by means of this listener, and
2. The data binding writes the value to the model.
I can write my own listener class and register it with each text widget that needs to do integer input. (If this is the right way, many people should have done similar things.)
My problem with it is, first, the duplicate validation code: one in this listener and the other one in the data binding validator.
Second, it is not as simple as I would expect.
Is that what the designers of the framework (SWT, JFace and Data Binding) had in mind?
Or maybe there is some simpler way?
In Microsoft .NET setting, I am familiar with inheriting the text control by adding to it this listener. This would save some repetitive code. However, the class
org.eclipse.swt.widgets.Text says :"IMPORTANT: This class is <em>not</em> intended to be subclassed."
Maybe such simple subclassing (by adding just one listener) would be OK?
Many thanks for any advice.
[Updated on: Mon, 11 November 2013 07:45]
Report message to a moderator
Powered by FUDForum
. Page generated in 0.01910 seconds