Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Data binding, validation and verification(How to bind an Integer to a text field and drop non-digit characters silently)
icon5.gif  Data binding, validation and verification [message #1181194] Mon, 11 November 2013 07:42 Go to next message
Baruch Youssin is currently offline Baruch Youssin
Messages: 25
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 07:45]

Report message to a moderator

Re: Data binding, validation and verification [message #1277804 is a reply to message #1181194] Wed, 26 March 2014 09:44 Go to previous messageGo to next message
Nigel Westbury is currently offline Nigel Westbury
Messages: 16
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.
>
>
>
>
Re: Data binding, validation and verification [message #1277855 is a reply to message #1277804] Wed, 26 March 2014 11:00 Go to previous message
Baruch Youssin is currently offline Baruch Youssin
Messages: 25
Registered: April 2013
Junior Member
Thanks, Nigel.
This would do part of the task I was asking about.
Still, validation needs to be done in the target to drop all illegal characters separately from the conversion.
Nevertheless, this is a nice and useful addition.
Previous Topic:[Databinding] Bind a bean with observable values as properties to table
Next Topic:[Databinding] A complex scenario
Goto Forum:
  


Current Time: Wed Aug 27 11:04:48 EDT 2014

Powered by FUDForum. Page generated in 0.02141 seconds