1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.lf5.viewer;
18
19 import org.apache.log4j.lf5.util.DateFormatManager;
20
21 import javax.swing.*;
22 import javax.swing.event.ListSelectionEvent;
23 import javax.swing.event.ListSelectionListener;
24 import javax.swing.table.TableColumn;
25 import javax.swing.table.TableColumnModel;
26 import java.awt.*;
27 import java.util.Enumeration;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Vector;
31
32 /***
33 * LogTable.
34 *
35 * @author Michael J. Sikorsky
36 * @author Robert Shaw
37 * @author Brad Marlborough
38 * @author Brent Sprecher
39 */
40
41
42
43 public class LogTable extends JTable {
44 private static final long serialVersionUID = 4867085140195148458L;
45
46
47
48
49
50
51
52 protected int _rowHeight = 30;
53 protected JTextArea _detailTextArea;
54
55
56 protected int _numCols = 9;
57 protected TableColumn[] _tableColumns = new TableColumn[_numCols];
58 protected int[] _colWidths = {40, 40, 40, 70, 70, 360, 440, 200, 60};
59 protected LogTableColumn[] _colNames = LogTableColumn.getLogTableColumnArray();
60 protected int _colDate = 0;
61 protected int _colThread = 1;
62 protected int _colMessageNum = 2;
63 protected int _colLevel = 3;
64 protected int _colNDC = 4;
65 protected int _colCategory = 5;
66 protected int _colMessage = 6;
67 protected int _colLocation = 7;
68 protected int _colThrown = 8;
69
70 protected DateFormatManager _dateFormatManager = null;
71
72
73
74
75
76
77
78
79
80 public LogTable(JTextArea detailTextArea) {
81 super();
82
83 init();
84
85 _detailTextArea = detailTextArea;
86
87 setModel(new FilteredLogTableModel());
88
89 Enumeration columns = getColumnModel().getColumns();
90 int i = 0;
91 while (columns.hasMoreElements()) {
92 TableColumn col = (TableColumn) columns.nextElement();
93 col.setCellRenderer(new LogTableRowRenderer());
94 col.setPreferredWidth(_colWidths[i]);
95
96 _tableColumns[i] = col;
97 i++;
98 }
99
100 ListSelectionModel rowSM = getSelectionModel();
101 rowSM.addListSelectionListener(new LogTableListSelectionListener(this));
102
103
104 }
105
106
107
108
109
110 /***
111 * Get the DateFormatManager for formatting dates.
112 */
113 public DateFormatManager getDateFormatManager() {
114 return _dateFormatManager;
115 }
116
117 /***
118 * Set the date format manager for formatting dates.
119 */
120 public void setDateFormatManager(DateFormatManager dfm) {
121 _dateFormatManager = dfm;
122 }
123
124 public synchronized void clearLogRecords() {
125
126
127
128
129 getFilteredLogTableModel().clear();
130 }
131
132 public FilteredLogTableModel getFilteredLogTableModel() {
133 return (FilteredLogTableModel) getModel();
134 }
135
136
137 public void setDetailedView() {
138
139 TableColumnModel model = getColumnModel();
140
141 for (int f = 0; f < _numCols; f++) {
142 model.removeColumn(_tableColumns[f]);
143 }
144
145 for (int i = 0; i < _numCols; i++) {
146 model.addColumn(_tableColumns[i]);
147 }
148
149 sizeColumnsToFit(-1);
150 }
151
152 public void setView(List columns) {
153 TableColumnModel model = getColumnModel();
154
155
156 for (int f = 0; f < _numCols; f++) {
157 model.removeColumn(_tableColumns[f]);
158 }
159 Iterator selectedColumns = columns.iterator();
160 Vector columnNameAndNumber = getColumnNameAndNumber();
161 while (selectedColumns.hasNext()) {
162
163 model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
164 }
165
166
167 sizeColumnsToFit(-1);
168 }
169
170 public void setFont(Font font) {
171 super.setFont(font);
172 Graphics g = this.getGraphics();
173 if (g != null) {
174 FontMetrics fm = g.getFontMetrics(font);
175 int height = fm.getHeight();
176 _rowHeight = height + height / 3;
177 setRowHeight(_rowHeight);
178 }
179
180
181 }
182
183
184
185
186
187
188 protected void init() {
189 setRowHeight(_rowHeight);
190 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
191 }
192
193
194 protected Vector getColumnNameAndNumber() {
195 Vector columnNameAndNumber = new Vector();
196 for (int i = 0; i < _colNames.length; i++) {
197 columnNameAndNumber.add(i, _colNames[i]);
198 }
199 return columnNameAndNumber;
200 }
201
202
203
204
205
206
207
208
209
210 class LogTableListSelectionListener implements ListSelectionListener {
211 protected JTable _table;
212
213 public LogTableListSelectionListener(JTable table) {
214 _table = table;
215 }
216
217 public void valueChanged(ListSelectionEvent e) {
218
219 if (e.getValueIsAdjusting()) {
220 return;
221 }
222
223 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
224 if (lsm.isSelectionEmpty()) {
225
226 } else {
227 StringBuffer buf = new StringBuffer();
228 int selectedRow = lsm.getMinSelectionIndex();
229
230 for (int i = 0; i < _numCols - 1; i++) {
231 String value = "";
232 Object obj = _table.getModel().getValueAt(selectedRow, i);
233 if (obj != null) {
234 value = obj.toString();
235 }
236
237 buf.append(_colNames[i] + ":");
238 buf.append("\t");
239
240 if (i == _colThread || i == _colMessage || i == _colLevel) {
241 buf.append("\t");
242 }
243
244 if (i == _colDate || i == _colNDC) {
245 buf.append("\t\t");
246 }
247
248
249
250
251
252
253 buf.append(value);
254 buf.append("\n");
255 }
256 buf.append(_colNames[_numCols - 1] + ":\n");
257 Object obj = _table.getModel().getValueAt(selectedRow, _numCols - 1);
258 if (obj != null) {
259 buf.append(obj.toString());
260 }
261
262 _detailTextArea.setText(buf.toString());
263 }
264 }
265 }
266 }
267
268
269
270
271
272