1
16
17 package gate.gui;
18
19 import java.awt.Color;
20 import java.awt.Rectangle;
21 import java.awt.event.*;
22 import java.io.*;
23
24 import javax.swing.*;
25 import javax.swing.text.*;
26
27 import gate.swing.XJTextPane;
28 import gate.util.Err;
29 import gate.util.Out;
30
31
38 public class LogArea extends XJTextPane {
39
40
41 protected LogArea thisLogArea = null;
42
43
44 protected JPopupMenu popup = null;
45
46
47 protected PrintStream originalOut;
48
49
50 protected PrintStream originalErr;
51
52 protected SelectAllAction selectAllAction = null;
53
54
55 protected CopyAction copyAction = null;
56
57
58 protected ClearAllAction clearAllAction = null;
59
60
63 public LogArea(){
64 thisLogArea = this;
65 this.setEditable(false);
66
67 LogAreaOutputStream err = new LogAreaOutputStream(true);
68 LogAreaOutputStream out = new LogAreaOutputStream(false);
69
70 try{
72 Err.setPrintWriter(new UTF8PrintWriter(err,true));
73 }catch(UnsupportedEncodingException uee){
74 uee.printStackTrace();
75 }
76 try{
78 Out.setPrintWriter(new UTF8PrintWriter(out,true));
79 }catch(UnsupportedEncodingException uee){
80 uee.printStackTrace();
81 }
82
83 originalOut = System.out;
85 try{
86 System.setOut(new UTF8PrintStream(out, true));
87 }catch(UnsupportedEncodingException uee){
88 uee.printStackTrace();
89 }
90
91 originalErr = System.err;
93 try{
94 System.setErr(new UTF8PrintStream(err, true));
95 }catch(UnsupportedEncodingException uee){
96 uee.printStackTrace();
97 }
98 popup = new JPopupMenu();
99 selectAllAction = new SelectAllAction();
100 copyAction = new CopyAction();
101 clearAllAction = new ClearAllAction();
102
103 popup.add(selectAllAction);
104 popup.add(copyAction);
105 popup.addSeparator();
106 popup.add(clearAllAction);
107 initListeners();
108 }
110
111 public void initListeners(){
112 super.initListeners();
113 this.addMouseListener(new MouseAdapter(){
114 public void mouseClicked(MouseEvent e){
115 if(SwingUtilities.isRightMouseButton(e)){
116 popup.show(thisLogArea, e.getPoint().x, e.getPoint().y);
117 } } }); }
121
122
123 public PrintStream getOriginalErr() {
124 return originalErr;
125 }
126
127
128 public PrintStream getOriginalOut() {
129 return originalOut;
130 }
132
133 protected class SelectAllAction extends AbstractAction{
134 public SelectAllAction(){
135 super("Select all");
136 } public void actionPerformed(ActionEvent e){
138 thisLogArea.selectAll();
139 } }
142
143 protected class CopyAction extends AbstractAction{
144 public CopyAction(){
145 super("Copy");
146 } public void actionPerformed(ActionEvent e){
148 thisLogArea.copy();
149 } }
152
156 protected class SwingWriter implements Runnable{
157 SwingWriter(String text, Style style){
158 this.text = text;
159 this.style = style;
160 }
161
162 public void run(){
163 try{
164 if(getDocument().getLength() > 0){
165 Rectangle place = modelToView(getDocument().getLength() - 1);
166 if(place != null) scrollRectToVisible(place);
167 }
168 getDocument().insertString(getDocument().getLength(), text, style);
169 } catch(BadLocationException e){
170 e.printStackTrace(System.err);
171 } }
173 String text;
174 Style style;
175 }
176
177
180 public static class UTF8PrintWriter extends PrintWriter{
181 public UTF8PrintWriter(OutputStream out)
182 throws UnsupportedEncodingException{
183 this(out, true);
184 }
185
186 public UTF8PrintWriter(OutputStream out, boolean autoFlush)
187 throws UnsupportedEncodingException{
188 super(new BufferedWriter(new OutputStreamWriter(out, "UTF-8")),
189 autoFlush);
190 }
191 }
192
193
196 public static class UTF8PrintStream extends PrintStream{
197 public UTF8PrintStream(OutputStream out)
198 throws UnsupportedEncodingException{
199 this(out, true);
200 }
201
202 public UTF8PrintStream(OutputStream out, boolean autoFlush)
203 throws UnsupportedEncodingException{
204 super(out, autoFlush);
205 }
206
207
211 public void print(String s) {
212 try{
213 write(s.getBytes("UTF-8"));
214 }catch(UnsupportedEncodingException uee){
215 }catch(IOException ioe){
217 setError();
219 }
220 }
221
222
226 public void print(char s[]) {
227 print(String.valueOf(s));
228 }
229 }
230
231
232 protected class ClearAllAction extends AbstractAction{
233 public ClearAllAction(){
234 super("Clear all");
235 } public void actionPerformed(ActionEvent e){
237 try{
238 thisLogArea.getDocument().remove(0,thisLogArea.getDocument().getLength());
239 } catch (BadLocationException e1){
240 e1.printStackTrace(Err.getPrintWriter());
241 } } }
245
248 class LogAreaOutputStream extends OutputStream{
249
250 private boolean isErr = false;
251
252 private Style style = null;
253
254
255 public LogAreaOutputStream(boolean anIsErr){
256 isErr = anIsErr;
257 if (isErr){
258 style = addStyle("error", getStyle("default"));
259 StyleConstants.setForeground(style, Color.red);
260 }else {
261 style = addStyle("out",getStyle("default"));
262 StyleConstants.setForeground(style, Color.black);
263 } }
266
269 public void write(int charCode){
270 charCode &= 0x000000FF;
272 char c = (char)charCode;
274 SwingUtilities.invokeLater(new SwingWriter(String.valueOf(c), style));
276 }
278
281 public void write(byte[] data, int offset, int length){
282 try{
284 SwingUtilities.invokeLater(new SwingWriter(new String(data,offset,
285 length, "UTF-8"),
286 style));
287 }catch(UnsupportedEncodingException uee){
288 uee.printStackTrace();
289 }
290 } }}