HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"Data-Driven Components"
Volume: 4 Issue: 5, p. 8


Listing 1.
public interface dataListener 
extends java.util.EventListener { 
  public void dataDestroy(dataEvent event); 
  public void dataUpdated(dataEvent event); 
  public void dataLoad(dataEvent event); 
  public void dataFilter(dataEvent event); 

Listing 2.
public class dataEvent 
extends java.util.EventObject { 
  protected int id; 
  public static final int DESTROY = 0, 
              UPDATED = 1, 
              LOAD = 2, 
              FILTER = 3; 

  public dataEvent(Object source, int id) { 
   this.id = id; 

  public int getID() { return id; } 

Listing 3.
public class dataNotifier { 
  protected Vector dataListeners = new Vector(); 

  public dataNotifier() { } 

  public void addDataListener( dataListener listener ) { 
   dataListeners.addElement( listener ); 

  public void removeDataListener( dataListener listener ) { 
   dataListeners.removeElement( listener ); 

  public void notifyListeners( dataEvent event ) { 
   // only dataComponents can fire dataEvents, 
      since dataEvents will need to call 
   // methods from the dataComponent interface. 
   if ((event.getSource() instanceof dataComponent) == false) 

   // check if there are listeners 
   if (dataListeners == null || dataListeners.size() == 0) 

   // protect the state of the vector of listeners 
   Vector listeners = (Vector)dataListeners.clone(); 

   // tell each listener an event has occured 
   for (int current = 0; current < listeners.size(); current++) { 
     switch(event.getID()) { 
     case dataEvent.DESTROY: 
      ((dataListener)listeners.elementAt( current )).dataDestroy( event ); 
     case dataEvent.UPDATED: 
      ((dataListener)listeners.elementAt( current )).dataUpdated( event ); 
     case dataEvent.LOAD: 
      ((dataListener)listeners.elementAt( current )).dataLoad( event ); 
     case dataEvent.FILTER: 
      ((dataListener)listeners.elementAt( current )).dataFilter( event ); 



All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.