Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [data binding] how to make a binding be optional?
[data binding] how to make a binding be optional? [message #875686] Wed, 23 May 2012 07:17 Go to next message
David BY Chan is currently offline David BY ChanFriend
Messages: 40
Registered: July 2009
Member
Hi, I created a data binding between a SWT control and a Bean property,
assume there's a control named 'textAge' and a Bean class Person has a
field named 'age' whose type is int.

Now I bind them, binding framework first get value from textAge control,
it will get a empty string if user didn't input anything because
attribute 'age' is not required. But framework don't think so, it still
invoke converter to transform value between control and model. It
eventually called Integer.parseInt(value) and throwed a Exception
java.lang.NumberFormatException: For input string: ""

what I want is If framework found an EMPTY (null or length=0) string, it
give a waring and don't process further.

How to do? thank for your great help!!
Re: [data binding] how to make a binding be optional? [message #875698 is a reply to message #875686] Wed, 23 May 2012 07:41 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
The first sync should always go from model to control. Can you show us
how you've set up the binding? My current suspicion is that you mixed
mixed the arguments.

You could naturally also add your custom converter by using passing an
UpdateStrategy object when doing the bind.

Tom

Am 23.05.12 09:17, schrieb David BY Chan:
> Hi, I created a data binding between a SWT control and a Bean property,
> assume there's a control named 'textAge' and a Bean class Person has a
> field named 'age' whose type is int.
>
> Now I bind them, binding framework first get value from textAge control,
> it will get a empty string if user didn't input anything because
> attribute 'age' is not required. But framework don't think so, it still
> invoke converter to transform value between control and model. It
> eventually called Integer.parseInt(value) and throwed a Exception
> java.lang.NumberFormatException: For input string: ""
>
> what I want is If framework found an EMPTY (null or length=0) string, it
> give a waring and don't process further.
>
> How to do? thank for your great help!!
Re: [data binding] how to make a binding be optional? [message #875726 is a reply to message #875698] Wed, 23 May 2012 08:30 Go to previous messageGo to next message
David BY Chan is currently offline David BY ChanFriend
Messages: 40
Registered: July 2009
Member
No, I did not mix the arguments. Here's my example:

EMF Model:
public interface GeneralStatement{
Integer getTimeout();
void setTimeout(Integer value);
}

Binding:

IObservableValue ob_txt_timeout =
SWTObservables.observeText(textTimeout,SWT.Modify);
IObservableValue ob_model_timeout = EMFObservables.observeValue(stmt,
stmt.eClass().getEStructuralFeature(Mapperv2Package.GENERAL_STATEMENT__TIMEOUT));
timeoutBinding = current_binding_context.bindValue(ob_txt_timeout,
ob_model_timeout);

I didn't use any converter because framework defined internal
StringToInteger conveter.

Exception:
java.lang.NumberFormatException: For input string: ""
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.valueOf(Integer.java:554)
at
org.eclipse.emf.ecore.impl.EcoreFactoryImpl.createEIntegerObjectFromString(EcoreFactoryImpl.java:519)

I debugged the code, the first sync is from model to control, right! New
created GeneralStatement contains a field 'timeout' with null as its
value, internal ObjectToString converter converted null to "", so text
control display a blank, but the second round is from control to model,
framework found the target type is Integer, and try to convert "" to int
via Integer.parseInt(String).

On 2012/5/23 15:41, Tom Schindl wrote:
> The first sync should always go from model to control. Can you show us
> how you've set up the binding? My current suspicion is that you mixed
> mixed the arguments.
>
> You could naturally also add your custom converter by using passing an
> UpdateStrategy object when doing the bind.
>
> Tom
>
> Am 23.05.12 09:17, schrieb David BY Chan:
>> Hi, I created a data binding between a SWT control and a Bean property,
>> assume there's a control named 'textAge' and a Bean class Person has a
>> field named 'age' whose type is int.
>>
>> Now I bind them, binding framework first get value from textAge control,
>> it will get a empty string if user didn't input anything because
>> attribute 'age' is not required. But framework don't think so, it still
>> invoke converter to transform value between control and model. It
>> eventually called Integer.parseInt(value) and throwed a Exception
>> java.lang.NumberFormatException: For input string: ""
>>
>> what I want is If framework found an EMPTY (null or length=0) string, it
>> give a waring and don't process further.
>>
>> How to do? thank for your great help!!
>
Re: [data binding] how to make a binding be optional? [message #875740 is a reply to message #875726] Wed, 23 May 2012 08:59 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
The only solution I have for you at the moment is that you pass your
custom converter to the UpdateStrategy which handles the empty string
case and converts it to NULL.

The default conversion leverages EMFs internal conversion support which
works the way it works at the moment. You can naturally file a bugzilla
for EMF-Databinding to treat the "" case but I can't promise if and when
I'll have time to implement it.

Tom

Am 23.05.12 10:30, schrieb David BY Chan:
> No, I did not mix the arguments. Here's my example:
>
> EMF Model:
> public interface GeneralStatement{
> Integer getTimeout();
> void setTimeout(Integer value);
> }
>
> Binding:
>
> IObservableValue ob_txt_timeout =
> SWTObservables.observeText(textTimeout,SWT.Modify);
> IObservableValue ob_model_timeout =
> EMFObservables.observeValue(stmt,
> stmt.eClass().getEStructuralFeature(Mapperv2Package.GENERAL_STATEMENT__TIMEOUT));
>
> timeoutBinding =
> current_binding_context.bindValue(ob_txt_timeout, ob_model_timeout);
>
> I didn't use any converter because framework defined internal
> StringToInteger conveter.
>
> Exception:
> java.lang.NumberFormatException: For input string: ""
> at
> java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
>
> at java.lang.Integer.parseInt(Integer.java:470)
> at java.lang.Integer.valueOf(Integer.java:554)
> at
> org.eclipse.emf.ecore.impl.EcoreFactoryImpl.createEIntegerObjectFromString(EcoreFactoryImpl.java:519)
>
>
> I debugged the code, the first sync is from model to control, right! New
> created GeneralStatement contains a field 'timeout' with null as its
> value, internal ObjectToString converter converted null to "", so text
> control display a blank, but the second round is from control to model,
> framework found the target type is Integer, and try to convert "" to int
> via Integer.parseInt(String).
>
> On 2012/5/23 15:41, Tom Schindl wrote:
>> The first sync should always go from model to control. Can you show us
>> how you've set up the binding? My current suspicion is that you mixed
>> mixed the arguments.
>>
>> You could naturally also add your custom converter by using passing an
>> UpdateStrategy object when doing the bind.
>>
>> Tom
>>
>> Am 23.05.12 09:17, schrieb David BY Chan:
>>> Hi, I created a data binding between a SWT control and a Bean property,
>>> assume there's a control named 'textAge' and a Bean class Person has a
>>> field named 'age' whose type is int.
>>>
>>> Now I bind them, binding framework first get value from textAge control,
>>> it will get a empty string if user didn't input anything because
>>> attribute 'age' is not required. But framework don't think so, it still
>>> invoke converter to transform value between control and model. It
>>> eventually called Integer.parseInt(value) and throwed a Exception
>>> java.lang.NumberFormatException: For input string: ""
>>>
>>> what I want is If framework found an EMPTY (null or length=0) string, it
>>> give a waring and don't process further.
>>>
>>> How to do? thank for your great help!!
>>
>
Previous Topic:how to show tooltip description off-line(without the internet) in eclipse
Next Topic:View with secondary ID is not placed properly
Goto Forum:
  


Current Time: Tue Mar 19 09:23:41 GMT 2024

Powered by FUDForum. Page generated in 0.02569 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top