1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
11 * Vincent Perot - Add percentages to the label provider
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.statistics
.model
;
16 import java
.util
.List
;
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
;
28 import com
.google
.common
.collect
.ImmutableList
;
29 import com
.google
.common
.collect
.ImmutableSet
;
32 * Create a basic list of columns with providers.
34 * @author Mathieu Denis
36 public class TmfBaseColumnDataProvider
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 /** Level column names */
43 private static final String LEVEL_COLUMN
= Messages
.TmfStatisticsView_LevelColumn
;
45 /** Number of events column names */
46 private static final String EVENTS_COUNT_COLUMN
= Messages
.TmfStatisticsView_NbEventsColumn
;
48 /** Number of events in time range column names */
49 private static final String PARTIAL_EVENTS_COUNT_COLUMN
= Messages
.TmfStatisticsView_NbEventsTimeRangeColumn
;
51 /** Level column tooltips */
52 private static final String LEVEL_COLUMN_TIP
= Messages
.TmfStatisticsView_LevelColumnTip
;
54 /** Number of events column tooltips */
55 private static final String EVENTS_COUNT_COLUMN_TIP
= Messages
.TmfStatisticsView_NbEventsTip
;
57 /** Number of events in time range column tooltips */
58 private static final String PARTIAL_COUNT_COLUMN_TIP
= Messages
.TmfStatisticsView_NbEventsTimeRangeTip
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * Level for which statistics should not be displayed.
67 public static final Set
<String
> HIDDEN_FOLDER_LEVELS
= ImmutableSet
.of("Event Types"); //$NON-NLS-1$
69 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
71 // ------------------------------------------------------------------------
72 // Column index (Ideally, this should not be hardcoded).
73 // ------------------------------------------------------------------------
76 * Possible columns in the view
78 public static enum StatsColumn
{
80 * Column index for the event type column.
84 * Column index for the event total count column.
88 * Column index for the event partial count column.
92 * Column index for the dummy column.
96 private final int colIndex
;
98 private StatsColumn(int index
) {
103 * Getter method for the column index.
105 * @return the index of the column
107 public int getIndex() {
112 * Method to get the column at a certain index.
114 * @param index the index of the column
116 * @return the column at the specified index
118 public static StatsColumn
getColumn(int index
) {
132 // Other values are illegal.
134 throw new IllegalArgumentException();
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Contains the list of the columns
147 private final List
<TmfBaseColumnData
> fColumnData
;
150 * Create basic columns to represent the statistics data
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(
161 new ColumnLabelProvider() {
163 public String
getText(Object element
) {
164 return ((TmfStatisticsTreeNode
) element
).getName();
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
);
173 return PlatformUI
.getWorkbench().getSharedImages().getImage(ISharedImages
.IMG_OBJ_ELEMENT
);
176 new ViewerComparator() {
178 public int compare(Viewer viewer
, Object e1
, Object e2
) {
179 TmfStatisticsTreeNode n1
= (TmfStatisticsTreeNode
) e1
;
180 TmfStatisticsTreeNode n2
= (TmfStatisticsTreeNode
) e2
;
182 return n1
.getName().compareTo(n2
.getName());
187 /* Column showing the total number of events */
188 builder
.add(new TmfBaseColumnData(
192 EVENTS_COUNT_COLUMN_TIP
,
193 new ColumnLabelProvider() {
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
);
203 new ViewerComparator() {
205 public int compare(Viewer viewer
, Object e1
, Object e2
) {
206 TmfStatisticsTreeNode n1
= (TmfStatisticsTreeNode
) e1
;
207 TmfStatisticsTreeNode n2
= (TmfStatisticsTreeNode
) e2
;
209 return (int) (n1
.getValues().getTotal() - n2
.getValues().getTotal());
212 new ITmfColumnPercentageProvider() {
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();
221 /* Column showing the number of events within the selected time range */
222 builder
.add(new TmfBaseColumnData(
223 PARTIAL_EVENTS_COUNT_COLUMN
,
226 PARTIAL_COUNT_COLUMN_TIP
,
227 new ColumnLabelProvider() {
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
);
238 new ViewerComparator() {
240 public int compare(Viewer viewer
, Object e1
, Object e2
) {
241 TmfStatisticsTreeNode n1
= (TmfStatisticsTreeNode
) e1
;
242 TmfStatisticsTreeNode n2
= (TmfStatisticsTreeNode
) e2
;
244 return (int) (n1
.getValues().getPartial() - n2
.getValues().getPartial());
247 new ITmfColumnPercentageProvider() {
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();
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() {
260 public String
getText(Object element
) {
264 new ViewerComparator(),
265 new ITmfColumnPercentageProvider() {
267 public double getPercentage(TmfStatisticsTreeNode node
) {
272 fColumnData
= builder
.build();
276 * Return a list of the column created for the view
278 * @return columns list
280 public List
<TmfBaseColumnData
> getColumnData() {