1

在我的 Eclipse RCP 应用程序中,我有一个 TreeViewer,我可以从中选择不同的编辑器来绘制元素,双击后会显示这些元素。在我的顶部菜单中,我有一个允许启用/禁用绘图的选项。编辑器的操作如下所示:

public class EnableEditorAction implements IEditorActionDelegate {

IEditor hallEditor = null;

@Override
public void run(IAction action) {       
    if (hallEditor != null){
        hallEditor.setMachineHallEditMode(true);
    }       
}

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
    // check for enabled
    boolean bEnabled = false;
    if (targetEditor != null && targetEditor instanceof IMachineHallEditor) {
        hallEditor = (IMachineHallEditor) targetEditor;
        bEnabled = !hallEditor.isMachineHallEditingMode();
    } 
    action.setEnabled(bEnabled);
}

@Override
public void selectionChanged(IAction action, ISelection selection) {
    if (hallEditor != null) {
        action.setEnabled(!hallEditor.isMachineHallEditingMode());
    }       
}

}

我遇到的问题是只有在编辑器中单击时才启用菜单选项。我想要的是在单击左侧 TreeViewer 中的一个编辑器后也启用菜单选项。

我该怎么做?

4

1 回答 1

0

首先,您不需要检查是否targetEditornull,因为该操作已经通过plugin.xml.

其次,我可以看到你有一个 API isMachineHallEditingMode()。这应该告诉您是否选择了左树,并且该操作应该正常工作。

将您的操作设置始终plugin.xml. 该Enables for:参数应该为空,因为启用处理是在您的selectionChanged.

public class EnableEditorAction implements IEditorActionDelegate {

     IEditor hallEditor;

     @Override
     public void run(IAction action) {       
          hallEditor.setMachineHallEditMode(true);
     }       

     @Override
     public void setActiveEditor(IAction action, IEditorPart targetEditor) {
          hallEditor = (IMachineHallEditor) targetEditor;
     }

     @Override
     public void selectionChanged(IAction action, ISelection selection) {
          action.setEnabled(!hallEditor.isMachineHallEditingMode());
     }       
}
于 2013-08-09T10:08:50.973 回答