Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Overriding standard ECore validation messages
Overriding standard ECore validation messages [message #901788] Tue, 14 August 2012 15:18 Go to next message
Simon Barnett is currently offline Simon BarnettFriend
Messages: 28
Registered: July 2012
Junior Member
Hi,

What is the proper way to override the standard Ecore validation messages?

If part of my ecore model has a feature called Collections which must have at least 1 Item and in my instance of the model, I create a Collection which doesn't have any Items then Ecore gives me a message like this:-

"The feature 'items' of com.blah.blah.model.impl.CollectionImpl@<object ref> with 0 values must have at least 1 values"

This error message might not mean as much to the user as something like:-

"Collections must contain at least 1 Item"

so I'd like to override the way that the EMF error messages are created.

I've looked around a fair bit and the best I could come up with was to override getEResourceLocator in the EObjectValidator class like this:-

@Override
protected ResourceLocator getEcoreResourceLocator() {
	return OIDKModel.INSTANCE.getPluginResourceLocator();
}


and use my own properties file to define the message keys.

But the message substitutions don't work so I get:-

"{0} must contain at least {1} item"

and it occurs to me that if I override that method, I should provide ALL of the EMF message keys in case they ever come up as I will be responsible for creating them all now.

So there must be another way to do this where I can override specific messages. But does anyone know how this should be done?

Thanks.
Re: Overriding standard ECore validation messages [message #901799 is a reply to message #901788] Tue, 14 August 2012 15:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
Simon,

That's really the only way and yes, you'll have to be careful to support
all the messages (or have your model's plugin delegate to the Ecore plugin).


On 14/08/2012 5:18 PM, Simon Barnett wrote:
> Hi,
>
> What is the proper way to override the standard Ecore validation
> messages?
>
> If part of my ecore model has a feature called Collections which must
> have at least 1 Item and in my instance of the model, I create a
> Collection which doesn't have any Items then Ecore gives me a message
> like this:-
>
> "The feature 'items' of
> com.blah.blah.model.impl.CollectionImpl@<object ref> with 0 values
> must have at least 1 values"
>
> This error message might not mean as much to the user as something like:-
>
> "Collections must contain at least 1 Item"
>
> so I'd like to override the way that the EMF error messages are created.
>
> I've looked around a fair bit and the best I could come up with was to
> override getEResourceLocator in the EObjectValidator class like this:-
>
> @Override
> protected ResourceLocator getEcoreResourceLocator() {
> return OIDKModel.INSTANCE.getPluginResourceLocator();
> }
>
> and use my own properties file to define the message keys.
>
> But the message substitutions don't work so I get:-
>
> "{0} must contain at least {1} item"
>
> and it occurs to me that if I override that method, I should provide
> ALL of the EMF message keys in case they ever come up as I will be
> responsible for creating them all now.
>
> So there must be another way to do this where I can override specific
> messages. But does anyone know how this should be done?
>
> Thanks.

* 2.44 How do I map between an EMF Resource and an Eclipse IFile?
<http://wiki.eclipse.org/index.php/EMF-FAQ#How_do_I_map_between_an_EMF_Resource_and_an_Eclipse_IFile.3F>

From that IFile, you can get the containing folder and determine all
the other files in that folder.

On 14/08/2012 3:01 PM, Bastian Barton wrote:
> Hi xTexters,
>
> I have been searching for a pretty long time, but it shouldn't be that
> complicated:
>
> Having a Resource and therefore a platform URI, how can I get a list
> of all files in the same directory? I need it for an auto-import
> mechanism in the IScopeProvider.
>
> Thanks!
>
> BB
Re: Overriding standard ECore validation messages [message #901805 is a reply to message #901799] Tue, 14 August 2012 15:56 Go to previous messageGo to next message
John M. is currently offline John M.Friend
Messages: 198
Registered: July 2010
Senior Member
Basically I had a similar question, perhaps Ed's advice can help you too:
http://www.eclipse.org/forums/index.php/mv/msg/368837/899561/#msg_899561
Re: Overriding standard ECore validation messages [message #901825 is a reply to message #901799] Tue, 14 August 2012 17:23 Go to previous messageGo to next message
Simon Barnett is currently offline Simon BarnettFriend
Messages: 28
Registered: July 2012
Junior Member
Thanks Ed. I had a horrible feeling you were going to say that.

So I'm left with two questions

- how would I go about delegating to the Ecore plugin in my validator code?

- do you have any idea why my message substitutions don't work when I try and use my own message?

Thanks.
Re: Overriding standard ECore validation messages [message #901834 is a reply to message #901825] Tue, 14 August 2012 17:41 Go to previous messageGo to next message
Simon Barnett is currently offline Simon BarnettFriend
Messages: 28
Registered: July 2012
Junior Member
OK, I think I've sorted the delegation question using the following code in the validator:-

@Override
protected String getEcoreString(String key, Object[] substitutions) {
	try {
		return getString(getEcoreResourceLocator(), key, substitutions);
	} catch (MissingResourceException ex) {
		return getString(EcorePlugin.INSTANCE, key, substitutions);
	}
}

private String getString(ResourceLocator resourceLocator, String key,
		Object[] substitutions) {
	return substitutions == null ? resourceLocator.getString(key)
			: resourceLocator.getString(key, substitutions);
}


But I still don't get my substitutions in the message.
Any ideas on that one?
Re: Overriding standard ECore validation messages [message #901864 is a reply to message #901834] Tue, 14 August 2012 19:33 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
Simon,

In the constructor for your XyzPlugin, you can specify other resource
locators for it to delegate to in the call to super.


On 14/08/2012 7:41 PM, Simon Barnett wrote:
> OK, I think I've sorted the delegation question using the following
> code in the validator:-
>
> @Override
> protected String getEcoreString(String key, Object[] substitutions) {
> try {
> return getString(getEcoreResourceLocator(), key, substitutions);
> } catch (MissingResourceException ex) {
> return getString(EcorePlugin.INSTANCE, key, substitutions);
> }
> }
>
> private String getString(ResourceLocator resourceLocator, String key,
> Object[] substitutions) {
> return substitutions == null ? resourceLocator.getString(key)
> : resourceLocator.getString(key, substitutions);
> }
>
> But I still don't get my substitutions in the message.
> Any ideas on that one?
Re: Overriding standard ECore validation messages [message #901902 is a reply to message #901864] Tue, 14 August 2012 22:54 Go to previous messageGo to next message
Simon Barnett is currently offline Simon BarnettFriend
Messages: 28
Registered: July 2012
Junior Member
Ah OK - that sounds like a better way - thanks.

But I still can't get my message substitutions working when I'm overriding the ECore messages.

I override the _UI_FeatureHasTooFewValues_diagnostic message in the plugin.properties file like this:-

_UI_FeatureHasTooFewValues_diagnostic = It's {0} and {1} and {2}

but the message comes out literally as:-

It's {0} and {1} and {2}
Re: Overriding standard ECore validation messages [message #901916 is a reply to message #901902] Wed, 15 August 2012 03:21 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26226
Registered: July 2009
Senior Member
Simon,

I think the problem is likely the one single quote in "it's". Note that
Ecore defines it this way:

_UI_FeatureHasTooFewValues_diagnostic = The feature ''{0}'' of ''{1}''
with {2} values must have at least {3} values

Note in particular that the single quotes are expressed twice as a way
of escaping them. So I hink you need "it''s".

I think if you just override getEcoreResourceLocator to return your EMF
plugin and have your plugin delegate to the Ecore one, that's all you
need specialize...


On 15/08/2012 12:54 AM, Simon Barnett wrote:
> Ah OK - that sounds like a better way - thanks.
>
> But I still can't get my message substitutions working when I'm
> overriding the ECore messages.
>
> I override the _UI_FeatureHasTooFewValues_diagnostic message in the
> plugin.properties file like this:-
>
> _UI_FeatureHasTooFewValues_diagnostic = It's {0} and {1} and {2}
>
> but the message comes out literally as:-
>
> It's {0} and {1} and {2}
Re: Overriding standard ECore validation messages [message #901920 is a reply to message #901799] Wed, 15 August 2012 05:05 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4154
Registered: July 2009
Senior Member
Hi Simon

A slightly wacky idea that might work. But I've not tried any of it.

Eclipse (and EMF) supports internationalization, so if you configure
your Eclipse to use Barnetese and provide the Barnetese
internationalization fragment for EMF, then every internationalized
message might use Barnetese as first choice and English as a fall-back.

The tooling used by the Babel project might help you synchronize your
contribution.

Regards

Ed Willink

On 14/08/2012 16:49, Ed Merks wrote:
> Simon,
>
> That's really the only way and yes, you'll have to be careful to
> support all the messages (or have your model's plugin delegate to the
> Ecore plugin).
>
Re: Overriding standard ECore validation messages [message #901958 is a reply to message #901916] Wed, 15 August 2012 10:06 Go to previous messageGo to next message
Simon Barnett is currently offline Simon BarnettFriend
Messages: 28
Registered: July 2012
Junior Member
Argh! Thanks Ed!

No idea why I didn't think of the quotes thing - I've spent SO much time trying to trace through it in debug!

Thanks again for all your help on this.
Re: Overriding standard ECore validation messages [message #901968 is a reply to message #901920] Wed, 15 August 2012 11:03 Go to previous message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 386
Registered: December 2010
Location: Germany
Senior Member
Ed Willink wrote on Wed, 15 August 2012 07:05
Eclipse (and EMF) supports internationalization, so if you configure
your Eclipse to use Barnetese and provide the Barnetese
internationalization fragment for EMF, then every internationalized
message might use Barnetese as first choice and English as a fall-back.


That's the way we provide custom messages in German. You have to create a fragment for the host org.eclipse.emf.ecore.

I'm not sure, but imho it would be enough to provide a plugin_en.properties file for English. This should override the default plugin.properties in org.eclipse.emf.ecore.

Greetings
Christoph
Previous Topic:Metamodel hierarchies / model layers
Next Topic:EMF dependencies in plain OSGi application
Goto Forum:
  


Current Time: Thu Nov 27 01:59:28 GMT 2014

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

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