1 16 package org.apache.log4j.lf5.viewer; 17 18 import org.apache.log4j.lf5.util.DateFormatManager; 19 20 import javax.swing.*; 21 import javax.swing.event.ListSelectionEvent ; 22 import javax.swing.event.ListSelectionListener ; 23 import javax.swing.table.TableColumn ; 24 import javax.swing.table.TableColumnModel ; 25 import java.awt.*; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Vector ; 30 31 39 40 42 public class LogTable extends JTable { 43 47 protected int _rowHeight = 30; 51 protected JTextArea _detailTextArea; 52 53 protected int _numCols = 9; 55 protected TableColumn [] _tableColumns = new TableColumn [_numCols]; 56 protected int[] _colWidths = {40, 40, 40, 70, 70, 360, 440, 200, 60}; 57 protected LogTableColumn[] _colNames = LogTableColumn.getLogTableColumnArray(); 58 protected int _colDate = 0; 59 protected int _colThread = 1; 60 protected int _colMessageNum = 2; 61 protected int _colLevel = 3; 62 protected int _colNDC = 4; 63 protected int _colCategory = 5; 64 protected int _colMessage = 6; 65 protected int _colLocation = 7; 66 protected int _colThrown = 8; 67 68 protected DateFormatManager _dateFormatManager = null; 69 70 74 78 public LogTable(JTextArea detailTextArea) { 79 super(); 80 81 init(); 82 83 _detailTextArea = detailTextArea; 84 85 setModel(new FilteredLogTableModel()); 86 87 Enumeration columns = getColumnModel().getColumns(); 88 int i = 0; 89 while (columns.hasMoreElements()) { 90 TableColumn col = (TableColumn ) columns.nextElement(); 91 col.setCellRenderer(new LogTableRowRenderer()); 92 col.setPreferredWidth(_colWidths[i]); 93 94 _tableColumns[i] = col; 95 i++; 96 } 97 98 ListSelectionModel rowSM = getSelectionModel(); 99 rowSM.addListSelectionListener(new LogTableListSelectionListener(this)); 100 101 } 103 104 108 111 public DateFormatManager getDateFormatManager() { 112 return _dateFormatManager; 113 } 114 115 118 public void setDateFormatManager(DateFormatManager dfm) { 119 _dateFormatManager = dfm; 120 } 121 122 public synchronized void clearLogRecords() { 123 126 getFilteredLogTableModel().clear(); 128 } 129 130 public FilteredLogTableModel getFilteredLogTableModel() { 131 return (FilteredLogTableModel) getModel(); 132 } 133 134 public void setDetailedView() { 136 TableColumnModel model = getColumnModel(); 138 for (int f = 0; f < _numCols; f++) { 140 model.removeColumn(_tableColumns[f]); 141 } 142 for (int i = 0; i < _numCols; i++) { 144 model.addColumn(_tableColumns[i]); 145 } 146 sizeColumnsToFit(-1); 148 } 149 150 public void setView(List columns) { 151 TableColumnModel model = getColumnModel(); 152 153 for (int f = 0; f < _numCols; f++) { 155 model.removeColumn(_tableColumns[f]); 156 } 157 Iterator selectedColumns = columns.iterator(); 158 Vector columnNameAndNumber = getColumnNameAndNumber(); 159 while (selectedColumns.hasNext()) { 160 model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]); 162 } 163 164 sizeColumnsToFit(-1); 166 } 167 168 public void setFont(Font font) { 169 super.setFont(font); 170 Graphics g = this.getGraphics(); 171 if (g != null) { 172 FontMetrics fm = g.getFontMetrics(font); 173 int height = fm.getHeight(); 174 _rowHeight = height + height / 3; 175 setRowHeight(_rowHeight); 176 } 177 178 179 } 180 181 182 186 protected void init() { 187 setRowHeight(_rowHeight); 188 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 189 } 190 191 protected Vector getColumnNameAndNumber() { 193 Vector columnNameAndNumber = new Vector (); 194 for (int i = 0; i < _colNames.length; i++) { 195 columnNameAndNumber.add(i, _colNames[i]); 196 } 197 return columnNameAndNumber; 198 } 199 200 204 208 class LogTableListSelectionListener implements ListSelectionListener { 209 protected JTable _table; 210 211 public LogTableListSelectionListener(JTable table) { 212 _table = table; 213 } 214 215 public void valueChanged(ListSelectionEvent e) { 216 if (e.getValueIsAdjusting()) { 218 return; 219 } 220 221 ListSelectionModel lsm = (ListSelectionModel) e.getSource(); 222 if (lsm.isSelectionEmpty()) { 223 } else { 225 StringBuffer buf = new StringBuffer (); 226 int selectedRow = lsm.getMinSelectionIndex(); 227 228 for (int i = 0; i < _numCols - 1; i++) { 229 String value = ""; 230 Object obj = _table.getModel().getValueAt(selectedRow, i); 231 if (obj != null) { 232 value = obj.toString(); 233 } 234 235 buf.append(_colNames[i] + ":"); 236 buf.append("\t"); 237 238 if (i == _colThread || i == _colMessage || i == _colLevel) { 239 buf.append("\t"); } 241 242 if (i == _colDate || i == _colNDC) { 243 buf.append("\t\t"); } 245 246 251 buf.append(value); 252 buf.append("\n"); 253 } 254 buf.append(_colNames[_numCols - 1] + ":\n"); 255 Object obj = _table.getModel().getValueAt(selectedRow, _numCols - 1); 256 if (obj != null) { 257 buf.append(obj.toString()); 258 } 259 260 _detailTextArea.setText(buf.toString()); 261 } 262 } 263 } 264 } 265 266 267 268 269 270 271 | Popular Tags |