博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to inject a new Action into existing Node?
阅读量:7118 次
发布时间:2019-06-28

本文共 3368 字,大约阅读时间需要 11 分钟。

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

 

 

 

 

你可能感兴趣的文章
领域驱动设计系列(2)浅析VO、DTO、DO、PO的概念、区别和用处
查看>>
Java的反射机制(Reflection)
查看>>
李洪强iOS经典面试题156 - Runtime详解(面试必备)
查看>>
转 文件路径相关的字符串操作
查看>>
mysql 5.6 分区与不分区的区别
查看>>
Material Theme
查看>>
mysql 字符串函数
查看>>
为什么zookeeper集群中节点配置个数是奇数个?
查看>>
TCP/IP协议详解内容总结(怒喷一口老血)
查看>>
RedHat Linux 5企业版开启VNCSERVER远程桌面功能[转]
查看>>
更改Zend Studio/Eclipse代码风格主题
查看>>
RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍
查看>>
leetcode第一刷_Count and Say
查看>>
Leetcode: Excel Sheet Column Number
查看>>
李炯生同志去世
查看>>
如何在Oracle中导入dmp文件
查看>>
iOS - OC NSLocale 本地化信息
查看>>
异构GoldenGate 12c 单向复制配置
查看>>
Leetcode: Rearrange String k Distance Apart
查看>>
android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
查看>>