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
|
/*
* Copyright 1999 Phil Burk, Mobileer Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.softsynth.util;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* TextOutput sends text to System.out and/or a TextArea.
*
* @author Phil Burk (C) 1999 SoftSynth.com
*/
public class TextOutput extends Frame {
private static final long serialVersionUID = 1L;
/** Maximum number of characters allowed in TextArea. */
public int maxChars = (8 * 1024);
int numChars = 0;
static TextOutput staticTextOutput;
static Logger logger;
boolean isTextAreaEnabled = true;
boolean isSystemOutEnabled = true;
TextArea textArea;
Button buttonClear;
String lineTerminator = System.getProperty("line.separator", "\n");
public TextOutput() {
super("Text Output");
setSize(620, 400);
setLayout(new BorderLayout());
textArea = new TextArea("", 30, 120, TextArea.SCROLLBARS_BOTH);
textArea.setEditable(false);
textArea.setFont(Font.getFont("Monospaced-18")); // FIXME - why doesn't this work???
add("Center", textArea);
add("South", buttonClear = new Button("Clear"));
buttonClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText("");
numChars = 0;
}
});
/* If user tries to close window, hide dialog. */
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
hide();
}
});
}
public void setTextAreaEnable(boolean enabled) {
isTextAreaEnabled = enabled;
}
public void setSystemOutEnable(boolean enabled) {
isSystemOutEnabled = enabled;
}
/**
* Append text to the display area. If the TextArea gets full, then remove 1/4 of the text.
*/
private void append(String text) {
int len = text.length();
if ((numChars + len) > maxChars) {
int numKill = maxChars / 4;
/* Delete 1/4 the buffer. */
textArea.replaceRange("", 0, numKill);
numChars -= numKill;
}
textArea.append(text);
numChars += len;
}
public void log(String msg) {
if (isSystemOutEnabled)
System.out.print(msg);
if (isTextAreaEnabled)
append(msg);
}
public void logln(String msg) {
log(msg);
logln();
}
/* Just output a new line. */
public void logln() {
if (isSystemOutEnabled)
System.out.println();
if (isTextAreaEnabled)
append(lineTerminator);
}
/**
* Set a Logger to be used for directing a copy of the output to a log file.
*/
public static void setLogger(Logger lgr) {
logger = lgr;
}
public static Logger getLogger() {
return logger;
}
public static void print(String msg) {
if (staticTextOutput != null)
staticTextOutput.log(msg);
else
System.out.print(msg);
if (logger != null)
logger.log(msg);
}
public static void println(String msg) {
if (staticTextOutput != null)
staticTextOutput.logln(msg);
else
System.out.println(msg);
if (logger != null)
logger.logln(msg);
}
public static void println() {
if (staticTextOutput != null)
staticTextOutput.logln();
else
System.out.println();
if (logger != null)
logger.logln();
}
public static void error(String msg) {
println("ERROR: " + msg);
throw new RuntimeException(msg);
}
public static void setStaticLocation(int x, int y) {
getStaticInstance().setLocation(x, y);
}
// TODO - why can't this be called from Wire?
public static TextOutput getStaticInstance() {
if (staticTextOutput == null) {
staticTextOutput = new TextOutput();
staticTextOutput.setLocation(30, 0);
}
return staticTextOutput;
}
public static void open() {
getStaticInstance().setVisible(true);
}
public static void close() {
if (staticTextOutput != null)
staticTextOutput.setVisible(false);
}
public static void bringToFront() {
if (staticTextOutput != null)
staticTextOutput.toFront();
}
}
|