Responding to Events

Try selecting glyphs in the map below using the mouse. Notice how the status line of your browser reports events.

We have extended SimpleMap in a few ways to allow for selection.

These things are done in our constructor. But to register as a listener to the NeoMap we must implement a Listener interface. The event we are interested in is a NeoRubberBandEvent. So we register the SimpleSelectableMap as a NeoRubberBandListener. The NeoRubberBandListener interface consists of just the rubberBandChanged method.

package tutorial;

import com.affymetrix.genoviz.event.EventListenerI;
import com.affymetrix.genoviz.event.NeoRubberBandListener;
import com.affymetrix.genoviz.event.NeoRubberBandEvent;
import java.awt.Event;
import java.awt.Rectangle;
import java.util.Vector;

public class SimpleSelectableMap extends SimpleMap0 implements NeoRubberBandListener {

  public SimpleSelectableMap() {
    super();
    this.map.setSelectionEvent(this.map.ON_MOUSE_DOWN);
    this.map.setSelectionAppearance(this.map.SELECT_OUTLINE);
    this.map.addRubberBandListener(this);
  }

  public String getAppletInfo() {
    return "Simple Selectable Map";
  }

  public void rubberBandChanged(NeoRubberBandEvent theEvent) {
    // Here we add some selection by rubberband.
    if (theEvent.getID() == NeoRubberBandEvent.BAND_END
      && theEvent.getSource() == map
      && map.NO_SELECTION != map.getSelectionEvent())
    {
      Rectangle pixelBox = theEvent.getPixelBox();
      pixelBox.setSize(pixelBox.width+1, pixelBox.height+1);
      int fuzziness = map.getPixelFuzziness();
      if (fuzziness <= pixelBox.height || fuzziness <= pixelBox.width) {
        // Rubberband is non-trivial.
        // Select items within it.
        Vector items = map.getItems(pixelBox);
        map.select(items);
        map.updateWidget();
      }
    }
  }

}
        

Exercise

  1. Notice that a shifted rubber band motion will not deselect previously selected items like a simple click will. Fix this.

Next: Drawing