Friday April 25, 2008
NetBeans Lookup Example
Lookup example, producing a new message automatically every 2 seconds:
- Provider module:
public final class Selection { private Selection() { } private static MyLookup LKP = new MyLookup(); //Make the Lookup accessible: public static Lookup getSelection() { return LKP; } private static final class MyLookup extends ProxyLookup implements Runnable { private static ScheduledExecutorService EX = Executors.newSingleThreadScheduledExecutor(); public MyLookup() { EX.schedule(this, 2000, TimeUnit.MILLISECONDS); } private int i; @Override public void run() { //Add to the Lookup a new MyHello: setLookups(Lookups.singleton(new MyHello(i++))); EX.schedule(this, 2000, TimeUnit.MILLISECONDS); } } private static final class MyHello implements HelloProvider { private String text; public MyHello(int i) { text = i % 2 == 0 ? "Hello from Tom" : "Hello from Jerry"; } public String sayHello() { return text; } } }public interface HelloProvider { public String sayHello(); } - Consumer module, with dependency on the provider module:
final class HelloTopComponent extends TopComponent implements LookupListener { private static HelloTopComponent instance; private static final String PREFERRED_ID = "HelloTopComponent"; private Resultresult; private HelloTopComponent() { ... ... ... //We have a dependency on the provider module, //where we can access Selection.getSelection(): Lookup lookup = Selection.getSelection(); //Get the HelloProvider from the result: result = lookup.lookupResult(HelloProvider.class); //Add LookupListener on the result: result.addLookupListener(this); //Call result changed: resultChanged(null); } StringBuilder sb = new StringBuilder(); int i; @Override public void resultChanged(LookupEvent arg0) { long mills = System.currentTimeMillis(); Collection extends HelloProvider> instances = result.allInstances(); for (HelloProvider helloProvider : instances) { String hello = helloProvider.sayHello(); sb.append(mills + ": " + hello + "\n"); jTextArea1.setText(sb.toString()); } } ... ... ...
Result:

Apr 25 2008, 07:54:49 AM PDT Permalink
Hi Geertjan,
at the moment I am really interested in things like servicelocator, dependency injection (DI) and the stuff, too. I only knew spring, but some days ago I discovered this container:
http://www.picocontainer.org
(and guice ...)
I like picocontainer, because it is quite small (<200 KB), there are a lot of interesting features and the authors are quite helpful. If you use nanocontainer.org on the top you can e.g. configure the container via xml, beanshell, groovy ...
And I have learned that a singleton could be an anti-pattern:
http://www.picocontainer.org/singleton-antipattern.html
They (from picocontainer) wouldn't use "Lookup Selection.getSelection()" they would use DI:
private HelloTopComponent(Lookup lookup){
...
}
But at the moment I don't know if the picocontainer API would provide sth. similar to the result.addLookupListener method. Maybe the start/stop methods there could help, I'll figure it out if you are interested.
If you would like to see my first (stupid) swing app you can visit:
http://timefinder.sourceforge.net/files/PicoExample-1.1.zip
To build it, just copy dist/lib/* to lib and use netbeans ;-)
Regards,
Peter.
PS: Keep on good posting ;-)
xxx
Posted by 193.204.253.179 on April 28, 2008 at 08:44 AM PDT #


