1

In my eclipse RCP application I have a TreeViewer from where I can select different editors, for drawing elements, which show up after double clicking. In my top menu I have an option that allows to enable/disbale the drawing. The action for the editors looks like the following:

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

}

The problem I have is that the menu option is only enabled when clicking inside an editor. What i want is to enable the menu option also after clicking on one of the editors in the TreeViewer to the left.

How would I do that?

Markus
  • 1,452
  • 2
  • 21
  • 47

1 Answers1

0

First, you don't need to check if targetEditor is null, since the action is already hooked to the editor via plugin.xml.

Second, I can see that you have an API isMachineHallEditingMode(). This should tell you if the left tree is selected, and the action should work properly.

It's important to set your action to always enabled in the plugin.xml. The Enables for: parameter should be empty, because enablement handling is done in your 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());
     }       
}
Georgian
  • 8,795
  • 8
  • 46
  • 87