upstream version 1.2.2
[debian/freetts] / com / sun / speech / engine / EngineEventPanel.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.awt.BorderLayout;
11 import java.awt.Point;
12
13 import java.awt.event.ActionListener;
14 import java.awt.event.ActionEvent;
15
16 import javax.swing.BorderFactory;
17 import javax.swing.JPanel;
18 import javax.swing.JTextArea;
19 import javax.swing.JScrollPane;
20 import javax.swing.JButton;
21
22 /**
23  * Simple GUI for monitoring events of an <code>Engine</code>.  Used
24  * for debugging and testing purposes.
25  */
26 public class EngineEventPanel extends JPanel {
27     /**
28      * The area where engine events are posted.
29      */
30     protected JTextArea textArea;
31
32     /**
33      * The scroll pane containing the <code>textArea</code>.
34      *
35      * @see #textArea
36      */
37     protected JScrollPane scroller;
38
39     /**
40      * The button for clearing the <code>textArea</code>.
41      *
42      * @see #textArea
43      */
44     protected JButton clearButton;
45
46     /**
47      * Class constructor.
48      */
49     public EngineEventPanel() {
50         setLayout(new BorderLayout());
51         setBorder(BorderFactory.createTitledBorder("Events:"));
52         
53         clearButton = new JButton("Clear");
54         clearButton.setMnemonic('C');
55         clearButton.addActionListener(new ActionListener() {
56             public void actionPerformed(ActionEvent evt) {
57                 clearText();
58             }
59         });
60         
61         textArea = new JTextArea();
62         scroller = new JScrollPane(textArea);
63         
64         add(scroller,BorderLayout.CENTER);
65         add(clearButton,BorderLayout.SOUTH);
66     }
67
68     /**
69      * Clears the text in the text area.
70      */
71     public void clearText() {
72         textArea.setText("");
73     }
74
75     /**
76      * Sets the text in the text area.
77      *
78      * @param s the new text
79      */
80     public void setText(String s) {
81         textArea.setText(s);
82     }
83
84     /**
85      * Appends text to the text area and scrolls the text area so the
86      * new text is visible.
87      *
88      * @param s the text to append
89      */
90     public void addText(String s) {
91         textArea.append(s);
92         Point pt = new Point(0, textArea.getHeight() - 1);
93         scroller.getViewport().setViewPosition(pt);        
94     }
95 }