| 1 17 18 package com.finalist.jaggenerator; 19 20 import java.awt.Color ; 21 import java.util.HashMap ; 22 23 import javax.swing.JTextArea ; 24 import javax.swing.JTextPane ; 25 import javax.swing.text.AttributeSet ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.JTextComponent ; 28 import javax.swing.text.SimpleAttributeSet ; 29 import javax.swing.text.StyleConstants ; 30 31 import org.apache.log4j.lf5.LogLevel; 32 33 39 public class ConsoleLogger { 40 41 public static final HashMap styleMap = new HashMap (); 42 43 static SimpleAttributeSet BLACK = new SimpleAttributeSet (); 44 static SimpleAttributeSet YELLOW = new SimpleAttributeSet (); 45 static SimpleAttributeSet RED = new SimpleAttributeSet (); 46 47 static { 48 styleMap.put(LogLevel.INFO, BLACK); 49 styleMap.put(LogLevel.DEBUG, YELLOW); 50 styleMap.put(LogLevel.ERROR, RED); 51 52 StyleConstants.setForeground(BLACK, Color.BLACK); 53 StyleConstants.setFontFamily(BLACK, "Lucida"); 54 StyleConstants.setFontSize(BLACK, 10); 55 56 StyleConstants.setForeground(YELLOW, Color.YELLOW); 57 StyleConstants.setFontFamily(YELLOW, "Lucida"); 58 StyleConstants.setFontSize(YELLOW, 10); 59 60 StyleConstants.setForeground(RED, Color.RED); 61 StyleConstants.setFontFamily(RED, "Lucida"); 62 StyleConstants.setFontSize(RED, 10); 63 } 64 65 private boolean colored; 66 67 private JTextComponent genericConsole; 68 73 public ConsoleLogger(JTextArea console) { 74 setGenericConsole(console); 75 setColored(false); 76 } 77 82 public ConsoleLogger(JTextPane console) { 83 setGenericConsole(console); 84 setColored(true); 85 } 86 87 93 public void log(String message) { 94 log(message, LogLevel.INFO); 95 } 96 97 105 public void log(String message, LogLevel level) { 106 if(isColored()) 107 { 108 insertText(message, getSyle4Level(level)); 109 } else 110 insertText(message, BLACK); 111 insertText("\n", BLACK); 112 setEndSelection(); 113 } 114 115 private SimpleAttributeSet getSyle4Level(LogLevel level) { 116 SimpleAttributeSet c = (SimpleAttributeSet ) ConsoleLogger.styleMap.get(level); 117 SimpleAttributeSet ret = BLACK; 118 if (c != null) 119 ret = c; 120 121 return ret; 122 } 123 public boolean isColored() { 124 return colored; 125 } 126 public void setColored(boolean colored) { 127 this.colored = colored; 128 } 129 public JTextComponent getGenericConsole() { 130 return genericConsole; 131 } 132 public void setGenericConsole(JTextComponent genericConsole) { 133 this.genericConsole = genericConsole; 134 } 135 136 protected void insertText(String text, AttributeSet set) { 137 try { 138 genericConsole.getDocument().insertString( 139 genericConsole.getDocument().getLength(), text, set); 140 } catch (BadLocationException e) { 141 e.printStackTrace(); 142 } 143 } 144 145 protected void setEndSelection() { 147 genericConsole.setSelectionStart(genericConsole.getDocument().getLength()); 148 genericConsole.setSelectionEnd(genericConsole.getDocument().getLength()); 149 } 150 151 157 public void error(String message) { 158 log(message, LogLevel.ERROR); 159 } 160 161 167 public void debug(String message) { 168 log(message, LogLevel.DEBUG); 169 } 170 } 171 | Popular Tags |