KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > view > LogTextPanel


1 package org.oddjob.monitor.view;
2
3 /**
4  * Display messages. Based on an example from Log4j.
5  */

6
7 import java.awt.BorderLayout JavaDoc;
8 import java.awt.Color JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Observable JavaDoc;
14 import java.util.Observer JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import javax.swing.JCheckBox JavaDoc;
18 import javax.swing.JPanel JavaDoc;
19 import javax.swing.JScrollPane JavaDoc;
20 import javax.swing.JTextPane JavaDoc;
21 import javax.swing.text.BadLocationException JavaDoc;
22 import javax.swing.text.MutableAttributeSet JavaDoc;
23 import javax.swing.text.SimpleAttributeSet JavaDoc;
24 import javax.swing.text.StyleConstants JavaDoc;
25 import javax.swing.text.StyledDocument JavaDoc;
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 JavaDoc
32 implements Observer JavaDoc, LogEventProcessor {
33
34     private static final int MAX_DOC_LENGTH = 100000;
35     
36     private JTextPane JavaDoc textPane;
37     private JCheckBox JavaDoc cbxTail;
38     private StyledDocument JavaDoc doc;
39     private Hashtable JavaDoc fontAttributes;
40
41     private long sequence = 0;
42
43     public LogTextPanel(Observable JavaDoc model) {
44         model.addObserver(this);
45         
46         constructComponents();
47         createDefaultFontAttributes();
48     }
49
50     private void constructComponents() {
51         // setup the panel's additional components...
52
this.setLayout(new BorderLayout JavaDoc());
53
54         cbxTail = new JCheckBox JavaDoc();
55         cbxTail.setSelected(true);
56         cbxTail.setText("Tail log events");
57         cbxTail.addActionListener(new ActionListener JavaDoc() {
58             public void actionPerformed(ActionEvent JavaDoc e) {
59                 if (cbxTail.isSelected()) {
60                     textPane.setCaretPosition(doc.getLength());
61                 }
62             }
63         });
64
65         JPanel JavaDoc bottomPanel = new JPanel JavaDoc();
66         bottomPanel.add(cbxTail, null);
67
68         textPane = new JTextPane JavaDoc();
69         textPane.setEditable(false);
70         textPane.setText("");
71         doc = textPane.getStyledDocument();
72         JScrollPane JavaDoc scroll = new JScrollPane JavaDoc();
73         scroll.setViewportView(textPane);
74
75         this.add(bottomPanel, BorderLayout.SOUTH);
76         this.add(scroll, BorderLayout.CENTER);
77     }
78
79     public void setTextBackground(Color JavaDoc color) {
80         textPane.setBackground(color);
81     }
82
83     public void setTextBackground(String JavaDoc 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 JavaDoc();
92         for (int i = 0; i < prio.length; i++) {
93             MutableAttributeSet JavaDoc att = new SimpleAttributeSet JavaDoc();
94             fontAttributes.put(prio[i], att);
95             //StyleConstants.setFontSize(att,11);
96
}
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 JavaDoc parseColor(String JavaDoc v) {
106         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc(val[0], val[1], val[2], val[3]);
114     }
115
116     void setTextColor(LogLevel l, String JavaDoc v) {
117         StyleConstants.setForeground((MutableAttributeSet JavaDoc) fontAttributes
118                 .get(l), parseColor(v));
119     }
120
121     void setTextColor(LogLevel l, Color JavaDoc c) {
122         StyleConstants.setForeground((MutableAttributeSet JavaDoc) fontAttributes
123                 .get(l), c);
124     }
125
126     void setTextFontSize(int size) {
127         Enumeration JavaDoc e = fontAttributes.elements();
128         while (e.hasMoreElements()) {
129             StyleConstants.setFontSize((MutableAttributeSet JavaDoc) e.nextElement(),
130                     size);
131         }
132         return;
133     }
134
135     void setTextFontName(String JavaDoc name) {
136         Enumeration JavaDoc e = fontAttributes.elements();
137         while (e.hasMoreElements()) {
138             StyleConstants.setFontFamily((MutableAttributeSet JavaDoc) e.nextElement(),
139                     name);
140         }
141         return;
142     }
143
144     public void onClear() {
145         try {
146             doc.remove(0, doc.getLength());
147         } catch (BadLocationException JavaDoc 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 JavaDoc text, final LogLevel level) {
157
158                     try {
159                         doc.insertString(doc.getLength(), text,
160                                 (MutableAttributeSet JavaDoc) fontAttributes
161                                         .get(level));
162                         int overflow = doc.getLength() - MAX_DOC_LENGTH;
163                         if (overflow > 0) {
164                             doc.remove(0, overflow);
165                             // this will work when we move to 1.5 and can use
166
// DefaultCaret.NEVER_UPDATE but at the moment the caret
167
// does it's own thing on remove and we can't change it.
168
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 JavaDoc e) {
180                         e.printStackTrace();
181                     }
182     }
183     
184     /* (non-Javadoc)
185      * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
186      */

187     public void update(Observable JavaDoc o, Object JavaDoc arg) {
188         LogAction a = (LogAction) arg;
189         a.accept(this);
190     }
191 }
192
Popular Tags