KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > awt > AWTConsoleOutputStream


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.sslexplorer.agent.client.gui.awt;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Button JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.FlowLayout JavaDoc;
26 import java.awt.Frame JavaDoc;
27 import java.awt.Panel JavaDoc;
28 import java.awt.TextArea JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.WindowAdapter JavaDoc;
32 import java.awt.event.WindowEvent JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 import com.sshtools.ui.awt.UIUtil;
38 import com.sslexplorer.agent.client.Console;
39
40 /**
41  * {@link OutputStream} implementation that writes a GUI console component. This
42  * may be set as the {@link System#out} stream as a generic GUI console.
43  * <p>
44  * In order to improve performance, the frame will not be created and output
45  * will not be captured until it is first shown (usually as the result of a user
46  * action).
47  *
48  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
49  */

50 public class AWTConsoleOutputStream extends Console {
51
52     // Private instace variables
53

54     private StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
55     private Frame JavaDoc frame;
56     private TextArea JavaDoc textArea;
57     private Method JavaDoc deleteMethod;
58     private OutputStream JavaDoc oldSysOut;
59     private boolean userScrolled;
60
61     /**
62      * Constructor.
63      *
64      * @param oldSysOut previous system out stream to also write to
65      */

66     public AWTConsoleOutputStream(OutputStream JavaDoc oldSysOut) {
67         this.oldSysOut = oldSysOut;
68     }
69
70     /**
71      * Show the console.
72      */

73     public void show() {
74         if (textArea == null) {
75
76             try {
77                 deleteMethod = StringBuffer JavaDoc.class.getMethod("delete", new Class JavaDoc[] { int.class, int.class }); //$NON-NLS-1$
78
} catch (Throwable JavaDoc t) {
79             }
80             textArea = new TextArea JavaDoc();
81             textArea.setEditable(false);
82             textArea.setBackground(Color.white);
83             textArea.setForeground(Color.black);
84             Panel JavaDoc panel = new Panel JavaDoc(new BorderLayout JavaDoc());
85             panel.setBackground(Color.gray);
86             panel.setForeground(Color.black);
87             panel.add(textArea, BorderLayout.CENTER);
88             Panel JavaDoc buttonPanel = new Panel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
89             buttonPanel.setBackground(Color.gray);
90             buttonPanel.setForeground(Color.black);
91             Button JavaDoc clear = new Button JavaDoc(Messages.getString("ConsoleOutputStream.actions.clear")); //$NON-NLS-1$
92
clear.addActionListener(new ActionListener JavaDoc() {
93                 public void actionPerformed(ActionEvent JavaDoc evt) {
94                     clear();
95                 }
96             });
97             buttonPanel.add(clear);
98             Button JavaDoc close = new Button JavaDoc(Messages.getString("ConsoleOutputStream.actions.close")); //$NON-NLS-1$
99
close.addActionListener(new ActionListener JavaDoc() {
100                 public void actionPerformed(ActionEvent JavaDoc evt) {
101                     frame.setVisible(false);
102                 }
103             });
104             buttonPanel.add(close);
105             panel.add(buttonPanel, BorderLayout.SOUTH);
106             frame = new Frame JavaDoc(Messages.getString("ConsoleOutputStream.title")); //$NON-NLS-1$
107
frame.addWindowListener(new WindowAdapter JavaDoc() {
108                 public void windowClosing(WindowEvent JavaDoc evt) {
109                     frame.setVisible(false);
110
111                 }
112             });
113             frame.setIconImage(UIUtil.loadImage(getClass(), "/images/frame-agent.gif")); //$NON-NLS-1$
114
frame.add(panel);
115             frame.pack();
116             frame.setLocation(100, 100);
117             frame.setSize(300, 400);
118         }
119         textArea.setText(buf.toString());
120         frame.setVisible(true);
121         frame.toFront();
122         textArea.setCaretPosition(buf.length());
123         userScrolled = false;
124     }
125
126     void clear() {
127         synchronized (buf) {
128             buf.setLength(0);
129             if (frame.isVisible()) {
130                 textArea.setText(buf.toString());
131                 textArea.setCaretPosition(buf.length());
132             }
133         }
134     }
135
136     void append(String JavaDoc text) {
137         synchronized (buf) {
138             buf.append(text);
139             if (buf.length() > 65535) {
140                 if (deleteMethod != null) {
141                     try {
142                         deleteMethod.invoke(buf, new Object JavaDoc[] { new Integer JavaDoc(0), new Integer JavaDoc(buf.length() - 65535) });
143                     } catch (Throwable JavaDoc t) {
144                         String JavaDoc newBuf = buf.toString().substring(buf.length() - 65535);
145                         buf.setLength(0);
146                         buf.append(newBuf);
147                     }
148                 } else {
149                     String JavaDoc newBuf = buf.toString().substring(buf.length() - 65535);
150                     buf.setLength(0);
151                     buf.append(newBuf);
152                 }
153             }
154             if (frame != null && frame.isVisible()) {
155                 textArea.setText(buf.toString());
156                 if (!userScrolled) {
157                     textArea.setCaretPosition(buf.length());
158                 }
159             }
160         }
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see java.io.OutputStream#write(int)
167      */

168     public void write(int b) throws IOException JavaDoc {
169         append(String.valueOf((char) b));
170         if (oldSysOut != null) {
171             oldSysOut.write(b);
172         }
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see java.io.OutputStream#write(byte[], int, int)
179      */

180     public void write(byte[] buf, int off, int len) throws IOException JavaDoc {
181         append(new String JavaDoc(buf, off, len));
182         if (oldSysOut != null) {
183             oldSysOut.write(buf, off, len);
184         }
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see java.io.OutputStream#flush()
191      */

192     public void flush() throws IOException JavaDoc {
193         super.flush();
194         if (oldSysOut != null) {
195             oldSysOut.flush();
196         }
197     }
198
199     public void dispose() {
200         if(frame != null)
201             frame.dispose();
202     }
203 }
Popular Tags