Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » ReplaceCommand for root objects
ReplaceCommand for root objects [message #1140741] Wed, 16 October 2013 15:21 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

I'm using the ReplaceCommand to replace an object with another in the
context of an EditingDomain (so that Undo/Redo work).

First of all

public static Command create(EditingDomain domain, Object value,
Collection<?> collection)

will never work since owner (and then ownerList) will be null, and
execution will fail.

Thus I assume the only correct way of creating such a command is
something like

Command command = ReplaceCommand.create(domain,
original.eContainer(), original.eContainingFeature(),
original, Collections.singleton(edited));

but this will not work for root objects anyway, will it?
For root objects should I create a custom ReplaceCommand?

thanks in advance
cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: ReplaceCommand for root objects [message #1140752 is a reply to message #1140741] Wed, 16 October 2013 15:31 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 16/10/2013 17:21, Lorenzo Bettini wrote:
> Hi
>
> I'm using the ReplaceCommand to replace an object with another in the
> context of an EditingDomain (so that Undo/Redo work).
>
> First of all
>
> public static Command create(EditingDomain domain, Object value,
> Collection<?> collection)
>
> will never work since owner (and then ownerList) will be null, and
> execution will fail.
>
> Thus I assume the only correct way of creating such a command is
> something like
>
> Command command = ReplaceCommand.create(domain,
> original.eContainer(), original.eContainingFeature(),
> original, Collections.singleton(edited));
>
> but this will not work for root objects anyway, will it?
> For root objects should I create a custom ReplaceCommand?
>

OK, I think I can answer myself :)

EObject eContainer = original.eContainer();
EStructuralFeature eContainingFeature = original
.eContainingFeature();
Command command = null;

if (eContainer != null)
command = ReplaceCommand.create(domain, eContainer,
eContainingFeature, original,
Collections.singleton(edited));
else
command = new ReplaceCommand(domain,
original.eResource().getContents(),
original, Collections.singleton(edited));

domain.getCommandStack().execute(command);


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: ReplaceCommand for root objects [message #1140753 is a reply to message #1140741] Wed, 16 October 2013 15:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Lorenzo,

Comments below.

On 16/10/2013 5:21 PM, Lorenzo Bettini wrote:
> Hi
>
> I'm using the ReplaceCommand to replace an object with another in the
> context of an EditingDomain (so that Undo/Redo work).
>
> First of all
>
> public static Command create(EditingDomain domain, Object value,
> Collection<?> collection)
>
> will never work since owner (and then ownerList) will be null, and
> execution will fail.
No, there's code like this to deduce the owner via the value's parent:

else if (commandClass == ReplaceCommand.class)
{
Object obj = commandParameter.getValue();
Object parent = (obj == null) ? null : getParent(obj);
if (parent == null) parent = obj;
return createCommand(ReplaceCommand.class, new
CommandParameter(parent, null, obj, commandParameter.getCollection()));
}
>
> Thus I assume the only correct way of creating such a command is
> something like
>
> Command command = ReplaceCommand.create(domain,
> original.eContainer(), original.eContainingFeature(),
> original, Collections.singleton(edited));
>
> but this will not work for root objects anyway, will it?
By root object you mean one that's contained by a resource?
> For root objects should I create a custom ReplaceCommand?
Or specialize ResourceItemProvider to support the commands you want to
support...
>
> thanks in advance
> cheers
> Lorenzo
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ReplaceCommand for root objects [message #1145442 is a reply to message #1140753] Sat, 19 October 2013 14:19 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 16/10/2013 17:32, Ed Merks wrote:
> Lorenzo,
>
> Comments below.
>
> On 16/10/2013 5:21 PM, Lorenzo Bettini wrote:
>> Hi
>>
>> I'm using the ReplaceCommand to replace an object with another in the
>> context of an EditingDomain (so that Undo/Redo work).
>>
>> First of all
>>
>> public static Command create(EditingDomain domain, Object value,
>> Collection<?> collection)
>>
>> will never work since owner (and then ownerList) will be null, and
>> execution will fail.
> No, there's code like this to deduce the owner via the value's parent:
>
> else if (commandClass == ReplaceCommand.class)
> {
> Object obj = commandParameter.getValue();
> Object parent = (obj == null) ? null : getParent(obj);
> if (parent == null) parent = obj;
> return createCommand(ReplaceCommand.class, new
> CommandParameter(parent, null, obj, commandParameter.getCollection()));
> }

mh... but I create the command myself and execute it in the editing
domain command stack, and that code is not executed... however, I
managed to implement a custom command myself :)

thanks!
Lorenzo


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Previous Topic:Building a Bidirectional Containment Map
Next Topic:Enforcing Uniqueness
Goto Forum:
  


Current Time: Tue Apr 23 16:22:49 GMT 2024

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

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

Back to the top