|
Re: Adding existing domain element to the diagram at specific location [message #1186110 is a reply to message #1185958] |
Thu, 14 November 2013 13:40 |
Vitaly Savickas Messages: 62 Registered: March 2010 |
Member |
|
|
*sigh* As always with GMF, scratching your ear has to be done with a foot. Solved it myself, but had to debug and inspect the whole command chain of the creation tool and adapt it to my needs. Anyway, if you have the same problem, here's how I did it:
// requests and adapters
IElementType type = ComponentsElementTypes.Component_2002;
CreateElementRequest createElementRequest = new CreateElementRequest(
getEditingDomain(), ((View) getModel()).getElement(),
ComponentsElementTypes.Component_2002);
CreateElementRequestAdapter adapter = new CreateElementRequestAdapter(createElementRequest);
ViewAndElementDescriptor descriptor = new ViewAndElementDescriptor(
adapter,
Node.class,
((IHintedType) type).getSemanticHint(),
getDiagramPreferencesHint());
CreateViewAndElementRequest request = new CreateViewAndElementRequest(descriptor);
// element command (customised to return an existing element)
Command createElementCommand = new ICommandProxy(
new EventBComponentCreateCommand((CreateElementRequest) descriptor.getCreateElementRequestAdapter().getAdapter(CreateElementRequest.class)) {
@Override
protected CommandResult doExecuteWithResult(
IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
EventBComponent newElement = (EventBComponent) Platform
.getAdapterManager().getAdapter(element,
Component.class);
ComponentDiagram owner = (ComponentDiagram) getElementToEdit();
owner.getComponents().add(newElement);
doConfigure(newElement, monitor, info);
((CreateElementRequest) getRequest())
.setNewElement(newElement);
return CommandResult.newOKCommandResult(newElement);
}
});
// semantic command
SemanticCreateCommand semanticCommand = new SemanticCreateCommand(adapter, createElementCommand);
// view command
TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
CreateCommand viewCommand = new CreateCommand(editingDomain, descriptor, (View) (getHost().getModel()));
// relocate command
Point dropLocation = dropRequest.getLocation().getCopy();
getFigure().translateToRelative(dropLocation);
SetBoundsCommand relocateCommand = new SetBoundsCommand(editingDomain, "Set location", request.getViewAndElementDescriptor(), dropLocation);
// compound command
CompositeCommand cc = new CompositeCommand(semanticCommand.getLabel());
cc.compose(semanticCommand);
cc.compose(viewCommand);
cc.compose(relocateCommand);
return new ICommandProxy(cc);
The resulting command is similar to what you get when you drag a tool from the palette to the diagram, except you can customise the domain element here. For a simple tasks it is however quite a heavy solution, in my opinion. So, if anyone knows a simpler way, you are very welcome to share it.
Cheers,
Vitaly
[Updated on: Thu, 14 November 2013 13:42] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02447 seconds