1 16 17 18 import org.apache.log4j.helpers.CyclicBuffer; 19 import org.apache.log4j.Level; 20 import org.apache.log4j.Logger; 21 import org.apache.log4j.Layout; 22 import org.apache.log4j.PatternLayout; 23 import org.apache.log4j.spi.LoggingEvent; 24 25 import javax.swing.JFrame ; 26 import javax.swing.JButton ; 27 import javax.swing.JTable ; 28 import javax.swing.JTextArea ; 29 import javax.swing.table.AbstractTableModel ; 30 import javax.swing.table.TableCellRenderer ; 31 import javax.swing.JScrollPane ; 32 import javax.swing.BoxLayout ; 33 34 import java.awt.Component ; 35 import java.awt.Dimension ; 36 import java.awt.event.ActionListener ; 37 import java.awt.event.ActionEvent ; 38 import java.awt.Container ; 39 40 60 public class AppenderTable extends JTable { 61 62 63 static Logger logger = Logger.getLogger(AppenderTable.class); 64 65 static public void main(String [] args) { 66 67 if(args.length != 2) { 68 System.err.println( 69 "Usage: java AppenderTable bufferSize runLength\n" 70 +" where bufferSize is the size of the cyclic buffer in the TableModel\n" 71 +" and runLength is the total number of elements to add to the table in\n" 72 +" this test run."); 73 return; 74 } 75 76 JFrame frame = new JFrame ("JTableAppennder test"); 77 Container container = frame.getContentPane(); 78 79 AppenderTable tableAppender = new AppenderTable(); 80 81 int bufferSize = Integer.parseInt(args[0]); 82 AppenderTableModel model = new AppenderTableModel(bufferSize); 83 tableAppender.setModel(model); 84 85 int runLength = Integer.parseInt(args[1]); 86 87 JScrollPane sp = new JScrollPane (tableAppender); 88 sp.setPreferredSize(new Dimension (250, 80)); 89 90 container.setLayout(new BoxLayout (container, BoxLayout.X_AXIS)); 91 container.add(sp); 92 93 JButton button = new JButton ("ADD"); 96 container.add(button); 97 button.addActionListener(new JTableAddAction(tableAppender)); 98 99 frame.setSize(new Dimension (500,300)); 100 frame.setVisible(true); 101 102 long before = System.currentTimeMillis(); 103 104 int i = 0; 105 while(i++ < runLength) { 106 LoggingEvent event = new LoggingEvent("x", logger, Level.ERROR, 107 "Message "+i, null); 108 tableAppender.doAppend(event); 109 } 110 111 long after = System.currentTimeMillis(); 112 113 long totalTime = (after-before); 114 115 System.out.println("Total time :"+totalTime+ " milliseconds for "+ 116 "runLength insertions."); 117 System.out.println("Average time per insertion :" 118 +(totalTime*1000/runLength)+ " micro-seconds."); 119 120 121 } 122 123 public 124 AppenderTable() { 125 this.setDefaultRenderer(Object .class, new Renderer ()); 126 } 127 128 134 public 135 void doAppend(LoggingEvent event) { 136 ((AppenderTableModel)getModel()).insert(event); 137 } 138 139 145 class Renderer extends JTextArea implements TableCellRenderer { 146 147 PatternLayout layout; 148 149 public 150 Renderer() { 151 layout = new PatternLayout("%r %p %c [%t] - %m"); 152 } 153 154 public Component getTableCellRendererComponent(JTable table, 155 Object value, 156 boolean isSelected, 157 boolean hasFocus, 158 int row, 159 int column) { 160 161 if(value instanceof LoggingEvent) { 163 LoggingEvent event = (LoggingEvent) value; 164 String str = layout.format(event); 165 setText(str); 166 } else { 167 setText(value.toString()); 168 } 169 return this; 170 } 171 } 172 } 173 174 class AppenderTableModel extends AbstractTableModel { 175 176 CyclicBuffer cb; 177 178 AppenderTableModel(int size) { 179 cb = new CyclicBuffer(size); 180 } 181 182 186 public 187 void insert(LoggingEvent event) { 188 cb.add(event); 189 fireTableDataChanged(); 190 } 191 192 195 public 196 int getColumnCount() { 197 return 1; 198 } 199 200 204 public int getRowCount() { 205 return cb.length(); 206 } 207 208 216 public 217 Object getValueAt(int row, int col) { 218 return cb.get(row); 219 } 220 } 221 222 223 227 class JTableAddAction implements ActionListener { 228 229 AppenderTable appenderTable; 230 Logger dummy = Logger.getLogger("x"); 231 int counter = 0; 232 public 233 JTableAddAction(AppenderTable appenderTable) { 234 this.appenderTable = appenderTable; 235 } 236 237 public 238 void actionPerformed(ActionEvent e) { 239 counter++; 240 LoggingEvent event = new LoggingEvent("x", dummy, Level.DEBUG, 241 "Message "+counter, null); 242 appenderTable.doAppend(event); 243 } 244 } 245
| Popular Tags
|