Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Adding existing domain element to the diagram at specific location
icon9.gif  Adding existing domain element to the diagram at specific location [message #1185958] Thu, 14 November 2013 11:27 Go to next message
Vitaly Savickas is currently offline Vitaly SavickasFriend
Messages: 62
Registered: March 2010
Member
Does anyone know how can I add an existing domain element to the diagram at particular location?

I have spent three days figuring it out and debugging the existing implementation of creation tools from the palette, but nothing works. The CreateViewAndElement request is the closest thing to suit me, BUT I don't want a new element to be created. I already have an element with some attributes set, not belonging to the diagram yet. There's a method setResultElement() on the request, but it does not have any effect - still creates a new empty element.

Cheers,
Vitaly
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 Go to previous message
Vitaly Savickas is currently offline Vitaly SavickasFriend
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

Previous Topic:GMF toolbar actions refresh/update issue
Next Topic:I've got the selection, I need to make invisible old!
Goto Forum:
  


Current Time: Fri Apr 19 02:58:21 GMT 2024

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

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

Back to the top