2011-04-30 89 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