PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/src/main/java/org/apache/log4j/lf5/viewer/LogTable.java

#
Java | 277 lines | 147 code | 55 blank | 75 comment | 26 complexity | d24d8d158be3fd1dd8c0d769e30cf46d MD5 | raw file
Possible License(s): Apache-2.0
  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. import java.awt.Font;
  19. import java.awt.FontMetrics;
  20. import java.awt.Graphics;
  21. import java.util.Enumeration;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Vector;
  25. import javax.swing.JTable;
  26. import javax.swing.JTextArea;
  27. import javax.swing.ListSelectionModel;
  28. import javax.swing.event.ListSelectionEvent;
  29. import javax.swing.event.ListSelectionListener;
  30. import javax.swing.table.TableColumn;
  31. import javax.swing.table.TableColumnModel;
  32. import org.apache.log4j.lf5.util.DateFormatManager;
  33. /**
  34. * LogTable.
  35. *
  36. * @author Michael J. Sikorsky
  37. * @author Robert Shaw
  38. * @author Brad Marlborough
  39. * @author Brent Sprecher
  40. */
  41. // Contributed by ThoughtWorks Inc.
  42. public class LogTable extends JTable {
  43. private static final long serialVersionUID = 4867085140195148458L;
  44. //--------------------------------------------------------------------------
  45. // Constants:
  46. //--------------------------------------------------------------------------
  47. //--------------------------------------------------------------------------
  48. // Protected Variables:
  49. //--------------------------------------------------------------------------
  50. protected int _rowHeight = 30;
  51. protected JTextArea _detailTextArea;
  52. // For the columns:
  53. protected int _numCols = 9;
  54. protected TableColumn[] _tableColumns = new TableColumn[_numCols];
  55. protected int[] _colWidths = {40, 40, 40, 70, 70, 360, 440, 200, 60};
  56. protected LogTableColumn[] _colNames = LogTableColumn.getLogTableColumnArray();
  57. protected int _colDate = 0;
  58. protected int _colThread = 1;
  59. protected int _colMessageNum = 2;
  60. protected int _colLevel = 3;
  61. protected int _colNDC = 4;
  62. protected int _colCategory = 5;
  63. protected int _colMessage = 6;
  64. protected int _colLocation = 7;
  65. protected int _colThrown = 8;
  66. protected DateFormatManager _dateFormatManager = null;
  67. //--------------------------------------------------------------------------
  68. // Private Variables:
  69. //--------------------------------------------------------------------------
  70. //--------------------------------------------------------------------------
  71. // Constructors:
  72. //--------------------------------------------------------------------------
  73. public LogTable(JTextArea detailTextArea) {
  74. super();
  75. init();
  76. _detailTextArea = detailTextArea;
  77. setModel(new FilteredLogTableModel());
  78. Enumeration columns = getColumnModel().getColumns();
  79. int i = 0;
  80. while (columns.hasMoreElements()) {
  81. TableColumn col = (TableColumn) columns.nextElement();
  82. col.setCellRenderer(new LogTableRowRenderer());
  83. col.setPreferredWidth(_colWidths[i]);
  84. _tableColumns[i] = col;
  85. i++;
  86. }
  87. ListSelectionModel rowSM = getSelectionModel();
  88. rowSM.addListSelectionListener(new LogTableListSelectionListener(this));
  89. //setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  90. }
  91. //--------------------------------------------------------------------------
  92. // Public Methods:
  93. //--------------------------------------------------------------------------
  94. /**
  95. * Get the DateFormatManager for formatting dates.
  96. */
  97. public DateFormatManager getDateFormatManager() {
  98. return _dateFormatManager;
  99. }
  100. /**
  101. * Set the date format manager for formatting dates.
  102. */
  103. public void setDateFormatManager(DateFormatManager dfm) {
  104. _dateFormatManager = dfm;
  105. }
  106. public synchronized void clearLogRecords() {
  107. //For JDK1.3
  108. //((DefaultTableModel)getModel()).setRowCount(0);
  109. // For JDK1.2.x
  110. getFilteredLogTableModel().clear();
  111. }
  112. public FilteredLogTableModel getFilteredLogTableModel() {
  113. return (FilteredLogTableModel) getModel();
  114. }
  115. // default view if a view is not set and saved
  116. public void setDetailedView() {
  117. //TODO: Defineable Views.
  118. TableColumnModel model = getColumnModel();
  119. // Remove all the columns:
  120. for (int f = 0; f < _numCols; f++) {
  121. model.removeColumn(_tableColumns[f]);
  122. }
  123. // Add them back in the correct order:
  124. for (int i = 0; i < _numCols; i++) {
  125. model.addColumn(_tableColumns[i]);
  126. }
  127. //SWING BUG:
  128. sizeColumnsToFit(-1);
  129. }
  130. public void setView(List columns) {
  131. TableColumnModel model = getColumnModel();
  132. // Remove all the columns:
  133. for (int f = 0; f < _numCols; f++) {
  134. model.removeColumn(_tableColumns[f]);
  135. }
  136. Iterator selectedColumns = columns.iterator();
  137. Vector columnNameAndNumber = getColumnNameAndNumber();
  138. while (selectedColumns.hasNext()) {
  139. // add the column to the view
  140. model.addColumn(_tableColumns[columnNameAndNumber.indexOf(selectedColumns.next())]);
  141. }
  142. //SWING BUG:
  143. sizeColumnsToFit(-1);
  144. }
  145. public void setFont(Font font) {
  146. super.setFont(font);
  147. Graphics g = this.getGraphics();
  148. if (g != null) {
  149. FontMetrics fm = g.getFontMetrics(font);
  150. int height = fm.getHeight();
  151. _rowHeight = height + height / 3;
  152. setRowHeight(_rowHeight);
  153. }
  154. }
  155. //--------------------------------------------------------------------------
  156. // Protected Methods:
  157. //--------------------------------------------------------------------------
  158. protected void init() {
  159. setRowHeight(_rowHeight);
  160. setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  161. }
  162. // assign a column number to a column name
  163. protected Vector getColumnNameAndNumber() {
  164. Vector columnNameAndNumber = new Vector();
  165. for (int i = 0; i < _colNames.length; i++) {
  166. columnNameAndNumber.add(i, _colNames[i]);
  167. }
  168. return columnNameAndNumber;
  169. }
  170. //--------------------------------------------------------------------------
  171. // Private Methods:
  172. //--------------------------------------------------------------------------
  173. //--------------------------------------------------------------------------
  174. // Nested Top-Level Classes or Interfaces:
  175. //--------------------------------------------------------------------------
  176. class LogTableListSelectionListener implements ListSelectionListener {
  177. protected JTable _table;
  178. public LogTableListSelectionListener(JTable table) {
  179. _table = table;
  180. }
  181. public void valueChanged(ListSelectionEvent e) {
  182. //Ignore extra messages.
  183. if (e.getValueIsAdjusting()) {
  184. return;
  185. }
  186. ListSelectionModel lsm = (ListSelectionModel) e.getSource();
  187. if (lsm.isSelectionEmpty()) {
  188. //no rows are selected
  189. } else {
  190. StringBuffer buf = new StringBuffer();
  191. int selectedRow = lsm.getMinSelectionIndex();
  192. for (int i = 0; i < _numCols - 1; i++) {
  193. String value = "";
  194. Object obj = _table.getModel().getValueAt(selectedRow, i);
  195. if (obj != null) {
  196. value = obj.toString();
  197. }
  198. buf.append(_colNames[i] + ":");
  199. buf.append("\t");
  200. if (i == _colThread || i == _colMessage || i == _colLevel) {
  201. buf.append("\t"); // pad out the date.
  202. }
  203. if (i == _colDate || i == _colNDC) {
  204. buf.append("\t\t"); // pad out the date.
  205. }
  206. // if( i == _colSequence)
  207. // {
  208. // buf.append("\t\t\t"); // pad out the Sequnce.
  209. // }
  210. buf.append(value);
  211. buf.append("\n");
  212. }
  213. buf.append(_colNames[_numCols - 1] + ":\n");
  214. Object obj = _table.getModel().getValueAt(selectedRow, _numCols - 1);
  215. if (obj != null) {
  216. buf.append(obj.toString());
  217. }
  218. _detailTextArea.setText(buf.toString());
  219. }
  220. }
  221. }
  222. }