The NetBeans Lookup Library and... Bernard Madoff!!

So what do the NetBeans Lookup Library and Bernard Madoff have in common?

That both of them can pull rabbits out of hats.

Well, almost. The fact is that the NetBeans Lookup Library can pull out rabbits out of hats, and that B. Madoff would like to instead.

But, how come?! Can the NB Lookup Library do that?

Well, yes. And that's one of its main strenghts. Because it allows you to uncouple event senders from event listeners. Much like an EventBus does.

The NetBeans Lookup Library can be used to send arbitrary objects from one NetBeans module to another, quite easily.

In previous entries I explained how to use the NetBeans Lookup Library to search stuff, how to create your custom lookups and how to use it to design incredibly transmogrifying apis.

In this entry I'll explain how to use it to pull out rabbits out from hats.

Step I: Get a hat and add your rabbits

NetBeans hats are called "Lookup" objects. You can then decorate your objects with a hat, and let your objects add rabbits to the hat whenever something important happens.

Let's see some examples:

  • You can have a hat called MimeLookup.getLookup("text/x-java") that adds rabbits when the indent settings of "text/x-java" files are changed.
  • Or you can decorate a Java file (JavaDataObject x) with a hat (x.getLookup()) that adds rabbits of type SaveCookie whenever the file needs to be saved.
  • Or say you have a window (a TopComponent) with a hat (returned by the "getLookup" method). You can add rabbits to the window's hat adding (for instance) the stuff selected in the window.
  • Or you can even have a global object (Utilities.actionsGlobalContext()) with a hat (a lookup) that you can use to track the objects (rabbits) that have been selected by the user.

I think you get the idea: you can decorate your objects with a hat (a Lookup object), and add rabbits to the hat of your object (whatever stuff you want, ranging from change events to selection details to properties such as "needs saving" flags). That way other people can detect changes in the hat, and react to those.

For exact details on how to create hats and add rabbits you can take a look at this previous entry

Step II: Pulling out rabbits

Pulling out rabbits is very easy with the NetBeans Lookup Library.

You just get the hat and look into it.

For instance, you can see if a Java file needs saving like this:

  JavaDataObject jdo = ...
  if ( jdo.getLookup().lookup( SaveCookie.class ) != null )
  {
    // Yeah, it needs saving
  }

Of course this is pulling out rabbits on demand.

You can also make the rabbits notify you whenever they're pulled out. This is much more convenient (and lazy) mechanism if you don't want to waste time looking-up rabbits all the time.

This mechanism consists of two steps. The first one is to get a "ResultSet" of all rabbits added to the hat, like this:

  JavaDataObject jdo = ...
  // The query
  Lookup.Template<SaveCookie> query =
    new Lookup.Template<SaveCookie>( SaveCookie.class );

  // The resultset
  Lookup.Result result = jdo.getLookup().lookup( query );

And once you have the resultset you just sit down and wait for rabbits to appear out of the hat. Like this:

  result.addLookupListener(
    new LookupListener()
    {
       public void resultChanged( LookupEvent evt )
       {
         System.out.println("D'oh! A rabbit! File needs saving!");
       }
     } );

For more details on how you can "listen" or "lookup" stuff in hats you can take a look at this entry

Summary

So in this entry we've seen how the NetBeans Lookup Library (that, of course, can be used for free in your standalone applications) is a good mechanism to uncouple parts of your applications, allowing you to pull-out rabbits out of hats easily.

Now, I'm sure Mr. Madoff would like to know about the NetBeans Lookup Library earlier. Don't you agree? :-D

Happy rabbit hunting,

Antonio

blog comments powered by Disqus