KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > PositionControlAdapter


1 package JSci.instruments;
2
3 import java.util.*;
4 import java.awt.*;
5 import javax.swing.*;
6 import java.awt.event.*;
7 import javax.swing.event.*;
8
9
10 /** A mechanical device that can control the position of
11  * something. Implements the methods to notify changes.
12  */

13 public abstract class PositionControlAdapter implements PositionControl {
14
15     /** set the position
16      * @param p the position
17      */

18     abstract public void setPosition(double p);
19
20     /** get the position that must be reached. Since the mechanical
21      * device needs some time to reach the position, getPosition()
22      * can be different from getActualPosition()
23      * @return the position that must be reached
24      */

25     abstract public double getPosition();
26
27     /** get the actual position of the device. Since the mechanical
28      * device needs some time to reach the position, getPosition()
29      * can be different from getActualPosition()
30      * @return the actual position
31      */

32     abstract public double getActualPosition();
33
34     /** get the minimum position
35      * @return the minimum position
36      */

37     abstract public double getMinimum();
38
39     /** get the maximum position
40      * @return the maximum position
41      */

42     abstract public double getMaximum();
43
44     /** sleeps for the time needed to stabilize the position.
45      */

46     abstract public void sleep();
47
48     /** get a Component through which we can control the position
49      * @return the Component with the controls for the position
50      */

51     abstract public Component getControlComponent();
52
53     /** get a String with the description of the units used
54      * for all the values.
55      * @return the unit
56      */

57     abstract public String JavaDoc getUnit();
58     
59
60
61     //////////////////////////////////////////////////////////////////
62
// Events
63

64     /**
65      * Only one ChangeEvent is needed per model instance since the
66      * event's only (read-only) state is the source property. The source
67      * of events generated here is always "this".
68      */

69     private transient ChangeEvent changeEvent = null;
70
71     /**
72      * The list of ChangeListeners for this model. Subclasses may
73      * store their own listeners here.
74      */

75     protected EventListenerList listenerList = new EventListenerList();
76
77
78     /**
79      * Adds a ChangeListener to the model's listener list. The
80      * ChangeListeners must be notified when the models value changes.
81      *
82      * @param l the ChangeListener to add
83      * @see #removeChangeListener
84      */

85     public void addChangeListener(ChangeListener l) {
86         listenerList.add(ChangeListener.class, l);
87     }
88     
89
90     /**
91      * Removes a ChangeListener from the model's listener list.
92      *
93      * @param l the ChangeListener to remove
94      * @see #addChangeListener
95      */

96     public void removeChangeListener(ChangeListener l) {
97         listenerList.remove(ChangeListener.class, l);
98     }
99
100
101     /**
102      * Returns an array of all the <code>ChangeListener</code>s added
103      * to this model with addChangeListener().
104      *
105      * @return all of the <code>ChangeListener</code>s added or an empty
106      * array if no listeners have been added
107      */

108     public ChangeListener[] getChangeListeners() {
109         return (ChangeListener[])listenerList.getListeners(
110                                ChangeListener.class);
111     }
112
113
114     /**
115      * Run each ChangeListeners stateChanged() method.
116      *
117      * @see EventListenerList
118      */

119     protected void fireStateChanged()
120     {
121         Object JavaDoc[] listeners = listenerList.getListenerList();
122         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
123             if (listeners[i] == ChangeListener.class) {
124                 if (changeEvent == null) {
125                     changeEvent = new ChangeEvent(this);
126                 }
127                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
128             }
129         }
130     }
131
132
133     /**
134      * Return an array of all the listeners of the given type that
135      * were added to this model.
136      *
137      * @param listenerType the type of listeners to return, e.g. ChangeListener.class
138      * @return all of the objects receiving <em>listenerType</em> notifications
139      * from this model
140      */

141     public EventListener[] getListeners(Class JavaDoc listenerType) {
142     return listenerList.getListeners(listenerType);
143     }
144
145
146
147 }
148
149
Popular Tags