upstream version 1.2.2
[debian/freetts] / com / sun / speech / engine / BaseEngineProperties.java
1 /**
2  * Copyright 1998-2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package com.sun.speech.engine;
9
10 import java.beans.PropertyChangeEvent;
11 import java.beans.PropertyChangeListener;
12 import java.util.Collection;
13 import java.util.EventObject;
14 import java.util.Iterator;
15
16 import javax.speech.EngineProperties;
17 import javax.speech.SpeechError;
18 import javax.speech.SpeechEvent;
19
20 /**
21  * Supports the JSAPI 1.0 <code>EngineProperties</code>
22  * interface.
23  */
24 public abstract class BaseEngineProperties
25     implements EngineProperties, SpeechEventDispatcher {
26     
27     /**
28      * List of <code>PropertyChangeListeners</code> registered for
29      * <code>PropertyChangeEvents</code> on this object.
30      */
31     protected Collection propertyChangeListeners;
32
33     /**
34      * Class constructor.
35      */
36     protected BaseEngineProperties() {
37         propertyChangeListeners = new java.util.ArrayList();
38     }
39
40     /**
41      * Obtains the AWT <code>Component</code> that provides the
42      * default user interface 
43      * for setting the properties of the <code>Engine</code>
44      * associated with this object.
45      *
46      * @return an AWT <code>Component</code> to manipulate this object
47      */
48     public java.awt.Component getControlComponent() {
49         return null;
50     }
51
52     /**
53      * Returns all properties to reasonable defaults
54      * for the <code>Engine</code>.  A
55      * <code>PropertyChangeEvent</code> is issued
56      * for each property that changes as the reset takes effect.
57      */
58     public abstract void reset();
59     
60     /**
61      * Adds a <code>PropertyChangeListener</code> to the listener list.
62      *
63      * @param listener the <code>PropertyChangeListener</code> to add
64      */
65     public void addPropertyChangeListener(PropertyChangeListener listener) {
66         if (!propertyChangeListeners.contains(listener)) {
67             propertyChangeListeners.add(listener);
68         }
69     }
70
71     /**
72      * Removes a <code>PropertyChangeListener</code> from the listener
73      * list.
74      * 
75      * @param listener the <code>PropertyChangeListener</code> to remove
76      */
77     public void removePropertyChangeListener(PropertyChangeListener listener) {
78         propertyChangeListeners.remove(listener);
79     }
80
81     /**
82      * Generates a
83      * <code>PropertyChangeEvent</code> for an <code>Object</code> value
84      * and posts it to the event queue.  Eventually
85      * <code>firePropertyChangeEvent</code> will be called by
86      * <code>dispatchSpeechEvent</code> as a result of this action.
87      *
88      * @param propName the name of the property
89      * @param oldValue the old value
90      * @param newValue the new value
91      *
92      * @see #firePropertyChangeEvent
93      * @see #dispatchSpeechEvent
94      */
95     protected void postPropertyChangeEvent(String propName,
96                                            Object oldValue,
97                                            Object newValue) {
98         EventObject e = new PropertyChangeEvent(this,
99                                                 propName,
100                                                 oldValue,
101                                                 newValue);
102         SpeechEvent se = new SpeechEventWrapper(e);
103         SpeechEventUtilities.postSpeechEvent(this, se);
104     }
105
106     /**
107      * Generates a
108      * <code>PropertyChangeEvent</code> for a <code>float</code> value
109      * and posts it to the event queue.  Eventually
110      * <code>firePropertyChangeEvent</code> will be called by
111      * <code>dispatchSpeechEvent</code> as a result of this action.
112      *
113      * @param propName the name of the property
114      * @param oldValue the old value
115      * @param newValue the new value
116      *
117      * @see #firePropertyChangeEvent
118      * @see #dispatchSpeechEvent
119      */
120     protected void postPropertyChangeEvent(String propName,
121                                            float oldValue,
122                                            float newValue) {
123         EventObject e = new PropertyChangeEvent(this,
124                                                 propName, 
125                                                 new Float(oldValue), 
126                                                 new Float(newValue));
127         SpeechEvent se = new SpeechEventWrapper(e);
128         SpeechEventUtilities.postSpeechEvent(this, se);
129     }
130
131     /**
132      * Generates a
133      * <code>PropertyChangeEvent</code> for a <code>int</code> value
134      * and posts it to the event queue.  Eventually
135      * <code>firePropertyChangeEvent</code> will be called by
136      * <code>dispatchSpeechEvent</code> as a result of this action.
137      *
138      * @param propName the name of the property
139      * @param oldValue the old value
140      * @param newValue the new value
141      *
142      * @see #firePropertyChangeEvent
143      * @see #dispatchSpeechEvent
144      */
145     protected void postPropertyChangeEvent(String propName,
146                                            int oldValue,
147                                            int newValue) {
148         EventObject e = new PropertyChangeEvent(this,
149                                                 propName, 
150                                                 new Integer(oldValue), 
151                                                 new Integer(newValue));
152         SpeechEvent se = new SpeechEventWrapper(e);
153         SpeechEventUtilities.postSpeechEvent(this, se);
154     }
155
156     /**
157      * Generates a
158      * <code>PropertyChangeEvent</code> for a <code>boolean</code> value
159      * and posts it to the event queue.  Eventually
160      * <code>firePropertyChangeEvent</code> will be called by
161      * <code>dispatchSpeechEvent</code> as a result of this action.
162      *
163      * @param propName the name of the property
164      * @param oldValue the old value
165      * @param newValue the new value
166      *
167      * @see #firePropertyChangeEvent
168      * @see #dispatchSpeechEvent
169      */
170     protected void postPropertyChangeEvent(String propName,
171                                            boolean oldValue,
172                                            boolean newValue) {
173         EventObject e = new PropertyChangeEvent(this,
174                                                 propName, 
175                                                 new Boolean(oldValue), 
176                                                 new Boolean(newValue));
177         SpeechEvent se = new SpeechEventWrapper(e);
178         SpeechEventUtilities.postSpeechEvent(this, se);
179     }
180     
181     /**
182      * Sends a <code>PropertyChangeEvent</code>
183      * to all <code>PropertyChangeListeners</code> registered with
184      * this object.  Called by <code>dispatchSpeechEvent</code>.
185      *
186      * @param event the <code>PropertyChangeEvent</code> to send
187      *
188      * @see #firePropertyChangeEvent
189      * @see #dispatchSpeechEvent
190      */
191     public void firePropertyChangeEvent(PropertyChangeEvent event) {
192         if (propertyChangeListeners == null) {
193             return;
194         }
195         Iterator iterator = propertyChangeListeners.iterator();
196         while (iterator.hasNext()) {
197             PropertyChangeListener pl =
198                 (PropertyChangeListener) iterator.next();
199             pl.propertyChange(event);
200         }
201     }
202
203     /**
204      * Dispatches a <code>PropertyChangeEvent</code>.
205      * The dispatcher should notify all <code>PropertyChangeListeners</code>
206      * from this method.  The <code>SpeechEvent</code> was added
207      * via the various post methods of this class.
208      *
209      * @param event the <code>SpeechEvent</code> containing a
210      *   <code>PropertyChangeEvent</code>
211      *
212      * @see #postPropertyChangeEvent
213      */
214     public void dispatchSpeechEvent(SpeechEvent event) {
215         if (event instanceof SpeechEventWrapper) {
216             SpeechEventWrapper se = (SpeechEventWrapper)event;
217             PropertyChangeEvent pe =
218                 (PropertyChangeEvent)(se.getEventObject());
219             firePropertyChangeEvent(pe);
220         }
221         else {
222             throw new SpeechError(
223                 "BaseEngineProperties: speech event type error");
224         }
225     }
226 }