upstream version 1.2.2
[debian/freetts] / com / sun / speech / engine / synthesis / SynthesizerMonitor.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.synthesis;
9
10 import java.awt.Component;
11 import java.awt.GridLayout;
12 import java.util.Enumeration;
13
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;
22
23 import com.sun.speech.engine.EngineMonitor;
24
25 /**
26  * Simple GUI that monitors events and state changes of an
27  * <code>Synthesizer</code>.  Used for debugging and testing purposes.
28  */
29 public class SynthesizerMonitor extends EngineMonitor {
30     /**
31      * Label containing "queue empty"
32      */
33     protected JLabel queueEmptyLabel;
34
35     /**
36      * Label containing "queue not empty"
37      */
38     protected JLabel queueNotEmptyLabel;
39
40     /**
41      * Label containing "queue size"
42      */
43     protected JLabel queueSizeLabel;
44
45     /**
46      * Class constructor.
47      *
48      * @param synth the <code>Synthesizer</code> to monitor
49      */
50     public SynthesizerMonitor(Synthesizer synth) {
51         super(synth);
52     }
53
54     // Inherited javadoc.
55     //
56     protected EngineListener getEngineListener() {
57         if (engineListener == null) {
58             engineListener = new SynthesizerMonitorEngineListener();
59         }
60         return engineListener;
61     }
62
63     /**
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.
67      *
68      * @return the panel containing the labels for representing the
69      *   current engine state.
70      */
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);
85         }
86         return statePanel;
87     }
88
89     // Inherited javadoc.
90     //
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));
98
99             Synthesizer synth = (Synthesizer) engine;
100             int queueSize = countElements(synth.enumerateQueue());
101             queueSizeLabel.setText("Queue Size: " + queueSize + "  ");
102         }
103     }
104
105     /**
106      * Counts the number of elements in the enumeration.
107      *
108      * @param e the enumeration
109      *
110      * @return the number of elements in the enumeration
111      */
112     private int countElements(Enumeration e) {
113         int count = 0;
114         while (e.hasMoreElements()) {
115             e.nextElement();
116             count++;
117         }
118         return count;
119     }
120     
121     // Inherited javadoc.
122     //
123     protected String engineStateString(long state) {
124         StringBuffer buf = new StringBuffer();
125
126         appendBuffer(buf,super.engineStateString(state));
127         
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");
132         
133         return buf.toString();
134     }
135
136     // Inherited javadoc.
137     //
138     protected void handleEvent(EngineEvent e) {
139         super.handleEvent(e);
140     }
141     
142     /**
143      * Handles engine events from the engine.  Extended to include
144      * <code>SynthesizerEvents</code>.
145      */
146     class SynthesizerMonitorEngineListener
147         extends EngineMonitorEngineListener implements SynthesizerListener {
148         public SynthesizerMonitorEngineListener() {
149         }
150         public void queueEmptied(SynthesizerEvent e) {
151             handleEvent(e);
152         }
153         public void queueUpdated(SynthesizerEvent e) {
154             handleEvent(e);
155         }
156     }
157 }