View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.model;
25  
26  import java.text.SimpleDateFormat;
27  import java.util.*;
28  
29  import javax.swing.table.AbstractTableModel;
30  
31  import com.mindtree.techworks.insight.InsightConstants;
32  import com.mindtree.techworks.insight.spi.LogEvent;
33  
34  /**
35  *
36  * The <code>LogEventTableModel</code> class is a derivative of the Swing AbstractTableModel.
37  * It defines the table model used by the log viewer to display LoggingEvent summary information.
38  *
39  * @author  Regunath B
40  * @version 1.0, 04/10/25
41  * @see     javax.swing.AbstractTableModel
42  * @see     org.apache.log4j.spi.LoggingEvent
43  */
44  
45  public class LogEventTableModel extends AbstractTableModel {
46  	
47  	/**
48  	 * Used for object serialization
49  	 */
50  	private static final long serialVersionUID = -6989331565976765303L;
51  
52  	/*
53  	 * The SimpleDateFormat instance for formatting dates
54  	 */
55  	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
56  
57  	/*
58  	 * Hashmap of column names for the JTable
59  	 */
60      public static final HashMap COL_NAMES = new HashMap();
61      
62      /*
63       * Useful constants to identify the column indexes
64       */
65      public static final int FIRST_INDEX = 0;    
66      public static final int TIME_INDEX = 0;
67      public static final int THREAD_INDEX = 1;
68      public static final int PRIORITY_INDEX = 2;
69      public static final int CATEGORY_INDEX = 3;
70      public static final int MESSAGE_INDEX = 4;
71      public static final int EXCEPTION_NAME_INDEX = 5;
72      public static final int METHOD_INDEX = 6;
73      public static final int LINE_INDEX = 7;
74      public static final int FILE_INDEX = 8;
75      public static final int CLASS_INDEX = 9;
76      public static final int RELATIVETIME_INDEX = 10;
77      public static final int LAST_INDEX = 10;
78      
79      /**
80       * Static initializer block to populate the column name HashMap
81       */
82      static{
83      	COL_NAMES.put(new Integer(TIME_INDEX),InsightConstants.getLiteral("TIME_LABEL"));
84      	COL_NAMES.put(new Integer(THREAD_INDEX),InsightConstants.getLiteral("THREAD_NAME_LABEL"));
85      	COL_NAMES.put(new Integer(PRIORITY_INDEX),InsightConstants.getLiteral("PRIORITY_LABEL"));
86      	COL_NAMES.put(new Integer(CATEGORY_INDEX),InsightConstants.getLiteral("LOGGER_NAME")); 
87      	COL_NAMES.put(new Integer(MESSAGE_INDEX),InsightConstants.getLiteral("MESSAGE_LABEL")); 
88      	COL_NAMES.put(new Integer(EXCEPTION_NAME_INDEX),InsightConstants.getLiteral("EXCEPTION_LABEL")); 
89      	COL_NAMES.put(new Integer(METHOD_INDEX),InsightConstants.getLiteral("METHOD_LABEL"));
90      	COL_NAMES.put(new Integer(LINE_INDEX),InsightConstants.getLiteral("LINE_LABEL")); 
91      	COL_NAMES.put(new Integer(FILE_INDEX),InsightConstants.getLiteral("FILE_LABEL")); 
92      	COL_NAMES.put(new Integer(CLASS_INDEX),InsightConstants.getLiteral("CLASS_LABEL")); 
93      	COL_NAMES.put(new Integer(RELATIVETIME_INDEX),InsightConstants.getLiteral("RELATIVETIME_LABEL")); 
94      };    
95  	    
96  	/*
97  	 * Collection that stores the LoggingEvent instances currently rendered by
98  	 * the JTable backed by this AbstractTableModel
99  	 */
100 	private List eventList = new ArrayList();
101 
102 	/*
103 	 * The ModelDataSorter instance that does data sorting for this TableModel
104 	 */
105 	private ModelDataSorter modelDataSorter;
106 	
107 	/*
108 	 * Array of table column identifiers
109 	 */
110 	private int[] columnList;
111 	
112 	/**
113 	 * Constructor for this class 
114 	 * @param int array containing valid column identifiers defined in this class
115 	 */
116 	public LogEventTableModel(int[] columnList) {
117 		this.columnList = columnList;
118 		modelDataSorter = new ModelDataSorter();
119 	}
120 	
121 	/**
122 	 * Dynamically changes the columns displayed by this model to the ones
123 	 * specified
124 	 * @param columnList int array containing valid column identifiers defined in this class
125 	 */
126 	public void changeDisplayColumns(int[] columnList) {
127 		this.columnList = columnList;
128 		modelDataSorter.resetSortParams();
129 		super.fireTableStructureChanged();
130 	}
131 	
132 	/**
133 	 * Superclass method implementation
134 	 * @see javax.swing.table.TableModel#getRowCount()
135 	 */
136 	public int getRowCount() {
137 		return eventList.size();
138 	}
139 	
140 	/**
141 	 * Superclass method implementation
142 	 * @see javax.swing.table.TableModel#getColumnCount()
143 	 */
144 	public int getColumnCount() {
145 		return columnList.length;
146 	}
147 
148 	/**
149 	 * Superclass method implementation
150 	 * @see javax.swing.table.TableModel#getColumnName(int)
151 	 */
152     public String getColumnName(int col) {
153         return (String)COL_NAMES.get(new Integer(columnList[col]));
154     }
155 
156     /**
157      * Adds the specified LoggingEvent to the list of events rendered by the
158      * JTable backed by this AbstractTableModel
159      * @param event the LogEvent to be added to the model
160      */
161     public void addLogEvent(LogEvent event) {
162 		eventList.add(event);
163 		int startIndex = eventList.size() - 2;
164 		int endIndex = eventList.size() - 1;
165 		fireTableRowsInserted(startIndex < 0 ? 0 : startIndex, endIndex < 0 ? 0 : endIndex);
166 	}
167 
168     /**
169      * Gets the LogEvent from the model, identifed by the specified index
170      * @param index index of the LogEvent in the internal List
171      * @return LogEvent available at the specified index
172      */
173 	public LogEvent getLoggingEvent(int index) {
174 		// Need this check to take care of value-change events that fire while data is being added to this Model
175 		if (index >= eventList.size()) {
176 			return null;
177 		}
178 		return (LogEvent)eventList.get(index);
179 	}
180 
181 	/**
182 	 * Superclass method implementation
183 	 * @see javax.swing.table.TableModel#getValueAt(int, int)
184 	 */
185 	public Object getValueAt(int rowIndex, int columnIndex) {		
186 		if (rowIndex < eventList.size()) {
187 			LogEvent event = (LogEvent)eventList.get(rowIndex);
188 			switch(columnList[columnIndex]) {
189 				case TIME_INDEX:
190 		        	return DATE_FORMAT.format(new Date(event.getTimeStamp()));
191 				case THREAD_INDEX:
192 					return event.getThreadName();
193 				case PRIORITY_INDEX:
194 					return event.getLevel();
195 				case CATEGORY_INDEX:
196 					return event.getLoggerName();
197 				case MESSAGE_INDEX:
198 					return event.getMessage();
199 				case EXCEPTION_NAME_INDEX:
200 					return event.getExceptionName();
201 				case METHOD_INDEX:
202 					return event.getLocationInformation().getMethodName();
203 				case LINE_INDEX:
204 					return event.getLocationInformation().getLineNumber();
205 				case FILE_INDEX:
206 					return event.getLocationInformation().getFileName();
207 				case CLASS_INDEX:
208 					return event.getLocationInformation().getClassName();
209 				case RELATIVETIME_INDEX:
210 					return String.valueOf(event.getRelativeTime());
211 			}
212 		}
213 		return null;
214 	}
215 	
216 	/**
217 	 * Sorts the data held by this TableModel.
218 	 * @param columnIndex the index of column data that needs to be sorted
219 	 */
220 	public void sortColumn(int columnIndex) {
221 		modelDataSorter.sortData(columnIndex, this.eventList);
222 		super.fireTableDataChanged();		
223 	}
224 	
225     /**
226      * Returns the sort order for the column specified by the columnIndex
227      * @param columnIndex the index of the column whose sort order is needed
228      * @return the valid sort order as defined by the ModelDataSorter class
229      */
230     public int getSortOrder(int columnIndex) {
231     	return modelDataSorter.getSortOrder(this.columnList[columnIndex]);
232     }
233     
234     /**
235      * Clears the data contained by this TableModel. Updates the JTable
236      * backed by this TableModel.
237      */
238     public void clearModel() {
239     	eventList.clear();
240     	super.fireTableDataChanged();
241     	modelDataSorter.resetSortParams();
242     }
243 
244 	/**
245 	 * Updates this TableModel with the data specified. The data 
246 	 * maintained by this model is cleared and the ones in the specified
247 	 * List added.
248 	 * @param logEventList the List of LogEvent instances to update this model with
249 	 */
250 	public void updateDataModel(List logEventList) {
251     	eventList.clear();
252     	modelDataSorter.resetSortParams();
253     	// add the log events from the supplied list to the list maintained by this 
254     	// log event model
255     	for (int i = 0; i < logEventList.size(); i++) {
256     		this.eventList.add(logEventList.get(i));
257     	}
258 		/*
259 		 * Note: 
260 		 * super.fireTableDataChanged(); is intentionally not being called
261 		 * because this method is called in response to a page change where
262 		 * the table is anyway re-inited.
263 		 */
264 	}    
265 	
266 	/**
267 	 * Updates this TableModel and the display with the data specified. The data 
268 	 * maintained by this model is cleared and the ones in the specified
269 	 * List added.
270 	 * @param logEventList the List of LogEvent instances to display
271 	 */
272 	public void updateDataModelAndDisplay(List logEventList) {
273 		this.updateDataModel(logEventList);
274 		super.fireTableDataChanged();
275 	}
276 	
277 	/**
278 	 * @return Returns the columnList.
279 	 */
280 	public int[] getColumnList() {
281 		return columnList;
282 	}
283 	/**
284 	 * @param columnList The columnList to set.
285 	 */
286 	public void setColumnList(int[] columnList) {
287 		this.columnList = columnList;
288 	}
289 }