upstream version 1.2.2
[debian/freetts] / com / sun / speech / engine / EngineMonitor.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.util.Date;
11
12 import java.awt.Component;
13 import java.awt.GridLayout;
14
15 import javax.swing.BorderFactory;
16 import javax.swing.JPanel;
17 import javax.swing.JLabel;
18
19 import javax.speech.Engine;
20 import javax.speech.EngineListener;
21 import javax.speech.EngineEvent;
22 import javax.speech.EngineErrorEvent;
23
24 /**
25  * Simple GUI for monitoring events and state changes of an
26  * <code>Engine</code>.  Used for debugging and testing purposes.
27  */
28 public class EngineMonitor {
29     /**
30      * The <code>Engine</code> to monitor.
31      */
32     protected Engine engine;
33
34     /**
35      * The <code>EngineListener</code> registered with the engine.
36      */
37     protected EngineListener engineListener;
38
39     /**
40      * The panel used to post engine events.
41      */
42     protected EngineEventPanel eventPanel;
43
44     /**
45      * The panel containing the current engine states.
46      */
47     protected JPanel statePanel;
48
49     /**
50      * The label containing the string "deallocated".
51      */
52     protected JLabel deallocatedLabel;
53
54     /**
55      * The label containing the string "allocating resources".
56      */
57     protected JLabel allocatingResourcesLabel;
58
59     /**
60      * The label containing the string "allocated".
61      */
62     protected JLabel allocatedLabel;
63
64     /**
65      * The label containing the string "deallocating resources".
66      */
67     protected JLabel deallocatingResourcesLabel;
68
69     /**
70      * The label containing the string "paused".
71      */
72     protected JLabel pausedLabel;
73
74     /**
75      * The label containing the string "resumed".
76      */
77     protected JLabel resumedLabel;
78
79     /**
80      * Class constructor.
81      *
82      * @param eng the <code>Engine</code> to watch
83      */
84     public EngineMonitor(Engine eng) {
85         this.engine = eng;
86         engine.addEngineListener(getEngineListener());
87     }
88
89     /**
90      * Creates the engine listener if necessary, and then returns it.
91      * There should be only one.
92      *
93      * @return the engine listener
94      */
95     protected EngineListener getEngineListener() {
96         if (engineListener == null) {
97             engineListener = new EngineMonitorEngineListener();
98         }
99         return engineListener;
100     }
101
102     /**
103      * Gets the panel containing the area to post engine events in.
104      *
105      * @return the panel containing the area to post engine events in
106      */
107     public Component getEventPanel() {
108         if (eventPanel == null) {
109             eventPanel = new EngineEventPanel();
110         }
111         return eventPanel;
112     }
113
114     /**
115      * Gets the panel containing the labels for representing the
116      * current engine state.
117      *
118      * @return the panel containing the labels for representing the
119      *   current engine state.
120      */
121     public Component getStatePanel() {
122         if (statePanel == null) {
123             JPanel newStatePanel = new JPanel();
124             newStatePanel.setLayout(new GridLayout(1,2));
125             
126             JPanel engineStatePanel = new JPanel();
127             engineStatePanel.setLayout(new GridLayout(4,2));
128             engineStatePanel.setBorder(
129                 BorderFactory.createTitledBorder("Engine State:"));
130             
131             deallocatedLabel = new JLabel("DEALLOCATED");
132             allocatingResourcesLabel = new JLabel("ALLOCATING_RESOURCES");
133             allocatedLabel = new JLabel("ALLOCATED");
134             deallocatingResourcesLabel = new JLabel("DEALLOCATING_RESOURCES");
135             pausedLabel = new JLabel("PAUSED");
136             resumedLabel = new JLabel("RESUMED");
137             
138             engineStatePanel.add(deallocatedLabel);
139             engineStatePanel.add(pausedLabel);
140             engineStatePanel.add(allocatedLabel);
141             engineStatePanel.add(resumedLabel);
142             engineStatePanel.add(deallocatingResourcesLabel);
143             engineStatePanel.add(new JLabel(""));
144             engineStatePanel.add(allocatingResourcesLabel);
145             newStatePanel.add(engineStatePanel);
146             statePanel = newStatePanel;
147         }
148         return statePanel;
149     }
150
151     /**
152      * Handles an event from the engine.
153      *
154      * @param e the event from the engine
155      */
156     protected void handleEvent(EngineEvent e) {
157         if (eventPanel != null) {
158             eventPanel.addText(new Date().toString() + ": "
159                                + e.toString()
160                                + "\n");
161             eventPanel.addText("   Old state: "
162                                + engineStateString(e.getOldEngineState())
163                                + "\n");
164             eventPanel.addText("   New state: "
165                                + engineStateString(e.getNewEngineState())
166                                + "\n");
167         }
168         updateGUIComponents();
169     }
170
171     /**
172      * Checks the current state of the engine and makes sure the GUI
173      * components reflect this state accurately.
174      */
175     protected void updateGUIComponents() {
176         updateEngineStateComponents();
177     }
178     
179     /**
180      * Checks the current state of the engine and makes sure the GUI
181      * components reflect this state accurately.
182      */
183     protected void updateEngineStateComponents() {
184         if (statePanel != null) {
185             deallocatedLabel.setEnabled(
186                 engine.testEngineState(Engine.DEALLOCATED));
187             allocatingResourcesLabel.setEnabled(
188                 engine.testEngineState(Engine.ALLOCATING_RESOURCES));
189             allocatedLabel.setEnabled(
190                 engine.testEngineState(Engine.ALLOCATED));
191             deallocatingResourcesLabel.setEnabled(
192                 engine.testEngineState(Engine.DEALLOCATING_RESOURCES));
193             pausedLabel.setEnabled(
194                 engine.testEngineState(Engine.PAUSED));
195             resumedLabel.setEnabled(
196                 engine.testEngineState(Engine.RESUMED));
197         }
198     }
199     
200     /**
201      * Returns a <code>String</code> representing the
202      * <code>state</code>.
203      *
204      * @param state the state to turn into a <code>String</code>
205      *
206      * @return  a <code>String</code> representing the
207      *   <code>state</code>
208      */
209     protected String engineStateString(long state) {
210         StringBuffer buf = new StringBuffer();
211
212         if ((state & Engine.DEALLOCATED) != 0)
213             appendBuffer(buf, "DEALLOCATED");
214         if ((state & Engine.ALLOCATING_RESOURCES) != 0)
215             appendBuffer(buf, "ALLOCATING_RESOURCES");
216         if ((state & Engine.ALLOCATED) != 0)
217             appendBuffer(buf, "ALLOCATED");
218         if ((state & Engine.DEALLOCATING_RESOURCES) != 0)
219             appendBuffer(buf, "DEALLOCATING_RESOURCES");
220         
221         if ((state & Engine.PAUSED) != 0)
222             appendBuffer(buf, "PAUSED");
223         if ((state & Engine.RESUMED) != 0)
224             appendBuffer(buf, "RESUMED");
225         
226         return buf.toString();
227     }
228
229     /**
230      * Adds a <code>String</code> to a buffer, with each
231      * <code>String</code> being separated by a ":".
232      *
233      * @param b the buffer to which to append <code>s</code to
234      * @param s the <code>String</code> to append to <code>b</code>
235      */
236     protected void appendBuffer(StringBuffer b, String s) {
237         if (b.length() > 0)
238             b.append(":");
239         b.append(s);
240     }
241     
242     /**
243      * Handles engine events from the engine.
244      */
245     protected class EngineMonitorEngineListener implements EngineListener {
246         public EngineMonitorEngineListener() {
247         }
248         public void enginePaused(EngineEvent e) {
249             handleEvent(e);
250         }
251         public void engineResumed(EngineEvent e)  {
252             handleEvent(e);
253         }
254         public void engineAllocated(EngineEvent e) {
255             handleEvent(e);
256         }
257         public void engineDeallocated(EngineEvent e) {
258             handleEvent(e);
259         }
260         public void engineAllocatingResources(EngineEvent e) {
261             handleEvent(e);
262         }
263         public void engineDeallocatingResources(EngineEvent e) {
264             handleEvent(e);
265         }
266         public void engineError(EngineErrorEvent e) {
267             handleEvent(e);
268         }
269     }
270 }