|
|
|
Re: EMF Command for updating EMaps? [message #1719140 is a reply to message #1719082] |
Tue, 05 January 2016 15:20 |
Ed Merks Messages: 33218 Registered: July 2009 |
Senior Member |
|
|
Jim,
And you want to do this as commands?
In the Ecore editor, a new details entry is created because of this code
in
org.eclipse.emf.ecore.provider.EAnnotationItemProvider.collectNewChildDescriptors(Collection<Object>,
Object)
protected void collectNewChildDescriptors(Collection<Object>
newChildDescriptors, Object object)
{
// super.collectNewChildDescriptors(newChildDescriptors, object);
newChildDescriptors.add
(createChildParameter
(EcorePackage.Literals.EANNOTATION__DETAILS,
EcoreFactory.eINSTANCE.create(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY)));
}
So that's how you create a new Detail entry if you need to add
something, and you can add it using an AddCommand. If you want to
update the value of an entry, you can do that with a SetCommand.
On 05/01/2016 2:25 AM, Jim Boone wrote:
> Ed,
>
> I stumbled across this posting because I basically want to do the same
> thing that the other Ed wanted to. I want to programmatically add or
> update the documentation text used when generating XML (see below):
>
> source: http://www.eclipse.org/emf/2002/GenModel
> keys: documentation and appinfo
>
> I have searched for coding examples and snippets that would show me
> how to do this using an editing domain without any luck. Your posting
> is interesting, but still vague to me. Can you point to a complete
> example of how this is done? I tried to find where in the code this
> is done in the generated editor but could not find it. Thanks in
> advance!
>
> --Jim
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Re: EMF Command for updating EMaps? [message #1719197 is a reply to message #1719175] |
Wed, 06 January 2016 07:36 |
Ed Merks Messages: 33218 Registered: July 2009 |
Senior Member |
|
|
Jim,
You need to consider what the implementation normally does:
public static void setAnnotation(EModelElement eModelElement, String
sourceURI, String key, String value)
{
EAnnotation eAnnotation = eModelElement.getEAnnotation(sourceURI);
if (value == null)
{
if (eAnnotation != null)
{
eAnnotation.getDetails().removeKey(key);
}
}
else
{
if (eAnnotation == null)
{
eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
eAnnotation.setSource(sourceURI);
eModelElement.getEAnnotations().add(eAnnotation);
}
eAnnotation.getDetails().put(key, value);
}
}
So one brute force approach is to use a ChangeCommand and just invoke
that method. The other approach is to consider all the cases. If there
isn't an EAnnotation with the expected source URI present, you need to
create one and add it to the EModelElement.eAnnotations feature. The
next case to consider is trickier. The EAnnotation.details feature
might contain an entry for the key; in that case, you need to find that
entry and set the value feature. If it doesn't contain an entry, you
need to create an entry and add it. All this can be done using add and
set commands. A ChangeCommand wouldn't be so bad either. You only
need to attach the change recorder to the EModelElement because it's the
only thing you'll change.
On 05/01/2016 9:28 PM, Jim Boone wrote:
> I am still struggling. Perhaps a simple illustration is in order.
>
> Let's say that I have an eClass with an annotation as shown below.
> Then update the value of as shown. Now I want to use the editing
> domain to update it. How would I do that?
>
>
> ...
> String documentationValue = EcoreUtil.getAnnotation(clazz,
> "Test", "documentation");
> System.out.println("Original Value: " + documentationValue);
>
> String newValue = "This is updated documentation";
> EcoreUtil.setAnnotation(clazz, "Test", "documentation",
> newValue);
> System.out.println("Updated Value: " + newValue);
>
> /*
> * Command cmd = SetCommand.create(editingDomain, clazz,
> feature???, value??); *
> editingDomain.getCommandStack().execute(cmd);
> */
> ...
> Output:
> Original Value: this is documentation text
> Updated Value: This is updated documentation
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.03265 seconds