Olivier has posted 1 posts at DZone. View Full User Profile

Mini Tutorial on the NetBeans Lookups

12.23.2011
| 5510 views |
  • submit to reddit

MINI TUTORIAL PART #1

Here's a mini tutorial I made about the usage of the Lookup class to summarize what I've learned so far.

Let's define a few tool classes

class Tool{}
class Screwdriver extends Tool{}
class Hammer extends Tool{}

That makes a lot of tools... Let's put a few of them into a handy bag !

InstanceContent myContent = new InstanceContent();
Lookup myHandyBag = new Lookup(myContent);

myContent.add( new Screwdriver() );
myContent.add( new Hammer() );
myContent.add( new Hammer() );

Let's lookup what is in my bag !

Collection<Tool> allMyTools = myHandyBag.lookupAll(Tool.class);
System.out.println("What tools do I have ?");
for(Tool myTool : allMyTools){
    System.out.println("I've got a "+myTool.getClass().getName());
}

What if I'm looking for screwdrivers right now, not just any Tool...

Collection<Screwdriver> allMyScrewdrivers = myHandyBag.lookupAll(Screwdriver.class);
System.out.println("What screwdrivers do I have ?");
for(Screwdriver myScrewdriver : allMyScrewdrivers){
    System.out.println("I've got a "+myScrewdriver.getClass().getName());
}

Great, I can very easily find the content of my bag, and even specific content-only !

MINI TUTORIAL PART #2

But what if someone else puts a tool of his in my bag ? I'd love to know...
So I need someone to watch my bag !
Here's how to put a guy watching my bag. Great, he'll tell me as soon as anybody puts something in the bag !

class GuyWatchingMyBag implements LookupListener{
    @override
    public void resultChanged(LookupEvent e){
        System.out.println("Hey, someone has put (or removed) a tool into your bag !");
    }
}

GuyWatchingMyBag harry = new GuyWatchingMyBag();
Lookup.Result<Tool> bagWatch = myHandyBag.lookupResult(Tool.class);
bagWatch.addLookupListener( harry );

//let's try it !
//[note : it would be better for the tutorial if another class made that, or maybe it could be triggered by a button ? just to show it's external]
myContent.add( new Screwdriver() );
myContent.add( new Hammer() );
//I should get a message in the output ! Great !

I can also have a guy watch only the addition of specific Tool classes !

//let's tell harry to stop watching the bag first
bagSpecificWatch.removeLookupListener( harry );

GuyWatchingMyBag james = new GuyWatchingMyBag();
Lookup.Result<Tool> bagSpecificWatch = myHandyBag.lookupResult(Hammer.class);
bagSpecificWatch.addLookupListener( james );

//Let's try it !
//[note : it would be better for the tutorial if another class made that,  or maybe it could be triggered by a button ? just to show it's external]
myContent.add( new Screwdriver() );
myContent.add( new Hammer() );
//Great ! You have only been alerted that the hammer was added, and haven't been disturbed for the Screwdriver that was added since it did not interest you.

Let's let anybody watch my bag, so if they need tools, they can get them !

public Lookup getLookup(){
    return myHandyBag;
}
//This is exactly the implementation of a Lookup.Provider interface.

MINI TUTORIAL CONCLUSION

How can this be useful in the problem of modules communication ?

  • There is already a global lookup, which any modules can Listen to.
  • It is called Utilities.actionsGlobalContext();
  • It actually proxies to the Lookup of the currently active TopComponent.
  • TopComponents natively implement the Lookup.Provider interface, which means each TopComponent already has a Lookup.
  • A TopComponent's lookup primary purpose is to hold it's selection. This way, thanks to Utilities.actionsGlobalContext(), any Module can watch the selection of the active TopComponent.

This means that without even knowing what TopComponent is active, you now can listen to it's selection !

So, that's the main point of Lookups.

Continue by reading
NetBeans Selection Management Tutorial I.
and
The post put me on the right trail (it covers the same as this mini-tutorial and goes a bit further)

Published at DZone with permission of its author, Olivier Dalang.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Javier Ortiz replied on Mon, 2011/12/26 - 2:57pm

Great basic tutorial! If not done yet please add a link in the NetBeans FAQs

Carl Van Denzen replied on Sun, 2012/01/15 - 9:09am

Thanks for this post.

I think the second code snippet in part #2 the line:

bagSpecificWatch.removeLookupListener( harry );

is wrong, because bagSpecificWatch is not yet declared.

Carl

Matt Coleman replied on Wed, 2012/07/11 - 2:37am

Wonderful and eay to follow tutorial

buffalo graphic design

Mateo Gomez replied on Thu, 2012/08/09 - 12:41am

wow...mini tutorial and it very doable..thanks netbeans!

mexican dessert

Winston Dehaney replied on Mon, 2013/07/01 - 11:40pm

This was really funny, and informative. I have one question though,just to be sure about something. Let's say that I have an action that is in the toolbar of my application. whenever the action is clicked, I wanted to effect some change in the currently viewed topcomponent. I'm assuming that I would need to utilize the lookup api for this. Any suggestion or guidance in this?

Cata Nic replied on Tue, 2013/09/03 - 3:28am

The tutorial is complete and easy to understand. In the end, the results will impress your clients too.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.