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.util.*;
27  
28  import com.mindtree.techworks.insight.spi.LogEvent;
29  
30  /**
31  *
32  * The <code>ModelDataSorter</code> class implements sorting of data held by the
33  * TableModel that it refers to. Provides methods to invoke sorting by specifying
34  * the column index and the data list. Maintains map of sorted columns and their sorting
35  * order such that consecutive calls to sort on a column flips the sorting order.
36  *
37  * @author  Regunath B
38  * @version 1.0, 04/11/03
39  * @see     org.apache.log4j.spi.LoggingEvent
40  */
41  
42  public class ModelDataSorter implements Comparator {
43  
44      /*
45       * Useful constants to identify order of sorting
46       */
47      public static final int ASCENDING = 10;
48      public static final int DESCENDING = 20;
49      public static final int NOTSORTED = 30;
50  
51  	/*
52  	 * Map containing indexes of columns that have been sorted and the order
53  	 */
54  	private Map columnSortMap = new HashMap();
55  
56  
57      /*
58       * Index of column that is being sorted
59       */
60      private int sortColumn = -1;
61  
62  	/*
63  	 * Indicator that says if the sorting is asecinding or descending
64  	 */
65  	private boolean isAscending = true;
66  
67      /**
68       * Sorts the specified column data in the specified list. Toggles the sort
69       * order if the column had been sorted previously.
70       * Does nothing if this sorter is already sorting data
71       * @param columnIndex index of the column whose data needs to be sorted
72       * @param data List containing data to be sorted
73       */
74      public void sortData(int columnIndex, List data) {
75      	if (this.sortColumn > -1) {
76      		return;
77      	}
78      	this.sortColumn = columnIndex;
79      	String columnKey = String.valueOf(columnIndex);
80      	Object columnSortOrder = columnSortMap.get(columnKey);
81      	if (columnSortOrder != null) {
82      		this.isAscending = !(((Integer)columnSortOrder).intValue() == ASCENDING);
83      	}
84      	Collections.sort(data, this);
85  		columnSortMap.put(columnKey,this.isAscending ? new Integer(ASCENDING) : new Integer(DESCENDING));
86  		// reset the sort order of all the other columns to NOTSORTED
87  		Iterator iterator = columnSortMap.keySet().iterator();
88  		while(iterator.hasNext()) {
89  			String key = (String)iterator.next();
90  			if (!key.equals(columnKey)) {
91  				columnSortMap.put(key, new Integer(NOTSORTED));
92  			}
93  		}
94  		this.sortColumn = -1;
95      }
96  
97      /**
98       * Returns the sort order for the column specified by the columnIndex
99       * @param columnIndex the index of the column whose sort order is needed
100      * @return the valid sort order as defined by this class
101      */
102     public int getSortOrder(int columnIndex) {
103     	int result = NOTSORTED;
104     	String columnKey = String.valueOf(columnIndex);
105     	Object columnSortOrder = columnSortMap.get(columnKey);
106     	if (columnSortOrder != null) {
107     		return ((Integer)columnSortOrder).intValue();
108     	}
109     	return result;
110     }
111 
112 	/**
113 	 * Comparator interface method implementation
114 	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
115 	 */
116 	public int compare(Object arg0, Object arg1) {
117 		int result = 0;
118 		LogEvent le1 = (LogEvent)arg0;
119 		LogEvent le2 = (LogEvent)arg1;
120 		switch(this.sortColumn) {
121 			case LogEventTableModel.CATEGORY_INDEX:
122 				result = le1.getLoggerName().compareTo(le2.getLoggerName());
123 				break;
124 			case LogEventTableModel.EXCEPTION_NAME_INDEX:
125 				result = le1.getExceptionName().compareTo(le2.getExceptionName());
126 				break;
127 			case LogEventTableModel.MESSAGE_INDEX:
128 				result = le1.getMessage().toString().compareTo(le2.getMessage().toString());
129 				break;
130 			case LogEventTableModel.PRIORITY_INDEX:
131 				result = le1.getLevel().toInt() - le2.getLevel().toInt();
132 				break;
133 			case LogEventTableModel.THREAD_INDEX:
134 				result = le1.getThreadName().compareTo(le2.getThreadName());
135 				break;
136 			case LogEventTableModel.TIME_INDEX:
137 				result = (int)(le1.getTimeStamp() - le2.getTimeStamp());
138 				break;
139 			case LogEventTableModel.RELATIVETIME_INDEX:
140 				result = (int)(le1.getRelativeTime() - le2.getRelativeTime());
141 				break;
142 			case LogEventTableModel.METHOD_INDEX:
143 				if (le1.getLocationInformation().getMethodName() == null || le2.getLocationInformation().getMethodName() == null) {
144 					result = 0;
145 					break;
146 				}
147 				result = le1.getLocationInformation().getMethodName().compareTo(le2.getLocationInformation().getMethodName());
148 				break;
149 			case LogEventTableModel.LINE_INDEX:
150 				if (le1.getLocationInformation().getLineNumber() == null || le2.getLocationInformation().getLineNumber() == null) {
151 					result = 0;
152 					break;
153 				}
154 				result = le1.getLocationInformation().getLineNumber().compareTo(le2.getLocationInformation().getLineNumber());
155 				break;
156 			case LogEventTableModel.FILE_INDEX:
157 				if (le1.getLocationInformation().getFileName() == null || le2.getLocationInformation().getFileName() == null) {
158 					result = 0;
159 					break;
160 				}				
161 				result = le1.getLocationInformation().getFileName().compareTo(le2.getLocationInformation().getFileName());
162 				break;
163 			case LogEventTableModel.CLASS_INDEX:
164 				if (le1.getLocationInformation().getClassName() == null || le2.getLocationInformation().getClassName() == null) {
165 					result = 0;
166 					break;
167 				}				
168 				result = le1.getLocationInformation().getClassName().compareTo(le2.getLocationInformation().getClassName());
169 				break;
170 		}
171 		return (this.isAscending ? result : (-1)*result);
172 	}
173 	
174 	/**
175 	 * Resets the parameters that define the sorting of this ModelDataSorter
176 	 */
177 	public void resetSortParams() {
178 		this.isAscending = true;
179 		this.sortColumn = -1;
180 		columnSortMap.clear();
181 	}
182 
183 }