MessageEvents not are used by the core VisAD classes. They are reserved
for application-level events. They could be used by
- an application to send events to multiple controls connected to a Display
- client applications to send events to a server Display (or by a server
sending events to all clients connected to its Display)
- a distributed application to send events to all its parts connected
via a Display
MessageEvents are sent by the Display's
sendMessage(MessageEvent)
method to listeners who have implemented the
receiveMessage(MessageEvent)
method and have registered themselves using the
addMessageListener(MessageListener)
method.
A trivial example of an application using MessageEvents looks like this:
import java.rmi.RemoteException;
import visad.java2d.DisplayImplJ2D;
import visad.MessageEvent;
import visad.VisADException;
public class SimpleMsgEvent
implements visad.MessageListener
{
private DisplayImplJ2D dpy;
public SimpleMsgEvent()
throws RemoteException, VisADException
{
dpy = new DisplayImplJ2D("dpy");
dpy.addMessageListener(this);
dpy.sendMessage(new MessageEvent(0));
}
public void receiveMessage(MessageEvent evt)
throws RemoteException
{
final int id = evt.getId();
System.out.println(id);
if (id > 10) System.exit(0);
dpy.sendMessage(new MessageEvent(id + 1));
}
public static void main(String[] args)
throws RemoteException, VisADException
{
new SimpleMsgEvent();
}
}
A more complex example can be found in visad.ss.BasicSSCell, which
uses the MessageEvent ID to hold both a message type and a spreadsheet
cell ID. It defines message types 0 through 8 to mean things like
ADD_DATE, ADD_SOURCE, etc. and then computes the
event ID by multiplying the cell ID by 9 and adding it to the message type.
The receiving end can thus extract the cell ID with a simple
event.getID() / 9 calculation. Similarly, the
message type can be extracted with event.getID() % 9.
The receiving cell can then ignore its own messages or those from
cells it doesn't care about, only response to certain message types,
etc.
Last modified: Thu Feb 14 09:20:20 CST 2002