EMF Command for updating EMaps? [message #394459] |
Wed, 20 July 2005 15:08  |
Eclipse User |
|
|
|
I'd like to set an EAnnotation detail through an AddCommand/SetCommand.
I have an editingDomain that contains an EAnnotation. I know about
EcorePackage.eINSTANCE.getEAnnotation_Details(). What I can't figure out
is how to add the entry "Foo" -> "Bar" through this mechanism.
AddCommand has a constructor whose second parameter is an EList -- and an
EMap is an EList -- but the EntryImpls that EMap uses are protected so I
can't simply make one myself and add it to the EMap-as-an-EList.
I could use the SetCommand() with the EAnnotation and it's
EcorePackage.eINSTANCE.getEAnnotation_Details(). To do that I'd need to
make a copy of the EMap, update the copy, and use the copy for SetCommand
(right?) I don't know how to duplicate an EMap -- EcoreUtil.copy() only
copies EObjects.
Please advise.
|
|
|
|
|
Re: EMF Command for updating EMaps? [message #1719140 is a reply to message #1719082] |
Tue, 05 January 2016 10:20   |
Eclipse User |
|
|
|
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
|
|
|
|
Re: EMF Command for updating EMaps? [message #1719197 is a reply to message #1719175] |
Wed, 06 January 2016 02:36  |
Eclipse User |
|
|
|
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
>
|
|
|
Powered by
FUDForum. Page generated in 0.25031 seconds