1 package org.oddjob.monitor.view; 2 3 6 7 import java.awt.BorderLayout ; 8 import java.awt.Color ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 import java.util.Observable ; 14 import java.util.Observer ; 15 import java.util.StringTokenizer ; 16 17 import javax.swing.JCheckBox ; 18 import javax.swing.JPanel ; 19 import javax.swing.JScrollPane ; 20 import javax.swing.JTextPane ; 21 import javax.swing.text.BadLocationException ; 22 import javax.swing.text.MutableAttributeSet ; 23 import javax.swing.text.SimpleAttributeSet ; 24 import javax.swing.text.StyleConstants ; 25 import javax.swing.text.StyledDocument ; 26 27 import org.oddjob.logging.LogLevel; 28 import org.oddjob.monitor.model.LogAction; 29 import org.oddjob.monitor.model.LogEventProcessor; 30 31 public class LogTextPanel extends JPanel 32 implements Observer , LogEventProcessor { 33 34 private static final int MAX_DOC_LENGTH = 100000; 35 36 private JTextPane textPane; 37 private JCheckBox cbxTail; 38 private StyledDocument doc; 39 private Hashtable fontAttributes; 40 41 private long sequence = 0; 42 43 public LogTextPanel(Observable model) { 44 model.addObserver(this); 45 46 constructComponents(); 47 createDefaultFontAttributes(); 48 } 49 50 private void constructComponents() { 51 this.setLayout(new BorderLayout ()); 53 54 cbxTail = new JCheckBox (); 55 cbxTail.setSelected(true); 56 cbxTail.setText("Tail log events"); 57 cbxTail.addActionListener(new ActionListener () { 58 public void actionPerformed(ActionEvent e) { 59 if (cbxTail.isSelected()) { 60 textPane.setCaretPosition(doc.getLength()); 61 } 62 } 63 }); 64 65 JPanel bottomPanel = new JPanel (); 66 bottomPanel.add(cbxTail, null); 67 68 textPane = new JTextPane (); 69 textPane.setEditable(false); 70 textPane.setText(""); 71 doc = textPane.getStyledDocument(); 72 JScrollPane scroll = new JScrollPane (); 73 scroll.setViewportView(textPane); 74 75 this.add(bottomPanel, BorderLayout.SOUTH); 76 this.add(scroll, BorderLayout.CENTER); 77 } 78 79 public void setTextBackground(Color color) { 80 textPane.setBackground(color); 81 } 82 83 public void setTextBackground(String v) { 84 textPane.setBackground(parseColor(v)); 85 } 86 87 private void createDefaultFontAttributes() { 88 LogLevel[] prio = new LogLevel[] { LogLevel.FATAL, LogLevel.ERROR, 89 LogLevel.WARN, LogLevel.INFO, LogLevel.DEBUG }; 90 91 fontAttributes = new Hashtable (); 92 for (int i = 0; i < prio.length; i++) { 93 MutableAttributeSet att = new SimpleAttributeSet (); 94 fontAttributes.put(prio[i], att); 95 } 97 98 setTextColor(LogLevel.FATAL, Color.red); 99 setTextColor(LogLevel.ERROR, Color.magenta); 100 setTextColor(LogLevel.WARN, Color.orange); 101 setTextColor(LogLevel.INFO, Color.blue); 102 setTextColor(LogLevel.DEBUG, Color.black); 103 } 104 105 private Color parseColor(String v) { 106 StringTokenizer st = new StringTokenizer (v, ","); 107 int val[] = { 255, 255, 255, 255 }; 108 int i = 0; 109 while (st.hasMoreTokens()) { 110 val[i] = Integer.parseInt(st.nextToken()); 111 i++; 112 } 113 return new Color (val[0], val[1], val[2], val[3]); 114 } 115 116 void setTextColor(LogLevel l, String v) { 117 StyleConstants.setForeground((MutableAttributeSet ) fontAttributes 118 .get(l), parseColor(v)); 119 } 120 121 void setTextColor(LogLevel l, Color c) { 122 StyleConstants.setForeground((MutableAttributeSet ) fontAttributes 123 .get(l), c); 124 } 125 126 void setTextFontSize(int size) { 127 Enumeration e = fontAttributes.elements(); 128 while (e.hasMoreElements()) { 129 StyleConstants.setFontSize((MutableAttributeSet ) e.nextElement(), 130 size); 131 } 132 return; 133 } 134 135 void setTextFontName(String name) { 136 Enumeration e = fontAttributes.elements(); 137 while (e.hasMoreElements()) { 138 StyleConstants.setFontFamily((MutableAttributeSet ) e.nextElement(), 139 name); 140 } 141 return; 142 } 143 144 public void onClear() { 145 try { 146 doc.remove(0, doc.getLength()); 147 } catch (BadLocationException e) { 148 e.printStackTrace(); 149 } 150 } 151 152 public void onUnavailable() { 153 textPane.setText(("No log available.")); 154 } 155 156 public void onEvent(final String text, final LogLevel level) { 157 158 try { 159 doc.insertString(doc.getLength(), text, 160 (MutableAttributeSet ) fontAttributes 161 .get(level)); 162 int overflow = doc.getLength() - MAX_DOC_LENGTH; 163 if (overflow > 0) { 164 doc.remove(0, overflow); 165 if (!cbxTail.isSelected()) { 169 int position = textPane.getCaretPosition(); 170 if (position - overflow < 0) { 171 position = 0; 172 } 173 textPane.setCaretPosition(position); 174 } 175 } 176 if (cbxTail.isSelected()) { 177 textPane.setCaretPosition(doc.getLength()); 178 } 179 } catch (BadLocationException e) { 180 e.printStackTrace(); 181 } 182 } 183 184 187 public void update(Observable o, Object arg) { 188 LogAction a = (LogAction) arg; 189 a.accept(this); 190 } 191 } 192 | Popular Tags |