1 19 20 package com.sslexplorer.agent.client.gui.awt; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Button ; 24 import java.awt.Color ; 25 import java.awt.FlowLayout ; 26 import java.awt.Frame ; 27 import java.awt.Panel ; 28 import java.awt.TextArea ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.WindowAdapter ; 32 import java.awt.event.WindowEvent ; 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.lang.reflect.Method ; 36 37 import com.sshtools.ui.awt.UIUtil; 38 import com.sslexplorer.agent.client.Console; 39 40 50 public class AWTConsoleOutputStream extends Console { 51 52 54 private StringBuffer buf = new StringBuffer (); 55 private Frame frame; 56 private TextArea textArea; 57 private Method deleteMethod; 58 private OutputStream oldSysOut; 59 private boolean userScrolled; 60 61 66 public AWTConsoleOutputStream(OutputStream oldSysOut) { 67 this.oldSysOut = oldSysOut; 68 } 69 70 73 public void show() { 74 if (textArea == null) { 75 76 try { 77 deleteMethod = StringBuffer .class.getMethod("delete", new Class [] { int.class, int.class }); } catch (Throwable t) { 79 } 80 textArea = new TextArea (); 81 textArea.setEditable(false); 82 textArea.setBackground(Color.white); 83 textArea.setForeground(Color.black); 84 Panel panel = new Panel (new BorderLayout ()); 85 panel.setBackground(Color.gray); 86 panel.setForeground(Color.black); 87 panel.add(textArea, BorderLayout.CENTER); 88 Panel buttonPanel = new Panel (new FlowLayout (FlowLayout.RIGHT)); 89 buttonPanel.setBackground(Color.gray); 90 buttonPanel.setForeground(Color.black); 91 Button clear = new Button (Messages.getString("ConsoleOutputStream.actions.clear")); clear.addActionListener(new ActionListener () { 93 public void actionPerformed(ActionEvent evt) { 94 clear(); 95 } 96 }); 97 buttonPanel.add(clear); 98 Button close = new Button (Messages.getString("ConsoleOutputStream.actions.close")); close.addActionListener(new ActionListener () { 100 public void actionPerformed(ActionEvent evt) { 101 frame.setVisible(false); 102 } 103 }); 104 buttonPanel.add(close); 105 panel.add(buttonPanel, BorderLayout.SOUTH); 106 frame = new Frame (Messages.getString("ConsoleOutputStream.title")); frame.addWindowListener(new WindowAdapter () { 108 public void windowClosing(WindowEvent evt) { 109 frame.setVisible(false); 110 111 } 112 }); 113 frame.setIconImage(UIUtil.loadImage(getClass(), "/images/frame-agent.gif")); 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 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 [] { new Integer (0), new Integer (buf.length() - 65535) }); 143 } catch (Throwable t) { 144 String newBuf = buf.toString().substring(buf.length() - 65535); 145 buf.setLength(0); 146 buf.append(newBuf); 147 } 148 } else { 149 String 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 168 public void write(int b) throws IOException { 169 append(String.valueOf((char) b)); 170 if (oldSysOut != null) { 171 oldSysOut.write(b); 172 } 173 } 174 175 180 public void write(byte[] buf, int off, int len) throws IOException { 181 append(new String (buf, off, len)); 182 if (oldSysOut != null) { 183 oldSysOut.write(buf, off, len); 184 } 185 } 186 187 192 public void flush() throws IOException { 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 |