CS 696 Emerging Technologies: Distributed Objects |
---|
Vector listeners = new Vector(); public synchronized void addTimeAdvancedListener( TimeAdvancedListener listener ) { listeners.addElement( listener ); }
TimeAdvancedListener myListener; public synchronized void addTimeAdvancedListener( TimeAdvancedListener listener ) throws java.util.TooManyListenersException { if ( myListener != null ) throw new TooManyListenersException(); myListener = listener; }
public interface ModelChangedListener extends java.util.EventListener { void modelChanged(EventObject e); } public abstract class Model { private Vector listeners = new Vector(); public synchronized void addModelChangedListener( ModelChangedListener mcl) { listeners.addElement(mcl); } public synchronized void removeModelChangedListener( ModelChangedListener mcl) { listeners.removeElement(mcl); } protected void notifyModelChanged() { Vector l; EventObject e = new EventObject(this); synchronized(this) { l = (Vector)listeners.clone(); } for (int i = 0; i < l.size(); i++) ((ModelChangedListener)l.elementAt(i)).modelChanged(e); } }
package beanExamples; public interface DatagramListener extends java.util.EventListener { public void newData( DatagramEvent data ); }
package beanExamples; public class DatagramEvent extends java.util.EventObject { int datagram; public DatagramEvent( SDSUDataSource source, int data) { super( source ); datagram = data; } public int getDatagram() { return datagram; } }
package beanExamples; public class SDSUDataConsumer implements DatagramListener { public void newData( DatagramEvent datagram ) { SDSUDataSource mySource = (SDSUDataSource) datagram.getSource(); int data = datagram.getDatagram(); System.out.println( "Source: " + mySource.getName() + " data: " + data ); } }
package beanExamples; import java.util.Random; import java.util.Vector; public class SDSUDataSource implements TimeAdvancedListener { Vector listeners = new Vector(); Random dataGenerator = new Random(); String name; public SDSUDataSource() { this("SDSU"); } public SDSUDataSource(String name) { setName( name ); SDSUClock myClock = new SDSUClock( 10000 ); myClock.addTimeAdvancedListener( this ); } public String getName() { return name; } public void setName( String name) {this.name = name; } public void timeAdvanced( TimeAdvancedEvent timeEvent ) { notifyListeners(); }
public synchronized void addDatagramListener( DatagramListener listener ) { listeners.addElement( listener ); } public synchronized void removeDatagramListener( DatagramListener listener ) { listeners.removeElement( listener ); } protected void notifyListeners() { int data = dataGenerator.nextInt(); DatagramEvent time = new DatagramEvent(this, data); for ( int k = 0; k < listeners.size(); k++) { DatagramListener aListener = (DatagramListener) listeners.elementAt( k ); aListener.newData( time ); } } }
public void report() { System.out.println( "Report number: " + (count++) + " from " + name ); }
public interface TimeAdvancedListener extends java.util.EventListener { void timeAdvanced( TimeAdvancedEvent timeEvent ); }
package beanExamples; public class TimeAdvancedListenerAdapter { SDSUReporter adaptee; public TimeAdvancedListenerAdapter( SDSUReporter adaptee ) { this.adaptee = adaptee; } public void timeAdvanced( TimeAdvancedEvent timeEvent ) { adaptee.report(); } }The BeanBox generated such a adapter in the demo on Doc 31, slide 13-14
package beanExamples; import java.lang.reflect.*; public class TimeAdvancedListenerAdapter { Object adaptee; Method adapteeMethod; public TimeAdvancedListenerAdapter( Object adaptee, String methodName ) throws NoSuchMethodException { this.adaptee = adaptee; Class[] noParameters = new Class[0]; adapteeMethod = adaptee.getClass(). getMethod( methodName, noParameters ); } public void timeAdvanced( TimeAdvancedEvent timeEvent ) throws AdapterException { Object[] arguements = new Object[0]; try { adapteeMethod.invoke( adaptee, arguements); } catch ( Exception error ) { throw new AdapterException( error.toString() ); } } } public class AdapterException extends RuntimeException { public AdapterException() { super(); } public AdapterException(String s) { super(s); } }
import beanExamples.*; public class TrivialApplication { public static void main(String args[]) throws Exception { System.out.println( "Hello World!" ); SDSUClock myClock = new SDSUClock(); SDSUReporter reporter = new SDSUReporter(); TimeAdvanceListenerAdapter adapt = new TimeAdvanceListenerAdapter( reporter, "report"); myClock.addTimeAdvancedListener( adapt ); } }Allows one adapter replace multiple adapter classes
void setX( int index, PropertyType value); PropertyType getX( int index ); void setX( PropertyType[] values ); PropertyType[] getX( );
public abstract void propertyChange(PropertyChangeEvent evt)
getNewValue() getOldValue() getPropagationId() The "propagationId" field is reserved for future use. getPropertyName() setPropagationId(Object)
public PropertyChangeEvent(Object source,String propertyName, Object oldValue, Object newValue)
addPropertyChangeListener(PropertyChangeListener)
removePropertyChangeListener(PropertyChangeListener)
firePropertyChange(String propertyName,Object oldValue, Object newValue)
package beanExamples; import java.beans.*; public class SDSUReporter implements TimeAdvancedListener { String name = "SDSU"; int count = 0; PropertyChangeSupport propertyListeners = new PropertyChangeSupport( this ); public void setName( String newName) { // change the property before notifying listeners String oldName = name; name = newName; propertyListeners.firePropertyChange( "name", oldName, name ); } public String getName() { return name; } public void addPropertyChangeListener ( PropertyChangeListener listener) { propertyListeners.addPropertyChangeListener( listener ); } public void removePropertyChangeListener ( PropertyChangeListener listener) { propertyListeners.removePropertyChangeListener( listener ); } public void timeAdvanced( TimeAdvancedEvent timeEvent ) { report(); } public void report(){ System.out.println( "Report number: " + (count++) + " from " + name ); } }
PropertyType getFoo(); void setFoo(PropertyType value) throws PropertyVetoException;Beans and containers that validate a property change must register them selves as VetoableChangeListeners with the property's bean
public void addVetoableChangeListener(VetoableChangeListener x); public void removeVetoableChangeListener(VetoableChangeListener x);
public abstract void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException
package beanExamples; import java.beans.*; public class SDSUReporter implements TimeAdvancedListener { String name = "SDSU"; int count = 0; PropertyChangeSupport propertyListeners = new PropertyChangeSupport( this ); VetoableChangeSupport propertyVetors = new VetoableChangeSupport ( this ); public void setName( String newName) throws PropertyVetoException { String oldName = name; // See if we can make the change propertyVetors.fireVetoableChange( "name", oldName, newName ); // No Vetos so make change name = newName; // Now let them know the change happened propertyListeners.firePropertyChange( "name", oldName, name ); }
public String getName() { return name; } public void addVetoableChangeListener ( VetoableChangeListener listener) { propertyVetors.addVetoableChangeListener( listener ); } public void removeVetoableChangeListener ( VetoableChangeListener listener) { propertyVetors.removeVetoableChangeListener( listener ); } public void addPropertyChangeListener ( PropertyChangeListener listener) { propertyListeners.addPropertyChangeListener( listener ); } public void removePropertyChangeListener ( PropertyChangeListener listener) { propertyListeners.removePropertyChangeListener( listener ); } public void timeAdvanced( TimeAdvancedEvent timeEvent ) { report(); } public void report() { System.out.println( "Report number: " + (count++) + " from " + name ); } }