Home » Eclipse Projects » JFace » Data binding, validation and verification(How to bind an Integer to a text field and drop non-digit characters silently)
Data binding, validation and verification [message #1181194] |
Mon, 11 November 2013 12:42 |
Baruch Youssin Messages: 29 Registered: April 2013 |
Junior Member |
|
|
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
org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.updating
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
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet19.java
in the list of snippets
http://www.eclipse.org/swt/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 12:45] Report message to a moderator
|
|
|
Re: Data binding, validation and verification [message #1277804 is a reply to message #1181194] |
Wed, 26 March 2014 13:44 |
Nigel Westbury Messages: 18 Registered: July 2009 |
Junior Member |
|
|
Baruch,
If you are using the new provisional data binding API then you can use
the 'bounceBack' method. This will convert the text to an integer and
convert back to text so that normalized text appears in the UI. See
https://wiki.eclipse.org/JFace_Data_Binding/The_New_Binding_API#Bounce-back_bindings
Nigel Westbury
On 11/11/2013 12:42, Baruch Youssin wrote:
> 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
> org.eclipse.core.internal.databinding.property.value.SimplePropertyObservableValue.updating
>
> 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
> http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet19.java
>
> in the list of snippets
> http://www.eclipse.org/swt/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 field drops wrong characters by itself by means of this
> listener, and
> 2. The data binding writes the value to the model.
>
> My problem with it is, first, the duplicate validation code: one in this
> listener and the other one in the data binding validator. 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.)
>
> 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.
>
>
>
>
|
|
| |
Goto Forum:
Current Time: Sat May 04 10:13:10 GMT 2024
Powered by FUDForum. Page generated in 0.03716 seconds
|