001: /*
002: ** $Id: Model.java,v 1.2 2000/10/26 08:34:15 mrw Exp $
003: **
004: ** Mike Wilson, July 2000, mrw@whisperingwind.co.uk
005: **
006: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
007: **
008: ** This program is free software; you can redistribute it and/or modify
009: ** it under the terms of the GNU General Public License as published by
010: ** the Free Software Foundation; either version 2 of the License, or
011: ** (at your option) any later version.
012: **
013: ** This program is distributed in the hope that it will be useful,
014: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
015: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: ** GNU General Public License for more details.
017: **
018: ** You should have received a copy of the GNU Library General
019: ** Public License along with this library; if not, write to the
020: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
021: ** Boston, MA 02111-1307 USA.
022: */
023:
024: package uk.co.whisperingwind.framework;
025:
026: import java.util.Observable;
027: import javax.swing.SwingUtilities;
028: import java.lang.reflect.InvocationTargetException;
029:
030: /**
031: ** Base class for simple models.
032: */
033:
034: public abstract class Model extends Observable {
035: private ModelEvent modelEvent = null;
036:
037: /**
038: ** Fire a model event. Makes sure the model event is fired in the
039: ** event thread so updating any Swing components is thread safe.
040: **
041: ** @param initiator. The object which initiated the event.
042: ** @param field. The name of the field which is changing.
043: ** @param value. The new value of the field.
044: **
045: ** @return nothing.
046: */
047:
048: protected void fireEvent(Object initiator, String field,
049: Object value) {
050: modelEvent = new ModelEvent(initiator, field, value);
051:
052: if (SwingUtilities.isEventDispatchThread()) {
053: setChanged();
054: notifyObservers(modelEvent);
055: } else {
056: SwingUtilities.invokeLater(new FireThread());
057: }
058: }
059:
060: /**
061: ** Fire a model event with the current object as the initiator.
062: ** @see #fireEvent (Object initiator, String field, Object value).
063: */
064:
065: protected void fireEvent(String field, Object value) {
066: fireEvent(this , field, value);
067: }
068:
069: /**
070: ** Like fireEvent, but waits for the message to be handled.
071: */
072:
073: protected void waitEvent(Object initiator, String field,
074: Object value) {
075: modelEvent = new ModelEvent(initiator, field, value);
076:
077: if (SwingUtilities.isEventDispatchThread()) {
078: setChanged();
079: notifyObservers(modelEvent);
080: } else {
081: try {
082: SwingUtilities.invokeAndWait(new FireThread());
083: } catch (InterruptedException ex) {
084: // Do nothing
085: } catch (InvocationTargetException ex) {
086: // Do nothing
087: }
088: }
089: }
090:
091: protected void waitEvent(String field, Object value) {
092: waitEvent(this , field, value);
093: }
094:
095: /**
096: ** Runnable used to pass events to the event dispatch thread.
097: ** An instance of this is passed to SwingUtilities.invokeLater
098: ** when fireEvent is called from some thread other than the event
099: ** thread.
100: */
101:
102: private class FireThread implements Runnable {
103: public void run() {
104: setChanged();
105: notifyObservers(modelEvent);
106: }
107: }
108: }
|