KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > swt > SWTConsoleOutputStream


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.swt;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ShellAdapter;
28 import org.eclipse.swt.events.ShellEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Listener;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Text;
36
37 import com.sslexplorer.agent.client.Console;
38
39 /**
40  * {@link OutputStream} implementation that writes a GUI console component. This
41  * may be set as the {@link System#out} stream as a generic GUI console.
42  * <p>
43  * In order to improve performance, the frame will not be created and output
44  * will not be captured until it is first shown (usually as the result of a user
45  * action).
46  *
47  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
48  */

49 public class SWTConsoleOutputStream extends Console {
50
51     // Private instace variables
52

53     private StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
54     private Shell shell;
55     private Text text;
56     private Method JavaDoc deleteMethod;
57     private OutputStream JavaDoc oldSysOut;
58     private boolean userScrolled;
59     private SWTSystemTrayGUI gui;
60
61     /**
62      * Constructor.
63      *
64      * @param oldSysOut previous system out stream to also write to
65      * @param gui gui
66      */

67     public SWTConsoleOutputStream(OutputStream JavaDoc oldSysOut, SWTSystemTrayGUI gui) {
68         this.oldSysOut = oldSysOut;
69         this.gui = gui;
70     }
71
72     /**
73      * Show the console.
74      */

75     public void show() {
76         if (shell == null) {
77
78             try {
79                 deleteMethod = StringBuffer JavaDoc.class.getMethod("delete", new Class JavaDoc[] { int.class, int.class }); //$NON-NLS-1$
80
} catch (Throwable JavaDoc t) {
81             }
82
83             // Create the shell
84
shell = new Shell(gui.getDisplay(), SWT.RESIZE | SWT.TITLE);
85             GridLayout gridLayout = new GridLayout();
86             gridLayout.numColumns = 3;
87             shell.setLayout(gridLayout);
88             shell.addShellListener(new ShellAdapter() {
89                 public void shellClosed(ShellEvent e) {
90                 }
91             });
92             shell.setText(Messages.getString("ConsoleOutputStream.title")); //$NON-NLS-1$
93
shell.setImage(gui.loadImage(SWTSystemTrayGUI.class, "/images/frame-agent.png")); //$NON-NLS-1$
94

95             // Text
96

97             text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
98             text.setEditable(false);
99             GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL
100                 | GridData.VERTICAL_ALIGN_FILL
101                 | GridData.GRAB_VERTICAL);
102             data.horizontalSpan = 3;
103             data.verticalSpan = 3;
104             data.heightHint = 300;
105             data.widthHint = 480;
106             text.setLayoutData(data);
107
108             // Clear Button
109
final Button clearButton = new Button(shell, SWT.PUSH);
110             clearButton.setText(Messages.getString("ConsoleOutputStream.actions.clear")); //$NON-NLS-1$
111
data = new GridData();
112             data.horizontalAlignment = GridData.END;
113             data.horizontalSpan = 2;
114             data.grabExcessHorizontalSpace = true;
115             clearButton.setLayoutData(data);
116
117             // Close button
118

119             final Button closeButton = new Button(shell, SWT.PUSH);
120             closeButton.setText(Messages.getString("ConsoleOutputStream.actions.close"));
121
122             // Button listener
123

124             Listener listener = new Listener() {
125                 public void handleEvent(Event event) {
126                     if (event.widget == clearButton) {
127                         clear();
128                     } else {
129                         shell.setVisible(false);
130                     }
131                 }
132             };
133             clearButton.addListener(SWT.Selection, listener);
134             closeButton.addListener(SWT.Selection, listener);
135
136             shell.pack();
137         }
138         text.setText(buf.toString());
139         shell.open();
140         userScrolled = false;
141     }
142
143     void clear() {
144         synchronized (buf) {
145             buf.setLength(0);
146             if (shell.isVisible()) {
147                 text.setText(buf.toString());
148             }
149         }
150     }
151
152     void append(final String JavaDoc text) {
153         try {
154             synchronized (buf) {
155                 buf.append(text);
156                 if (buf.length() > 65535) {
157                     if (deleteMethod != null) {
158                         try {
159                             deleteMethod.invoke(buf, new Object JavaDoc[] { new Integer JavaDoc(0), new Integer JavaDoc(buf.length() - 65535) });
160                         } catch (Throwable JavaDoc t) {
161                             String JavaDoc newBuf = buf.toString().substring(buf.length() - 65535);
162                             buf.setLength(0);
163                             buf.append(newBuf);
164                         }
165                     } else {
166                         String JavaDoc newBuf = buf.toString().substring(buf.length() - 65535);
167                         buf.setLength(0);
168                         buf.append(newBuf);
169                     }
170                 }
171                 if (gui.getDisplay() != null) {
172                     gui.getDisplay().asyncExec(new Runnable JavaDoc() {
173                         public void run() {
174                             if (shell != null && shell.isVisible()) {
175                                 SWTConsoleOutputStream.this.text.setText(buf.toString());
176                                 if (!userScrolled) {
177                                     SWTConsoleOutputStream.this.text.setSelection(buf.length());
178                                     // textArea.setCaretPosition(buf.length());
179
}
180                             }
181                         }
182                     });
183                 }
184             }
185         } catch (Throwable JavaDoc t) {
186         }
187     }
188
189     /*
190      * (non-Javadoc)
191      *
192      * @see java.io.OutputStream#write(int)
193      */

194     public void write(int b) throws IOException JavaDoc {
195         append(String.valueOf((char) b));
196         if (oldSysOut != null) {
197             oldSysOut.write(b);
198         }
199     }
200
201     /*
202      * (non-Javadoc)
203      *
204      * @see java.io.OutputStream#write(byte[], int, int)
205      */

206     public void write(byte[] buf, int off, int len) throws IOException JavaDoc {
207         append(new String JavaDoc(buf, off, len));
208         if (oldSysOut != null) {
209             oldSysOut.write(buf, off, len);
210         }
211     }
212
213     /*
214      * (non-Javadoc)
215      *
216      * @see java.io.OutputStream#flush()
217      */

218     public void flush() throws IOException JavaDoc {
219         super.flush();
220         if (oldSysOut != null) {
221             oldSysOut.flush();
222         }
223     }
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see com.sslexplorer.agent.client.Console#dispose()
229      */

230     public void dispose() {
231         if (shell != null) {
232             shell.getDisplay().syncExec(new Runnable JavaDoc() {
233                 public void run() {
234                     shell.dispose();
235                 }
236             });
237         }
238     }
239 }
Popular Tags