KickJava   Java API By Example, From Geeks To Geeks.

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


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
17
18 package org.apache.log4j.gui;
19
20 import org.apache.log4j.helpers.CyclicBuffer;
21 import org.apache.log4j.helpers.LogLog;
22 import org.apache.log4j.Priority;
23 import org.apache.log4j.Category;
24 import org.apache.log4j.Layout;
25 import org.apache.log4j.PatternLayout;
26 import org.apache.log4j.spi.LoggingEvent;
27
28 import javax.swing.JList JavaDoc;
29 import javax.swing.AbstractListModel JavaDoc;
30 import javax.swing.JFrame JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JTable JavaDoc;
34 import javax.swing.JTextArea JavaDoc;
35 import javax.swing.table.AbstractTableModel JavaDoc;
36 import javax.swing.table.TableCellRenderer JavaDoc;
37 import javax.swing.JScrollPane JavaDoc;
38 import javax.swing.ListCellRenderer JavaDoc;
39 import java.awt.Component JavaDoc;
40 import java.awt.FlowLayout JavaDoc;
41 import javax.swing.BoxLayout JavaDoc;
42
43 import java.awt.BorderLayout JavaDoc;
44 import java.awt.Dimension JavaDoc;
45 import java.awt.event.ActionListener JavaDoc;
46 import java.awt.event.ActionEvent JavaDoc;
47 import java.awt.Container JavaDoc;
48 import javax.swing.ImageIcon JavaDoc;
49 import java.awt.Image JavaDoc;
50 import java.awt.Toolkit JavaDoc;
51 import java.net.URL JavaDoc;
52 import java.awt.Rectangle JavaDoc;
53
54 public class JTableAppender extends JTable JavaDoc {
55
56
57   static Category cat = Category.getInstance(JTableAppender.class.getName());
58
59   PatternLayout layout;
60
61   public
62   JTableAppender() {
63     layout = new PatternLayout("%r %p %c [%t] - %m");
64     this.setDefaultRenderer(Object JavaDoc.class, new Renderer JavaDoc());
65
66   }
67
68   public
69   void add(LoggingEvent event) {
70     ((JTableAppenderModel)getModel()).add(event);
71   }
72
73   public
74   Dimension JavaDoc getPreferredSize() {
75     System.out.println("getPreferredSize() called");
76     return super.getPreferredSize();
77   }
78
79   static public void main(String JavaDoc[] args) {
80
81     JFrame JavaDoc frame = new JFrame JavaDoc("JListView test");
82     Container JavaDoc container = frame.getContentPane();
83
84     JTableAppender appender = new JTableAppender();
85     
86     JTableAppenderModel model = new
87                               JTableAppenderModel(Integer.parseInt(args[0]));
88     appender.setModel(model);
89     //appender.createDefaultColumnsFromModel();
90

91
92     JScrollPane JavaDoc sp = new JScrollPane JavaDoc(appender);
93     sp.setPreferredSize(new Dimension JavaDoc(250, 80));
94     
95     container.setLayout(new BoxLayout JavaDoc(container, BoxLayout.X_AXIS));
96     //container.add(view);
97
container.add(sp);
98
99     JButton JavaDoc button = new JButton JavaDoc("ADD");
100     container.add(button);
101     
102
103     button.addActionListener(new JTableAddAction(appender));
104
105     frame.setVisible(true);
106     frame.setSize(new Dimension JavaDoc(700,700));
107
108     long before = System.currentTimeMillis();
109
110     int RUN = 10000;
111     int i = 0;
112     while(i++ < RUN) {
113       LoggingEvent event = new LoggingEvent("x", cat, Priority.ERROR,
114                         "Message "+i, null);
115       event.getThreadName();
116       if(i % 10 == 0) {
117     //event.throwable = new Exception("hello "+i);
118
}
119       appender.add(event);
120     }
121
122     long after = System.currentTimeMillis();
123     System.out.println("Time taken :"+ ((after-before)*1000/RUN));
124
125   }
126
127   class Renderer extends JTextArea JavaDoc implements TableCellRenderer JavaDoc {
128
129     Object JavaDoc o = new Object JavaDoc();
130     int i = 0;
131
132     public
133     Renderer() {
134       System.out.println("Render() called ----------------------");
135     }
136
137     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table,
138                            Object JavaDoc value,
139                            boolean isSelected,
140                            boolean hasFocus,
141                            int row,
142                            int column) {
143
144       System.out.println(o + " ============== " + i++);
145       //LogLog.error("=======", new Exception());
146
//setIcon(longIcon);
147
if(value instanceof LoggingEvent) {
148     LoggingEvent event = (LoggingEvent) value;
149     String JavaDoc str = layout.format(event);
150     String JavaDoc t = event.getThrowableInformation();
151     
152     if(t != null) {
153       System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
154       setText(str + Layout.LINE_SEP + t);
155     } else {
156       setText(str);
157     }
158     
159       } else {
160     setText(value.toString());
161       }
162
163
164       return this;
165     }
166   }
167 }
168
169
170
171 class JTableAppenderModel extends AbstractTableModel JavaDoc {
172
173   CyclicBuffer cb;
174   
175   JTableAppenderModel(int size) {
176     cb = new CyclicBuffer(size);
177   }
178
179   public
180   void add(LoggingEvent event) {
181     //System.out.println("JListViewModel.add called");
182
cb.add(event);
183     int j = cb.length();
184
185     fireTableDataChanged();
186
187   }
188   public
189   int getColumnCount() {
190     return 1;
191   }
192
193   public int getRowCount() {
194     return cb.length();
195   }
196
197   //public
198
//Class getColumnClass(int index) {
199
// System.out.println("getColumnClass called " + index);
200
// return LoggingEvent.class;
201
//}
202

203   public
204   Object JavaDoc getValueAt(int row, int col) {
205     return cb.get(row);
206   }
207 }
208
209
210 class JTableAddAction implements ActionListener JavaDoc {
211     
212   int j;
213   JTableAppender appender;
214
215   Category cat = Category.getInstance("x");
216   
217   public
218   JTableAddAction(JTableAppender appender) {
219     this.appender = appender;
220     j = 0;
221   }
222     
223   public
224   void actionPerformed(ActionEvent JavaDoc e) {
225     System.out.println("Action occured");
226
227     LoggingEvent event = new LoggingEvent("x", cat, Priority.DEBUG,
228                         "Message "+j, null);
229     
230     if(j % 5 == 0) {
231       //event.throwable = new Exception("hello "+j);
232
}
233     j++;
234     appender.add(event);
235   }
236 }
237
Popular Tags