2011-04-30 116 views
1

我正在開發NetBeans模塊,並且已經聲明瞭一個與我的自定義項目類型(EsperProject類)一起工作的操作(使用註釋,它們被轉換爲layer.xml記錄):NetBeans模塊中的操作 - 上下文菜單,主菜單

@ActionID(category = "Run", id = "my.package.RunEsperAction") 
@ActionRegistration(displayName = "My Action", asynchronous=true) 
@ActionReferences({ 
    @ActionReference(path = "Menu/BuildProject", position = 0) 
}) 
public final class RunEsperAction implements ActionListener { 

    private final EsperProject project; 

    public RunEsperAction(EsperProject project) { 
     this.project = project; 
    } 

    @Override 
    public void actionPerformed(ActionEvent ev) { 
     // do sth with project 
    } 
} 

我可以運行從BuildProject菜單中選擇操作(這是actualy運行菜單),但我不能讓它在兩種情況下工作,我需要(這兩個異步調用作爲註解來聲明):

  1. 我想從項目上下文菜單中運行該操作。
  2. 我需要從主菜單項「運行主項目」中運行我的EsperProject時觸發的操作 。

感謝您的任何建議。

回答

0

我已經想通了,第一點:要運行從上下文菜單的行動,我們必須將它添加到的getActions()方法的項目根節點的,是這樣的:

class RootNode extends FilterNode { 
    @Override 
    public Action[] getActions(boolean arg0) { 
     List<Action> nodeActions = new ArrayList<Action>(); 
     nodeActions.addAll(Utilities.actionsForPath("Actions/MyCategoryInLayer")); 
     return nodeActions.toArray(new Action[nodeActions.size()]); 
    } 
} 

行動出現在項目的上下文菜單中並且異步運行。但是,重寫「運行主項目」對我來說仍然是不可能的。我嘗試simmilar形式給出,但是這個失敗:

@Override 
public void invokeAction(String actionName, Lookup lookup) throws IllegalArgumentException { 

if (actionName.equalsIgnoreCase(ActionProvider.COMMAND_RUN)) { 
    List<? extends Action> runActions = Utilities.actionsForPath("Actions/Run"); 
     for (Action action : runActions) { 
      action.actionPerformed(null); 
     } 
} 

java.lang.NullPointerException at org.openide.util.actions.ActionInvoker$ActionRunnable.needsToBeSynchronous(ActionInvoker.java:147) 
1

1.I'd喜歡跑從項目上下文菜單中的操作。

將有可能只是通過添加@ActionReference(路徑= 「項目/操作」,位置= 0)到@ActionReferences