KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > gui > LogTextPanel


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.gui;
17
18
19 /**
20  * Title:
21  * Description:
22  * Copyright: Copyright (c) 2001
23  * Company:
24  * @author
25  * @version 1.0
26  */

27
28 import java.awt.Color JavaDoc;
29 import java.awt.Image JavaDoc;
30 import java.awt.Toolkit JavaDoc;
31 import java.awt.BorderLayout JavaDoc;
32
33 import javax.swing.*;
34 import javax.swing.text.StyledDocument JavaDoc;
35 import javax.swing.text.SimpleAttributeSet JavaDoc;
36 import javax.swing.text.MutableAttributeSet JavaDoc;
37 import javax.swing.text.StyleConstants JavaDoc;
38
39 import java.util.Hashtable JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.ArrayList JavaDoc;
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 JavaDoc doc;
52
53   private Hashtable JavaDoc fontAttributes;
54
55   private int eventBufferMaxSize = 10000;
56   private ArrayList JavaDoc eventBuffer = new ArrayList JavaDoc(eventBufferMaxSize);
57   private int eventViewIndex = 0;
58
59   public LogTextPanel() {
60     constructComponents();
61     createDefaultFontAttributes();
62   }
63
64   private void constructComponents() {
65       // setup the panel's additional components...
66
this.setLayout(new BorderLayout JavaDoc());
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 JavaDoc color) {
89     textPane.setBackground(color);
90   }
91
92   public
93   void setTextBackground(String JavaDoc v) {
94     textPane.setBackground(parseColor(v));
95   }
96
97   private void createDefaultFontAttributes() {
98     Priority[] prio = Priority.getAllPossiblePriorities();
99
100     fontAttributes = new Hashtable JavaDoc();
101     for (int i=0; i<prio.length;i++) {
102       MutableAttributeSet JavaDoc att = new SimpleAttributeSet JavaDoc();
103       fontAttributes.put(prio[i], att);
104       //StyleConstants.setFontSize(att,11);
105
}
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 JavaDoc parseColor (String JavaDoc v) {
116     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc(val[0],val[1],val[2],val[3]);
124   }
125
126   void setTextColor(Priority p, String JavaDoc v) {
127     StyleConstants.setForeground(
128           (MutableAttributeSet JavaDoc)fontAttributes.get(p),parseColor(v));
129   }
130
131   void setTextColor(Priority p, Color JavaDoc c) {
132     StyleConstants.setForeground(
133           (MutableAttributeSet JavaDoc)fontAttributes.get(p),c);
134   }
135
136   void setTextFontSize(int size) {
137     Enumeration JavaDoc e = fontAttributes.elements();
138     while (e.hasMoreElements()) {
139       StyleConstants.setFontSize((MutableAttributeSet JavaDoc)e.nextElement(),size);
140     }
141     return;
142   }
143
144   void setTextFontName(String JavaDoc name) {
145     Enumeration JavaDoc e = fontAttributes.elements();
146     while (e.hasMoreElements()) {
147       StyleConstants.setFontFamily((MutableAttributeSet JavaDoc)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     // only redraw if new line is visible...
174
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 JavaDoc 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 JavaDoc)fontAttributes.get(evt.prio));
192       } catch(Exception JavaDoc e) { e.printStackTrace(); }
193     }
194   }
195
196
197 }
Popular Tags