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.synthesis;
10 import java.awt.Component;
11 import java.awt.GridLayout;
12 import java.util.Enumeration;
14 import javax.speech.EngineEvent;
15 import javax.speech.EngineListener;
16 import javax.speech.synthesis.Synthesizer;
17 import javax.speech.synthesis.SynthesizerEvent;
18 import javax.speech.synthesis.SynthesizerListener;
19 import javax.swing.BorderFactory;
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
23 import com.sun.speech.engine.EngineMonitor;
26 * Simple GUI that monitors events and state changes of an
27 * <code>Synthesizer</code>. Used for debugging and testing purposes.
29 public class SynthesizerMonitor extends EngineMonitor {
31 * Label containing "queue empty"
33 protected JLabel queueEmptyLabel;
36 * Label containing "queue not empty"
38 protected JLabel queueNotEmptyLabel;
41 * Label containing "queue size"
43 protected JLabel queueSizeLabel;
48 * @param synth the <code>Synthesizer</code> to monitor
50 public SynthesizerMonitor(Synthesizer synth) {
56 protected EngineListener getEngineListener() {
57 if (engineListener == null) {
58 engineListener = new SynthesizerMonitorEngineListener();
60 return engineListener;
64 * Gets the panel containing the labels for representing the
65 * current engine state. This augments the super class's panel
66 * by adding synthesizer queue state.
68 * @return the panel containing the labels for representing the
69 * current engine state.
71 public Component getStatePanel() {
72 if (statePanel == null) {
73 statePanel = (JPanel) super.getStatePanel();
74 JPanel queueStatePanel = new JPanel();
75 queueStatePanel.setBorder(
76 BorderFactory.createTitledBorder("Synthesizer State:"));
77 queueStatePanel.setLayout(new GridLayout(4,1));
78 queueEmptyLabel = new JLabel("QUEUE_EMPTY");
79 queueNotEmptyLabel = new JLabel("QUEUE_NOT_EMPTY");
80 queueSizeLabel = new JLabel("Queue Size: XXX");
81 queueStatePanel.add(queueEmptyLabel);
82 queueStatePanel.add(queueNotEmptyLabel);
83 queueStatePanel.add(queueSizeLabel);
84 statePanel.add(queueStatePanel);
91 protected void updateGUIComponents() {
92 super.updateGUIComponents();
93 if (statePanel != null) {
94 queueEmptyLabel.setEnabled(
95 engine.testEngineState(Synthesizer.QUEUE_EMPTY));
96 queueNotEmptyLabel.setEnabled(
97 engine.testEngineState(Synthesizer.QUEUE_NOT_EMPTY));
99 Synthesizer synth = (Synthesizer) engine;
100 int queueSize = countElements(synth.enumerateQueue());
101 queueSizeLabel.setText("Queue Size: " + queueSize + " ");
106 * Counts the number of elements in the enumeration.
108 * @param e the enumeration
110 * @return the number of elements in the enumeration
112 private int countElements(Enumeration e) {
114 while (e.hasMoreElements()) {
121 // Inherited javadoc.
123 protected String engineStateString(long state) {
124 StringBuffer buf = new StringBuffer();
126 appendBuffer(buf,super.engineStateString(state));
128 if ((state & Synthesizer.QUEUE_EMPTY) != 0)
129 appendBuffer(buf, "QUEUE_EMPTY");
130 if ((state & Synthesizer.QUEUE_NOT_EMPTY) != 0)
131 appendBuffer(buf, "QUEUE_NOT_EMPTY");
133 return buf.toString();
136 // Inherited javadoc.
138 protected void handleEvent(EngineEvent e) {
139 super.handleEvent(e);
143 * Handles engine events from the engine. Extended to include
144 * <code>SynthesizerEvents</code>.
146 class SynthesizerMonitorEngineListener
147 extends EngineMonitorEngineListener implements SynthesizerListener {
148 public SynthesizerMonitorEngineListener() {
150 public void queueEmptied(SynthesizerEvent e) {
153 public void queueUpdated(SynthesizerEvent e) {