多次删除确认 - Eclipse GMF

时间:2013-02-18 12:55:26

标签: eclipse eclipse-plugin eclipse-rcp eclipse-emf eclipse-gmf

我想在从Eclipse GMF中删除对象时向用户显示确认删除对话框。我已在特定EditPart的组件角色编辑策略中覆盖了createDeleteSemanticCommand以包含删除确认对话框。以下是我所做的代码片段。

/////// RESPECTIVE EDIT PART CLASS,其中安装了editpolicy

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

/////// RESPECTIVE EDITPOLICY CLASS,自定义删除对话框操作完成(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;
}

对于每个不同的编辑部分,我最初编写了这段代码,要求删除确认。后来,我意识到在图表中选择多个编辑部分并执行删除按钮,我得到了'n'删除图中所选“n”件的确认框。如何解决这个问题呢。请帮我解决这个问题。

感谢。

1 个答案:

答案 0 :(得分:0)

以下是解决问题的解决方案。

在自定义生成的DeleteAction类中,覆盖以下方法

// 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();
    }
}

添加删除键操作,在生成的XXXDiagramEditor类中,

// 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);

添加这些代码后,一切都像魅力一样。当我们按下删除时(即使选择了任意数量的元素后)它将要求确认对话框并执行所需的操作。当我们右键单击并从模型中选择删除时,它也可以工作。此外,我们不需要组件角色编辑策略。我已将其从installEditPolicy() [还包含editPolicy文件]中删除。