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 java.awt.Font;
20 import java.awt.FontMetrics;
21 import java.awt.Graphics;
22 import java.util.Enumeration;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Vector;
26
27 import javax.swing.JTable;
28 import javax.swing.JTextArea;
29 import javax.swing.ListSelectionModel;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
32 import javax.swing.table.TableColumn;
33 import javax.swing.table.TableColumnModel;
34
35 import org.apache.log4j.lf5.util.DateFormatManager;
36
37
38
39
40
41
42
43
44
45
46
47
48 public class LogTable extends JTable {
49 private static final long serialVersionUID = 4867085140195148458L;
50
51
52
53
54
55
56
57 protected int _rowHeight = 30;
58 protected JTextArea _detailTextArea;
59
60
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
79
80
81
82
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
109 }
110
111
112
113
114
115
116
117
118 public DateFormatManager getDateFormatManager() {
119 return _dateFormatManager;
120 }
121
122
123
124
125 public void setDateFormatManager(DateFormatManager dfm) {
126 _dateFormatManager = dfm;
127 }
128
129 public synchronized void clearLogRecords() {
130
131
132
133
134 getFilteredLogTableModel().clear();
135 }
136
137 public FilteredLogTableModel getFilteredLogTableModel() {
138 return (FilteredLogTableModel) getModel();
139 }
140
141
142 public void setDetailedView() {
143
144 TableColumnModel model = getColumnModel();
145
146 for (int f = 0; f < _numCols; f++) {
147 model.removeColumn(_tableColumns[f]);
148 }
149
150 for (int i = 0; i < _numCols; i++) {
151 model.addColumn(_tableColumns[i]);
152 }
153
154 sizeColumnsToFit(-1);
155 }
156
157 public void setView(List columns) {
158 TableColumnModel model = getColumnModel();
159
160
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
168 model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
169 }
170
171
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
191
192
193 protected void init() {
194 setRowHeight(_rowHeight);
195 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
196 }
197
198
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
209
210
211
212
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
224 if (e.getValueIsAdjusting()) {
225 return;
226 }
227
228 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
229 if (lsm.isSelectionEmpty()) {
230
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");
247 }
248
249 if (i == _colDate || i == _colNDC) {
250 buf.append("\t\t");
251 }
252
253
254
255
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