/apache-log4j-1.2.17/src/main/java/org/apache/log4j/lf5/viewer/LogTable.java
Java | 277 lines | 147 code | 55 blank | 75 comment | 26 complexity | d24d8d158be3fd1dd8c0d769e30cf46d MD5 | raw file
Possible License(s): Apache-2.0
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.log4j.lf5.viewer;
18
19import java.awt.Font;
20import java.awt.FontMetrics;
21import java.awt.Graphics;
22import java.util.Enumeration;
23import java.util.Iterator;
24import java.util.List;
25import java.util.Vector;
26
27import javax.swing.JTable;
28import javax.swing.JTextArea;
29import javax.swing.ListSelectionModel;
30import javax.swing.event.ListSelectionEvent;
31import javax.swing.event.ListSelectionListener;
32import javax.swing.table.TableColumn;
33import javax.swing.table.TableColumnModel;
34
35import org.apache.log4j.lf5.util.DateFormatManager;
36
37/**
38 * LogTable.
39 *
40 * @author Michael J. Sikorsky
41 * @author Robert Shaw
42 * @author Brad Marlborough
43 * @author Brent Sprecher
44 */
45
46// Contributed by ThoughtWorks Inc.
47
48public class LogTable extends JTable {
49 private static final long serialVersionUID = 4867085140195148458L;
50 //--------------------------------------------------------------------------
51 // Constants:
52 //--------------------------------------------------------------------------
53
54 //--------------------------------------------------------------------------
55 // Protected Variables:
56 //--------------------------------------------------------------------------
57 protected int _rowHeight = 30;
58 protected JTextArea _detailTextArea;
59
60 // For the columns:
61 protected int _numCols = 9;
62 protected TableColumn[] _tableColumns = new TableColumn[_numCols];
63 protected int[] _colWidths = {40, 40, 40, 70, 70, 360, 440, 200, 60};
64 protected LogTableColumn[] _colNames = LogTableColumn.getLogTableColumnArray();
65 protected int _colDate = 0;
66 protected int _colThread = 1;
67 protected int _colMessageNum = 2;
68 protected int _colLevel = 3;
69 protected int _colNDC = 4;
70 protected int _colCategory = 5;
71 protected int _colMessage = 6;
72 protected int _colLocation = 7;
73 protected int _colThrown = 8;
74
75 protected DateFormatManager _dateFormatManager = null;
76
77 //--------------------------------------------------------------------------
78 // Private Variables:
79 //--------------------------------------------------------------------------
80
81 //--------------------------------------------------------------------------
82 // Constructors:
83 //--------------------------------------------------------------------------
84
85 public LogTable(JTextArea detailTextArea) {
86 super();
87
88 init();
89
90 _detailTextArea = detailTextArea;
91
92 setModel(new FilteredLogTableModel());
93
94 Enumeration columns = getColumnModel().getColumns();
95 int i = 0;
96 while (columns.hasMoreElements()) {
97 TableColumn col = (TableColumn) columns.nextElement();
98 col.setCellRenderer(new LogTableRowRenderer());
99 col.setPreferredWidth(_colWidths[i]);
100
101 _tableColumns[i] = col;
102 i++;
103 }
104
105 ListSelectionModel rowSM = getSelectionModel();
106 rowSM.addListSelectionListener(new LogTableListSelectionListener(this));
107
108 //setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
109 }
110
111 //--------------------------------------------------------------------------
112 // Public Methods:
113 //--------------------------------------------------------------------------
114
115 /**
116 * Get the DateFormatManager for formatting dates.
117 */
118 public DateFormatManager getDateFormatManager() {
119 return _dateFormatManager;
120 }
121
122 /**
123 * Set the date format manager for formatting dates.
124 */
125 public void setDateFormatManager(DateFormatManager dfm) {
126 _dateFormatManager = dfm;
127 }
128
129 public synchronized void clearLogRecords() {
130 //For JDK1.3
131 //((DefaultTableModel)getModel()).setRowCount(0);
132
133 // For JDK1.2.x
134 getFilteredLogTableModel().clear();
135 }
136
137 public FilteredLogTableModel getFilteredLogTableModel() {
138 return (FilteredLogTableModel) getModel();
139 }
140
141 // default view if a view is not set and saved
142 public void setDetailedView() {
143 //TODO: Defineable Views.
144 TableColumnModel model = getColumnModel();
145 // Remove all the columns:
146 for (int f = 0; f < _numCols; f++) {
147 model.removeColumn(_tableColumns[f]);
148 }
149 // Add them back in the correct order:
150 for (int i = 0; i < _numCols; i++) {
151 model.addColumn(_tableColumns[i]);
152 }
153 //SWING BUG:
154 sizeColumnsToFit(-1);
155 }
156
157 public void setView(List columns) {
158 TableColumnModel model = getColumnModel();
159
160 // Remove all the columns:
161 for (int f = 0; f < _numCols; f++) {
162 model.removeColumn(_tableColumns[f]);
163 }
164 Iterator selectedColumns = columns.iterator();
165 Vector columnNameAndNumber = getColumnNameAndNumber();
166 while (selectedColumns.hasNext()) {
167 // add the column to the view
168 model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
169 }
170
171 //SWING BUG:
172 sizeColumnsToFit(-1);
173 }
174
175 public void setFont(Font font) {
176 super.setFont(font);
177 Graphics g = this.getGraphics();
178 if (g != null) {
179 FontMetrics fm = g.getFontMetrics(font);
180 int height = fm.getHeight();
181 _rowHeight = height + height / 3;
182 setRowHeight(_rowHeight);
183 }
184
185
186 }
187
188
189 //--------------------------------------------------------------------------
190 // Protected Methods:
191 //--------------------------------------------------------------------------
192
193 protected void init() {
194 setRowHeight(_rowHeight);
195 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
196 }
197
198 // assign a column number to a column name
199 protected Vector getColumnNameAndNumber() {
200 Vector columnNameAndNumber = new Vector();
201 for (int i = 0; i < _colNames.length; i++) {
202 columnNameAndNumber.add(i, _colNames[i]);
203 }
204 return columnNameAndNumber;
205 }
206
207 //--------------------------------------------------------------------------
208 // Private Methods:
209 //--------------------------------------------------------------------------
210
211 //--------------------------------------------------------------------------
212 // Nested Top-Level Classes or Interfaces:
213 //--------------------------------------------------------------------------
214
215 class LogTableListSelectionListener implements ListSelectionListener {
216 protected JTable _table;
217
218 public LogTableListSelectionListener(JTable table) {
219 _table = table;
220 }
221
222 public void valueChanged(ListSelectionEvent e) {
223 //Ignore extra messages.
224 if (e.getValueIsAdjusting()) {
225 return;
226 }
227
228 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
229 if (lsm.isSelectionEmpty()) {
230 //no rows are selected
231 } else {
232 StringBuffer buf = new StringBuffer();
233 int selectedRow = lsm.getMinSelectionIndex();
234
235 for (int i = 0; i < _numCols - 1; i++) {
236 String value = "";
237 Object obj = _table.getModel().getValueAt(selectedRow, i);
238 if (obj != null) {
239 value = obj.toString();
240 }
241
242 buf.append(_colNames[i] + ":");
243 buf.append("\t");
244
245 if (i == _colThread || i == _colMessage || i == _colLevel) {
246 buf.append("\t"); // pad out the date.
247 }
248
249 if (i == _colDate || i == _colNDC) {
250 buf.append("\t\t"); // pad out the date.
251 }
252
253// if( i == _colSequence)
254// {
255// buf.append("\t\t\t"); // pad out the Sequnce.
256// }
257
258 buf.append(value);
259 buf.append("\n");
260 }
261 buf.append(_colNames[_numCols - 1] + ":\n");
262 Object obj = _table.getModel().getValueAt(selectedRow, _numCols - 1);
263 if (obj != null) {
264 buf.append(obj.toString());
265 }
266
267 _detailTextArea.setText(buf.toString());
268 }
269 }
270 }
271}
272
273
274
275
276
277