| CARVIEW |
|
uitopics
|
| Summary | An event bus for Swing GUI development. |
|---|---|
| Categories | None |
| License | Common Development and Distribution License |
| Owner(s) | vieiro |
Message from the owner(s)
UITopics
UITopics is a small library that allows redirecting Swing events through topics. This allows for decoupling event sources (components) from listeners (topic listeners).
UITopics is experimental. This is an alpha release. I'm still playing with it so I don't recommend using it in production applications at the moment. Until it reaches a stable phase I'm afraid I won't accept contributions to it (comments and suggestions are welcome, though).
UITopics overview
UITopics decouples event sources from event listeners by using topics. Topics are used as event destinations for publish/subscribe messaging. Different event sources deliver events to one or more topics in a TopicManager. The TopicManager is responsible for delivering those events, either synchronously (in the Swing thread) or asynchronously (in a worker thread) to one or more TopicListeners. The diagram below depicts it:
Creating topic managers
To use UITopics in your application you have to instantiate a TopicManager, and then register event sources in the topic manager and add topics listeners to the topic manager. To create a TopicManager just instantiate it:
TopicManager tm = new TopicManager();
Registering event sources in the topic manager
Once instantiated you can add event sources to the topic manager. To add an event source you must specify the topic to which events from the event source are to be delivered and the types of events (well, listeners, actually) that are to be delivered through that topic.
For instance, imagine I have a JFrame and I want to deliver all WindowEvents generated by the JFrame to the topic "application/mainwindow". The code would then be:
JFrame myFrame = ...
TopicManager tm = ...
tm.addEventSource( "application/mainwindow", myFrame, WindowListener.class );
Imagine now that I want to deliver all ActionEvents and MouseEvents from a JButton to the topic "application/mybutton". Then the code would be:
JButton myButton = ...
TopicManager tm = ...
tm.addEventSource( "application/mybutton", myButton, ActionListener.class, MouseListener.class );
Creating topic listeners
Events delivered from one topic are delivered to all topic listeners that have been previously registered in the same topic manager.
Topic listeners are any sort of object of your liking. Events are delivered to methods annotated with the "TopicListener" annotation.
Let's explain topic listeners with an example:
import java.awt.event.*;
import java.util.EventObject;
public class MyClass
{
@TopicListener( topicName="application/mybutton" )
public void buttonPressed( TopicManager tm, ActionEvent ae )
{
System.out.println("Button pressed");
}
@TopicListener( topicName="application/mybutton" )
public void buttonMouse( TopicManager tm, MouseEvent ae )
{
System.out.println("Mouse event from button");
}
@TopicListener( topicName="application/mybutton", isAsynchronous=true )
public void buttonEvent( TopicManager tm, EventObject eo )
{
System.out.println("Either button pressed or mouse event");
}
}
The class above defines three topic listeners listening on
the same topic "application/mybutton"
- The topic listener named "buttonPressed" will receive only ActionEvents coming from the "application/mybutton" topic.
- The topic listener named "buttonMouse" will receive only MouseEvents coming from the "application/mybutton" topic.
- The topic listener named "buttonEvent" will receive any event (either ActionEvent or MouseEvent or any other extending EventObject) coming from the "application/mybutton" topic. Since the asynchronous flag is set (isAsynchronous=true) then events delivered to this topic listener will be executed by a worker thread, and not by the Swing thread.
Registering topic listeners in topic managers
You must register topic listeners into topic managers for the events to be delivered. You can associate as many topic listeners as you want to a single topic manager. The code below shows how to register a topic listener to a topic manager
TopicManager tm = ...
MyClass myTopicListeners = new MyClass();
tm.addTopicListener( myTopicListeners );
The code above makes the instance "myTopicListeners" be registered in the
topic manager. Topics are automatically registered with the appropriate
event types.
Documentation, please
Code is available through CVS. JAVADOC available
Remember, this is experimental
UITopics is still experimental. I'm still working on the API. You're welcome to experiment with it too and see how well (or how bad) it fits your needs. I would greatly appreciate feedback on it. Thanks!
| Powered by CollabNet | Feedback |
FAQ |
Press |
Developer tools
© 1995 - 2007 CollabNet. CollabNet is a registered trademark of CollabNet, Inc. |
