NetBeans Lookups explained!

Source: Internet
Author: User
Tags netbeans

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

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

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

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

Lookup as a Data Structure

So first let ' s has 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-a Lookup is so 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 are want to listen for changes, you add your Listener to lookup.result an inner class that represents a query Result. This is 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 the usually use of an existing lookup. If you want to create one, there is some implementations to help you. The most basic one are Lookups.singleton, a Lookup that's only contains one object:

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

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 a 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 would be a informed when something changes. If you would-to-query more than one Lookup at a time you can use a proxylookup. This for example, combines, the lookups created above into one:

Lookup.provider

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

Public Lookup getlookup ();

Again, extremely simple. Someone interested in "s in your Objects Lookup can ask for it and register a listener. In the NetBeans topcomponents implement this interface, so you can ask any topcomponent for it ' s Lookup. The easiest-to-get hold of the 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 are selected, for example the entries in a list, can add a listene R to track the Selection in a topcomponent. If your For example interested in the selected Nodes you can does it like this:

Lookup.result <Node> Noderesult = Tclookup.lookupresult (Node.class);
Result.allinstances ();

That's especially handy when you want to provide a master-detail-view. If you want to provide your own Lookup in your topcomponent what do it like this:

Global Selection

Sometimes you might being interested not @ What's selected in one specific topcomponent, but in whatever topcomponent currently has the focus. That's easy as well because NetBeans provides a lookup this proxies the lookup of the topcomponent that currently have the Focus. To use the this you simply need to does this:

Lookup global = Utilities.actionsglobalcontext ();

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

Nodes

Nodes also implement Lookup.provider so what 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 have the using Nodes you probably does in combination with the Explorer API to display them. If you do so 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 is the everything someone might is interested in shows up in your topcomponent ' s Lookup.

Service Loader and other uses

As you ' ve seen lookups is 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, whic H is also a important use. To does that NetBeans provides a default Lookup this 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:

IF You register your service in the layer.xml you can get a lookup of a certain Folder like this:

Lookup LKP = Lookups.forpath ("Serviceproviders");

I guess that's all your need to know to get started with lookups, so has fun trying it out, it's really simple.

NetBeans Lookups explained!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.