tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / statistics / model / TmfBaseColumnDataProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
11 * Vincent Perot - Add percentages to the label provider
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model;
15
16 import java.util.List;
17 import java.util.Set;
18
19 import org.eclipse.jface.viewers.ColumnLabelProvider;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.viewers.ViewerComparator;
22 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.ui.ISharedImages;
26 import org.eclipse.ui.PlatformUI;
27
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableSet;
30
31 /**
32 * Create a basic list of columns with providers.
33 *
34 * @author Mathieu Denis
35 * @since 3.0
36 */
37 public class TmfBaseColumnDataProvider {
38
39 // ------------------------------------------------------------------------
40 // Localized strings
41 // ------------------------------------------------------------------------
42
43 /** Level column names */
44 private static final String LEVEL_COLUMN = Messages.TmfStatisticsView_LevelColumn;
45
46 /** Number of events column names */
47 private static final String EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsColumn;
48
49 /** Number of events in time range column names */
50 private static final String PARTIAL_EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsTimeRangeColumn;
51
52 /** Level column tooltips */
53 private static final String LEVEL_COLUMN_TIP = Messages.TmfStatisticsView_LevelColumnTip;
54
55 /** Number of events column tooltips */
56 private static final String EVENTS_COUNT_COLUMN_TIP = Messages.TmfStatisticsView_NbEventsTip;
57
58 /** Number of events in time range column tooltips */
59 private static final String PARTIAL_COUNT_COLUMN_TIP = Messages.TmfStatisticsView_NbEventsTimeRangeTip;
60
61 // ------------------------------------------------------------------------
62 // Class attributes
63 // ------------------------------------------------------------------------
64
65 /**
66 * Level for which statistics should not be displayed.
67 *
68 * @since 3.0
69 */
70 public static final Set<String> HIDDEN_FOLDER_LEVELS = ImmutableSet.of("Event Types"); //$NON-NLS-1$
71
72 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
73
74 // ------------------------------------------------------------------------
75 // Column index (Ideally, this should not be hardcoded).
76 // ------------------------------------------------------------------------
77
78 /**
79 * Possible columns in the view
80 *
81 * @since 3.0
82 */
83 public static enum StatsColumn {
84 /**
85 * Column index for the event type column.
86 */
87 EVENT(0),
88 /**
89 * Column index for the event total count column.
90 */
91 TOTAL(1),
92 /**
93 * Column index for the event partial count column.
94 */
95 PARTIAL(2),
96 /**
97 * Column index for the dummy column.
98 */
99 DUMMY(3);
100
101 private final int colIndex;
102
103 private StatsColumn(int index) {
104 colIndex = index;
105 }
106
107 /**
108 * Getter method for the column index.
109 *
110 * @return the index of the column
111 */
112 public int getIndex() {
113 return colIndex;
114 }
115
116 /**
117 * Method to get the column at a certain index.
118 *
119 * @param index the index of the column
120 *
121 * @return the column at the specified index
122 */
123 public static StatsColumn getColumn(int index) {
124 switch(index) {
125 case 0:
126 return EVENT;
127
128 case 1:
129 return TOTAL;
130
131 case 2:
132 return PARTIAL;
133
134 case 3:
135 return DUMMY;
136
137 // Other values are illegal.
138 default:
139 throw new IllegalArgumentException();
140 }
141
142 }
143 }
144
145 // ------------------------------------------------------------------------
146 // Instance fields
147 // ------------------------------------------------------------------------
148
149 /**
150 * Contains the list of the columns
151 */
152 private final List<TmfBaseColumnData> fColumnData;
153
154 /**
155 * Create basic columns to represent the statistics data
156 */
157 public TmfBaseColumnDataProvider() {
158 /* List that will be used to create the table. */
159 ImmutableList.Builder<TmfBaseColumnData> builder = new ImmutableList.Builder<>();
160 /* Column showing the name of the events and its level in the tree */
161 builder.add(new TmfBaseColumnData(
162 LEVEL_COLUMN,
163 200,
164 SWT.LEFT,
165 LEVEL_COLUMN_TIP,
166 new ColumnLabelProvider() {
167 @Override
168 public String getText(Object element) {
169 return ((TmfStatisticsTreeNode) element).getName();
170 }
171
172 @Override
173 public Image getImage(Object element) {
174 TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
175 if (HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
176 return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
177 }
178 return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
179 }
180 },
181 new ViewerComparator() {
182 @Override
183 public int compare(Viewer viewer, Object e1, Object e2) {
184 TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1;
185 TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2;
186
187 return n1.getName().compareTo(n2.getName());
188 }
189 },
190 null));
191
192 /* Column showing the total number of events */
193 builder.add(new TmfBaseColumnData(
194 EVENTS_COUNT_COLUMN,
195 140,
196 SWT.RIGHT,
197 EVENTS_COUNT_COLUMN_TIP,
198 new ColumnLabelProvider() {
199 @Override
200 public String getText(Object element) {
201 TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
202 if (!HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
203 return TmfStatisticsFormatter.toColumnData(node, StatsColumn.TOTAL);
204 }
205 return EMPTY_STRING;
206 }
207 },
208 new ViewerComparator() {
209 @Override
210 public int compare(Viewer viewer, Object e1, Object e2) {
211 TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1;
212 TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2;
213
214 return (int) (n1.getValues().getTotal() - n2.getValues().getTotal());
215 }
216 },
217 new ITmfColumnPercentageProvider() {
218 @Override
219 public double getPercentage(TmfStatisticsTreeNode node) {
220 TmfStatisticsTreeNode top = node.getTop();
221 return (top == null || top.getValues().getTotal() == 0) ?
222 0 : (double) (node.getValues().getTotal()) / top.getValues().getTotal();
223 }
224 }));
225
226 /* Column showing the number of events within the selected time range */
227 builder.add(new TmfBaseColumnData(
228 PARTIAL_EVENTS_COUNT_COLUMN,
229 140,
230 SWT.RIGHT,
231 PARTIAL_COUNT_COLUMN_TIP,
232 new ColumnLabelProvider() {
233 @Override
234 public String getText(Object element) {
235 TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
236 if (!HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
237 return TmfStatisticsFormatter.toColumnData(node, StatsColumn.PARTIAL);
238 }
239 return EMPTY_STRING;
240 }
241
242 },
243 new ViewerComparator() {
244 @Override
245 public int compare(Viewer viewer, Object e1, Object e2) {
246 TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1;
247 TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2;
248
249 return (int) (n1.getValues().getPartial() - n2.getValues().getPartial());
250 }
251 },
252 new ITmfColumnPercentageProvider() {
253 @Override
254 public double getPercentage(TmfStatisticsTreeNode node) {
255 TmfStatisticsTreeNode top = node.getTop();
256 return (top == null || top.getValues().getPartial() == 0) ?
257 0 : (double) (node.getValues().getPartial()) / top.getValues().getPartial();
258 }
259 }));
260
261 /* Dummy column used to "fix" the display on Linux (using GTK) */
262 builder.add(new TmfBaseColumnData(EMPTY_STRING, 1, SWT.RIGHT, EMPTY_STRING,
263 new ColumnLabelProvider() {
264 @Override
265 public String getText(Object element) {
266 return EMPTY_STRING;
267 }
268 },
269 new ViewerComparator(),
270 new ITmfColumnPercentageProvider() {
271 @Override
272 public double getPercentage(TmfStatisticsTreeNode node) {
273 return 0;
274 }
275 }));
276
277 fColumnData = builder.build();
278 }
279
280 /**
281 * Return a list of the column created for the view
282 *
283 * @return columns list
284 */
285 public List<TmfBaseColumnData> getColumnData() {
286 return fColumnData;
287 }
288
289 }
This page took 0.052577 seconds and 5 git commands to generate.