NetBeans 模块中的操作 - 上下文菜单、主菜单

发布于 2024-11-04 22:28:44 字数 822 浏览 4 评论 0原文

我正在开发 NetBeans 模块,并且已经声明了一个与我的自定义项目类型(EsperProject 类)配合使用的操作(使用注释,它们被转换为 layer.xml 记录):

@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菜单),但我无法使其在我需要的两种情况下工作(都按照注释中声明的方式异步调用):

  1. 我想从项目上下文菜单运行该操作。
  2. 我需要在运行 EsperProject 时触发操作 从主菜单项“运行主项目”。

感谢您的任何建议。

I am developing NetBeans module and I have declared an action (using annotations, they are translated to layer.xml record) which works with my custom project type (EsperProject class):

@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
    }
}

I can run the action from the BuildProject Menu (which is actualy Run menu), but i CANNOT make it work in two cases I need (both called asynchronously as declared in the annotation):

  1. I'd like to run the action from the project context menu.
  2. I need to have the action triggered when my EsperProject is run
    from the main menu item "Run Main Project".

Thanks for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2024-11-11 22:28:44

1.我想从项目上下文菜单运行该操作。

只需将 @ActionReference(path = "Project/Actions",position = 0) 添加到 @ActionReferences 即可

1.I'd like to run the action from the project context menu.

It will be possible just by adding @ActionReference(path = "Project/Actions", position = 0) to @ActionReferences

亢潮 2024-11-11 22:28:44

我已经弄清楚了第一点:要从上下文菜单运行操作,我们必须将其添加到项目根节点的 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()]);
    }
}

操作出现在项目的上下文菜单中并运行异步地。但是,覆盖“运行主项目”对我来说仍然是不可能的。我尝试了类似的方法,但这失败了

@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)

I have figured out the first point: To run the action from the context menu, we must add it to the getActions() method of the project root node, something like this:

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

The action appears in the context menu of the project and is run asynchronously. However, overriding "Run main project" is still imposible for me. I try the simmilar aproach but this fails:

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

with

java.lang.NullPointerException at org.openide.util.actions.ActionInvoker$ActionRunnable.needsToBeSynchronous(ActionInvoker.java:147)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文