/** * I present here a simple widget mediator which in this case (started * in motion by the main routine of SimpleWidgetMediator) adds a Dial * and a Text Box. It can hold any number of simple widgets that extend * extend the Simple Widget class and take only a float value. The * mediator has a list of its children and parents and notifies each * (after setting an updating flag to stop circularities) of updates. */ /** * SimpleWidgetMediator class - I didn't show a more generic Mediator * class, but the lists could be moved up a level. */ public class SimpleWidgetMediator extends Mediator{ // list of Mediators (could be null) which use this Mediators output, // these will be notified on change private List usedOnMediators = new ArrayList(); // list of Widgets controlled by this Mediator private List myWidgets = new ArrayList(); // flag indicating if we are in an update cycle. private boolean updating = false; // method to add widgets to the list. a remove method // is not provided, because the Widgets should be // built at startup time, not dynamically (new feature for release 2.0) public void addWidget( SimpleWidget w ) { myWidgets.add( w ); } // method for other to add this one as a composite (I didn't use // this for this simple prototype public void addParentMediator( Mediator _pMediator ) { usedOnMediators.add(_pMediator); } // method to remove parent Mediators (as windows disappear) public void removeParentMediator( Mediator _pMediator ) { usedOnMediators.remove( _pMediator ); } // method to notify parent Mediators that this value has changed. I won't // implement this any farther. public void notifyParentMediators( float x ) { Iterator ai = usedOnMediators.iterator(); while ( ai.hasNext() ) { Mediator av = (Mediator)ai.getNext(); av.valueChanged( x ); } } // method signifying a parent Mediator has changed, notify children // if we're not in an update cycle public void valueChanged( float x ) { // if we are in an update cycle, then we initiated this change, so // don't process it if ( !updating ) { updating = true; // prevent looping updates // notify the simple child widgets Iterator cw = myWidgets.iterator(); while ( cw.hasNext() ) { SimpleWidget sw = (SimpleWidget)cw.getNext(); sw.changeValue( x ); } updating = false; // reset the updating flag } } // method called from the child widgets signifying they have changed public void changeValue( float y ) { // set updating to true updating = true; // notify the children this.valueChanged( y ); // and pass the message to parent Mediators this.notifyParentMediators( y ); // reset the updating flag updating = false; } // to kick this thing off, have a main routine which will instantiate the widgets, and show // the frame public static void main( String[] args ) { // construct the box widget (I don't remember - nor is it particularly important - // the exact constructor call swing, // I'll assume lower, upper, step, initial value. public SimpleWidget wTextBox = new Widget( cwValue, new JTextBox( String.valueOf(cwValue.getCurrentValue() ))); // construct the box widget (I don't remember - nor is it particularly important - // the exact constructor call swing, // I'll assume lower, upper, step, initial value. public SimpleWidget wDial = new Widget( cwValue, new JSlider(0,360,1.0,cwValue.getCurrentValue()) ); // add these simple widgets to the mediator (this) this.addWidget( wTextBox ); this.addWidget( wDial ); } } /** SimpleWidget is the base class which is extended for dial and box widget */ public class SimpleWidget { // Mediator reference of the controller private Mediator myMediator = null; // to construct a SimpleWidget, a Mediator public Widget( Mediator _myMediator ) { myMediator = _myMediator; } // isChanged method would be called by the mouse listener or event // flag from the gui system with the new value public void isChanged( float _value ) { // tell the parent Mediator, let it change all the children myValue.changeValue( _value ); } } /** the BoxWidget class will build a text box, extending SimpleWidget */ public class BoxWidget extends SimpleWidget { public double value = 0.0; // if the gui system detects an update, notify the mediator // (i'm assuming this would be kicked off on change in the widget) public void guiListener( float newValue ) { super.isChanged( newValue ); } // method update will take the current value and update it on the widget public void update( float _value ) { value = _value; } } /** the DialWidget class will build a dial box, extending SimpleWidget */ public class DialWidget extends SimpleWidget { public double value = 0.0; // if the gui system detects an update, notify the mediator // (i'm assuming this would be kicked off on change in the widget) public void guiListener( float newValue ) { super.isChanged( newValue ); } // method update will take the current value and update it on the widget public void update( float _value) { value = _value; } }