1 16 package org.apache.log4j.lf5.viewer; 17 18 import org.apache.log4j.lf5.LogRecord; 19 import org.apache.log4j.lf5.LogRecordFilter; 20 import org.apache.log4j.lf5.PassingLogRecordFilter; 21 22 import javax.swing.table.AbstractTableModel ; 23 import java.util.ArrayList ; 24 import java.util.Date ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 29 35 36 38 public class FilteredLogTableModel 39 extends AbstractTableModel { 40 44 48 protected LogRecordFilter _filter = new PassingLogRecordFilter(); 49 protected List _allRecords = new ArrayList (); 50 protected List _filteredRecords; 51 protected int _maxNumberOfLogRecords = 5000; 52 protected String [] _colNames = {"Date", 53 "Thread", 54 "Message #", 55 "Level", 56 "NDC", 57 "Category", 58 "Message", 59 "Location", 60 "Thrown"}; 61 62 66 70 public FilteredLogTableModel() { 71 super(); 72 } 73 74 78 public void setLogRecordFilter(LogRecordFilter filter) { 79 _filter = filter; 80 } 81 82 public LogRecordFilter getLogRecordFilter() { 83 return _filter; 84 } 85 86 public String getColumnName(int i) { 87 return _colNames[i]; 88 } 89 90 public int getColumnCount() { 91 return _colNames.length; 92 } 93 94 public int getRowCount() { 95 return getFilteredRecords().size(); 96 } 97 98 public int getTotalRowCount() { 99 return _allRecords.size(); 100 } 101 102 public Object getValueAt(int row, int col) { 103 LogRecord record = getFilteredRecord(row); 104 return getColumn(col, record); 105 } 106 107 public void setMaxNumberOfLogRecords(int maxNumRecords) { 108 if (maxNumRecords > 0) { 109 _maxNumberOfLogRecords = maxNumRecords; 110 } 111 112 } 113 114 public synchronized boolean addLogRecord(LogRecord record) { 115 116 _allRecords.add(record); 117 118 if (_filter.passes(record) == false) { 119 return false; 120 } 121 getFilteredRecords().add(record); 122 fireTableRowsInserted(getRowCount(), getRowCount()); 123 trimRecords(); 124 return true; 125 } 126 127 131 public synchronized void refresh() { 132 _filteredRecords = createFilteredRecordsList(); 133 fireTableDataChanged(); 134 } 135 136 public synchronized void fastRefresh() { 137 _filteredRecords.remove(0); 138 fireTableRowsDeleted(0, 0); 139 } 140 141 142 145 public synchronized void clear() { 146 _allRecords.clear(); 147 _filteredRecords.clear(); 148 fireTableDataChanged(); 149 } 150 151 155 protected List getFilteredRecords() { 156 if (_filteredRecords == null) { 157 refresh(); 158 } 159 return _filteredRecords; 160 } 161 162 protected List createFilteredRecordsList() { 163 List result = new ArrayList (); 164 Iterator records = _allRecords.iterator(); 165 LogRecord current; 166 while (records.hasNext()) { 167 current = (LogRecord) records.next(); 168 if (_filter.passes(current)) { 169 result.add(current); 170 } 171 } 172 return result; 173 } 174 175 protected LogRecord getFilteredRecord(int row) { 176 List records = getFilteredRecords(); 177 int size = records.size(); 178 if (row < size) { 179 return (LogRecord) records.get(row); 180 } 181 return (LogRecord) records.get(size - 1); 186 187 } 188 189 protected Object getColumn(int col, LogRecord lr) { 190 if (lr == null) { 191 return "NULL Column"; 192 } 193 String date = new Date (lr.getMillis()).toString(); 194 switch (col) { 195 case 0: 196 return date + " (" + lr.getMillis() + ")"; 197 case 1: 198 return lr.getThreadDescription(); 199 case 2: 200 return new Long (lr.getSequenceNumber()); 201 case 3: 202 return lr.getLevel(); 203 case 4: 204 return lr.getNDC(); 205 case 5: 206 return lr.getCategory(); 207 case 6: 208 return lr.getMessage(); 209 case 7: 210 return lr.getLocation(); 211 case 8: 212 return lr.getThrownStackTrace(); 213 default: 214 String message = "The column number " + col + "must be between 0 and 8"; 215 throw new IllegalArgumentException (message); 216 } 217 } 218 219 223 protected void trimRecords() { 226 if (needsTrimming()) { 227 trimOldestRecords(); 228 } 229 } 230 231 protected boolean needsTrimming() { 232 return (_allRecords.size() > _maxNumberOfLogRecords); 233 } 234 235 protected void trimOldestRecords() { 236 synchronized (_allRecords) { 237 int trim = numberOfRecordsToTrim(); 238 if (trim > 1) { 239 List oldRecords = 240 _allRecords.subList(0, trim); 241 oldRecords.clear(); 242 refresh(); 243 } else { 244 _allRecords.remove(0); 245 fastRefresh(); 246 } 247 } 248 249 } 250 251 private int numberOfRecordsToTrim() { 255 return _allRecords.size() - _maxNumberOfLogRecords; 256 } 257 258 } 262 263 | Popular Tags |