1 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 ; 29 import javax.swing.AbstractListModel ; 30 import javax.swing.JFrame ; 31 import javax.swing.JButton ; 32 import javax.swing.JLabel ; 33 import javax.swing.JPanel ; 34 import javax.swing.JTextArea ; 35 import javax.swing.JScrollPane ; 36 import javax.swing.ListCellRenderer ; 37 import java.awt.Component ; 38 import java.awt.FlowLayout ; 39 import java.awt.GridLayout ; 40 import javax.swing.BoxLayout ; 41 42 import java.awt.BorderLayout ; 43 import java.awt.Dimension ; 44 import java.awt.event.ActionListener ; 45 import java.awt.event.ActionEvent ; 46 import java.awt.Container ; 47 import javax.swing.ImageIcon ; 48 import java.awt.Image ; 49 import java.awt.Toolkit ; 50 import java.net.URL ; 51 import java.awt.Rectangle ; 52 53 public class JListView extends JList { 54 55 56 static Category cat = Category.getInstance(JListView.class.getName()); 57 58 59 PatternLayout layout; 61 62 static LoggingEvent proto = new LoggingEvent("x", cat, Priority.ERROR, 63 "Message ", new Throwable ()); 64 65 public 66 JListView(JListViewModel model) { 67 super(model); 68 layout = new PatternLayout("%r %p %c [%t] - %m"); 69 this.setCellRenderer(new MyCellRenderer()); 71 74 } 75 76 public 77 void add(LoggingEvent event) { 78 ((JListViewModel)getModel()).add(event); 79 } 80 81 108 109 118 127 134 139 140 141 static public void main(String [] args) { 142 143 JFrame frame = new JFrame ("JListView test"); 144 Container container = frame.getContentPane(); 145 146 JListView view = new JListView(new JListViewModel(Integer.parseInt(args[0]))); 147 148 149 JScrollPane sp = new JScrollPane (view); 150 sp.setPreferredSize(new Dimension (250, 80)); 151 152 container.setLayout(new BoxLayout (container, BoxLayout.X_AXIS)); 153 container.add(sp); 155 156 JButton b1 = new JButton ("Add 1"); 157 JButton b10 = new JButton ("Add 10"); 158 JButton b100 = new JButton ("Add 100"); 159 JButton b1000 = new JButton ("Add 1000"); 160 JButton b10000 = new JButton ("Add 10000"); 161 162 JPanel panel = new JPanel (new GridLayout (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 (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 t = new Exception ("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 implements ListCellRenderer { 215 216 Object o = new Object (); 217 int i = 0; 218 final ImageIcon longIcon = new ImageIcon ("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 loadIcon ( String path ) { 234 Image img = null; 235 try { 236 URL url = ClassLoader.getSystemResource(path); 237 img = (Image ) (Toolkit.getDefaultToolkit()).getImage(url); 238 } catch (Exception e) { 239 System.out.println("Exception occured: " + e.getMessage() + 240 " - " + e ); 241 } 242 return (img); 243 } 244 245 public Component getListCellRendererComponent(JList list, 246 Object value, 247 int index, boolean isSelected, 249 boolean cellHasFocus) { 250 251 if(value instanceof LoggingEvent) { 255 LoggingEvent event = (LoggingEvent) value; 256 String str = layout.format(event); 257 String 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 { 278 279 CyclicBuffer cb; 280 281 JListViewModel(int size) { 282 cb = new CyclicBuffer(size); 283 } 284 285 public 286 void add(LoggingEvent event) { 287 cb.add(event); 289 int j = cb.length(); 290 fireContentsChanged(this, 0, j); 291 } 292 293 294 295 public 296 Object 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 { 308 309 Thread 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 e) { 321 System.out.println("Action occured"); 322 synchronized(t) { 323 t.notify(); 324 } 325 } 326 327 class AddThread extends Thread { 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 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 } 358 counter++; 359 view.add(event); 360 } 361 } 362 } 363 } 364 } 365 | Popular Tags |