Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Pasting and ID handling(What is the right way to ensure unique IDs when pasting?)
Pasting and ID handling [message #1053579] Mon, 06 May 2013 15:12 Go to next message
Michael Jastram is currently offline Michael JastramFriend
Messages: 163
Registered: April 2010
Location: Düsseldorf, Germany
Senior Member
Howdy,

I am looking for the best (right?) approach to ensure unique IDs upon pasting in EMF. Basically, I have model elements with IDs that have to be unique. When elements are created, they receive a unique id (UUID).

I am concerned about clipboard pasting. If elements are cut and pasted, I don't want the IDs to change. But if elements are copied and pasted (or pasted multiple times), I need to generate new IDs for those elements.

So: What's the best approach to do this? I would have expected the ItemProviders to provide a createPasteCommand() method to override, but such a method does not exist.

The EMF Book mentions the PasteFromClipboardCommand, which sounds exactly like what I need. Unfortunately, I am not sure where I would customize such a command.

And last, it would be great if the solution would also work for drag&drop, which can also be used for moving and copying, depending on the keyboard modifier.

Thanks in advance,

- Michael
Re: Pasting and ID handling [message #1053587 is a reply to message #1053579] Mon, 06 May 2013 15:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26287
Registered: July 2009
Senior Member
Michael,

Comments below.

On 06/05/2013 5:12 PM, Michael Jastram wrote:
> Howdy,
>
> I am looking for the best (right?) approach to ensure unique IDs upon
> pasting in EMF. Basically, I have model elements with IDs that have
> to be unique. When elements are created, they receive a unique id
> (UUID).
>
> I am concerned about clipboard pasting. If elements are cut and
> pasted, I don't want the IDs to change. But if elements are copied
> and pasted (or pasted multiple times), I need to generate new IDs for
> those elements.
>
> So: What's the best approach to do this? I would have expected the
> ItemProviders to provide a createPasteCommand() method to override,
> but such a method does not exist.
No. There is a PasteFromClipboardCommand, but that's a "global" command
created by AdapterFactoryEditingDomain.createCommand.
>
> The EMF Book mentions the PasteFromClipboardCommand, which sounds
> exactly like what I need. Unfortunately, I am not sure where I would
> customize such a command.
You could specialize that in your editing domain.
>
> And last, it would be great if the solution would also work for
> drag&drop, which can also be used for moving and copying, depending on
> the keyboard modifier.
That's the problem. There are several ways to end up with the same
behavior.

Perhaps the simplest way would be to specialize the AddCommand so before
you add something, you check that the ID each object being added
(includes any contained objects) are unique, and if not, that you assign
somehow a unique identifier; you should probably make such changes using
more commands to be sure it's undone...
>
> Thanks in advance,
>
> - Michael
Re: Pasting and ID handling [message #1053695 is a reply to message #1053587] Tue, 07 May 2013 09:24 Go to previous messageGo to next message
Michael Jastram is currently offline Michael JastramFriend
Messages: 163
Registered: April 2010
Location: Düsseldorf, Germany
Senior Member
Hi Ed,

Thanks for your answers, they pretty much pointed me in the right direction.

> Perhaps the simplest way would be to specialize the AddCommand

I modified createAddCommand() and createDragAndDropCommand(), and it's working for both scenarios (copy&paste and drag&drop with copy). I can't think of another scenario that I may have missed.

> so before
> you add something, you check that the ID each object being added
> (includes any contained objects) are unique, and if not, that you assign
> somehow a unique identifier;

To check the unique ID, I am checking against owner.eResource().getIntrinsicIDToEObjectMap().keySet(). Is this a smart way of doing this? In the initialization, I call setIntrinsicIDToEObjectMap(new HashMap<String, EObject>()), to make sure that the lookup is fast.

> you should probably make such changes using
> more commands to be sure it's undone...

I don't think so... I modify the values before adding the objects to the model. Undo will therefore simply remove the objects again. Even if the objects are pasted again after an undo, the ids will be checked again. Or am I missing something? The most important thing is that by setting new values, I don't modify the existing model, right? But that should not be the case.

Best,

- Michael
Re: Pasting and ID handling [message #1053699 is a reply to message #1053695] Tue, 07 May 2013 09:45 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 26287
Registered: July 2009
Senior Member
Michael,

Comments below.


On 07/05/2013 11:24 AM, Michael Jastram wrote:
> Hi Ed,
>
> Thanks for your answers, they pretty much pointed me in the right
> direction.
>
>> Perhaps the simplest way would be to specialize the AddCommand
>
> I modified createAddCommand() and createDragAndDropCommand(), and it's
> working for both scenarios (copy&paste and drag&drop with copy). I
> can't think of another scenario that I may have missed.
>
>> so before you add something, you check that the ID each object being
>> added (includes any contained objects) are unique, and if not, that
>> you assign somehow a unique identifier;
>
> To check the unique ID, I am checking against
> owner.eResource().getIntrinsicIDToEObjectMap().keySet(). Is this a
> smart way of doing this? In the initialization, I call
> setIntrinsicIDToEObjectMap(new HashMap<String, EObject>()), to make
> sure that the lookup is fast.
Yes, otherwise a full walk of the tree is necessary. Note that you can
use owner.eResource().getEObject(<id-string>) to check that nothing else
already maps to that ID; this will be either fast or slow depending on
whether you've set the intrinsic ID map, but should always work correct,
and has the advantage that you don't need to rely on Resource
implementation details in your AddCommand.
>
>> you should probably make such changes using more commands to be sure
>> it's undone...
>
> I don't think so... I modify the values before adding the objects to
> the model. Undo will therefore simply remove the objects again. Even
> if the objects are pasted again after an undo, the ids will be checked
> again. Or am I missing something? The most important thing is that by
> setting new values, I don't modify the existing model, right? But
> that should not be the case.
Yes I think you're right. I think even if you do a Cut, and then do a
Paste, that the object in the clipboard is copied so undoing the paste
and undoing the Cut should not result in the object with modified IDs
being placed back in the tree. Best to test that to be sure...
>
> Best,
>
> - Michael
Previous Topic:Class Cast exception thrown for an EMap during model instantiation
Next Topic:local language writting issue
Goto Forum:
  


Current Time: Sun Dec 21 11:36:19 GMT 2014

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

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