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