我正在开发NetBeans模块,我已经声明了一个操作(使用注释,它们被转换为layer.xml记录),它与我的自定义项目类型(EsperProject类)一起使用:
@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菜单(实际上是Run菜单)运行动作,但是我不能在我需要的两种情况下使它工作(都是在注释中声明的异步调用):
感谢您的任何建议。
答案 0 :(得分:1)
1.我想从项目上下文菜单中运行该操作。
只需将@ActionReference(path =“Project / Actions”,position = 0)添加到@ActionReferences
即可答案 1 :(得分: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 aproach,但这失败了:
@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)