1 16 package org.apache.log4j.gui; 17 18 19 27 28 import java.awt.Color ; 29 import java.awt.Image ; 30 import java.awt.Toolkit ; 31 import java.awt.BorderLayout ; 32 33 import javax.swing.*; 34 import javax.swing.text.StyledDocument ; 35 import javax.swing.text.SimpleAttributeSet ; 36 import javax.swing.text.MutableAttributeSet ; 37 import javax.swing.text.StyleConstants ; 38 39 import java.util.Hashtable ; 40 import java.util.StringTokenizer ; 41 import java.util.Enumeration ; 42 import java.util.ArrayList ; 43 44 import org.apache.log4j.*; 45 46 public class LogTextPanel extends JPanel { 47 48 private JScrollBar scrollBar; 49 private JTextPane textPane; 50 private JCheckBox cbxTail; 51 private StyledDocument doc; 52 53 private Hashtable fontAttributes; 54 55 private int eventBufferMaxSize = 10000; 56 private ArrayList eventBuffer = new ArrayList (eventBufferMaxSize); 57 private int eventViewIndex = 0; 58 59 public LogTextPanel() { 60 constructComponents(); 61 createDefaultFontAttributes(); 62 } 63 64 private void constructComponents() { 65 this.setLayout(new BorderLayout ()); 67 68 cbxTail = new JCheckBox(); 69 cbxTail.setSelected(true); 70 cbxTail.setText("Tail log events"); 71 72 JPanel bottomPanel = new JPanel(); 73 bottomPanel.add(cbxTail, null); 74 75 textPane = new JTextPane(); 76 textPane.setEditable(false); 77 textPane.setText(""); 78 doc = textPane.getStyledDocument(); 79 80 scrollBar = new JScrollBar(JScrollBar.VERTICAL); 81 82 this.add(bottomPanel, BorderLayout.SOUTH); 83 this.add(scrollBar, BorderLayout.EAST); 84 this.add(textPane, BorderLayout.CENTER); 85 } 86 87 public 88 void setTextBackground(Color color) { 89 textPane.setBackground(color); 90 } 91 92 public 93 void setTextBackground(String v) { 94 textPane.setBackground(parseColor(v)); 95 } 96 97 private void createDefaultFontAttributes() { 98 Priority[] prio = Priority.getAllPossiblePriorities(); 99 100 fontAttributes = new Hashtable (); 101 for (int i=0; i<prio.length;i++) { 102 MutableAttributeSet att = new SimpleAttributeSet (); 103 fontAttributes.put(prio[i], att); 104 } 106 107 setTextColor(Priority.FATAL, Color.red); 108 setTextColor(Priority.ERROR, Color.magenta); 109 setTextColor(Priority.WARN, Color.orange); 110 setTextColor(Priority.INFO, Color.blue); 111 setTextColor(Priority.DEBUG, Color.black); 112 } 113 114 private 115 Color parseColor (String v) { 116 StringTokenizer st = new StringTokenizer (v,","); 117 int val[] = {255,255,255,255}; 118 int i=0; 119 while (st.hasMoreTokens()) { 120 val[i]=Integer.parseInt(st.nextToken()); 121 i++; 122 } 123 return new Color (val[0],val[1],val[2],val[3]); 124 } 125 126 void setTextColor(Priority p, String v) { 127 StyleConstants.setForeground( 128 (MutableAttributeSet )fontAttributes.get(p),parseColor(v)); 129 } 130 131 void setTextColor(Priority p, Color c) { 132 StyleConstants.setForeground( 133 (MutableAttributeSet )fontAttributes.get(p),c); 134 } 135 136 void setTextFontSize(int size) { 137 Enumeration e = fontAttributes.elements(); 138 while (e.hasMoreElements()) { 139 StyleConstants.setFontSize((MutableAttributeSet )e.nextElement(),size); 140 } 141 return; 142 } 143 144 void setTextFontName(String name) { 145 Enumeration e = fontAttributes.elements(); 146 while (e.hasMoreElements()) { 147 StyleConstants.setFontFamily((MutableAttributeSet )e.nextElement(),name); 148 } 149 return; 150 } 151 152 void setEventBufferSize(int bufferSize) { 153 eventBufferMaxSize = bufferSize; 154 } 155 156 void newEvents(EventBufferElement[] evts) { 157 158 if((eventBuffer.size() + evts.length) >= eventBufferMaxSize) { 159 for(int i=0; i < evts.length; i++) { 160 eventBuffer.remove(0); 161 } 162 eventViewIndex -= evts.length; 163 if(eventViewIndex < 0) 164 eventViewIndex = 0; 165 } 166 for(int i=0; i < evts.length; i++) 167 eventBuffer.add(evts[i]); 168 169 if((eventBuffer.size() > maxR) && cbxTail.isSelected()) { 170 eventViewIndex = (eventBuffer.size() - maxR); 171 } 172 173 if((maxR < 0) || (eventBuffer.size() >= eventViewIndex && eventBuffer.size() <= (eventViewIndex + maxR))) 175 drawText(); 176 } 177 178 int maxR = -1; 179 180 void drawText() { 181 if(maxR < 0) 182 maxR = textPane.getHeight() / textPane.getFontMetrics(textPane.getFont()).getHeight(); 183 try { 184 doc.remove(0, doc.getLength()); 185 } catch(Exception e) { e.printStackTrace(); } 186 187 for(int i=eventViewIndex; (i < eventBuffer.size()) && (i < (eventViewIndex + maxR)); i++) { 188 EventBufferElement evt = (EventBufferElement)eventBuffer.get(i); 189 190 try { 191 doc.insertString(doc.getLength(), evt.text, (MutableAttributeSet )fontAttributes.get(evt.prio)); 192 } catch(Exception e) { e.printStackTrace(); } 193 } 194 } 195 196 197 } | Popular Tags |