01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.gui.widget;
17:
18: import java.awt.event.ActionEvent;
19: import java.awt.event.ActionListener;
20: import java.io.PrintStream;
21:
22: import javax.swing.JButton;
23: import javax.swing.JScrollPane;
24:
25: /**
26: *
27: * <p>Title: A JTextArea that can be set to show System.out or System.err</p>
28: * <p>Description: </p>
29: * <p>Copyright: Copyright (c) 2004</p>
30: * <p>Company: </p>
31: * @author Paul Hinds
32: * @version 1.0
33: */
34:
35: public class SystemOutJTextArea extends JScrollPane {
36:
37: private JTextAreaPrintStream stream;
38: private FollowingJTextArea textArea = new FollowingJTextArea();
39:
40: public SystemOutJTextArea() {
41: jInit();
42: }
43:
44: private void jInit() {
45: this .getViewport().setView(textArea);
46: textArea.setEditable(false);
47: JButton jumpButton = new JButton("");
48: setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
49: setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
50: setCorner(this .LOWER_RIGHT_CORNER, jumpButton);
51: jumpButton.addActionListener(new ActionListener() {
52: public void actionPerformed(ActionEvent e) {
53: textArea.setCaretPosition(textArea.getDocument()
54: .getLength());
55: }
56: });
57: jumpButton.setToolTipText("Click to jump to the end");
58:
59: }
60:
61: public synchronized PrintStream getOut() {
62: if (stream == null) {
63: stream = new JTextAreaPrintStream(textArea);
64: }
65: return stream;
66: }
67:
68: public void setAsSystemErr() {
69: System.setErr(getOut());
70: }
71:
72: public void setAsSystemOut() {
73: System.setOut(getOut());
74: }
75:
76: }
|