1 package com.sslexplorer.core; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Dimension ; 6 import java.awt.Font ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 import javax.swing.JPanel ; 11 import javax.swing.JScrollPane ; 12 import javax.swing.JTextPane ; 13 import javax.swing.SwingUtilities ; 14 import javax.swing.text.AttributeSet ; 15 import javax.swing.text.SimpleAttributeSet ; 16 import javax.swing.text.StyleConstants ; 17 18 19 public class SimpleConsole extends JPanel { 20 private final static int MAX_BUFFERED_LINES = 1000; 21 private JTextPane textPane; 22 private int maxLength; 23 private ConsoleWriter consoleWriter; 24 25 public SimpleConsole() { 26 super(new BorderLayout ()); 27 consoleWriter = new ConsoleWriter(); 28 textPane = new JTextPane () { 30 public boolean getScrollableTracksViewportWidth() { 31 return false; 32 } 33 34 public void setSize(Dimension d) { 35 if (d.width < getParent().getSize().width) { 36 d.width = getParent().getSize().width; 37 } 38 super.setSize(d); 39 } 40 }; 41 textPane.setEditable(false); 42 textPane.setFont(new Font ("Monospaced", Font.BOLD, 10)); 43 JScrollPane textScroller = new JScrollPane (textPane) { 44 public Dimension getPreferredSize() { 45 return new Dimension (super.getPreferredSize().width, 140); 46 } 47 }; 48 add(textScroller, BorderLayout.CENTER); 50 maxLength = 131072; 51 checkMaxLength(0); 52 } 53 54 private void checkMaxLength(int toAdd) { 55 try { 56 int z = textPane.getDocument().getLength() + toAdd; 57 if (z > maxLength) { 58 textPane.getDocument().remove(0, z - maxLength); 59 } 60 } catch (Throwable ex) { 61 } 62 } 63 64 public void writeMessage(boolean ok, final String text) { 65 consoleWriter.addMessage(ok, text); 66 } 67 68 static class WriteLock { 69 } 70 71 class ConsoleWriter extends Thread { 72 private List consoleBuffer; 73 74 ConsoleWriter() { 75 super("ConsoleWriter"); 76 setDaemon(true); 77 consoleBuffer = new ArrayList (); 78 start(); 79 } 80 81 public void addMessage(boolean ok, String text) { 82 synchronized (consoleBuffer) { 83 if (consoleBuffer.size() > MAX_BUFFERED_LINES) { 84 try { 85 consoleBuffer.wait(); 86 } catch (InterruptedException e) { 87 } 88 } 89 consoleBuffer.add(new ConsoleWriterWrapper(ok, text)); 90 } 91 synchronized (this) { 92 notifyAll(); 93 } 94 } 95 96 public void run() { 97 StringBuffer writeBuffer = new StringBuffer (); 98 SimpleAttributeSet attr = null; 99 int l = 0; 100 while (true) { 101 synchronized (this) { 102 try { 103 wait(2000); 104 } catch (InterruptedException ie) { 105 } 106 } 107 while (consoleBuffer.size() > 0) { 108 ConsoleWriterWrapper w = (ConsoleWriterWrapper) consoleBuffer.get(0); 109 String txt = w.text; 110 if (!txt.endsWith("\n")) { 111 txt = txt + "\n"; 112 } 113 if (writeBuffer.length() != 0) { 114 writeText(writeBuffer.toString(), attr); 115 writeBuffer.setLength(0); 116 l = 0; 117 } 118 attr = new SimpleAttributeSet (); 119 if (w.ok) 120 StyleConstants.setForeground(attr, Color.green.darker()); 121 else 122 StyleConstants.setForeground(attr, Color.red.darker()); 123 writeBuffer.append(txt); 124 l++; 125 if (l == 5) { 126 writeText(writeBuffer.toString(), attr); 127 writeBuffer.setLength(0); 128 l = 0; 129 } 130 synchronized (consoleBuffer) { 131 consoleBuffer.remove(0); 132 if (consoleBuffer.size() < (MAX_BUFFERED_LINES / 2)) { 133 consoleBuffer.notifyAll(); 134 } 135 } 136 } 137 if (writeBuffer.length() > 0) { 138 writeText(writeBuffer.toString(), attr); 139 writeBuffer.setLength(0); 140 l = 0; 141 } 142 } 143 } 144 145 public void writeText(final String writeTxt, final AttributeSet attr) { 146 try { 147 SwingUtilities.invokeAndWait(new Runnable () { 148 public void run() { 149 try { 150 checkMaxLength(0); 151 textPane.getDocument().insertString(textPane.getDocument().getLength(), writeTxt, attr); 152 textPane.scrollRectToVisible(textPane.getVisibleRect()); 153 textPane.setCaretPosition(textPane.getDocument().getLength()); 154 } catch (Throwable ex) { 155 } 156 } 157 }); 158 } catch (Exception e) { 159 } 160 } 161 162 class ConsoleWriterWrapper { 163 boolean ok; 164 String text; 165 166 ConsoleWriterWrapper(boolean ok, String text) { 167 this.ok = ok; 168 this.text = text; 169 } 170 } 171 } 172 } | Popular Tags |