Question

I wanted to show the user a confirmation delete dialog when deleting objects from Eclipse GMF. I have overridden createDeleteSemanticCommand in Component role edit policy for a particular EditPart to include the delete confirmation dialog. Following are the code snippets that i have done.

/////// RESPECTIVE EDIT PART CLASS, where editpolicy is installed

protected void createDefaultEditPolicies() {
installEditPolicy(EditPolicy.COMPONENT_ROLE, new 
            ComponentRoleEditPolicy()); 

/////// RESPECTIVE EDITPOLICY CLASS, where custom delete dialog operations are done (ComponentRoleEditPolicy)

protected Command createDeleteSemanticCommand(GroupRequest deleteRequest)
{
if(deleteRequest instanceof GroupRequestViaKeyboard) {
     if(deleteDialog(foo.getName())) 
            {
                return super.createDeleteSemanticCommand(deleteRequest);
            } 
            else {
                return UnexecutableCommand.INSTANCE;
            }
       }
return UnexecutableCommand.INSTANCE;
}

////// DELETE CONFIRMATION METHOD

public static boolean deleteDialog(String name) {
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    boolean delete = MessageDialog.openConfirm(shell, "Delete", "Are you sure you want to delete "
    + name + " ?");
    return delete;
}

For each different edit parts i have written this piece of code initially to ask for a delete confirmation. Later, I realized selecting multiple editparts in the diagram and performing a delete button, I get 'n' delete confirmation boxes for 'n' pieces selected in the diagram. How to solve this problem. Please help me through this.

Thanks.

Was it helpful?

Solution

Here is the solution that I have followed to solve the problem.

In the custom generated DeleteAction class, Override the following methods

// confirm dialog for rightclicking and selecting "Delete from Model"
@Override
public void runWithEvent(Event event) {
    if(deleteDialog("the selected element(s)"))
        super.runWithEvent(event);
}
// Confirm dialog for pressing delete key. 
@Override
public void run() {
    if(deleteDialog("the selected element(s)")) {
        super.run();
    }
}

to add the delete key action, In generated XXXDiagramEditor class,

// In the generated configureGraphicalViewer() - Change to @Generated NOT
KeyHandler keyHandler = getDiagramGraphicalViewer().getKeyHandler();
keyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0), new DeleteElementAction(this)/*Custom generated delete action*/);
getDiagramGraphicalViewer().setKeyHandler(keyHandler);

After adding these codes, everything worked like a charm. It will ask for a confirmation dialog when we press delete (Even after selecting any number of elements) and does the required operations. Also when we right click and select delete from model it works. Also, we don't need the component role edit policy. I have removed it from installEditPolicy() [also the editPolicy file].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top