zoukankan      html  css  js  c++  java
  • How to inject a new Action into existing Node?

    http://netbeans-org.1045718.n5.nabble.com/How-to-inject-a-new-Action-into-existing-Node-td2986918.html


    Hi,

    I have two modules A and B where B should bring some extensions to A.

    The module A is a source of custom Nodes and I need to add a new Action into the context menu of such nodes.
    How can I do that in module B declaratively? I looked at several samples and Wiki, but nowhere found a clear answer on this question.

    Thanks a lot for any help.

    Vlad.
    Vlad wrote:
    > Hi,
    >
    > I have two modules A and B where B should bring some extensions to A.
    >
    > The module A is a source of custom Nodes and I need to add a new Action into the context menu of such nodes.
    > How can I do that in module B declaratively? I looked at several samples and Wiki, but nowhere found a clear answer on this question.
    >

    In A's Node implementation:
        public Action[] getActions( boolean context ) {
            List<Action> actions = new ArrayList<Action>();
            for (Object o :
    Lookups.forPath("MyActionsFolder").lookupAll(Object.class)) {
                if (o instanceof Action) {
                    actions.add((Action) o);
                } else if (o instanceof JSeparator) {
                    actions.add(null);
                }
            }
            return actions.toArray(new Action[actions.size()]);
        }

    In B's layer file:
    <folder name="MyActionsFolder">
        <file name="MyBeeAction.instance"/>
    </folder>

    You've got the idea.

    > Thanks a lot for any help.
    >
    > Vlad.

    另外,为将当前所选结点的信息传递给MyBeeAction

    1 在A的Node实现中稍微修改一下:

    PeerInfo peerInfo = getLookup().lookup(PeerInfo.class);   // 假设peerInfo为当前选中结点信息

    Action[] actions = ...  // 获得Action数组(代码略)

    for(int i=0; i<actions.length; i++) {

        actions[i].putValue("PEERINFO", peerInfo);

    }

    return actions;

    2 在 B的Action中接收

    public void actionPerformed(ActionEvent e) {

         PeerInfo = (PeerInfo)getValue("PEERINFO");

        // other stuff...

    }

     -----------------------------------------------------------------------------------------------------------------------

    上面只是一个临时的解决方式,估计Action类中在设计putValue和getValue时的本意并不是这样的。

    还没有找到优雅一点的解决方式。

     -----------------------------------------------------------------------------------------------------------------------

    在《NetBeans Platform 6.9 Developer's Guide》中发现一条线索,似乎org.openide.util.Utilities.actionsGlobalContext就是干这个用的。

     The static method Utilities.actionsGlobalContext() gives you access to the Lookup of whichever TopComponent currently is the Activated TopComponent.

    As the user can only work on one TopComponent at a time, there is always at most one active TopComponent. With that in mind, you never needed to merge the Lookups of the two TopComponents at all. 

    尚未测试,也许我的理解有错误。

    -------------------------------------------------------------------------------------------------------------------------

    另外,在该书P143页有一段EditAction的代码:

    public class EditAction extends AbstractAction implements LookupListener, Presenter.Toolbar {

        
    private Lookup.Result<Task> result;
        
    private JButton toolbarBtn;

        
    public EditAction() {
            
    this(Utilities.actionsGlobalContext());
        }

        
    public EditAction(Lookup lookup) {
            
            
    super("Edit Task..."new ImageIcon("com/netbeansrcp/taskactions/Universal.png")));

            
    this.result = lookup.lookupResult(Task.class);
            
    this.result.addLookupListener(this);
            
    this.resultChanged(new LookupEvent(result);
        }

        
    public void actionPerformed(ActionEvent arg0) {
        
    if(null != this.result && 0<this.result.allInstances().size()) {
            Task task 
    = this.result.allInstances().iterator().next();
            EditAction.openInTaskEditor(task);
        }

        
    // ...
    }

     

     
  • 相关阅读:
    手把手教你用nodejs+SQL Server2012做增删改查
    js实现匀速运动及透明度动画
    SqlServer用sql对表名、字段做修改
    nodejs 使用fs实现多级联动
    关于此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。
    访问快科技(驱动之家)某个新闻会自动跳转到web.techtoutiao.win
    在windows server上配置java jdk后,可能要些时间生效。
    .net webapi 收不到json 实体类参数,返回的json中带有k__BackingField
    空调 水槽 堵 用吹或吸都可以
    .net 4.0 程序遇到 停止工作 appcrash ,kernelbase.dll 等提示
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2168678.html
Copyright © 2011-2022 走看看