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

    
//  ...
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值