1 16 17 package org.apache.log4j.gui; 18 19 20 import java.awt.Color ; 21 import java.awt.Image ; 22 import java.awt.Toolkit ; 23 import java.io.*; 24 import java.net.URL ; 25 import java.util.Enumeration ; 26 import java.util.StringTokenizer ; 27 import java.util.Hashtable ; 28 29 import javax.swing.Icon ; 30 import javax.swing.ImageIcon ; 31 import javax.swing.JTextPane ; 32 import javax.swing.text.AttributeSet ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.MutableAttributeSet ; 35 import javax.swing.text.SimpleAttributeSet ; 36 import javax.swing.text.StyleConstants ; 37 import javax.swing.text.StyledDocument ; 38 import javax.swing.text.TabSet ; 39 import javax.swing.text.TabStop ; 40 41 import org.apache.log4j.*; 42 43 import org.apache.log4j.spi.LoggingEvent; 44 import org.apache.log4j.helpers.Loader; 45 import org.apache.log4j.helpers.QuietWriter; 46 import org.apache.log4j.helpers.TracerPrintWriter; 47 import org.apache.log4j.helpers.OptionConverter; 48 49 50 58 59 public class TextPaneAppender extends AppenderSkeleton { 60 61 JTextPane textpane; 62 StyledDocument doc; 63 TracerPrintWriter tp; 64 StringWriter sw; 65 QuietWriter qw; 66 Hashtable attributes; 67 Hashtable icons; 68 69 private String label; 70 71 private boolean fancy; 72 73 final String LABEL_OPTION = "Label"; 74 final String COLOR_OPTION_FATAL = "Color.Emerg"; 75 final String COLOR_OPTION_ERROR = "Color.Error"; 76 final String COLOR_OPTION_WARN = "Color.Warn"; 77 final String COLOR_OPTION_INFO = "Color.Info"; 78 final String COLOR_OPTION_DEBUG = "Color.Debug"; 79 final String COLOR_OPTION_BACKGROUND = "Color.Background"; 80 final String FANCY_OPTION = "Fancy"; 81 final String FONT_NAME_OPTION = "Font.Name"; 82 final String FONT_SIZE_OPTION = "Font.Size"; 83 84 public static Image loadIcon ( String path ) { 85 Image img = null; 86 try { 87 URL url = ClassLoader.getSystemResource(path); 88 img = (Image ) (Toolkit.getDefaultToolkit()).getImage(url); 89 } catch (Exception e) { 90 System.out.println("Exception occured: " + e.getMessage() + 91 " - " + e ); 92 } 93 return (img); 94 } 95 96 public TextPaneAppender(Layout layout, String name) { 97 this(); 98 this.layout = layout; 99 this.name = name; 100 setTextPane(new JTextPane ()); 101 createAttributes(); 102 createIcons(); 103 } 104 105 public TextPaneAppender() { 106 super(); 107 setTextPane(new JTextPane ()); 108 createAttributes(); 109 createIcons(); 110 this.label=""; 111 this.sw = new StringWriter(); 112 this.qw = new QuietWriter(sw, errorHandler); 113 this.tp = new TracerPrintWriter(qw); 114 this.fancy =true; 115 } 116 117 public 118 void close() { 119 120 } 121 122 private void createAttributes() { 123 Priority prio[] = Priority.getAllPossiblePriorities(); 124 125 attributes = new Hashtable (); 126 for (int i=0; i<prio.length;i++) { 127 MutableAttributeSet att = new SimpleAttributeSet (); 128 attributes.put(prio[i], att); 129 StyleConstants.setFontSize(att,14); 130 } 131 StyleConstants.setForeground((MutableAttributeSet )attributes.get(Priority.ERROR),Color.red); 132 StyleConstants.setForeground((MutableAttributeSet )attributes.get(Priority.WARN),Color.orange); 133 StyleConstants.setForeground((MutableAttributeSet )attributes.get(Priority.INFO),Color.gray); 134 StyleConstants.setForeground((MutableAttributeSet )attributes.get(Priority.DEBUG),Color.black); 135 } 136 137 private void createIcons() { 138 Priority prio[] = Priority.getAllPossiblePriorities(); 139 140 icons = new Hashtable (); 141 for (int i=0; i<prio.length;i++) { 142 if (prio[i].equals(Priority.FATAL)) 143 icons.put(prio[i],new ImageIcon (loadIcon("icons/RedFlag.gif"))); 144 if (prio[i].equals(Priority.ERROR)) 145 icons.put(prio[i],new ImageIcon (loadIcon("icons/RedFlag.gif"))); 146 if (prio[i].equals(Priority.WARN)) 147 icons.put(prio[i],new ImageIcon (loadIcon("icons/BlueFlag.gif"))); 148 if (prio[i].equals(Priority.INFO)) 149 icons.put(prio[i],new ImageIcon (loadIcon("icons/GreenFlag.gif"))); 150 if (prio[i].equals(Priority.DEBUG)) 151 icons.put(prio[i],new ImageIcon (loadIcon("icons/GreenFlag.gif"))); 152 } 153 } 154 155 public void append(LoggingEvent event) { 156 String text = this.layout.format(event); 157 String trace=""; 158 if (event.throwable!=null) { 161 event.throwable.printStackTrace(tp); 162 for (int i=0; i< sw.getBuffer().length(); i++) { 163 if (sw.getBuffer().charAt(i)=='\t') 164 sw.getBuffer().replace(i,i+1," "); 165 } 166 trace = sw.toString(); 167 sw.getBuffer().delete(0,sw.getBuffer().length()); 168 } 169 try { 170 if (fancy) { 171 textpane.setEditable(true); 172 textpane.insertIcon((ImageIcon )icons.get(event.priority)); 173 textpane.setEditable(false); 174 } 175 doc.insertString(doc.getLength(),text+trace, 176 (MutableAttributeSet )attributes.get(event.priority)); 177 } 178 catch (BadLocationException badex) { 179 System.err.println(badex); 180 } 181 textpane.setCaretPosition(doc.getLength()); 182 } 183 184 public 185 JTextPane getTextPane() { 186 return textpane; 187 } 188 189 private 190 static 191 Color parseColor (String v) { 192 StringTokenizer st = new StringTokenizer (v,","); 193 int val[] = {255,255,255,255}; 194 int i=0; 195 while (st.hasMoreTokens()) { 196 val[i]=Integer.parseInt(st.nextToken()); 197 i++; 198 } 199 return new Color (val[0],val[1],val[2],val[3]); 200 } 201 202 private 203 static 204 String colorToString(Color c) { 205 String res = ""+c.getRed()+","+c.getGreen()+","+c.getBlue(); 207 return c.getAlpha() >= 255 ? res : res + ","+c.getAlpha(); 208 } 209 210 public 211 void setLayout(Layout layout) { 212 this.layout=layout; 213 } 214 215 public 216 void setName(String name) { 217 this.name = name; 218 } 219 220 221 public 222 void setTextPane(JTextPane textpane) { 223 this.textpane=textpane; 224 textpane.setEditable(false); 225 textpane.setBackground(Color.lightGray); 226 this.doc=textpane.getStyledDocument(); 227 } 228 229 private 230 void setColor(Priority p, String v) { 231 StyleConstants.setForeground( 232 (MutableAttributeSet )attributes.get(p),parseColor(v)); 233 } 234 235 private 236 String getColor(Priority p) { 237 Color c = StyleConstants.getForeground( 238 (MutableAttributeSet )attributes.get(p)); 239 return c == null ? null : colorToString(c); 240 } 241 242 245 public 246 void setLabel(String label) { 247 this.label = label; 248 } 249 public 250 String getLabel() { 251 return label; 252 } 253 254 public 255 void setColorEmerg(String color) { 256 setColor(Priority.FATAL, color); 257 } 258 public 259 String getColorEmerg() { 260 return getColor(Priority.FATAL); 261 } 262 263 public 264 void setColorError(String color) { 265 setColor(Priority.ERROR, color); 266 } 267 public 268 String getColorError() { 269 return getColor(Priority.ERROR); 270 } 271 272 public 273 void setColorWarn(String color) { 274 setColor(Priority.WARN, color); 275 } 276 public 277 String getColorWarn() { 278 return getColor(Priority.WARN); 279 } 280 281 public 282 void setColorInfo(String color) { 283 setColor(Priority.INFO, color); 284 } 285 public 286 String getColorInfo() { 287 return getColor(Priority.INFO); 288 } 289 290 public 291 void setColorDebug(String color) { 292 setColor(Priority.DEBUG, color); 293 } 294 public 295 String getColorDebug() { 296 return getColor(Priority.DEBUG); 297 } 298 299 public 300 void setColorBackground(String color) { 301 textpane.setBackground(parseColor(color)); 302 } 303 public 304 String getColorBackground() { 305 return colorToString(textpane.getBackground()); 306 } 307 308 public 309 void setFancy(boolean fancy) { 310 this.fancy = fancy; 311 } 312 public 313 boolean getFancy() { 314 return fancy; 315 } 316 317 public 318 void setFontSize(int size) { 319 Enumeration e = attributes.elements(); 320 while (e.hasMoreElements()) { 321 StyleConstants.setFontSize((MutableAttributeSet )e.nextElement(),size); 322 } 323 return; 324 } 325 326 public 327 int getFontSize() { 328 AttributeSet attrSet = (AttributeSet ) attributes.get(Priority.INFO); 329 return StyleConstants.getFontSize(attrSet); 330 } 331 332 public 333 void setFontName(String name) { 334 Enumeration e = attributes.elements(); 335 while (e.hasMoreElements()) { 336 StyleConstants.setFontFamily((MutableAttributeSet )e.nextElement(),name); 337 } 338 return; 339 } 340 341 public 342 String getFontName() { 343 AttributeSet attrSet = (AttributeSet ) attributes.get(Priority.INFO); 344 return StyleConstants.getFontFamily(attrSet); 345 } 346 347 public 348 boolean requiresLayout() { 349 return true; 350 } 351 } 353 354 355 | Popular Tags |