KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.DateFormatManager;
19
20 import javax.swing.*;
21 import javax.swing.event.ListSelectionEvent JavaDoc;
22 import javax.swing.event.ListSelectionListener JavaDoc;
23 import javax.swing.table.TableColumn JavaDoc;
24 import javax.swing.table.TableColumnModel JavaDoc;
25 import java.awt.*;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * LogTable.
33  *
34  * @author Michael J. Sikorsky
35  * @author Robert Shaw
36  * @author Brad Marlborough
37  * @author Brent Sprecher
38  */

39
40 // Contributed by ThoughtWorks Inc.
41

42 public class LogTable extends JTable {
43   //--------------------------------------------------------------------------
44
// Constants:
45
//--------------------------------------------------------------------------
46

47   //--------------------------------------------------------------------------
48
// Protected Variables:
49
//--------------------------------------------------------------------------
50
protected int _rowHeight = 30;
51   protected JTextArea _detailTextArea;
52
53   // For the columns:
54
protected int _numCols = 9;
55   protected TableColumn JavaDoc[] _tableColumns = new TableColumn JavaDoc[_numCols];
56   protected int[] _colWidths = {40, 40, 40, 70, 70, 360, 440, 200, 60};
57   protected LogTableColumn[] _colNames = LogTableColumn.getLogTableColumnArray();
58   protected int _colDate = 0;
59   protected int _colThread = 1;
60   protected int _colMessageNum = 2;
61   protected int _colLevel = 3;
62   protected int _colNDC = 4;
63   protected int _colCategory = 5;
64   protected int _colMessage = 6;
65   protected int _colLocation = 7;
66   protected int _colThrown = 8;
67
68   protected DateFormatManager _dateFormatManager = null;
69
70   //--------------------------------------------------------------------------
71
// Private Variables:
72
//--------------------------------------------------------------------------
73

74   //--------------------------------------------------------------------------
75
// Constructors:
76
//--------------------------------------------------------------------------
77

78   public LogTable(JTextArea detailTextArea) {
79     super();
80
81     init();
82
83     _detailTextArea = detailTextArea;
84
85     setModel(new FilteredLogTableModel());
86
87     Enumeration JavaDoc columns = getColumnModel().getColumns();
88     int i = 0;
89     while (columns.hasMoreElements()) {
90       TableColumn JavaDoc col = (TableColumn JavaDoc) columns.nextElement();
91       col.setCellRenderer(new LogTableRowRenderer());
92       col.setPreferredWidth(_colWidths[i]);
93
94       _tableColumns[i] = col;
95       i++;
96     }
97
98     ListSelectionModel rowSM = getSelectionModel();
99     rowSM.addListSelectionListener(new LogTableListSelectionListener(this));
100
101     //setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
102
}
103
104   //--------------------------------------------------------------------------
105
// Public Methods:
106
//--------------------------------------------------------------------------
107

108   /**
109    * Get the DateFormatManager for formatting dates.
110    */

111   public DateFormatManager getDateFormatManager() {
112     return _dateFormatManager;
113   }
114
115   /**
116    * Set the date format manager for formatting dates.
117    */

118   public void setDateFormatManager(DateFormatManager dfm) {
119     _dateFormatManager = dfm;
120   }
121
122   public synchronized void clearLogRecords() {
123     //For JDK1.3
124
//((DefaultTableModel)getModel()).setRowCount(0);
125

126     // For JDK1.2.x
127
getFilteredLogTableModel().clear();
128   }
129
130   public FilteredLogTableModel getFilteredLogTableModel() {
131     return (FilteredLogTableModel) getModel();
132   }
133
134   // default view if a view is not set and saved
135
public void setDetailedView() {
136     //TODO: Defineable Views.
137
TableColumnModel JavaDoc model = getColumnModel();
138     // Remove all the columns:
139
for (int f = 0; f < _numCols; f++) {
140       model.removeColumn(_tableColumns[f]);
141     }
142     // Add them back in the correct order:
143
for (int i = 0; i < _numCols; i++) {
144       model.addColumn(_tableColumns[i]);
145     }
146     //SWING BUG:
147
sizeColumnsToFit(-1);
148   }
149
150   public void setView(List JavaDoc columns) {
151     TableColumnModel JavaDoc model = getColumnModel();
152
153     // Remove all the columns:
154
for (int f = 0; f < _numCols; f++) {
155       model.removeColumn(_tableColumns[f]);
156     }
157     Iterator JavaDoc selectedColumns = columns.iterator();
158     Vector JavaDoc columnNameAndNumber = getColumnNameAndNumber();
159     while (selectedColumns.hasNext()) {
160       // add the column to the view
161
model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
162     }
163
164     //SWING BUG:
165
sizeColumnsToFit(-1);
166   }
167
168   public void setFont(Font font) {
169     super.setFont(font);
170     Graphics g = this.getGraphics();
171     if (g != null) {
172       FontMetrics fm = g.getFontMetrics(font);
173       int height = fm.getHeight();
174       _rowHeight = height + height / 3;
175       setRowHeight(_rowHeight);
176     }
177
178
179   }
180
181
182   //--------------------------------------------------------------------------
183
// Protected Methods:
184
//--------------------------------------------------------------------------
185

186   protected void init() {
187     setRowHeight(_rowHeight);
188     setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
189   }
190
191   // assign a column number to a column name
192
protected Vector JavaDoc getColumnNameAndNumber() {
193     Vector JavaDoc columnNameAndNumber = new Vector JavaDoc();
194     for (int i = 0; i < _colNames.length; i++) {
195       columnNameAndNumber.add(i, _colNames[i]);
196     }
197     return columnNameAndNumber;
198   }
199
200   //--------------------------------------------------------------------------
201
// Private Methods:
202
//--------------------------------------------------------------------------
203

204   //--------------------------------------------------------------------------
205
// Nested Top-Level Classes or Interfaces:
206
//--------------------------------------------------------------------------
207

208   class LogTableListSelectionListener implements ListSelectionListener JavaDoc {
209     protected JTable _table;
210
211     public LogTableListSelectionListener(JTable table) {
212       _table = table;
213     }
214
215     public void valueChanged(ListSelectionEvent JavaDoc e) {
216       //Ignore extra messages.
217
if (e.getValueIsAdjusting()) {
218         return;
219       }
220
221       ListSelectionModel lsm = (ListSelectionModel) e.getSource();
222       if (lsm.isSelectionEmpty()) {
223         //no rows are selected
224
} else {
225         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
226         int selectedRow = lsm.getMinSelectionIndex();
227
228         for (int i = 0; i < _numCols - 1; i++) {
229           String JavaDoc value = "";
230           Object JavaDoc obj = _table.getModel().getValueAt(selectedRow, i);
231           if (obj != null) {
232             value = obj.toString();
233           }
234
235           buf.append(_colNames[i] + ":");
236           buf.append("\t");
237
238           if (i == _colThread || i == _colMessage || i == _colLevel) {
239             buf.append("\t"); // pad out the date.
240
}
241
242           if (i == _colDate || i == _colNDC) {
243             buf.append("\t\t"); // pad out the date.
244
}
245
246 // if( i == _colSequence)
247
// {
248
// buf.append("\t\t\t"); // pad out the Sequnce.
249
// }
250

251           buf.append(value);
252           buf.append("\n");
253         }
254         buf.append(_colNames[_numCols - 1] + ":\n");
255         Object JavaDoc obj = _table.getModel().getValueAt(selectedRow, _numCols - 1);
256         if (obj != null) {
257           buf.append(obj.toString());
258         }
259
260         _detailTextArea.setText(buf.toString());
261       }
262     }
263   }
264 }
265
266
267
268
269
270
271
Popular Tags