1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
.linuxtools
.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
.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
;
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
37 public class TmfBaseColumnDataProvider
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 /** Level column names */
44 private static final String LEVEL_COLUMN
= Messages
.TmfStatisticsView_LevelColumn
;
46 /** Number of events column names */
47 private static final String EVENTS_COUNT_COLUMN
= Messages
.TmfStatisticsView_NbEventsColumn
;
49 /** Number of events in time range column names */
50 private static final String PARTIAL_EVENTS_COUNT_COLUMN
= Messages
.TmfStatisticsView_NbEventsTimeRangeColumn
;
52 /** Level column tooltips */
53 private static final String LEVEL_COLUMN_TIP
= Messages
.TmfStatisticsView_LevelColumnTip
;
55 /** Number of events column tooltips */
56 private static final String EVENTS_COUNT_COLUMN_TIP
= Messages
.TmfStatisticsView_NbEventsTip
;
58 /** Number of events in time range column tooltips */
59 private static final String PARTIAL_COUNT_COLUMN_TIP
= Messages
.TmfStatisticsView_NbEventsTimeRangeTip
;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Level for which statistics should not be displayed.
70 public static final Set
<String
> HIDDEN_FOLDER_LEVELS
= ImmutableSet
.of("Event Types"); //$NON-NLS-1$
72 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
74 // ------------------------------------------------------------------------
75 // Column index (Ideally, this should not be hardcoded).
76 // ------------------------------------------------------------------------
79 * Possible columns in the view
83 public static enum StatsColumn
{
85 * Column index for the event type column.
89 * Column index for the event total count column.
93 * Column index for the event partial count column.
97 * Column index for the dummy column.
101 private final int colIndex
;
103 private StatsColumn(int index
) {
108 * Getter method for the column index.
110 * @return the index of the column
112 public int getIndex() {
117 * Method to get the column at a certain index.
119 * @param index the index of the column
121 * @return the column at the specified index
123 public static StatsColumn
getColumn(int index
) {
137 // Other values are illegal.
139 throw new IllegalArgumentException();
145 // ------------------------------------------------------------------------
147 // ------------------------------------------------------------------------
150 * Contains the list of the columns
152 private final List
<TmfBaseColumnData
> fColumnData
;
155 * Create basic columns to represent the statistics data
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(
166 new ColumnLabelProvider() {
168 public String
getText(Object element
) {
169 return ((TmfStatisticsTreeNode
) element
).getName();
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
);
178 return PlatformUI
.getWorkbench().getSharedImages().getImage(ISharedImages
.IMG_OBJ_ELEMENT
);
181 new ViewerComparator() {
183 public int compare(Viewer viewer
, Object e1
, Object e2
) {
184 TmfStatisticsTreeNode n1
= (TmfStatisticsTreeNode
) e1
;
185 TmfStatisticsTreeNode n2
= (TmfStatisticsTreeNode
) e2
;
187 return n1
.getName().compareTo(n2
.getName());
192 /* Column showing the total number of events */
193 builder
.add(new TmfBaseColumnData(
197 EVENTS_COUNT_COLUMN_TIP
,
198 new ColumnLabelProvider() {
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
);
208 new ViewerComparator() {
210 public int compare(Viewer viewer
, Object e1
, Object e2
) {
211 TmfStatisticsTreeNode n1
= (TmfStatisticsTreeNode
) e1
;
212 TmfStatisticsTreeNode n2
= (TmfStatisticsTreeNode
) e2
;
214 return (int) (n1
.getValues().getTotal() - n2
.getValues().getTotal());
217 new ITmfColumnPercentageProvider() {
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();
226 /* Column showing the number of events within the selected time range */
227 builder
.add(new TmfBaseColumnData(
228 PARTIAL_EVENTS_COUNT_COLUMN
,
231 PARTIAL_COUNT_COLUMN_TIP
,
232 new ColumnLabelProvider() {
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
);
243 new ViewerComparator() {
245 public int compare(Viewer viewer
, Object e1
, Object e2
) {
246 TmfStatisticsTreeNode n1
= (TmfStatisticsTreeNode
) e1
;
247 TmfStatisticsTreeNode n2
= (TmfStatisticsTreeNode
) e2
;
249 return (int) (n1
.getValues().getPartial() - n2
.getValues().getPartial());
252 new ITmfColumnPercentageProvider() {
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();
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() {
265 public String
getText(Object element
) {
269 new ViewerComparator(),
270 new ITmfColumnPercentageProvider() {
272 public double getPercentage(TmfStatisticsTreeNode node
) {
277 fColumnData
= builder
.build();
281 * Return a list of the column created for the view
283 * @return columns list
285 public List
<TmfBaseColumnData
> getColumnData() {