View Javadoc

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   */
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  // Contributed by ThoughtWorks Inc.
42  
43  public class LogTable extends JTable {
44    private static final long serialVersionUID = 4867085140195148458L;
45    //--------------------------------------------------------------------------
46    //   Constants:
47    //--------------------------------------------------------------------------
48  
49    //--------------------------------------------------------------------------
50    //   Protected Variables:
51    //--------------------------------------------------------------------------
52    protected int _rowHeight = 30;
53    protected JTextArea _detailTextArea;
54  
55    // For the columns:
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    //   Private Variables:
74    //--------------------------------------------------------------------------
75  
76    //--------------------------------------------------------------------------
77    //   Constructors:
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     //setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
104   }
105 
106   //--------------------------------------------------------------------------
107   //   Public Methods:
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     //For JDK1.3
126     //((DefaultTableModel)getModel()).setRowCount(0);
127 
128     // For JDK1.2.x
129     getFilteredLogTableModel().clear();
130   }
131 
132   public FilteredLogTableModel getFilteredLogTableModel() {
133     return (FilteredLogTableModel) getModel();
134   }
135 
136   // default view if a view is not set and saved
137   public void setDetailedView() {
138     //TODO: Defineable Views.
139     TableColumnModel model = getColumnModel();
140     // Remove all the columns:
141     for (int f = 0; f < _numCols; f++) {
142       model.removeColumn(_tableColumns[f]);
143     }
144     // Add them back in the correct order:
145     for (int i = 0; i < _numCols; i++) {
146       model.addColumn(_tableColumns[i]);
147     }
148     //SWING BUG:
149     sizeColumnsToFit(-1);
150   }
151 
152   public void setView(List columns) {
153     TableColumnModel model = getColumnModel();
154 
155     // Remove all the columns:
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       // add the column to the view
163       model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
164     }
165 
166     //SWING BUG:
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   //   Protected Methods:
186   //--------------------------------------------------------------------------
187 
188   protected void init() {
189     setRowHeight(_rowHeight);
190     setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
191   }
192 
193   // assign a column number to a column name
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   //   Private Methods:
204   //--------------------------------------------------------------------------
205 
206   //--------------------------------------------------------------------------
207   //   Nested Top-Level Classes or Interfaces:
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       //Ignore extra messages.
219       if (e.getValueIsAdjusting()) {
220         return;
221       }
222 
223       ListSelectionModel lsm = (ListSelectionModel) e.getSource();
224       if (lsm.isSelectionEmpty()) {
225         //no rows are selected
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"); // pad out the date.
242           }
243 
244           if (i == _colDate || i == _colNDC) {
245             buf.append("\t\t"); // pad out the date.
246           }
247 
248 //               if( i == _colSequence)
249 //               {
250 //                  buf.append("\t\t\t"); // pad out the Sequnce.
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