Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to use CopyCommand
How to use CopyCommand [message #487798] Thu, 24 September 2009 13:00 Go to next message
Dirk Hoffmann is currently offline Dirk HoffmannFriend
Messages: 163
Registered: July 2009
Senior Member
Hi,

already searched the mailing list, the book and the internet but found
nothing appropriate to teach me how to use CopyCommand.

As naiv as I am I started with

CopyCommand.create(final EditingDomain domain, final Collection collection)

and immediately wondered where the destination of the copy operation
would have to go. Started anyway and got a
UnsupportedOperationException.

I probably need to flesh out some details of the copy operation but have
no idea how to go about that or what's the best practice here.

What's going to make the task even more complicated is the fact that the
copying is supposed to copy a tree of objects (the "Queries" tree) over
from one resource to another resource. The "Queries" tree contains
references to some other part (called "Schema") of the model instance
which will not be copied to the destination instance. However the
destination instance contains its own "Schema" part and the references
from the copied "Queries" tree need to be recreated by some algorithm.

A picture may make it clearer:

before the copying:

Source instance
|________
| |
Queries Schema
/\ --> /\
/#1\ -->/#1\
/ \-->/ \
^-- references

Destination instance
|________
| |
Queries Schema
(empty) /\
/#2\
/ \


after the copying:

Source instance
|________
| |
Queries Schema
/\ --> /\
/#1\ -->/#1\
/ \-->/ \
^-- references

Destination instance
|________
| |
Queries Schema
/\ --> /\
/#1\ -->/#2\
/ \-->/ \
^-- recreated references

The whole copy operation should be undoable and the two resources be
independent from each other, i.e. not in the same ResourceSet.

Any pointers on how to deal with the CopyCommand in general and in our
special case would be very much appreciated.

Thanks and Regards,
Dirk
Re: How to use CopyCommand [message #488028 is a reply to message #487798] Fri, 25 September 2009 11:55 Go to previous messageGo to next message
Dirk Hoffmann is currently offline Dirk HoffmannFriend
Messages: 163
Registered: July 2009
Senior Member
Some additions to the previous post:

1. The cause of the UnsupportedOperationException was the collection
passed to the create method being empty.

2. It came to my mind that it would be nice for the users being able to
use copy&paste as an additional means to perform the copy operation.
That would mean using the clipboard as the operartion spans two distinct
resources. The generated editor apparently doesn't provide that out of
the box. Could someone please provide some hints about how to achieve that?

3. The copy&paste idea brought me a bit further regarding the general
use of the CopyCommand (at least I believe so):

Looking at the code of the PasteFromClipboardCommand I found that
CopyCommand needs to be combined with AddCommand by putting them into a
CompoundCommand. I did it like so:

---- 8< -----------------
StrictCompoundCommand copyAndAddCommand =
new StrictCompoundCommand("Import Queries");

final Command copyCommand = CopyCommand.create(_editingDomain, queries);
copyAndAddCommand.appendAndExecute(copyCommand);

copyAndAddCommand.appendAndExecute(new CommandWrapper() {
@Override
protected Command createCommand() {
EReference feature =
SbqPackage.Literals.QUERIES__HOUSE_NQUERIES;
Command addCommand = AddCommand.create(_editingDomain,
_dstQueries, feature, copyCommand.getResult());
return addCommand;
}
});

CommandStack commandStack = _editingDomain.getCommandStack();
commandStack.execute(copyAndAddCommand);
---- >8 -----------------

This does a deep copy letting copied references point back into the
source model instance. So what I still have to implement is some method
to recreate the references so they point to the corresponding objects in
the destination instance, and of course the copy&paste thingy.

Thanks and Regards,
Dirk


Dirk Hoffmann schrieb:
> Hi,
>
> already searched the mailing list, the book and the internet but found
> nothing appropriate to teach me how to use CopyCommand.
>
> As naiv as I am I started with
>
> CopyCommand.create(final EditingDomain domain, final Collection collection)
>
> and immediately wondered where the destination of the copy operation
> would have to go. Started anyway and got a
> UnsupportedOperationException.
>
> I probably need to flesh out some details of the copy operation but have
> no idea how to go about that or what's the best practice here.
>
> What's going to make the task even more complicated is the fact that the
> copying is supposed to copy a tree of objects (the "Queries" tree) over
> from one resource to another resource. The "Queries" tree contains
> references to some other part (called "Schema") of the model instance
> which will not be copied to the destination instance. However the
> destination instance contains its own "Schema" part and the references
> from the copied "Queries" tree need to be recreated by some algorithm.
>
> A picture may make it clearer:
>
> before the copying:
>
> Source instance
> |________
> | |
> Queries Schema
> /\ --> /\
> /#1\ -->/#1\
> / \-->/ \
> ^-- references
>
> Destination instance
> |________
> | |
> Queries Schema
> (empty) /\
> /#2\
> / \
>
>
> after the copying:
>
> Source instance
> |________
> | |
> Queries Schema
> /\ --> /\
> /#1\ -->/#1\
> / \-->/ \
> ^-- references
>
> Destination instance
> |________
> | |
> Queries Schema
> /\ --> /\
> /#1\ -->/#2\
> / \-->/ \
> ^-- recreated references
>
> The whole copy operation should be undoable and the two resources be
> independent from each other, i.e. not in the same ResourceSet.
>
> Any pointers on how to deal with the CopyCommand in general and in our
> special case would be very much appreciated.
>
> Thanks and Regards,
> Dirk
Re: How to use CopyCommand [message #488048 is a reply to message #488028] Fri, 25 September 2009 12:35 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Dirk,

Comments below.

Dirk Hoffmann wrote:
> Some additions to the previous post:
>
> 1. The cause of the UnsupportedOperationException was the collection
> passed to the create method being empty.
>
> 2. It came to my mind that it would be nice for the users being able to
> use copy&paste as an additional means to perform the copy operation.
> That would mean using the clipboard as the operartion spans two distinct
> resources. The generated editor apparently doesn't provide that out of
> the box. Could someone please provide some hints about how to achieve
> that?
You could override the AdapterFactoryEditingDomain.setClipboard to do
more than just cache the collection locally in the editing domain instance.
>
> 3. The copy&paste idea brought me a bit further regarding the general
> use of the CopyCommand (at least I believe so):
>
> Looking at the code of the PasteFromClipboardCommand I found that
> CopyCommand needs to be combined with AddCommand by putting them into
> a CompoundCommand. I did it like so:
>
> ---- 8< -----------------
> StrictCompoundCommand copyAndAddCommand =
> new StrictCompoundCommand("Import Queries");
>
> final Command copyCommand = CopyCommand.create(_editingDomain, queries);
> copyAndAddCommand.appendAndExecute(copyCommand);
>
> copyAndAddCommand.appendAndExecute(new CommandWrapper() {
> @Override
> protected Command createCommand() {
> EReference feature =
> SbqPackage.Literals.QUERIES__HOUSE_NQUERIES;
> Command addCommand = AddCommand.create(_editingDomain,
> _dstQueries, feature, copyCommand.getResult());
> return addCommand;
> }
> });
>
> CommandStack commandStack = _editingDomain.getCommandStack();
> commandStack.execute(copyAndAddCommand);
> ---- >8 -----------------
>
> This does a deep copy letting copied references point back into the
> source model instance. So what I still have to implement is some
> method to recreate the references so they point to the corresponding
> objects in the destination instance, and of course the copy&paste thingy.
If you look closely at how the CopyCommand itself is factored, you'll
see CreateCopyCommand and InitializeCopyCommand are involved for the
individual parts of the deep structure, so you could focus on
specializing these. Note that the Helper is used for remapping, do
likely you can use that to redirect the references.
>
> Thanks and Regards,
> Dirk
>
>
> Dirk Hoffmann schrieb:
>> Hi,
>>
>> already searched the mailing list, the book and the internet but
>> found nothing appropriate to teach me how to use CopyCommand.
>>
>> As naiv as I am I started with
>>
>> CopyCommand.create(final EditingDomain domain, final Collection
>> collection)
>>
>> and immediately wondered where the destination of the copy operation
>> would have to go. Started anyway and got a
>> UnsupportedOperationException.
>>
>> I probably need to flesh out some details of the copy operation but
>> have no idea how to go about that or what's the best practice here.
>>
>> What's going to make the task even more complicated is the fact that
>> the copying is supposed to copy a tree of objects (the "Queries"
>> tree) over from one resource to another resource. The "Queries" tree
>> contains references to some other part (called "Schema") of the model
>> instance which will not be copied to the destination instance.
>> However the destination instance contains its own "Schema" part and
>> the references from the copied "Queries" tree need to be recreated by
>> some algorithm.
>>
>> A picture may make it clearer:
>>
>> before the copying:
>>
>> Source instance
>> |________
>> | |
>> Queries Schema
>> /\ --> /\
>> /#1\ -->/#1\
>> / \-->/ \
>> ^-- references
>>
>> Destination instance
>> |________
>> | |
>> Queries Schema
>> (empty) /\
>> /#2\
>> / \
>>
>>
>> after the copying:
>>
>> Source instance
>> |________
>> | |
>> Queries Schema
>> /\ --> /\
>> /#1\ -->/#1\
>> / \-->/ \
>> ^-- references
>>
>> Destination instance
>> |________
>> | |
>> Queries Schema
>> /\ --> /\
>> /#1\ -->/#2\
>> / \-->/ \
>> ^-- recreated references
>>
>> The whole copy operation should be undoable and the two resources be
>> independent from each other, i.e. not in the same ResourceSet.
>>
>> Any pointers on how to deal with the CopyCommand in general and in
>> our special case would be very much appreciated.
>>
>> Thanks and Regards,
>> Dirk
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[Teneo] database create stops responding with 1.1.RC4b
Next Topic:Override Drag and Drop
Goto Forum:
  


Current Time: Fri Apr 19 06:49:42 GMT 2024

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

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

Back to the top