NCSAHabanero

[ Previous ] [ Index ] [ Next ]

-------------------------------------------------------------------
How to pass events to peer hablets
-------------------------------------------------------------------

Simple Event Handling

Background Information

A collaborative application must inform all its collaborators when an event occurs. Habanero allows you to specify which events propagate.
 
ALERT 

This pattern assumes that your applet uses the Java 1.1 event model. If your applet uses the Java 1.0 event model, please read Using the Old Event Model. [Not yet written] 
If you would like a more detailed description of how all this works, you can read about Habanero's event model.

How does this work?

You need to tell Habanero which events to share. The class ncsa.habanero.Habanero has a method called addSharedEventType which shares events: The id above is a java.awt.Event [Outside link] ID so that you can specify a particular event within an event type. For example, if your event type is java.awt.event.MouseEvent[Outside link] and the id is MOUSE_DOWN, then only mouse down events will be shared because of this statement. If you leave out the id, as in the first line, then all mouse events would be shared.

The Mirrorable m supplies Habanero the current hablet, so you just pass the pseudovariable this to it, as illustrated in the running example below. (This is an example of double dispatching.)

These sharing calls should go in the startInFrame() method of your hablet that we explained in Change Superclass, Imports, Initialization.
 
Running Example from the example of habanerizing TextDemo.java.
Add or modify this method in the class HabTextDemo

  public void startInFrame(MirrorFrame f)  
  {  

    // Code for setting the default window parameters goes here. 

    try  
    { Habanero.addSharedEventType(  
        Class.forName("java.awt.event.ActionEvent"), this);  
    }  
    catch (ClassNotFoundException e)  
    { Habanero.println("could not find ActionEvent class object.");  
    }  
  } 

 
 
ALERT 

Note how we use a try...catch construct in case we get an exception. 

The special case of getSource()

If your listeners call getSource() on the event, you need to change it to Habanero.getSource() because the source of an event is lost when it gets sent through the collaboration system. This loss happens because getSource() actually returns a pointer to the source object, and in Habanero, that source object could be on a different machine, making the pointer irrelevant.
 
Running Example from the example of habanerizing TextDemo.java.
Because TextDemo.java does call getSource(), change the body of HabTextDemo.FieldListener.actionPerformed() to: 
      try {f = (TextField)Habanero.getSource();}
        // change getSource from the ActionEvent to Habanero
        // because serializing loses the event's source.
      catch (ClassCastException evt)
      { outArea.append("Error: source isn't a TextField!\n");
        return;
      }
 
      outArea.append(e.getActionCommand()+"\n");
        // change because if we did the original f.getText() we
        // would get the text from the *local* copy of the field
        // and not the one in the original Hablet.
      f.selectAll();
 
If you do not use getSource(), then you probably will not have to change the listener unless your applet performs a complicated action such as determining whether the event is to be shared from the listener.

See the Event Model, part B for a diagram of how this event sharing works.

What next?

  1. Install your new hablet into your Habanero environment.
  2. If you need to pass more complicated extra data along, use a ComponentAction. [This is an advanced technique not yet written.]


Habanero® is a registered trademark owned by The Board of Trustees of the University of Illinois. Copyright 1996-1998. All rights reserved.   Java(TM) is a proprietary trademark owned by Sun Microsystems, Inc.
NCSA
The National Center for Supercomputing Applications

University of Illinois at Urbana-Champaign

Contacts page