001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.log4j.lf5.viewer; 018 019import java.awt.Font; 020import java.awt.FontMetrics; 021import java.awt.Graphics; 022import java.util.Enumeration; 023import java.util.Iterator; 024import java.util.List; 025import java.util.Vector; 026 027import javax.swing.JTable; 028import javax.swing.JTextArea; 029import javax.swing.ListSelectionModel; 030import javax.swing.event.ListSelectionEvent; 031import javax.swing.event.ListSelectionListener; 032import javax.swing.table.TableColumn; 033import javax.swing.table.TableColumnModel; 034 035import org.apache.log4j.lf5.util.DateFormatManager; 036 037/** 038 * LogTable. 039 * 040 * @author Michael J. Sikorsky 041 * @author Robert Shaw 042 * @author Brad Marlborough 043 * @author Brent Sprecher 044 */ 045 046// Contributed by ThoughtWorks Inc. 047 048public class LogTable extends JTable { 049 private static final long serialVersionUID = 4867085140195148458L; 050 //-------------------------------------------------------------------------- 051 // Constants: 052 //-------------------------------------------------------------------------- 053 054 //-------------------------------------------------------------------------- 055 // Protected Variables: 056 //-------------------------------------------------------------------------- 057 protected int _rowHeight = 30; 058 protected JTextArea _detailTextArea; 059 060 // For the columns: 061 protected int _numCols = 9; 062 protected TableColumn[] _tableColumns = new TableColumn[_numCols]; 063 protected int[] _colWidths = {40, 40, 40, 70, 70, 360, 440, 200, 60}; 064 protected LogTableColumn[] _colNames = LogTableColumn.getLogTableColumnArray(); 065 protected int _colDate = 0; 066 protected int _colThread = 1; 067 protected int _colMessageNum = 2; 068 protected int _colLevel = 3; 069 protected int _colNDC = 4; 070 protected int _colCategory = 5; 071 protected int _colMessage = 6; 072 protected int _colLocation = 7; 073 protected int _colThrown = 8; 074 075 protected DateFormatManager _dateFormatManager = null; 076 077 //-------------------------------------------------------------------------- 078 // Private Variables: 079 //-------------------------------------------------------------------------- 080 081 //-------------------------------------------------------------------------- 082 // Constructors: 083 //-------------------------------------------------------------------------- 084 085 public LogTable(JTextArea detailTextArea) { 086 super(); 087 088 init(); 089 090 _detailTextArea = detailTextArea; 091 092 setModel(new FilteredLogTableModel()); 093 094 Enumeration columns = getColumnModel().getColumns(); 095 int i = 0; 096 while (columns.hasMoreElements()) { 097 TableColumn col = (TableColumn) columns.nextElement(); 098 col.setCellRenderer(new LogTableRowRenderer()); 099 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