Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/cybertivity/powerjournal/framework/Model.java


1   /*
2    * Title:     Similated Intelligence
3    * Copyright: Copyright (c) 2001 Cybertivity
4    * Company:   <A HREF="http://www.cybertivity.com">Cybertivity</A>
5    * @author    <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
6    * @version   $Id: Model.java,v 1.1.1.1 2001/11/24 03:51:34 arrowood Exp $
7    */
8   package com.cybertivity.powerjournal.framework;
9   import java.lang.reflect.InvocationTargetException;
10  
11  import java.util.Observable;
12  import javax.swing.SwingUtilities;
13  
14  /**
15   *  Description of the Class
16   *
17   * @author     arrowood
18   * @created    July 17, 2001
19   */
20  public abstract class Model extends Observable {
21    private ModelEvent modelEvent = null;
22  
23  
24    /**
25     *  Fire a model event. Makes sure the model event is fired in the event thread
26     *  so updating any Swing components is thread safe.
27     *
28     * @param  initiator  Object that initiated the change
29     * @param  field      The field that has change
30     * @param  value      The new value of the field
31     */
32  
33    protected void fireEvent(Object initiator, String field, Object value) {
34      modelEvent = new ModelEvent(initiator, field, value);
35  
36      if(SwingUtilities.isEventDispatchThread()) {
37        setChanged();
38        notifyObservers(modelEvent);
39      } else {
40        SwingUtilities.invokeLater(new FireThread());
41      }
42    }
43  
44  
45    /**
46     *  Fire a model event with the current object as the initiator.
47     *
48     * @param  field  The field that has change
49     * @param  value  The new value of the field
50     * @see           #fireEvent (Object initiator, String field, Object value).
51     */
52  
53    protected void fireEvent(String field, Object value) {
54      fireEvent(this, field, value);
55    }
56  
57  
58    /**
59     *  Like fireEvent, but waits for the message to be handled.
60     *
61     * @param  initiator  Object that initiated the
62     * @param  field      The field that has change
63     * @param  value      The new value of the field
64     */
65  
66    protected void waitEvent(Object initiator, String field, Object value) {
67      modelEvent = new ModelEvent(initiator, field, value);
68  
69      if(SwingUtilities.isEventDispatchThread()) {
70        setChanged();
71        notifyObservers(modelEvent);
72      } else {
73        try {
74          SwingUtilities.invokeAndWait(new FireThread());
75        } catch(InterruptedException ex) {
76          // Do nothing
77        } catch(InvocationTargetException ex) {
78          // Do nothing
79        }
80      }
81    }
82  
83  
84    /**
85     *  Description of the Method
86     *
87     * @param  field  The field that has change
88     * @param  value  The new value of the field
89     */
90    protected void waitEvent(String field, Object value) {
91      waitEvent(this, field, value);
92    }
93  
94  
95    /**
96     *  Runnable used to pass events to the event dispatch thread. An instance of
97     *  this is passed to SwingUtilities.invokeLater when fireEvent is called from
98     *  some thread other than the event thread.
99     *
100    * @author     arrowood
101    * @created    July 17, 2001
102    */
103 
104   private class FireThread implements Runnable {
105     /**
106      *  Main processing method for the FireThread object
107      */
108     public void run() {
109       setChanged();
110       notifyObservers(modelEvent);
111     }
112   }
113 }