Home » Modeling » EMF » [databinding] convert empty string to null
|
Re: [databinding] convert empty string to null [message #430735 is a reply to message #430734] |
Sat, 06 June 2009 00:30 |
Thomas Schindl Messages: 6651 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 |
Will Horn 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 |
Thomas Schindl Messages: 6651 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
>
|
|
| | | |
Goto Forum:
Current Time: Sat Apr 20 01:03:50 GMT 2024
Powered by FUDForum. Page generated in 0.03645 seconds
|