https://dzone.com/articles/netbeans-lookups-explained

————————————————————————————————————————————————————————

Lookups are one of the most important parts of the NetBeans Platform. They're used almost everywhere and most of the time when you ask something on a mailing list the answer is "Use Lookups!". Many times when the use of Lookups is explained it's in a very specific context, e.g. selection management or ServiceLoaders. That makes Lookups look complicated and hard to understand, while actually they are very simple and extremely powerful.

That's why I guess it's time to write an article that explains what lookups actually are and how they work. In the subsequent parts I'll explain how they can be used to solve some common problems.

Lookup as a Data Structure

So first let's have a look at Lookup as a data structure. A Lookup is a map with Class Objects as keys and a Set of instances of the key Class object as values. An additional feature of a Lookup is that you can listen for changes of what's in there. It's as simple as that! If you want to ask a lookup for it's contents, you can do so like this:

Lookup  lookup = //get a lookup somewhere...
Collection <String> strings =lookup.lookupAll(String.class);

If you want to listen for changes, you add your Listener to Lookup.Result an inner class that represents a query result. This way you add a Listener that listens for addition or removal of objects of a certain class:

Lookup.Result <String> strings = lookup.lookupResult(String.class);
strings.allItems();
strings.addLookupListener(new LookupListener(){
@override
public void resultChanged(LookupEvent e){
// do something
}}
);

This is how you usually use an existing lookup. If you want to create one, there are some implementations to help you. The most basic one is Lookups.Singleton, a Lookup that only contains one object:

Lookup simple = Lookups.singleton("Hello World!"); 

There's also an implementation for creating a lookup with more than one entry, still with fixed content:

Lookups moreElements = Lookups.fixed( "Hello", "World", new Integer(5) ); 

If you want to use a Lookup to dynamically put in stuff you'll need to choose an implementation that supports that. The most flexible one is using an InstanceContent Object to add and remove stuff:

InstanceContent content = new InstanceContent();
Lookup dynamicLookup = new AbstractLookup(content);
content.add("Hello");
content.add(5);

Listeners registered for the matching class will be informed when something changes. If you would like to query more than one Lookup at a time you can use a ProxyLookup. This for example, combines two of the Lookups created above into one:

ProxyLookup proxy = new ProxyLookup(dynamicLookup, moreElements); 

Lookup.Provider

If your Object has a Lookup to store a bunch of stuff, you can make it accessible to others by implementing Lookup.Provider an interface with only one method:

public Lookup getLookup();

Again, extremely simple. Someone interested in what's in your Objects Lookup can ask for it and register a listener. In NetBeans TopComponents implement this interface, so you can ask any TopComponent for it's Lookup. The easiest way to get hold of most of the TopComponents is via their ID:

TopComponent tc = WindowManager.getDefault().findTopComponent("AnInterestingTopComponent");
Lookup tcLookup = tc.getlookup();

As most TopComponents put into their Lookup whatever is selected, for example the entries in a list, you can add a Listener to track the Selection in a TopComponent. If your for example interested in the selected Nodes you can do it like this:

Lookup.result <Node> noderesult = tcLookup.lookupResult(Node.class);
result.allInstances();
noderesult.addLookuplistener(myLookupListener);

That's especially handy when you want to provide a Master-Detail-View. If you want to provide your own Lookup in your TopComponent you do it like this:

associateLookup(mylookup); 

Global Selection

Sometimes you might be interested not only in what is selected in one specific TopComponent, but in whatever TopComponent currently has the focus. That's easy as well because NetBeans provides a Lookup that proxies the Lookup of the TopComponent that currently has the focus. To use this you simply need to do this:

Lookup global = Utilities.actionsGlobalContext();

You can use this like any other Lookup and register your listeners, no magic involved.

Nodes

Nodes also implement Lookup.Provider so you can ask them for their Lookup as well. Something useful to store inside a Node's Lookup is the DataObject it may represent. If you're using Nodes you probably do so in combination with the Explorer API to display them. If you do that you'll usually create a lookup for your TopComponent with the help of the ExplorerManager:

associateLookup(ExplorerUtils.createLookup ( explorermanager, this.getActionMap() ) );

The resulting Lookup also proxies the content of the selected Nodes Lookup. This way everything someone might be interested in shows up in your TopComponent's Lookup.

Service Loader and other uses

As you've seen Lookups are actually a very simple yet powerful concept. Some articles here on NetBeans Zone also cover the use of Lookups for loading services in a NetBeans RCP application, which is also an important use. To do that NetBeans provides a default Lookup that looks in certain places for service registrations, e.g. in the META-INF/services folder of a modules jar file and in the modules layer.xml. If you're interested in getting an instance of a Service implementation you can do it like this:

Collection <ServiceInterface> services= Lookup.getDefault.lookupAll(ServiceInterface.class); 

If you register your service in the layer.xml you can get a lookup for a certain Folder like this:

Lookup lkp = Lookups.forPath("ServiceProviders");

I guess that's all you need to know to get started with Lookups, so have fun trying it out, it's really simple.

NetBeans Lookups Explained!的更多相关文章

  1. 解决NetBeans运行卡顿问题

    NetBeans安装目录下的此文件打开编辑 找到这一行,在后面添加最大的运行内存,这里我改成了900M(红色部分),重启NetBeans即可.netbeans_default_options=&quo ...

  2. 解决NetBeans运行web项目时出现的“未能正确设置java DB”问题

    1.在NetBeans导航器中,点击"服务"选项卡: 2.展开"数据库"菜单: 3.在"Java DB"上右键 –> 选择" ...

  3. NetBeans invalid jdkhome specified 问题解决方法

    JDK的路径变化会导致 NetBeans 启动时出现错误: 解决办法: There's is an easy way to fix this. Navigate to your NetBeans in ...

  4. NetBeans无法使用编码GBK安全打开文件

    刚才使用NetBeans打开php文件时,提示:NetBeans无法使用编码GBK安全地打开该路径下的文件. 找到了解决方案. 原文地址:http://qdjinxin.iteye.com/blog/ ...

  5. NetBeans连接SQL server数据库教程

    不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...

  6. 基于Netbeans的PHPUnit单元测试环境搭建

    一.配置 PHPUnit截至2015-10-16,稳定版已更新至5.0.6,要求使用PHP v5.6及以上的环境才能使用. PHPUnit的4.8系列要求在PHP v5.3.3以上环境使用. Netb ...

  7. Netbeans 8.2关于PHP的新特性

    Netbeans 8.2在这个国庆期间终于发布了,其与PHP相关的新特性主要有: 支持PHP 7 详见前面翻译的一篇文章:Netbeans 8.2将支持PHP 7 编辑器功能增强 文档好像没有明确说明 ...

  8. 使用NetBeans、Eclipse阅读JDK源码

    下面说明在Netbeans.Eclipse环境下怎么查看JDK源码: Netbeans: 在"工具->java平台->源"里添加下路径,如果你安装jdk的时候选择安装了 ...

  9. HttpClient, HttpClientHandler, and WebRequestHandler Explained

    原文地址 https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequest ...

随机推荐

  1. git 权限问题:insufficient permission for adding an object to repository database .git

    在git pull 的时候报错:insufficient permission for adding an object to repository database .git (去仓库里的objec ...

  2. JDK1.5新特性,基础类库篇,集合框架(Collections)

    集合框架在JDK1.5中增强特性如下: 一. 新语言特性的增强 泛型(Generics)- 增加了集合框架在编译时段的元素类型检查,节省了遍历元素时类型转换代码量. For-Loop循环(Enhanc ...

  3. Android开发系列(十五):【Android小游戏成语连连看】第一篇

            学了一个多月安卓.由于暑假的时候要给朋友说写个小游戏.并且也想检測下自己的能力,所以说从7号開始就着手写这个小游戏了,前前后后带上课到今天总算是写完了,可是写的这个小游戏还是有非常多问 ...

  4. LeetCode Permutaions II

    LeetCode解题之Permutaions II 原题 输出一个有反复数字的数组的全排列. 注意点: 反复数字的可能导致反复的排列 样例: 输入: nums = [1, 2, 1] 输出: [[1, ...

  5. 【Unity】10.4 类人动画角色的控制

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 导入角色网格和动画及设置 Avatar 之后,就可以准备开始在游戏中使用它们了.以下部分涵盖 Mecanim 提供的.用 ...

  6. Mysql数据库If语句的使用

    MySQL的if既可以作为表达式用,也可在存储过程中作为流程控制语句使用,如下是做为表达式使用: IF表达式 [sql] view plain copy 如果 expr1 是TRUE (expr1 & ...

  7. 非常详尽的 Shiro 架构解析

    Shiro是什么? Apache Shiro是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密. Apache Shiro的首要目标是易于使用和理解.安全有时候是很复杂 ...

  8. MATLAB学习之内存溢出的管理方法

    今天用Matlab跑程序,由于数据量太大,又出现 Out of memory. Type HELP MEMORY for your options.的问题.看到这篇文章非常实用,转过来方便查阅~ 用 ...

  9. 每日英语:Philippine Chapel Becomes a Medical Center

    In the darkest moments of the killer storm that ripped across this coastal city on Friday, residents ...

  10. vim学习日志(8):linux查看和修改文件编码

    查看文件的编码 方法一: 1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.注:如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码的问题,那么你可 ...