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.JTable ; 34 import javax.swing.JTextArea ; 35 import javax.swing.table.AbstractTableModel ; 36 import javax.swing.table.TableCellRenderer ; 37 import javax.swing.JScrollPane ; 38 import javax.swing.ListCellRenderer ; 39 import java.awt.Component ; 40 import java.awt.FlowLayout ; 41 import javax.swing.BoxLayout ; 42 43 import java.awt.BorderLayout ; 44 import java.awt.Dimension ; 45 import java.awt.event.ActionListener ; 46 import java.awt.event.ActionEvent ; 47 import java.awt.Container ; 48 import javax.swing.ImageIcon ; 49 import java.awt.Image ; 50 import java.awt.Toolkit ; 51 import java.net.URL ; 52 import java.awt.Rectangle ; 53 54 public class JTableAppender extends JTable { 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 .class, new Renderer ()); 65 66 } 67 68 public 69 void add(LoggingEvent event) { 70 ((JTableAppenderModel)getModel()).add(event); 71 } 72 73 public 74 Dimension getPreferredSize() { 75 System.out.println("getPreferredSize() called"); 76 return super.getPreferredSize(); 77 } 78 79 static public void main(String [] args) { 80 81 JFrame frame = new JFrame ("JListView test"); 82 Container 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 91 92 JScrollPane sp = new JScrollPane (appender); 93 sp.setPreferredSize(new Dimension (250, 80)); 94 95 container.setLayout(new BoxLayout (container, BoxLayout.X_AXIS)); 96 container.add(sp); 98 99 JButton button = new JButton ("ADD"); 100 container.add(button); 101 102 103 button.addActionListener(new JTableAddAction(appender)); 104 105 frame.setVisible(true); 106 frame.setSize(new Dimension (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 } 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 implements TableCellRenderer { 128 129 Object o = new Object (); 130 int i = 0; 131 132 public 133 Renderer() { 134 System.out.println("Render() called ----------------------"); 135 } 136 137 public Component getTableCellRendererComponent(JTable table, 138 Object value, 139 boolean isSelected, 140 boolean hasFocus, 141 int row, 142 int column) { 143 144 System.out.println(o + " ============== " + i++); 145 if(value instanceof LoggingEvent) { 148 LoggingEvent event = (LoggingEvent) value; 149 String str = layout.format(event); 150 String 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 { 172 173 CyclicBuffer cb; 174 175 JTableAppenderModel(int size) { 176 cb = new CyclicBuffer(size); 177 } 178 179 public 180 void add(LoggingEvent event) { 181 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 203 public 204 Object getValueAt(int row, int col) { 205 return cb.get(row); 206 } 207 } 208 209 210 class JTableAddAction implements ActionListener { 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 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 } 233 j++; 234 appender.add(event); 235 } 236 } 237 | Popular Tags |