|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Definitions for the class StatisticView |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.s60tools.analyzetool.ui.statistic; |
|
19 |
|
20 import java.util.AbstractList; |
|
21 import java.util.ArrayList; |
|
22 import java.util.Iterator; |
|
23 |
|
24 import org.eclipse.swt.SWT; |
|
25 import org.eclipse.swt.widgets.Event; |
|
26 import org.eclipse.swt.widgets.Listener; |
|
27 import org.eclipse.swt.widgets.TableItem; |
|
28 import org.eclipse.swt.widgets.Table; |
|
29 import org.eclipse.swt.widgets.TableColumn; |
|
30 |
|
31 import java.util.Comparator; |
|
32 import java.util.Collections; |
|
33 |
|
34 |
|
35 |
|
36 /** |
|
37 * Sorts statistic view contents |
|
38 * @author kihe |
|
39 * |
|
40 */ |
|
41 public class ATComparator implements Listener, Comparator<String[]> { |
|
42 |
|
43 /** Used table*/ |
|
44 private final Table memoryTable; |
|
45 |
|
46 /** Column index to sort */ |
|
47 private final int columnIndex; |
|
48 |
|
49 /** Sort only integer values */ |
|
50 private final boolean intComparator; |
|
51 |
|
52 /** Sort order */ |
|
53 boolean ascending; |
|
54 |
|
55 /** Parent class reference */ |
|
56 StatisticView parent; |
|
57 |
|
58 /** |
|
59 * Constructor |
|
60 * @param view Parent view reference |
|
61 * @param table Used table |
|
62 * @param clnIndex Used column index |
|
63 * @param compInteger Compare only for integer values |
|
64 */ |
|
65 public ATComparator(StatisticView view, Table table, int clnIndex, boolean compInteger) { |
|
66 parent = view; |
|
67 memoryTable = table; |
|
68 columnIndex = clnIndex; |
|
69 intComparator = compInteger; |
|
70 ascending = true; |
|
71 } |
|
72 |
|
73 |
|
74 /* |
|
75 * (non-Javadoc) |
|
76 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) |
|
77 */ |
|
78 |
|
79 public void handleEvent(Event e) { |
|
80 |
|
81 //get existing items |
|
82 TableItem[] items = memoryTable.getItems(); |
|
83 |
|
84 |
|
85 //add existing items to list => this helps item sorting |
|
86 AbstractList<String[]> list = new ArrayList<String[]>(); |
|
87 for(int i=0; i<items.length; i++) |
|
88 { |
|
89 TableItem oneItem = memoryTable.getItem(i); |
|
90 |
|
91 String[] values = { oneItem.getText(0),oneItem.getText(1), |
|
92 oneItem.getText(2),oneItem.getText(3), |
|
93 oneItem.getText(4),oneItem.getText(5), |
|
94 oneItem.getText(6) }; |
|
95 list.add(values); |
|
96 } |
|
97 |
|
98 //clear existing items |
|
99 memoryTable.clearAll(); |
|
100 |
|
101 //sort items, use compare method provide by this class |
|
102 Collections.sort(list, this); |
|
103 |
|
104 //thru sorted items |
|
105 Iterator<String[]> iterList = list.iterator(); |
|
106 memoryTable.setItemCount(list.size()); |
|
107 int index = 0; |
|
108 while(iterList.hasNext()) |
|
109 { |
|
110 String[] value = iterList.next(); |
|
111 TableItem item = memoryTable.getItem(index); |
|
112 item.setText(value); |
|
113 index++; |
|
114 } |
|
115 |
|
116 TableColumn currColumn = (TableColumn) e.widget; |
|
117 memoryTable.setSortColumn(currColumn); |
|
118 |
|
119 //update sort order |
|
120 if(ascending) { |
|
121 ascending = false; |
|
122 memoryTable.setSortDirection(SWT.DOWN); |
|
123 } |
|
124 else { |
|
125 ascending = true; |
|
126 memoryTable.setSortDirection(SWT.UP); |
|
127 } |
|
128 doPackColumns(); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Fit columns size to match column text size. |
|
133 */ |
|
134 public void doPackColumns() |
|
135 { |
|
136 TableColumn[] columns = memoryTable.getColumns(); |
|
137 for (int i = 0; i < columns.length; i++) { |
|
138 columns[i].pack(); |
|
139 } |
|
140 } |
|
141 /* |
|
142 * (non-Javadoc) |
|
143 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
|
144 */ |
|
145 public int compare(String[] o1, String[] o2) { |
|
146 int result = 0; |
|
147 |
|
148 //if compare only integer values |
|
149 if( intComparator ) |
|
150 { |
|
151 //get values |
|
152 int value1 = Integer.parseInt(o1[columnIndex]); |
|
153 int value2 = Integer.parseInt(o2[columnIndex]); |
|
154 result = (value1 < value2) ? -1 : 1; |
|
155 } |
|
156 else { |
|
157 //get values |
|
158 Long time1 = parent.getTimeFromCache(o1[columnIndex]); |
|
159 Long time2 = parent.getTimeFromCache(o2[columnIndex]); |
|
160 |
|
161 if( time1 == null || time2 == null ) { |
|
162 result = -1; |
|
163 }else { |
|
164 result = (time1 < time2) ? -1 : 1; |
|
165 } |
|
166 |
|
167 } |
|
168 //if sort order is set ascending change result to negative |
|
169 if( ascending ){ |
|
170 result = -result; |
|
171 } |
|
172 return result; |
|
173 } |
|
174 } |