1 19 20 package com.sshtools.ui.swing; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.FlowLayout ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.WindowAdapter ; 28 import java.awt.event.WindowEvent ; 29 import java.io.OutputStream ; 30 import java.io.PrintStream ; 31 import java.lang.reflect.Method ; 32 33 import javax.swing.JButton ; 34 import javax.swing.JFrame ; 35 import javax.swing.JPanel ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.JTextPane ; 38 import javax.swing.text.BadLocationException ; 39 import javax.swing.text.SimpleAttributeSet ; 40 import javax.swing.text.StyleConstants ; 41 42 import com.sshtools.ui.awt.UIUtil; 43 44 public class DebugConsole { 45 46 private JFrame frame; 47 private JTextPane textArea; 48 private Method deleteMethod; 49 private OutputStream oldSysOut; 50 private boolean userScrolled; 51 52 private static DebugConsole console; 53 54 private DebugConsole() { 55 textArea = new JTextPane (); 56 } 57 58 61 void doShow() { 62 if (frame == null) { 63 64 try { 65 deleteMethod = StringBuffer .class.getMethod("delete", new Class [] { int.class, int.class }); } catch (Throwable t) { 67 } 68 textArea.setEditable(false); 69 JPanel panel = new JPanel (new BorderLayout ()); 70 panel.setBackground(Color.gray); 71 panel.setForeground(Color.black); 72 panel.add(new JScrollPane (textArea), BorderLayout.CENTER); 73 JPanel buttonPanel = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 74 buttonPanel.setBackground(Color.gray); 75 buttonPanel.setForeground(Color.black); 76 JButton clear = new JButton ("Clear"); clear.addActionListener(new ActionListener () { 78 public void actionPerformed(ActionEvent evt) { 79 clear(); 80 } 81 }); 82 buttonPanel.add(clear); 83 JButton close = new JButton ("Close"); close.addActionListener(new ActionListener () { 85 public void actionPerformed(ActionEvent evt) { 86 frame.setVisible(false); 87 } 88 }); 89 buttonPanel.add(close); 90 panel.add(buttonPanel, BorderLayout.SOUTH); 91 frame = new JFrame ("Console"); frame.addWindowListener(new WindowAdapter () { 93 public void windowClosing(WindowEvent evt) { 94 frame.setVisible(false); 95 96 } 97 }); 98 frame.setIconImage(UIUtil.loadImage(getClass(), "/images/idle-16x16.gif")); 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 e) { 118 } 119 } 120 } 121 122 public void append(String text, Color color) { 123 synchronized (textArea) { 124 SimpleAttributeSet attr1 = new SimpleAttributeSet (); 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 ble) { 137 138 } 139 } 140 } 141 142 public static boolean isStarted() { 143 return console != null; 144 } 145 146 public static void start() { 147 OutputStream oldSysOut = System.out; 148 OutputStream oldSysErr = System.err; 149 console = new DebugConsole(); 150 System.setOut(new PrintStream (new ConsoleOutputStream(oldSysOut, Color.black, console), true)); 151 System.setErr(new PrintStream (new ConsoleOutputStream(oldSysErr, Color.red, console), true)); 152 } 153 154 public static void show() { 155 console.doShow(); 156 } 157 158 } 159 | Popular Tags |