How to inject a new Action into existing Node?

mac2022-06-30  22

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);    }     //  ... }

 

 

 

 

转载于:https://www.cnblogs.com/cuizhf/archive/2011/09/06/2168678.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)