1 19 20 package com.sslexplorer.agent.client.gui.swt; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.lang.reflect.Method ; 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 49 public class SWTConsoleOutputStream extends Console { 50 51 53 private StringBuffer buf = new StringBuffer (); 54 private Shell shell; 55 private Text text; 56 private Method deleteMethod; 57 private OutputStream oldSysOut; 58 private boolean userScrolled; 59 private SWTSystemTrayGUI gui; 60 61 67 public SWTConsoleOutputStream(OutputStream oldSysOut, SWTSystemTrayGUI gui) { 68 this.oldSysOut = oldSysOut; 69 this.gui = gui; 70 } 71 72 75 public void show() { 76 if (shell == null) { 77 78 try { 79 deleteMethod = StringBuffer .class.getMethod("delete", new Class [] { int.class, int.class }); } catch (Throwable t) { 81 } 82 83 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")); shell.setImage(gui.loadImage(SWTSystemTrayGUI.class, "/images/frame-agent.png")); 95 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 final Button clearButton = new Button(shell, SWT.PUSH); 110 clearButton.setText(Messages.getString("ConsoleOutputStream.actions.clear")); data = new GridData(); 112 data.horizontalAlignment = GridData.END; 113 data.horizontalSpan = 2; 114 data.grabExcessHorizontalSpace = true; 115 clearButton.setLayoutData(data); 116 117 119 final Button closeButton = new Button(shell, SWT.PUSH); 120 closeButton.setText(Messages.getString("ConsoleOutputStream.actions.close")); 121 122 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 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 [] { new Integer (0), new Integer (buf.length() - 65535) }); 160 } catch (Throwable t) { 161 String newBuf = buf.toString().substring(buf.length() - 65535); 162 buf.setLength(0); 163 buf.append(newBuf); 164 } 165 } else { 166 String 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 () { 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 } 180 } 181 } 182 }); 183 } 184 } 185 } catch (Throwable t) { 186 } 187 } 188 189 194 public void write(int b) throws IOException { 195 append(String.valueOf((char) b)); 196 if (oldSysOut != null) { 197 oldSysOut.write(b); 198 } 199 } 200 201 206 public void write(byte[] buf, int off, int len) throws IOException { 207 append(new String (buf, off, len)); 208 if (oldSysOut != null) { 209 oldSysOut.write(buf, off, len); 210 } 211 } 212 213 218 public void flush() throws IOException { 219 super.flush(); 220 if (oldSysOut != null) { 221 oldSysOut.flush(); 222 } 223 } 224 225 230 public void dispose() { 231 if (shell != null) { 232 shell.getDisplay().syncExec(new Runnable () { 233 public void run() { 234 shell.dispose(); 235 } 236 }); 237 } 238 } 239 } | Popular Tags |