1 package org.columba.core.gui.logdisplay; 17 18 import java.util.Date ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Set ; 24 import java.util.logging.Handler ; 25 import java.util.logging.Level ; 26 import java.util.logging.LogRecord ; 27 import java.util.logging.Logger ; 28 29 import javax.swing.ListModel ; 30 import javax.swing.event.ListDataEvent ; 31 import javax.swing.event.ListDataListener ; 32 import javax.swing.event.TableModelListener ; 33 import javax.swing.table.TableModel ; 34 35 36 40 public final class LogRecordList extends Handler implements TableModel , ListModel { 41 42 43 private static LogRecordList instance; 44 45 46 public static final String [] COLUMNS = new String [] {"Level", "Message", "Class", "Method", "Time", "Seq. nr", "Thread id" }; 47 48 private List logMessages = new LinkedList (); 49 private Set listeners = new HashSet (); 50 51 private boolean isStarted; 52 53 56 private LogRecordList() { 57 super(); 58 startLogging(); 59 } 60 61 65 public static LogRecordList getInstance() { 66 if (instance == null) { 67 instance = new LogRecordList(); 68 } 69 return instance; 70 } 71 72 77 public LogRecord getLogRecord(int rowIndex) { 78 if ((rowIndex >= 0) && (rowIndex < logMessages.size())) { 79 return (LogRecord ) logMessages.get(rowIndex); 80 } else { 81 throw new IndexOutOfBoundsException ("No such row in the table model [" + rowIndex + "]"); 82 } 83 } 84 85 88 public void clear() { 89 int size = logMessages.size(); 90 logMessages.clear(); 91 fireListRemoveEvent(0, size); 92 } 93 94 98 public void startLogging() { 99 if (!isStarted) { 100 Logger log = Logger.getLogger("org.columba"); 101 log.addHandler(this); 102 isStarted = true; 103 } 104 } 105 106 111 public void stopLogging() { 112 if (isStarted) { 113 Logger log = Logger.getLogger("org.columba"); 114 log.removeHandler(this); 115 isStarted = false; 116 } 117 } 118 119 123 124 public void close() { 125 } 126 127 128 public void flush() { 129 } 130 131 132 public void publish(LogRecord record) { 133 logMessages.add(record); 134 if (logMessages.size() > 1000) { 135 logMessages.remove(0); 136 fireListUpdatedEvent(); 137 } else { 139 fireNewRowEvent(logMessages.size()); 140 } 142 } 143 144 148 152 158 159 160 public int getColumnCount() { 161 return COLUMNS.length; 162 } 163 164 165 public int getRowCount() { 166 return logMessages.size(); 167 } 168 169 170 public boolean isCellEditable(int rowIndex, int columnIndex) { 171 return false; 172 } 173 174 175 public Class getColumnClass(int columnIndex) { 176 Class value; 177 switch (columnIndex) { 178 default: 179 throw new IndexOutOfBoundsException ("No such column in the table model [" + columnIndex + "]"); 180 case 1: 181 case 2: 182 case 3: 183 value = String .class; 184 break; 185 case 0: 186 value = Level .class; 187 break; 188 case 4: 189 value = Date .class; 190 break; 191 case 5: 192 value = Long .class; 193 break; 194 case 6: 195 value = Integer .class; 196 break; 197 } 198 return value; 199 } 200 201 202 public Object getValueAt(int rowIndex, int columnIndex) { 203 Object value; 204 if ((rowIndex >= 0) && (rowIndex < logMessages.size())) { 205 LogRecord record = (LogRecord ) logMessages.get(rowIndex); 206 switch (columnIndex) { 207 default: 208 throw new IndexOutOfBoundsException ("No such column in the table model [" + columnIndex + "]"); 209 case 0: 210 value = record.getLevel(); 211 break; 212 case 1: 213 value = record.getMessage(); 214 break; 215 case 2: 216 value = record.getSourceClassName(); 217 break; 218 case 3: 219 value = record.getSourceMethodName(); 220 break; 221 case 4: 222 value = new Date (record.getMillis()); 223 break; 224 case 5: 225 value = new Long (record.getSequenceNumber()); 226 break; 227 case 6: 228 value = new Integer (record.getThreadID()); 229 break; 230 } 231 } else { 232 throw new IndexOutOfBoundsException ("No such row in the table model [" + rowIndex + "]"); 233 } 234 return value; 235 } 236 237 238 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 239 240 throw new UnsupportedOperationException ("The table model is not editable"); 241 } 242 243 244 public String getColumnName(int columnIndex) { 245 return COLUMNS[columnIndex]; 246 } 247 248 249 public void addTableModelListener(TableModelListener l) { 250 listeners.add(l); 251 } 252 253 254 public void removeTableModelListener(TableModelListener l) { 255 listeners.remove(l); 256 } 257 258 262 263 public int getSize() { 264 return logMessages.size(); 265 } 266 267 268 public Object getElementAt(int index) { 269 return getLogRecord(index); 270 } 271 272 273 public void addListDataListener(ListDataListener l) { 274 listeners.add(l); 275 } 276 277 278 public void removeListDataListener(ListDataListener l) { 279 listeners.remove(l); 280 } 281 282 286 private void fireNewRowEvent(int row) { 287 ListDataEvent event = new ListDataEvent (this, ListDataEvent.INTERVAL_ADDED, row, row); 288 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 289 ListDataListener listener = (ListDataListener ) iterator.next(); 290 listener.intervalAdded(event); 291 } 292 } 293 294 297 private void fireListUpdatedEvent() { 298 ListDataEvent event = new ListDataEvent (this, ListDataEvent.CONTENTS_CHANGED, 0, logMessages.size()); 299 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 300 ListDataListener listener = (ListDataListener ) iterator.next(); 301 listener.contentsChanged(event); 302 } 303 } 304 305 310 private void fireListRemoveEvent(int rowStart, int rowEnd) { 311 ListDataEvent event = new ListDataEvent (this, ListDataEvent.INTERVAL_REMOVED, rowStart, rowEnd); 312 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 313 ListDataListener listener = (ListDataListener ) iterator.next(); 314 listener.intervalRemoved(event); 315 } 316 } 317 318 } 319 | Popular Tags |