How to delete Views/GraphicalEditParts from diagram programmatically? [message #908177] |
Tue, 04 September 2012 17:54  |
Eclipse User |
|
|
|
Hello guys,
I want to delete a GraphicalEditPart (EClassEditPart) or View (GraphicalEditPart.getModel()) from a GMF diagram (not the model!), i.e., I want to execute the action "Delete from Diagram" from the context menu programmatically. I have seen, that this action creates GroupRequestViaKeyboard with RequestConstants.REQ_DELETE as parameter. So I tried the following:
import org.eclipse.emf.ecoretools.diagram.part.EcoreDiagramEditor;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
import org.eclipse.gmf.runtime.diagram.ui.requests.GroupRequestViaKeyboard;
EcoreDiagramEditor diagramEditor = ...;
IDiagramGraphicalViewer viewer = diagramEditor.getDiagramGraphicalViewer();
EClassEditPart editpart = ...; //e.g., get with viewer.findEditPartsForElement() or viewer.getEditPartRegistry()
// selecting editpart
viewer.select(editpart);
// creating the delete request
GroupRequestViaKeyboard deleteRequest = new GroupRequestViaKeyboard(RequestConstants.REQ_DELETE);
deleteRequest.setShowInformationDialog(false);
// get command
Command command = diagramEditor.getDiagramEditPart().getCommand(deleteRequest));
// execute command
diagramEditor.getDiagramEditDomain().getDiagramCommandStack().execute(command);
However, I get an org.eclipse.gef.commands.UnexecutableCommand object. I debugged both the context menu (or press delete key) call and my code. The reason I get a unexecutable command, is that my code ends up in org.eclipse.gef.editpolicies.ComponentEditPolicy.getCommand() whereas the delete call get to org.eclipse.gmf.runtime.diagram.ui.editpolicies.ComponentEditPolicy.getCommand(). I do not understand why this is happening...
Is there another way to delete an element from the diagram such that the underlying model remains untouched? Please help. Thanks in advance.
Regards,
Andrej
|
|
|
|
[Solved] How to delete Views/GraphicalEditParts from diagram programmatically? [message #908353 is a reply to message #908327] |
Wed, 05 September 2012 04:13  |
Eclipse User |
|
|
|
Hello Ralph,
thank you, It worked!
For whose how have not understood the post, hope this can help:
import org.eclipse.emf.edit.command.RemoveCommand;
EcoreDiagramEditor diagramEditor = ...;
IDiagramGraphicalViewer viewer = diagramEditor.getDiagramGraphicalViewer();
EClassEditPart editpart = ...; //e.g., get with viewer.findEditPartsForElement() or viewer.getEditPartRegistry()
// here the new part
EditingDomain editingDomain = diagramEditor.getEditingDomain();
editingDomain.getCommandStack().execute(RemoveCommand.create(editingDomain, editpart.getModel());
Cheers,
Andrej
|
|
|
Powered by
FUDForum. Page generated in 0.32773 seconds