2 * Copyright 1998-2001 Sun Microsystems, Inc.
4 * See the file "license.terms" for information on usage and
5 * redistribution of this file, and for a DISCLAIMER OF ALL
8 package com.sun.speech.engine;
10 import java.util.Date;
12 import java.awt.Component;
13 import java.awt.GridLayout;
15 import javax.swing.BorderFactory;
16 import javax.swing.JPanel;
17 import javax.swing.JLabel;
19 import javax.speech.Engine;
20 import javax.speech.EngineListener;
21 import javax.speech.EngineEvent;
22 import javax.speech.EngineErrorEvent;
25 * Simple GUI for monitoring events and state changes of an
26 * <code>Engine</code>. Used for debugging and testing purposes.
28 public class EngineMonitor {
30 * The <code>Engine</code> to monitor.
32 protected Engine engine;
35 * The <code>EngineListener</code> registered with the engine.
37 protected EngineListener engineListener;
40 * The panel used to post engine events.
42 protected EngineEventPanel eventPanel;
45 * The panel containing the current engine states.
47 protected JPanel statePanel;
50 * The label containing the string "deallocated".
52 protected JLabel deallocatedLabel;
55 * The label containing the string "allocating resources".
57 protected JLabel allocatingResourcesLabel;
60 * The label containing the string "allocated".
62 protected JLabel allocatedLabel;
65 * The label containing the string "deallocating resources".
67 protected JLabel deallocatingResourcesLabel;
70 * The label containing the string "paused".
72 protected JLabel pausedLabel;
75 * The label containing the string "resumed".
77 protected JLabel resumedLabel;
82 * @param eng the <code>Engine</code> to watch
84 public EngineMonitor(Engine eng) {
86 engine.addEngineListener(getEngineListener());
90 * Creates the engine listener if necessary, and then returns it.
91 * There should be only one.
93 * @return the engine listener
95 protected EngineListener getEngineListener() {
96 if (engineListener == null) {
97 engineListener = new EngineMonitorEngineListener();
99 return engineListener;
103 * Gets the panel containing the area to post engine events in.
105 * @return the panel containing the area to post engine events in
107 public Component getEventPanel() {
108 if (eventPanel == null) {
109 eventPanel = new EngineEventPanel();
115 * Gets the panel containing the labels for representing the
116 * current engine state.
118 * @return the panel containing the labels for representing the
119 * current engine state.
121 public Component getStatePanel() {
122 if (statePanel == null) {
123 JPanel newStatePanel = new JPanel();
124 newStatePanel.setLayout(new GridLayout(1,2));
126 JPanel engineStatePanel = new JPanel();
127 engineStatePanel.setLayout(new GridLayout(4,2));
128 engineStatePanel.setBorder(
129 BorderFactory.createTitledBorder("Engine State:"));
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");
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;
152 * Handles an event from the engine.
154 * @param e the event from the engine
156 protected void handleEvent(EngineEvent e) {
157 if (eventPanel != null) {
158 eventPanel.addText(new Date().toString() + ": "
161 eventPanel.addText(" Old state: "
162 + engineStateString(e.getOldEngineState())
164 eventPanel.addText(" New state: "
165 + engineStateString(e.getNewEngineState())
168 updateGUIComponents();
172 * Checks the current state of the engine and makes sure the GUI
173 * components reflect this state accurately.
175 protected void updateGUIComponents() {
176 updateEngineStateComponents();
180 * Checks the current state of the engine and makes sure the GUI
181 * components reflect this state accurately.
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));
201 * Returns a <code>String</code> representing the
202 * <code>state</code>.
204 * @param state the state to turn into a <code>String</code>
206 * @return a <code>String</code> representing the
209 protected String engineStateString(long state) {
210 StringBuffer buf = new StringBuffer();
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");
221 if ((state & Engine.PAUSED) != 0)
222 appendBuffer(buf, "PAUSED");
223 if ((state & Engine.RESUMED) != 0)
224 appendBuffer(buf, "RESUMED");
226 return buf.toString();
230 * Adds a <code>String</code> to a buffer, with each
231 * <code>String</code> being separated by a ":".
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>
236 protected void appendBuffer(StringBuffer b, String s) {
243 * Handles engine events from the engine.
245 protected class EngineMonitorEngineListener implements EngineListener {
246 public EngineMonitorEngineListener() {
248 public void enginePaused(EngineEvent e) {
251 public void engineResumed(EngineEvent e) {
254 public void engineAllocated(EngineEvent e) {
257 public void engineDeallocated(EngineEvent e) {
260 public void engineAllocatingResources(EngineEvent e) {
263 public void engineDeallocatingResources(EngineEvent e) {
266 public void engineError(EngineErrorEvent e) {