KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > logdisplay > LogRecordList


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.gui.logdisplay;
17
18 import java.util.Date JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.logging.Handler JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.LogRecord JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.swing.ListModel JavaDoc;
30 import javax.swing.event.ListDataEvent JavaDoc;
31 import javax.swing.event.ListDataListener JavaDoc;
32 import javax.swing.event.TableModelListener JavaDoc;
33 import javax.swing.table.TableModel JavaDoc;
34
35
36 /**
37  * A List that contains all log records.
38  * @author redsolo
39  */

40 public final class LogRecordList extends Handler JavaDoc implements TableModel JavaDoc, ListModel JavaDoc {
41
42     /** Singleton instance for all log memory. */
43     private static LogRecordList instance;
44
45     /** Columns in the table. */
46     public static final String JavaDoc[] COLUMNS = new String JavaDoc[] {"Level", "Message", "Class", "Method", "Time", "Seq. nr", "Thread id" };
47
48     private List JavaDoc logMessages = new LinkedList JavaDoc();
49     private Set JavaDoc listeners = new HashSet JavaDoc();
50
51     private boolean isStarted;
52
53     /**
54      *
55      */

56     private LogRecordList() {
57         super();
58         startLogging();
59     }
60
61     /**
62      * Returns the singleton instance of the log record list.
63      * @return the LogRecordList object.
64      */

65     public static LogRecordList getInstance() {
66         if (instance == null) {
67             instance = new LogRecordList();
68         }
69         return instance;
70     }
71
72     /**
73      * Returns the LogRecord for the specified row;
74      * @param rowIndex the row.
75      * @return the LogRecord
76      */

77     public LogRecord JavaDoc getLogRecord(int rowIndex) {
78         if ((rowIndex >= 0) && (rowIndex < logMessages.size())) {
79             return (LogRecord JavaDoc) logMessages.get(rowIndex);
80         } else {
81             throw new IndexOutOfBoundsException JavaDoc("No such row in the table model [" + rowIndex + "]");
82         }
83     }
84
85     /**
86      * Clears all log messages.
87      */

88     public void clear() {
89         int size = logMessages.size();
90         logMessages.clear();
91         fireListRemoveEvent(0, size);
92     }
93
94     /**
95      * Starts storing the log messages.
96      * All log messages issued after this method, will be stored in this object.
97      */

98     public void startLogging() {
99         if (!isStarted) {
100             Logger JavaDoc log = Logger.getLogger("org.columba");
101             log.addHandler(this);
102             isStarted = true;
103         }
104     }
105
106     /**
107      * Stops storing the log messages.
108      * All log messages issued after this method, will not be stored in this object. This
109      * will not alter the actual log messages list.
110      */

111     public void stopLogging() {
112         if (isStarted) {
113             Logger JavaDoc log = Logger.getLogger("org.columba");
114             log.removeHandler(this);
115             isStarted = false;
116         }
117     }
118
119     //
120
// Logging stuff
121
//
122

123     /** {@inheritDoc} */
124     public void close() {
125     }
126
127     /** {@inheritDoc} */
128     public void flush() {
129     }
130
131     /** {@inheritDoc} */
132     public void publish(LogRecord JavaDoc record) {
133         logMessages.add(record);
134         if (logMessages.size() > 1000) {
135             logMessages.remove(0);
136             fireListUpdatedEvent();
137             //notifyTableListeners(new TableModelEvent(this, 0, logMessages.size()));
138
} else {
139             fireNewRowEvent(logMessages.size());
140             //notifyTableListeners(new TableModelEvent(this, logMessages.size(), logMessages.size(), TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
141
}
142     }
143
144     //
145
// Table model stuff
146
//
147

148     /**
149      * Notify all table listeners.
150      * @param event the event to notify with.
151      */

152     /*private void notifyTableListeners(TableModelEvent event) {
153         for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
154             TableModelListener listener = (TableModelListener) iterator.next();
155             listener.tableChanged(event);
156         }
157     }*/

158
159     /** {@inheritDoc} */
160     public int getColumnCount() {
161         return COLUMNS.length;
162     }
163
164     /** {@inheritDoc} */
165     public int getRowCount() {
166         return logMessages.size();
167     }
168
169     /** {@inheritDoc} */
170     public boolean isCellEditable(int rowIndex, int columnIndex) {
171         return false;
172     }
173
174     /** {@inheritDoc} */
175     public Class JavaDoc getColumnClass(int columnIndex) {
176         Class JavaDoc value;
177         switch (columnIndex) {
178             default:
179                 throw new IndexOutOfBoundsException JavaDoc("No such column in the table model [" + columnIndex + "]");
180             case 1:
181             case 2:
182             case 3:
183                 value = String JavaDoc.class;
184                 break;
185             case 0:
186                 value = Level JavaDoc.class;
187                 break;
188             case 4:
189                 value = Date JavaDoc.class;
190                 break;
191             case 5:
192                 value = Long JavaDoc.class;
193                 break;
194             case 6:
195                 value = Integer JavaDoc.class;
196                 break;
197         }
198         return value;
199     }
200
201     /** {@inheritDoc} */
202     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
203         Object JavaDoc value;
204         if ((rowIndex >= 0) && (rowIndex < logMessages.size())) {
205             LogRecord JavaDoc record = (LogRecord JavaDoc) logMessages.get(rowIndex);
206             switch (columnIndex) {
207                 default:
208                     throw new IndexOutOfBoundsException JavaDoc("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 JavaDoc(record.getMillis());
223                     break;
224                 case 5:
225                     value = new Long JavaDoc(record.getSequenceNumber());
226                     break;
227                 case 6:
228                     value = new Integer JavaDoc(record.getThreadID());
229                     break;
230             }
231         } else {
232             throw new IndexOutOfBoundsException JavaDoc("No such row in the table model [" + rowIndex + "]");
233         }
234         return value;
235     }
236
237     /** {@inheritDoc} */
238     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
239
240         throw new UnsupportedOperationException JavaDoc("The table model is not editable");
241     }
242
243     /** {@inheritDoc} */
244     public String JavaDoc getColumnName(int columnIndex) {
245         return COLUMNS[columnIndex];
246     }
247
248     /** {@inheritDoc} */
249     public void addTableModelListener(TableModelListener JavaDoc l) {
250         listeners.add(l);
251     }
252
253     /** {@inheritDoc} */
254     public void removeTableModelListener(TableModelListener JavaDoc l) {
255         listeners.remove(l);
256     }
257
258     //
259
// LIST model stuff
260
//
261

262     /** {@inheritDoc} */
263     public int getSize() {
264         return logMessages.size();
265     }
266
267     /** {@inheritDoc} */
268     public Object JavaDoc getElementAt(int index) {
269         return getLogRecord(index);
270     }
271
272     /** {@inheritDoc} */
273     public void addListDataListener(ListDataListener JavaDoc l) {
274         listeners.add(l);
275     }
276
277     /** {@inheritDoc} */
278     public void removeListDataListener(ListDataListener JavaDoc l) {
279         listeners.remove(l);
280     }
281
282     /**
283      * Notifies all list data listener that a row has been inserted.
284      * @param row the new row.
285      */

286     private void fireNewRowEvent(int row) {
287         ListDataEvent JavaDoc event = new ListDataEvent JavaDoc(this, ListDataEvent.INTERVAL_ADDED, row, row);
288         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();) {
289             ListDataListener JavaDoc listener = (ListDataListener JavaDoc) iterator.next();
290             listener.intervalAdded(event);
291         }
292     }
293
294     /**
295      * Notifies all list data listener that the contents has changed..
296      */

297     private void fireListUpdatedEvent() {
298         ListDataEvent JavaDoc event = new ListDataEvent JavaDoc(this, ListDataEvent.CONTENTS_CHANGED, 0, logMessages.size());
299         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();) {
300             ListDataListener JavaDoc listener = (ListDataListener JavaDoc) iterator.next();
301             listener.contentsChanged(event);
302         }
303     }
304
305     /**
306      * Notifies all list data listener that rows has been removed
307      * @param rowStart the first row removed.
308      * @param rowEnd the last row removed.
309      */

310     private void fireListRemoveEvent(int rowStart, int rowEnd) {
311         ListDataEvent JavaDoc event = new ListDataEvent JavaDoc(this, ListDataEvent.INTERVAL_REMOVED, rowStart, rowEnd);
312         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();) {
313             ListDataListener JavaDoc listener = (ListDataListener JavaDoc) iterator.next();
314             listener.intervalRemoved(event);
315         }
316     }
317
318 }
319
Popular Tags