KickJava   Java API By Example, From Geeks To Geeks.

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


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.JPanel JavaDoc;
34 import javax.swing.JTextArea JavaDoc;
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.ListCellRenderer JavaDoc;
37 import java.awt.Component JavaDoc;
38 import java.awt.FlowLayout JavaDoc;
39 import java.awt.GridLayout JavaDoc;
40 import javax.swing.BoxLayout JavaDoc;
41
42 import java.awt.BorderLayout JavaDoc;
43 import java.awt.Dimension JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45 import java.awt.event.ActionEvent JavaDoc;
46 import java.awt.Container JavaDoc;
47 import javax.swing.ImageIcon JavaDoc;
48 import java.awt.Image JavaDoc;
49 import java.awt.Toolkit JavaDoc;
50 import java.net.URL JavaDoc;
51 import java.awt.Rectangle JavaDoc;
52
53 public class JListView extends JList JavaDoc {
54
55
56   static Category cat = Category.getInstance(JListView.class.getName());
57
58
59   //JListViewModel model;
60
PatternLayout layout;
61
62   static LoggingEvent proto = new LoggingEvent("x", cat, Priority.ERROR,
63                            "Message ", new Throwable JavaDoc());
64
65   public
66   JListView(JListViewModel model) {
67     super(model);
68     layout = new PatternLayout("%r %p %c [%t] - %m");
69     //this.setModel(model);
70
this.setCellRenderer(new MyCellRenderer());
71     // setFixedCellWidth(10);
72
//setFixedCellHeight(20);
73

74   }
75
76   public
77   void add(LoggingEvent event) {
78     ((JListViewModel)getModel()).add(event);
79   }
80
81   /*
82   public
83   Dimension getPreferredSize() {
84     System.out.println("getPreferredSize() called");
85     return super.getPreferredSize();
86   }
87
88
89   public
90   int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
91                  int direction) {
92     System.out.println("getScrollableUnitIncrement called with " + visibleRect +
93                "orientation: "+orientation+", direction: "+direction);
94     return super.getScrollableUnitIncrement(visibleRect, orientation,
95                         direction);
96   }
97
98   public
99   int getScrollableBlockIncrement(Rectangle visibleRect, int orientation,
100                   int direction) {
101     System.out.println("getScrollableBlockIncrement called with " +
102                visibleRect + "orientation: "+orientation+
103                ", direction: "+direction);
104     return super.getScrollableBlockIncrement(visibleRect, orientation,
105                          direction);
106   }
107   */

108
109   //public
110
//boolean getScrollableTracksViewportWidth() {
111
//System.out.println("getScrollableTracksViewportWidth called.");
112
//return true;
113
//boolean b = super.getScrollableTracksViewportWidth();
114
//System.out.println("result is: "+b);
115
//return b;
116
//}
117

118   //public
119
//boolean getScrollableTracksViewportHeight() {
120
// System.out.println("getScrollableTracksViewportHeight called.");
121
// return true;
122
//boolean b = super.getScrollableTracksViewportHeight();
123
//System.out.println("result is: "+b);
124
//return b;
125
//}
126

127   //public
128
//int getFirstVisibleIndex() {
129
//int r = getFirstVisibleIndex();
130
// System.out.println("----------getFirstVisibleIndex called, result: "+r);
131
//return r;
132
//}
133

134   //public
135
//Object getPrototypeCellValue() {
136
//return proto;
137
//}
138

139   
140   
141   static public void main(String JavaDoc[] args) {
142
143     JFrame JavaDoc frame = new JFrame JavaDoc("JListView test");
144     Container JavaDoc container = frame.getContentPane();
145
146     JListView view = new JListView(new JListViewModel(Integer.parseInt(args[0])));
147
148
149     JScrollPane JavaDoc sp = new JScrollPane JavaDoc(view);
150     sp.setPreferredSize(new Dimension JavaDoc(250, 80));
151     
152     container.setLayout(new BoxLayout JavaDoc(container, BoxLayout.X_AXIS));
153     //container.add(view);
154
container.add(sp);
155
156     JButton JavaDoc b1 = new JButton JavaDoc("Add 1");
157     JButton JavaDoc b10 = new JButton JavaDoc("Add 10");
158     JButton JavaDoc b100 = new JButton JavaDoc("Add 100");
159     JButton JavaDoc b1000 = new JButton JavaDoc("Add 1000");
160     JButton JavaDoc b10000 = new JButton JavaDoc("Add 10000");
161
162     JPanel JavaDoc panel = new JPanel JavaDoc(new GridLayout JavaDoc(0,1));
163     container.add(panel);
164
165     panel.add(b1);
166     panel.add(b10);
167     panel.add(b100);
168     panel.add(b1000);
169     panel.add(b10000);
170     
171
172     AddAction a1 = new AddAction(view, 1);
173     AddAction a10 = new AddAction(view, 10);
174     AddAction a100 = new AddAction(view, 100);
175     AddAction a1000 = new AddAction(view, 1000);
176     AddAction a10000 = new AddAction(view, 10000);
177
178     b1.addActionListener(a1);
179     b10.addActionListener(a10);
180     b100.addActionListener(a100);
181     b1000.addActionListener(a1000);
182     b10000.addActionListener(a10000);
183
184     frame.setVisible(true);
185     frame.setSize(new Dimension JavaDoc(700,700));
186
187     long before = System.currentTimeMillis();
188
189     int RUN = 1000;
190     int i = 0;
191     while(i++ < RUN) {
192       LoggingEvent event0 = new LoggingEvent("x", cat, Priority.ERROR,
193                          "Message "+i, null);
194       
195       Throwable JavaDoc t = new Exception JavaDoc("hello "+i);
196       LoggingEvent event1 = new LoggingEvent("x", cat, Priority.ERROR,
197                          "Message "+i, t);
198       
199
200       if(i % 10 == 0) {
201     event1.getThreadName();
202     view.add(event1);
203       } else {
204     event0.getThreadName();
205     view.add(event0);
206       }
207     }
208
209     long after = System.currentTimeMillis();
210     System.out.println("Time taken :"+ ((after-before)*1000/RUN));
211
212   }
213
214   class MyCellRenderer extends JTextArea JavaDoc implements ListCellRenderer JavaDoc {
215
216     Object JavaDoc o = new Object JavaDoc();
217     int i = 0;
218     final ImageIcon JavaDoc longIcon = new ImageIcon JavaDoc("RedFlag.gif");
219
220     public
221     MyCellRenderer() {
222       System.out.println("----------------------");
223       
224     }
225
226
227
228     public
229     int getTabSize() {
230       return 2;
231     }
232
233     public Image JavaDoc loadIcon ( String JavaDoc path ) {
234     Image JavaDoc img = null;
235     try {
236       URL JavaDoc url = ClassLoader.getSystemResource(path);
237       img = (Image JavaDoc) (Toolkit.getDefaultToolkit()).getImage(url);
238     } catch (Exception JavaDoc e) {
239       System.out.println("Exception occured: " + e.getMessage() +
240              " - " + e );
241     }
242     return (img);
243   }
244
245     public Component JavaDoc getListCellRendererComponent(JList JavaDoc list,
246                         Object JavaDoc value,
247                         int index, // cell index
248
boolean isSelected,
249                         boolean cellHasFocus) {
250
251       // System.out.println(o + " ============== " + i++);
252
//LogLog.error("=======", new Exception());
253
//setIcon(longIcon);
254
if(value instanceof LoggingEvent) {
255     LoggingEvent event = (LoggingEvent) value;
256     String JavaDoc str = layout.format(event);
257     String JavaDoc t = event.getThrowableInformation();
258
259     if(t != null) {
260       setText(str + Layout.LINE_SEP + t);
261     } else {
262       setText(str);
263     }
264     
265       } else {
266     setText(value.toString());
267       }
268
269
270       return this;
271     }
272   }
273 }
274
275
276
277 class JListViewModel extends AbstractListModel JavaDoc {
278
279   CyclicBuffer cb;
280   
281   JListViewModel(int size) {
282     cb = new CyclicBuffer(size);
283   }
284
285   public
286   void add(LoggingEvent event) {
287     //System.out.println("JListViewModel.add called");
288
cb.add(event);
289     int j = cb.length();
290     fireContentsChanged(this, 0, j);
291   }
292     
293
294
295   public
296   Object JavaDoc getElementAt(int index) {
297     return cb.get(index);
298   }
299
300   public
301   int getSize() {
302     return cb.length();
303   }
304   
305 }
306
307 class AddAction implements ActionListener JavaDoc {
308
309   Thread JavaDoc t;
310
311   static int counter = 0;
312
313   public
314   AddAction(JListView view, int burst) {
315     this.t = new AddThread(view, burst);
316     t.start();
317   }
318     
319   public
320   void actionPerformed(ActionEvent JavaDoc e) {
321     System.out.println("Action occured");
322     synchronized(t) {
323       t.notify();
324     }
325   }
326
327   class AddThread extends Thread JavaDoc {
328     int burst;
329     JListView view;
330
331     Category cat = Category.getInstance("x");
332     
333     AddThread(JListView view, int burst) {
334       super();
335       this.burst = burst;
336       this.view = view;
337       setName("AddThread"+burst);
338     }
339
340     public
341     void run() {
342
343       while(true) {
344     synchronized(this) {
345       try {
346         this.wait();
347       } catch(Exception JavaDoc e) {
348       }
349     }
350     for(int i = 0; i < burst; i++) {
351       LoggingEvent event = new LoggingEvent("x", cat, Priority.DEBUG,
352                         "Message "+counter, null);
353
354       event.getThreadName();
355       if(counter % 50 == 0) {
356         //event.throwable = new Exception("hello "+counter);
357
}
358       counter++;
359       view.add(event);
360     }
361       }
362     }
363   }
364 }
365
Popular Tags