Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / statistics / model / TmfBaseColumnDataProvider.java
CommitLineData
79e08fd0 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2011, 2014 Ericsson
20ff3b75 3 *
79e08fd0
BH
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
20ff3b75 8 *
79e08fd0 9 * Contributors:
09667aa4 10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
df2b3dbb 11 * Vincent Perot - Add percentages to the label provider
79e08fd0
BH
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.ui.viewers.statistics.model;
79e08fd0 15
9fa32496 16import java.util.List;
79e08fd0 17import java.util.Set;
79e08fd0
BH
18
19import org.eclipse.jface.viewers.ColumnLabelProvider;
20import org.eclipse.jface.viewers.Viewer;
21import org.eclipse.jface.viewers.ViewerComparator;
79e08fd0
BH
22import org.eclipse.swt.SWT;
23import org.eclipse.swt.graphics.Image;
2bdf0193 24import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider;
79e08fd0
BH
25import org.eclipse.ui.ISharedImages;
26import org.eclipse.ui.PlatformUI;
27
b3a26928
AM
28import com.google.common.collect.ImmutableList;
29import com.google.common.collect.ImmutableSet;
30
79e08fd0 31/**
b544077e 32 * Create a basic list of columns with providers.
20ff3b75 33 *
b544077e 34 * @author Mathieu Denis
df2b3dbb 35 * @since 3.0
79e08fd0 36 */
b3a26928 37public class TmfBaseColumnDataProvider {
79e08fd0 38
b3a26928
AM
39 // ------------------------------------------------------------------------
40 // Localized strings
41 // ------------------------------------------------------------------------
09667aa4 42
b3a26928
AM
43 /** Level column names */
44 private static final String LEVEL_COLUMN = Messages.TmfStatisticsView_LevelColumn;
09667aa4 45
b3a26928
AM
46 /** Number of events column names */
47 private static final String EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsColumn;
09667aa4 48
b3a26928
AM
49 /** Number of events in time range column names */
50 private static final String PARTIAL_EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsTimeRangeColumn;
09667aa4 51
b3a26928
AM
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
df2b3dbb
VP
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 }
b3a26928
AM
144
145 // ------------------------------------------------------------------------
146 // Instance fields
147 // ------------------------------------------------------------------------
09667aa4 148
25a042b3 149 /**
b3a26928 150 * Contains the list of the columns
79e08fd0 151 */
b3a26928 152 private final List<TmfBaseColumnData> fColumnData;
09667aa4 153
79e08fd0
BH
154 /**
155 * Create basic columns to represent the statistics data
156 */
157 public TmfBaseColumnDataProvider() {
09667aa4 158 /* List that will be used to create the table. */
b3a26928 159 ImmutableList.Builder<TmfBaseColumnData> builder = new ImmutableList.Builder<>();
25a042b3 160 /* Column showing the name of the events and its level in the tree */
b3a26928
AM
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));
79e08fd0 191
25a042b3 192 /* Column showing the total number of events */
b3a26928
AM
193 builder.add(new TmfBaseColumnData(
194 EVENTS_COUNT_COLUMN,
195 140,
df2b3dbb 196 SWT.RIGHT,
b3a26928
AM
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())) {
df2b3dbb 203 return TmfStatisticsFormatter.toColumnData(node, StatsColumn.TOTAL);
b3a26928 204 }
df2b3dbb 205 return EMPTY_STRING;
b3a26928
AM
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) {
df2b3dbb
VP
220 TmfStatisticsTreeNode top = node.getTop();
221 return (top == null || top.getValues().getTotal() == 0) ?
222 0 : (double) (node.getValues().getTotal()) / top.getValues().getTotal();
b3a26928
AM
223 }
224 }));
25a042b3
MD
225
226 /* Column showing the number of events within the selected time range */
b3a26928
AM
227 builder.add(new TmfBaseColumnData(
228 PARTIAL_EVENTS_COUNT_COLUMN,
229 140,
df2b3dbb 230 SWT.RIGHT,
b3a26928 231 PARTIAL_COUNT_COLUMN_TIP,
25a042b3 232 new ColumnLabelProvider() {
b3a26928
AM
233 @Override
234 public String getText(Object element) {
235 TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
236 if (!HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
df2b3dbb 237 return TmfStatisticsFormatter.toColumnData(node, StatsColumn.PARTIAL);
b3a26928 238 }
df2b3dbb 239 return EMPTY_STRING;
b3a26928 240 }
df2b3dbb 241
b3a26928
AM
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) {
df2b3dbb
VP
255 TmfStatisticsTreeNode top = node.getTop();
256 return (top == null || top.getValues().getPartial() == 0) ?
257 0 : (double) (node.getValues().getPartial()) / top.getValues().getPartial();
258 }
259 }));
b3a26928 260
df2b3dbb
VP
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;
b3a26928
AM
274 }
275 }));
276
277 fColumnData = builder.build();
79e08fd0
BH
278 }
279
280 /**
b3a26928
AM
281 * Return a list of the column created for the view
282 *
283 * @return columns list
79e08fd0 284 */
9fa32496 285 public List<TmfBaseColumnData> getColumnData() {
79e08fd0
BH
286 return fColumnData;
287 }
288
289}
This page took 0.095816 seconds and 5 git commands to generate.