1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 */
16package org.tp23.gui.widget;
17
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.io.PrintStream;
21
22import javax.swing.JButton;
23import 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
35public 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().getLength());
54            }
55        });
56        jumpButton.setToolTipText("Click to jump to the end");
57
58
59    }
60
61    public synchronized PrintStream getOut(){
62        if(stream==null){
63            stream = new JTextAreaPrintStream(textArea);
64        }
65        return stream;
66    }
67    public void setAsSystemErr(){
68        System.setErr(getOut());
69    }
70    public void setAsSystemOut(){
71        System.setOut(getOut());
72    }
73
74
75}
76