Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [databinding] convert empty string to null
[databinding] convert empty string to null [message #430734] Sat, 06 June 2009 00:18 Go to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
I'm binding a Text widget to a BigDecimal attribute. The
EMFUpdateValueStrategy throws a NumberFormatException when the text box is
cleared. Instead, I'd like to set the attribute to null. I can do it with
my own UpdateValueStrategy, but just wondering if there are other better
options.

I noticed https://bugs.eclipse.org/bugs/show_bug.cgi?id=203492 which looks
cool but seems to have stalled.

-Will
Re: [databinding] convert empty string to null [message #430735 is a reply to message #430734] Sat, 06 June 2009 00:30 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6512
Registered: July 2009
Senior Member
Hi,

No naturally you can write your own UpdateValueStrategy which sets the
null-converter by default. The other IMHO better option is to delegate
the creation of UpdateValueStrategy to a Factory which makes the code
more readable and easier to maintain.

-----------8<------------
ctx.bindValue(
textProp.observe(t),
mprop.observe(committership),
UpdateStrategyFactory.stringToDate(
"Can't convert end date value into a valid date"),
UpdateStrategyFactory.dateToString()
);

// ....

ctx.bindValue(textProp.observe(t), mprop.observe(committership),
UpdateStrategyFactory.stringToDateNotNull(
"Can't convert start date value into a valid date",
"The start date can not be null"),
UpdateStrategyFactory.dateToString()
);
-----------8<------------

The source code is available from EMF-CVS
(org.eclipse.emf.examples.databinding.project.*) and hence all code
provided is available under EPL.

Tom

Will Horn schrieb:
> I'm binding a Text widget to a BigDecimal attribute. The
> EMFUpdateValueStrategy throws a NumberFormatException when the text box
> is cleared. Instead, I'd like to set the attribute to null. I can do
> it with my own UpdateValueStrategy, but just wondering if there are
> other better options.
>
> I noticed https://bugs.eclipse.org/bugs/show_bug.cgi?id=203492 which
> looks cool but seems to have stalled.
>
> -Will
Re: [databinding] convert empty string to null [message #430739 is a reply to message #430735] Sat, 06 June 2009 17:51 Go to previous messageGo to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
Thanks for the pointer to that very interesting example application. By the
way, I had to modify three files to run it on Windows:

ProjectAdminViewPart.java
- String path = getViewSite().getSecondaryId().replaceFirst("_", ":");
+ String path = getViewSite().getSecondaryId().replaceAll("_", ":");
OpenXMIHandler.java
- path = path.replaceFirst(":", "_");
+ path = path.replaceAll(":", "_");
NewXMIHandler.java
- path = path.replaceFirst(":", "_");
+ path = path.replaceAll(":", "_");

....since windows has more than one colon in a path, e.g. file://c:/abc.xmi

For my update value strategy, I end up with is this:

static UpdateValueStrategy convertEmptyStringToNull() {
return new EMFUpdateValueStrategy() {
@Override
protected IConverter createConverter(Object fromType, Object
toType) {
// get the default EMF converter for these types
final IConverter defaultConverter = super.createConverter(
fromType, toType);
return new Converter(fromType, toType) {
@Override
public Object convert(Object fromObject) {
// delegate to the default converter, but replace
empty string
// with null first
if (fromObject instanceof String
&& ((String) fromObject).isEmpty()) {
fromObject = null;
}
return defaultConverter.convert(fromObject);
}
};
}
};
}

It works, but just seems like a lot for a conceptually simple task.

By the way,
"Tom Schindl" <tom.schindl@bestsolution.at> wrote in message
news:h0cdam$mrn$1@build.eclipse.org...
> Hi,
>
> No naturally you can write your own UpdateValueStrategy which sets the
> null-converter by default. The other IMHO better option is to delegate
> the creation of UpdateValueStrategy to a Factory which makes the code
> more readable and easier to maintain.
>
> -----------8<------------
> ctx.bindValue(
> textProp.observe(t),
> mprop.observe(committership),
> UpdateStrategyFactory.stringToDate(
> "Can't convert end date value into a valid date"),
> UpdateStrategyFactory.dateToString()
> );
>
> // ....
>
> ctx.bindValue(textProp.observe(t), mprop.observe(committership),
> UpdateStrategyFactory.stringToDateNotNull(
> "Can't convert start date value into a valid date",
> "The start date can not be null"),
> UpdateStrategyFactory.dateToString()
> );
> -----------8<------------
>
> The source code is available from EMF-CVS
> (org.eclipse.emf.examples.databinding.project.*) and hence all code
> provided is available under EPL.
>
> Tom
>
> Will Horn schrieb:
>> I'm binding a Text widget to a BigDecimal attribute. The
>> EMFUpdateValueStrategy throws a NumberFormatException when the text box
>> is cleared. Instead, I'd like to set the attribute to null. I can do
>> it with my own UpdateValueStrategy, but just wondering if there are
>> other better options.
>>
>> I noticed https://bugs.eclipse.org/bugs/show_bug.cgi?id=203492 which
>> looks cool but seems to have stalled.
>>
>> -Will
Re: [databinding] convert empty string to null [message #430740 is a reply to message #430739] Sat, 06 June 2009 21:03 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6512
Registered: July 2009
Senior Member
Hi Will,

Well why are you not setting the convert on the newly created
EMFUpdateValueStrategy?

Thanks for the feedback on the win32 problem - I'll fix the bugs ASAP.
The first blog entries about the application and the new API are out
since a few minutes [1].

Tom

[1]http://tomsondev.bestsolution.at/

Will Horn schrieb:
> Thanks for the pointer to that very interesting example application. By
> the way, I had to modify three files to run it on Windows:
>
> ProjectAdminViewPart.java
> - String path = getViewSite().getSecondaryId().replaceFirst("_", ":");
> + String path = getViewSite().getSecondaryId().replaceAll("_", ":");
> OpenXMIHandler.java
> - path = path.replaceFirst(":", "_");
> + path = path.replaceAll(":", "_");
> NewXMIHandler.java
> - path = path.replaceFirst(":", "_");
> + path = path.replaceAll(":", "_");
>
> ...since windows has more than one colon in a path, e.g. file://c:/abc.xmi
>
> For my update value strategy, I end up with is this:
>
> static UpdateValueStrategy convertEmptyStringToNull() {
> return new EMFUpdateValueStrategy() {
> @Override
> protected IConverter createConverter(Object fromType, Object
> toType) {
> // get the default EMF converter for these types
> final IConverter defaultConverter = super.createConverter(
> fromType, toType);
> return new Converter(fromType, toType) {
> @Override
> public Object convert(Object fromObject) {
> // delegate to the default converter, but replace
> empty string
> // with null first
> if (fromObject instanceof String
> && ((String) fromObject).isEmpty()) {
> fromObject = null;
> }
> return defaultConverter.convert(fromObject);
> }
> };
> }
> };
> }
>
> It works, but just seems like a lot for a conceptually simple task.
>
> By the way,
> "Tom Schindl" <tom.schindl@bestsolution.at> wrote in message
> news:h0cdam$mrn$1@build.eclipse.org...
>> Hi,
>>
>> No naturally you can write your own UpdateValueStrategy which sets the
>> null-converter by default. The other IMHO better option is to delegate
>> the creation of UpdateValueStrategy to a Factory which makes the code
>> more readable and easier to maintain.
>>
>> -----------8<------------
>> ctx.bindValue(
>> textProp.observe(t),
>> mprop.observe(committership),
>> UpdateStrategyFactory.stringToDate(
>> "Can't convert end date value into a valid date"),
>> UpdateStrategyFactory.dateToString()
>> );
>>
>> // ....
>>
>> ctx.bindValue(textProp.observe(t), mprop.observe(committership),
>> UpdateStrategyFactory.stringToDateNotNull(
>> "Can't convert start date value into a valid date",
>> "The start date can not be null"),
>> UpdateStrategyFactory.dateToString()
>> );
>> -----------8<------------
>>
>> The source code is available from EMF-CVS
>> (org.eclipse.emf.examples.databinding.project.*) and hence all code
>> provided is available under EPL.
>>
>> Tom
>>
>> Will Horn schrieb:
>>> I'm binding a Text widget to a BigDecimal attribute. The
>>> EMFUpdateValueStrategy throws a NumberFormatException when the text box
>>> is cleared. Instead, I'd like to set the attribute to null. I can do
>>> it with my own UpdateValueStrategy, but just wondering if there are
>>> other better options.
>>>
>>> I noticed https://bugs.eclipse.org/bugs/show_bug.cgi?id=203492 which
>>> looks cool but seems to have stalled.
>>>
>>> -Will
>
Re: [databinding] convert empty string to null [message #430741 is a reply to message #430740] Sat, 06 June 2009 23:09 Go to previous messageGo to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
"Tom Schindl" <tom.schindl@bestsolution.at> wrote in message
news:h0elic$a3o$1@build.eclipse.org...
> Well why are you not setting the convert on the newly created
> EMFUpdateValueStrategy?

EMFUpdateValueStrategy by default can convert a String to any EAttribute
(using the data type, EFactory, etc). If I set my own converter, then I
have to know the fromType and toType and it is no longer generic to use on
different model observable types.

>>
>> static UpdateValueStrategy convertEmptyStringToNull() {
>> return new EMFUpdateValueStrategy() {
>> @Override
>> protected IConverter createConverter(Object fromType, Object
>> toType) {
>> // get the default EMF converter for these types
>> final IConverter defaultConverter = super.createConverter(
>> fromType, toType);
>> return new Converter(fromType, toType) {
>> @Override
>> public Object convert(Object fromObject) {
>> // delegate to the default converter, but replace
>> empty string
>> // with null first
>> if (fromObject instanceof String
>> && ((String) fromObject).isEmpty()) {
>> fromObject = null;
>> }
>> return defaultConverter.convert(fromObject);
>> }
>> };
>> }
>> };
>> }
Re: [databinding] convert empty string to null [message #431125 is a reply to message #430740] Mon, 29 June 2009 17:18 Go to previous messageGo to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
"Tom Schindl" <tom.schindl@bestsolution.at> wrote in message
news:h0elic$a3o$1@build.eclipse.org...
> Thanks for the feedback on the win32 problem - I'll fix the bugs ASAP.
I noticed you made a change to the EMF Databinding sample code about the bug
I mentioned. I tried it again and there is still one small bug on windows.
NewXMIHandler.java still does

path = path.replaceFirst(":", "#_#");

instead of

path = path.replaceAll(":", "#_#");

OpenXMIHandler works fine now.

Best,
Will
Re: [databinding] convert empty string to null [message #431129 is a reply to message #431125] Mon, 29 June 2009 21:04 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6512
Registered: July 2009
Senior Member
It was in the NewXMIHandler right? Thanks for the feedback - much
appreciated and fixed :-)

Tom

Will Horn schrieb:
> "Tom Schindl" <tom.schindl@bestsolution.at> wrote in message
> news:h0elic$a3o$1@build.eclipse.org...
>> Thanks for the feedback on the win32 problem - I'll fix the bugs ASAP.
> I noticed you made a change to the EMF Databinding sample code about the
> bug I mentioned. I tried it again and there is still one small bug on
> windows. NewXMIHandler.java still does
>
> path = path.replaceFirst(":", "#_#");
>
> instead of
>
> path = path.replaceAll(":", "#_#");
>
> OpenXMIHandler works fine now.
>
> Best,
> Will
Previous Topic:problem with prefix
Next Topic:Select using BooleanOCLCondition Question!
Goto Forum:
  


Current Time: Mon Dec 16 02:55:48 GMT 2019

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

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

Back to the top