KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > viewer > FilteredLogTableModel


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28
29 /**
30  * A TableModel for LogRecords which includes filtering support.
31  *
32  * @author Richard Wan
33  * @author Brent Sprecher
34  */

35
36 // Contributed by ThoughtWorks Inc.
37

38 public class FilteredLogTableModel
39     extends AbstractTableModel JavaDoc {
40   //--------------------------------------------------------------------------
41
// Constants:
42
//--------------------------------------------------------------------------
43

44   //--------------------------------------------------------------------------
45
// Protected Variables:
46
//--------------------------------------------------------------------------
47

48   protected LogRecordFilter _filter = new PassingLogRecordFilter();
49   protected List JavaDoc _allRecords = new ArrayList JavaDoc();
50   protected List JavaDoc _filteredRecords;
51   protected int _maxNumberOfLogRecords = 5000;
52   protected String JavaDoc[] _colNames = {"Date",
53                                   "Thread",
54                                   "Message #",
55                                   "Level",
56                                   "NDC",
57                                   "Category",
58                                   "Message",
59                                   "Location",
60                                   "Thrown"};
61
62   //--------------------------------------------------------------------------
63
// Private Variables:
64
//--------------------------------------------------------------------------
65

66   //--------------------------------------------------------------------------
67
// Constructors:
68
//--------------------------------------------------------------------------
69

70   public FilteredLogTableModel() {
71     super();
72   }
73
74   //--------------------------------------------------------------------------
75
// Public Methods:
76
//--------------------------------------------------------------------------
77

78   public void setLogRecordFilter(LogRecordFilter filter) {
79     _filter = filter;
80   }
81
82   public LogRecordFilter getLogRecordFilter() {
83     return _filter;
84   }
85
86   public String JavaDoc 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 JavaDoc 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   /**
128    * Forces the LogTableModel to requery its filters to determine
129    * which records to display.
130    */

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   /**
143    * Clears all records from the LogTableModel
144    */

145   public synchronized void clear() {
146     _allRecords.clear();
147     _filteredRecords.clear();
148     fireTableDataChanged();
149   }
150
151   //--------------------------------------------------------------------------
152
// Protected Methods:
153
//--------------------------------------------------------------------------
154

155   protected List JavaDoc getFilteredRecords() {
156     if (_filteredRecords == null) {
157       refresh();
158     }
159     return _filteredRecords;
160   }
161
162   protected List JavaDoc createFilteredRecordsList() {
163     List JavaDoc result = new ArrayList JavaDoc();
164     Iterator JavaDoc 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 JavaDoc records = getFilteredRecords();
177     int size = records.size();
178     if (row < size) {
179       return (LogRecord) records.get(row);
180     }
181     // a minor problem has happened. JTable has asked for
182
// a row outside the bounds, because the size of
183
// _filteredRecords has changed while it was looping.
184
// return the last row.
185
return (LogRecord) records.get(size - 1);
186
187   }
188
189   protected Object JavaDoc getColumn(int col, LogRecord lr) {
190     if (lr == null) {
191       return "NULL Column";
192     }
193     String JavaDoc date = new Date JavaDoc(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 JavaDoc(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 JavaDoc message = "The column number " + col + "must be between 0 and 8";
215         throw new IllegalArgumentException JavaDoc(message);
216     }
217   }
218
219   // We don't want the amount of rows to grow without bound,
220
// leading to a out-of-memory-exception. Especially not good
221
// in a production environment :)
222

223   // This method & clearLogRecords() are synchronized so we don't
224
// delete rows that don't exist.
225
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 JavaDoc 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   //--------------------------------------------------------------------------
252
// Private Methods:
253
//--------------------------------------------------------------------------
254
private int numberOfRecordsToTrim() {
255     return _allRecords.size() - _maxNumberOfLogRecords;
256   }
257
258   //--------------------------------------------------------------------------
259
// Nested Top-Level Classes or Interfaces
260
//--------------------------------------------------------------------------
261
}
262
263
Popular Tags