Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to get the result of a CreateElementCommand when doing Drag 'n Drop
How to get the result of a CreateElementCommand when doing Drag 'n Drop [message #200709] Wed, 06 August 2008 09:28 Go to next message
David Johnson is currently offline David JohnsonFriend
Messages: 10
Registered: July 2009
Junior Member
Hi everyone,

I'm trying to do drag and drop from a tree view onto a diagram view,
where on drop I create a new EditPart. I've managed to do this by doing
something like:

CustomEditPart selectedElement = ( CustomEditPart )getHost();
IElementType type = CustomElementTypes.ICustomElement_2001;
ViewAndElementDescriptor viewDescriptor = new ViewAndElementDescriptor
( new CreateElementRequestAdapter( new CreateElementRequest( type ) ),
Node.class, ( ( IHintedType )type ).getSemanticHint(),
selectedElement.getDiagramPreferencesHint() );
cmd = selectedElement.getCommand( new CreateViewAndElementRequest
(viewDescriptor) );
return cmd;

inside my CustomDragDropEditPolicy.getDropObjectsCommand(). So on drop,
it calls the correct CustomCreateCommand.

However I want to populate the new EditPart's properties with some values
- so how do I get/select the newly created EditPart?

Any ideas and input will be greatly appreciated.

Cheers,
-David
Re: How to get the result of a CreateElementCommand when doing Drag 'n Drop [message #201006 is a reply to message #200709] Thu, 07 August 2008 08:58 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: lunaris.bluemoon.gmail.com

Hi David,

If you keep the request in scope then you will be able to get the new
object from that, but only after the command for that request has been
executed. So you could do something as follows:

<code>

CustomEditPart selectedElement = (CustomEditPart)getHost();

.... // As before, until..

CreateViewAndElementRequest createRequest =
new CreateViewAndElementRequest(viewDescriptor);

cmd = new CompoundCommand();

cmd.add(selectedElement.getCommand(createRequest);

/*
* #getNewObject() will return a list of ViewDescriptors.
* You could also use #getViewAndElementDescriptor(),
* but you must have a CreateViewAndElementRequest to do that;
* #getNewObject() will work with CreateViewRequests also.
*/
cmd.add(
new MyAfterCommand((IAdaptable)createRequest.getNewObject().get( 0));

return cmd;

</code>

Where MyAfterCommand is the command that you want to make
the changes to the new element in. Something like this:

<code>

/*
* If you're changing semantic properties, you'll need a write
* transaction and so it may be worth extending
* AbstractTransactionalCommand instead and using an
* ICommandProxy to wrap it when putting it in the CompoundCommand.
*/
public class MyAfterCommand extends Command {

/**
* When this command is executed this adapter will
* be able to provide the element just created.
*/
private IAdaptable adapter;

public MyAfterCommand(IAdaptable adapter) {
this.adapter = adapter;
}

public void execute() {
EObject newElement = (EObject)adapter.getAdapter(EObject.class);

// Do something will newElement...
}

}

</code>

This is off the top of my head so apologies if there are some errors;
the gist of it should be correct though.

Hope that helps,
Will Jones

David Johnson wrote:
> Hi everyone,
>
> I'm trying to do drag and drop from a tree view onto a diagram view,
> where on drop I create a new EditPart. I've managed to do this by doing
> something like:
>
> CustomEditPart selectedElement = ( CustomEditPart )getHost();
> IElementType type = CustomElementTypes.ICustomElement_2001;
> ViewAndElementDescriptor viewDescriptor = new ViewAndElementDescriptor
> ( new CreateElementRequestAdapter( new CreateElementRequest( type ) ),
> Node.class, ( ( IHintedType )type ).getSemanticHint(),
> selectedElement.getDiagramPreferencesHint() );
> cmd = selectedElement.getCommand( new CreateViewAndElementRequest
> (viewDescriptor) );
> return cmd;
>
> inside my CustomDragDropEditPolicy.getDropObjectsCommand(). So on drop,
> it calls the correct CustomCreateCommand.
>
> However I want to populate the new EditPart's properties with some values
> - so how do I get/select the newly created EditPart?
>
> Any ideas and input will be greatly appreciated.
>
> Cheers,
> -David
Re: How to get the result of a CreateElementCommand when doing Drag 'n Drop [message #201036 is a reply to message #201006] Thu, 07 August 2008 12:32 Go to previous message
David Johnson is currently offline David JohnsonFriend
Messages: 10
Registered: July 2009
Junior Member
Hi Will,

Thanks a lot for your help - I've figured it out from the code you
provided me off the top of your head. Just a couple of modifications did
it, notably I changed:

<code>
cmd.add(new MyAfterCommand((IAdaptable)createRequest.getNewObject().get
(0));
</code>

to

<code>
cmd.add(new MyAfterCommand((Collection<IAdaptable>)
createRequest.getNewObject()).iterator().next());
</code>

and in MyAfterCommand I added in:

<code>
Object o = newElement.eCrossReferences().get( 0 );
if (o instanceof MyModelElement) {
// do stuff here
}
</code>

to modify my model directly.

Anyhow, thanks again for your help, Will!

Cheers,
-David

On Thu, 07 Aug 2008 09:58:41 +0100, Will Jones wrote:

> Hi David,
>
> If you keep the request in scope then you will be able to get the new
> object from that, but only after the command for that request has been
> executed. So you could do something as follows:
>
> <code>
>
> CustomEditPart selectedElement = (CustomEditPart)getHost();
>
> ... // As before, until..
>
> CreateViewAndElementRequest createRequest =
> new CreateViewAndElementRequest(viewDescriptor);
>
> cmd = new CompoundCommand();
>
> cmd.add(selectedElement.getCommand(createRequest);
>
> /*
> * #getNewObject() will return a list of ViewDescriptors. * You could
> also use #getViewAndElementDescriptor(), * but you must have a
> CreateViewAndElementRequest to do that; * #getNewObject() will work
> with CreateViewRequests also. */
> cmd.add(
> new MyAfterCommand((IAdaptable)createRequest.getNewObject().get( 0));
>
> return cmd;
>
> </code>
>
> Where MyAfterCommand is the command that you want to make the changes to
> the new element in. Something like this:
>
> <code>
>
> /*
> * If you're changing semantic properties, you'll need a write *
> transaction and so it may be worth extending *
> AbstractTransactionalCommand instead and using an * ICommandProxy to
> wrap it when putting it in the CompoundCommand. */
> public class MyAfterCommand extends Command {
>
> /**
> * When this command is executed this adapter will * be able to
> provide the element just created. */
> private IAdaptable adapter;
>
> public MyAfterCommand(IAdaptable adapter) {
> this.adapter = adapter;
> }
>
> public void execute() {
> EObject newElement = (EObject)adapter.getAdapter(EObject.class);
>
> // Do something will newElement...
> }
>
> }
>
> </code>
>
> This is off the top of my head so apologies if there are some errors;
> the gist of it should be correct though.
>
> Hope that helps,
> Will Jones
>
> David Johnson wrote:
>> Hi everyone,
>>
>> I'm trying to do drag and drop from a tree view onto a diagram view,
>> where on drop I create a new EditPart. I've managed to do this by doing
>> something like:
>>
>> CustomEditPart selectedElement = ( CustomEditPart )getHost();
>> IElementType type = CustomElementTypes.ICustomElement_2001;
>> ViewAndElementDescriptor viewDescriptor = new ViewAndElementDescriptor
>> ( new CreateElementRequestAdapter( new CreateElementRequest( type ) ),
>> Node.class, ( ( IHintedType )type ).getSemanticHint(),
>> selectedElement.getDiagramPreferencesHint() ); cmd =
>> selectedElement.getCommand( new CreateViewAndElementRequest
>> (viewDescriptor) );
>> return cmd;
>>
>> inside my CustomDragDropEditPolicy.getDropObjectsCommand(). So on drop,
>> it calls the correct CustomCreateCommand.
>>
>> However I want to populate the new EditPart's properties with some
>> values - so how do I get/select the newly created EditPart?
>>
>> Any ideas and input will be greatly appreciated.
>>
>> Cheers,
>> -David
Previous Topic:How to show a pop-up when a connection is made?
Next Topic:is the Layout Service example not compatible with GMF 2.1?
Goto Forum:
  


Current Time: Fri Apr 26 00:38:21 GMT 2024

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

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

Back to the top