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