KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > ProfileResultTableModel


1 /*
2  * Copyright (C) Jahia Ltd. All rights reserved.
3  *
4  * This software is published under the terms of the Jahia Open Software
5  * License version 1.1, a copy of which has been included with this
6  * distribution in the LICENSE.txt file.
7  */

8 package org.jahia.sqlprofiler.gui;
9
10 import javax.swing.table.*;
11 import java.util.*;
12 import java.text.*;
13 import java.awt.event.MouseAdapter JavaDoc;
14 import java.awt.event.MouseEvent JavaDoc;
15 import java.awt.event.InputEvent JavaDoc;
16 import javax.swing.JTable JavaDoc;
17
18 /**
19  * <p>Title: SQL Profiler</p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2003</p>
22  * <p>Company: Jahia Ltd</p>
23  * @author Serge Huber
24  * @version 1.0
25  */

26
27 public class ProfileResultTableModel extends AbstractTableModel
28     implements Comparator {
29
30     private ArrayList profileResults = new ArrayList();
31     protected int currCol = 0;
32     protected Vector ascendCol = new Vector(); // this vector stores the state (ascending or descending) of each column
33
protected Integer JavaDoc one = new Integer JavaDoc(1);
34     protected Integer JavaDoc minusOne = new Integer JavaDoc( -1);
35
36     private static final String JavaDoc[] COLUMN_NAMES = {
37         "%", "Time[ms]", "Count", "Table(s)", "Column(s)"};
38
39     public ProfileResultTableModel() {
40         for (int i=0; i < COLUMN_NAMES.length; i++) {
41             ascendCol.add(one);
42         }
43     }
44
45     public int getRowCount() {
46         return profileResults.size();
47     }
48
49     public int getColumnCount() {
50         return COLUMN_NAMES.length;
51     }
52
53     public String JavaDoc getColumnName(int aCol) {
54         // does not need to be synchronized
55
return COLUMN_NAMES[aCol];
56     }
57
58     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
59         ProfileReportResult curResult = (ProfileReportResult) profileResults.
60             get(rowIndex);
61         NumberFormat nf = NumberFormat.getInstance();
62         nf.setMaximumFractionDigits(2);
63         Object JavaDoc result = null;
64         switch (columnIndex) {
65             case 0:
66                 result = nf.format(curResult.getPercentage());
67                 break;
68             case 1:
69                 result = new Long JavaDoc(curResult.getTotalElapsedTime());
70                 break;
71             case 2:
72                 result = new Integer JavaDoc(curResult.getOccurences());
73                 break;
74             case 3:
75                 StringBuffer JavaDoc resultBuf = new StringBuffer JavaDoc();
76                 Iterator tableNameIter = curResult.getTableNames().iterator();
77                 while (tableNameIter.hasNext()) {
78                     String JavaDoc curTableName = (String JavaDoc) tableNameIter.next();
79                     resultBuf.append(curTableName);
80                     if (tableNameIter.hasNext()) {
81                         resultBuf.append(",");
82                     }
83                 }
84                 result = resultBuf.toString();
85                 break;
86             case 4:
87                 StringBuffer JavaDoc resultBuf2 = new StringBuffer JavaDoc();
88                 Iterator columnNameIter = curResult.getColumnNames().iterator();
89                 while (columnNameIter.hasNext()) {
90                     String JavaDoc curColumnName = (String JavaDoc) columnNameIter.next();
91                     resultBuf2.append(curColumnName);
92                     if (columnNameIter.hasNext()) {
93                         resultBuf2.append(",");
94                     }
95                 }
96                 result = resultBuf2.toString();
97                 break;
98         }
99         return result;
100     }
101
102     public void clear() {
103         profileResults.clear();
104         currCol = 0;
105         fireTableDataChanged();
106     }
107
108     public void addProfileReportResult(ProfileReportResult profileReportResult) {
109         profileResults.add(profileReportResult);
110     }
111
112     /*
113      * This method is the implementation of the Comparator interface.
114      * It is used for sorting the rows
115      */

116     public int compare(Object JavaDoc v1, Object JavaDoc v2) {
117
118         // the comparison is between 2 vectors, each representing a row
119
// the comparison is done between 2 objects from the different rows that are in the column that is being sorted
120

121         int ascending = ( (Integer JavaDoc) ascendCol.get(currCol)).intValue();
122         if (v1 == null && v2 == null) {
123             return 0;
124         } else if (v2 == null) { // Define null less than everything.
125
return 1 * ascending;
126         } else if (v1 == null) {
127             return -1 * ascending;
128         }
129
130         ProfileReportResult left = (ProfileReportResult) v1;
131         ProfileReportResult right = (ProfileReportResult) v2;
132
133         Object JavaDoc o1 = null;
134         Object JavaDoc o2 = null;
135         switch (currCol) {
136             case 0:
137                 o1 = new Double JavaDoc(left.getPercentage());
138                 o2 = new Double JavaDoc(right.getPercentage());
139                 break;
140             case 1:
141                 o1 = new Long JavaDoc(left.getTotalElapsedTime());
142                 o2 = new Long JavaDoc(right.getTotalElapsedTime());
143                 break;
144             case 2:
145                 o1 = new Integer JavaDoc(left.getOccurences());
146                 o2 = new Integer JavaDoc(right.getOccurences());
147                 break;
148             case 3:
149                 o1 = left.getTableNames();
150                 o2 = right.getTableNames();
151                 break;
152             case 4:
153                 o1 = left.getColumnNames();
154                 o2 = right.getColumnNames();
155                 break;
156         }
157
158         // If both values are null, return 0.
159
if (o1 == null && o2 == null) {
160             return 0;
161         } else if (o2 == null) { // Define null less than everything.
162
return 1 * ascending;
163         } else if (o1 == null) {
164             return -1 * ascending;
165         }
166
167         if (o1 instanceof Number JavaDoc && o2 instanceof Number JavaDoc) {
168             Number JavaDoc n1 = (Number JavaDoc) o1;
169             double d1 = n1.doubleValue();
170             Number JavaDoc n2 = (Number JavaDoc) o2;
171             double d2 = n2.doubleValue();
172
173             if (d1 == d2) {
174                 return 0;
175             } else if (d1 > d2) {
176                 return 1 * ascending;
177             } else {
178                 return -1 * ascending;
179             }
180
181         } else if (o1 instanceof Boolean JavaDoc && o2 instanceof Boolean JavaDoc) {
182             Boolean JavaDoc bool1 = (Boolean JavaDoc) o1;
183             boolean b1 = bool1.booleanValue();
184             Boolean JavaDoc bool2 = (Boolean JavaDoc) o2;
185             boolean b2 = bool2.booleanValue();
186
187             if (b1 == b2) {
188                 return 0;
189             } else if (b1) {
190                 return 1 * ascending;
191             } else {
192                 return -1 * ascending;
193             }
194
195         } else {
196             // default case
197
if (o1 instanceof Comparable JavaDoc && o2 instanceof Comparable JavaDoc) {
198                 Comparable JavaDoc c1 = (Comparable JavaDoc) o1;
199                 Comparable JavaDoc c2 = (Comparable JavaDoc) o2; // superflous cast, no need for it!
200

201                 try {
202                     return c1.compareTo(c2) * ascending;
203                 } catch (ClassCastException JavaDoc cce) {
204                     // forget it... we'll deal with them like 2 normal objects below.
205
}
206             }
207
208             String JavaDoc s1 = o1.toString();
209             String JavaDoc s2 = o2.toString();
210             return s1.compareTo(s2) * ascending;
211         }
212     }
213
214     /*
215      * This method sorts the rows using Java's Collections class.
216      * After sorting, it changes the state of the column -
217      * if the column was ascending, its new state is descending, and vice versa.
218      */

219     public void sort() {
220         Collections.sort(profileResults, this);
221         Integer JavaDoc val = (Integer JavaDoc) ascendCol.get(currCol);
222         ascendCol.remove(currCol);
223         if (val.equals(one)) // change the state of the column
224
ascendCol.add(currCol, minusOne);
225         else
226             ascendCol.add(currCol, one);
227     }
228
229     public void sortByColumn(int column) {
230         this.currCol = column;
231         sort();
232         fireTableDataChanged();
233     }
234
235     // Add a mouse listener to the Table to trigger a table sort
236
// when a column heading is clicked in the JTable.
237
public void addMouseListenerToHeaderInTable(JTable JavaDoc table) {
238         final ProfileResultTableModel sorter = this;
239         final JTable JavaDoc tableView = table;
240         tableView.setColumnSelectionAllowed(false);
241         MouseAdapter JavaDoc listMouseListener = new MouseAdapter JavaDoc() {
242             public void mouseClicked(MouseEvent JavaDoc e) {
243                 TableColumnModel columnModel = tableView.getColumnModel();
244                 int viewColumn = columnModel.getColumnIndexAtX(e.getX());
245                 int column = tableView.convertColumnIndexToModel(viewColumn);
246                 if (e.getClickCount() == 1 && column != -1) {
247                     int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
248                     boolean ascending = (shiftPressed == 0);
249                     sorter.sortByColumn(column);
250                 }
251             }
252         };
253         JTableHeader th = tableView.getTableHeader();
254         th.addMouseListener(listMouseListener);
255     }
256
257 }
Popular Tags