KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > DebugConsole


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.swing;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.WindowAdapter JavaDoc;
28 import java.awt.event.WindowEvent JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.PrintStream JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JFrame JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JScrollPane JavaDoc;
37 import javax.swing.JTextPane JavaDoc;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import javax.swing.text.SimpleAttributeSet JavaDoc;
40 import javax.swing.text.StyleConstants JavaDoc;
41
42 import com.sshtools.ui.awt.UIUtil;
43
44 public class DebugConsole {
45
46     private JFrame JavaDoc frame;
47     private JTextPane JavaDoc textArea;
48     private Method JavaDoc deleteMethod;
49     private OutputStream JavaDoc oldSysOut;
50     private boolean userScrolled;
51
52     private static DebugConsole console;
53
54     private DebugConsole() {
55         textArea = new JTextPane JavaDoc();
56     }
57
58     /**
59      * Show the console.
60      */

61     void doShow() {
62         if (frame == null) {
63
64             try {
65                 deleteMethod = StringBuffer JavaDoc.class.getMethod("delete", new Class JavaDoc[] { int.class, int.class }); //$NON-NLS-1$
66
} catch (Throwable JavaDoc t) {
67             }
68             textArea.setEditable(false);
69             JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
70             panel.setBackground(Color.gray);
71             panel.setForeground(Color.black);
72             panel.add(new JScrollPane JavaDoc(textArea), BorderLayout.CENTER);
73             JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
74             buttonPanel.setBackground(Color.gray);
75             buttonPanel.setForeground(Color.black);
76             JButton JavaDoc clear = new JButton JavaDoc("Clear"); //$NON-NLS-1$
77
clear.addActionListener(new ActionListener JavaDoc() {
78                 public void actionPerformed(ActionEvent JavaDoc evt) {
79                     clear();
80                 }
81             });
82             buttonPanel.add(clear);
83             JButton JavaDoc close = new JButton JavaDoc("Close"); //$NON-NLS-1$
84
close.addActionListener(new ActionListener JavaDoc() {
85                 public void actionPerformed(ActionEvent JavaDoc evt) {
86                     frame.setVisible(false);
87                 }
88             });
89             buttonPanel.add(close);
90             panel.add(buttonPanel, BorderLayout.SOUTH);
91             frame = new JFrame JavaDoc("Console"); //$NON-NLS-1$
92
frame.addWindowListener(new WindowAdapter JavaDoc() {
93                 public void windowClosing(WindowEvent JavaDoc evt) {
94                     frame.setVisible(false);
95
96                 }
97             });
98             frame.setIconImage(UIUtil.loadImage(getClass(), "/images/idle-16x16.gif")); //$NON-NLS-1$
99
frame.add(panel);
100             frame.pack();
101             frame.setLocation(100, 100);
102             frame.setSize(300, 400);
103         }
104         frame.setVisible(true);
105         frame.toFront();
106         textArea.setCaretPosition(textArea.getDocument().getLength());
107         userScrolled = false;
108     }
109
110     void clear() {
111         synchronized (textArea) {
112             try {
113                 textArea.getStyledDocument().remove(0, textArea.getDocument().getLength());
114                 if (frame.isVisible()) {
115                     textArea.setCaretPosition(0);
116                 }
117             } catch (BadLocationException JavaDoc e) {
118             }
119         }
120     }
121
122     public void append(String JavaDoc text, Color JavaDoc color) {
123         synchronized (textArea) {
124             SimpleAttributeSet JavaDoc attr1 = new SimpleAttributeSet JavaDoc();
125             StyleConstants.setForeground(attr1, color);
126             try {
127                 textArea.getStyledDocument().insertString(textArea.getStyledDocument().getLength(), text, attr1);
128                 if (textArea.getStyledDocument().getLength() > 65535) {
129                     textArea.getStyledDocument().remove(65536, textArea.getStyledDocument().getLength() - 65535);
130                 }
131                 if (frame != null && frame.isVisible()) {
132                     if (!userScrolled) {
133                         textArea.setCaretPosition(textArea.getStyledDocument().getLength());
134                     }
135                 }
136             } catch (BadLocationException JavaDoc ble) {
137
138             }
139         }
140     }
141     
142     public static boolean isStarted() {
143         return console != null;
144     }
145
146     public static void start() {
147         OutputStream JavaDoc oldSysOut = System.out;
148         OutputStream JavaDoc oldSysErr = System.err;
149         console = new DebugConsole();
150         System.setOut(new PrintStream JavaDoc(new ConsoleOutputStream(oldSysOut, Color.black, console), true));
151         System.setErr(new PrintStream JavaDoc(new ConsoleOutputStream(oldSysErr, Color.red, console), true));
152     }
153     
154     public static void show() {
155         console.doShow();
156     }
157
158 }
159
Popular Tags