Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d6353752eb7c67ee008dd05772b0643af0078aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************************
 * Copyright (c) 2011, 2014 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
 *   Vincent Perot - Add percentages to the label provider
 *******************************************************************************/

package org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model;

import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

/**
 * Create a basic list of columns with providers.
 *
 * @author Mathieu Denis
 */
public class TmfBaseColumnDataProvider {

    // ------------------------------------------------------------------------
    // Localized strings
    // ------------------------------------------------------------------------

    /** Level column names */
    private static final String LEVEL_COLUMN = Messages.TmfStatisticsView_LevelColumn;

    /** Number of events column names */
    private static final String EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsColumn;

    /** Number of events in time range column names */
    private static final String PARTIAL_EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsTimeRangeColumn;

    /** Level column tooltips */
    private static final String LEVEL_COLUMN_TIP = Messages.TmfStatisticsView_LevelColumnTip;

    /** Number of events column tooltips */
    private static final String EVENTS_COUNT_COLUMN_TIP = Messages.TmfStatisticsView_NbEventsTip;

    /** Number of events in time range column tooltips */
    private static final String PARTIAL_COUNT_COLUMN_TIP = Messages.TmfStatisticsView_NbEventsTimeRangeTip;

    // ------------------------------------------------------------------------
    // Class attributes
    // ------------------------------------------------------------------------

    /**
     * Level for which statistics should not be displayed.
     */
    public static final Set<String> HIDDEN_FOLDER_LEVELS = ImmutableSet.of("Event Types"); //$NON-NLS-1$

    private static final String EMPTY_STRING = ""; //$NON-NLS-1$

    // ------------------------------------------------------------------------
    // Column index (Ideally, this should not be hardcoded).
    // ------------------------------------------------------------------------

    /**
     * Possible columns in the view
     */
    public static enum StatsColumn {
        /**
         * Column index for the event type column.
         */
        EVENT(0),
        /**
         * Column index for the event total count column.
         */
        TOTAL(1),
        /**
         * Column index for the event partial count column.
         */
        PARTIAL(2),
        /**
         * Column index for the dummy column.
         */
        DUMMY(3);

        private final int colIndex;

        private StatsColumn(int index) {
            colIndex = index;
        }

        /**
         * Getter method for the column index.
         *
         * @return the index of the column
         */
        public int getIndex() {
            return colIndex;
        }

        /**
         * Method to get the column at a certain index.
         *
         * @param index the index of the column
         *
         * @return the column at the specified index
         */
        public static StatsColumn getColumn(int index) {
            switch(index) {
            case 0:
                return EVENT;

            case 1:
                return TOTAL;

            case 2:
                return PARTIAL;

            case 3:
                return DUMMY;

            // Other values are illegal.
            default:
                throw new IllegalArgumentException();
            }

        }
    }

    // ------------------------------------------------------------------------
    // Instance fields
    // ------------------------------------------------------------------------

    /**
     * Contains the list of the columns
     */
    private final List<TmfBaseColumnData> fColumnData;

    /**
     * Create basic columns to represent the statistics data
     */
    public TmfBaseColumnDataProvider() {
        /* List that will be used to create the table. */
        ImmutableList.Builder<TmfBaseColumnData> builder = new ImmutableList.Builder<>();
        /* Column showing the name of the events and its level in the tree */
        builder.add(new TmfBaseColumnData(
                LEVEL_COLUMN,
                200,
                SWT.LEFT,
                LEVEL_COLUMN_TIP,
                new ColumnLabelProvider() {
                    @Override
                    public String getText(Object element) {
                        return ((TmfStatisticsTreeNode) element).getName();
                    }

                    @Override
                    public Image getImage(Object element) {
                        TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
                        if (HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
                            return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
                        }
                        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
                    }
                },
                new ViewerComparator() {
                    @Override
                    public int compare(Viewer viewer, Object e1, Object e2) {
                        TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1;
                        TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2;

                        return n1.getName().compareTo(n2.getName());
                    }
                },
                null));

        /* Column showing the total number of events */
        builder.add(new TmfBaseColumnData(
                EVENTS_COUNT_COLUMN,
                140,
                SWT.RIGHT,
                EVENTS_COUNT_COLUMN_TIP,
                new ColumnLabelProvider() {
                    @Override
                    public String getText(Object element) {
                        TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
                        if (!HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
                            return TmfStatisticsFormatter.toColumnData(node, StatsColumn.TOTAL);
                        }
                        return EMPTY_STRING;
                    }
                },
                new ViewerComparator() {
                    @Override
                    public int compare(Viewer viewer, Object e1, Object e2) {
                        TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1;
                        TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2;

                        return (int) (n1.getValues().getTotal() - n2.getValues().getTotal());
                    }
                },
                new ITmfColumnPercentageProvider() {
                    @Override
                    public double getPercentage(TmfStatisticsTreeNode node) {
                        TmfStatisticsTreeNode top = node.getTop();
                        return (top == null || top.getValues().getTotal() == 0) ?
                                0 : (double) (node.getValues().getTotal()) / top.getValues().getTotal();
                    }
                }));

        /* Column showing the number of events within the selected time range */
        builder.add(new TmfBaseColumnData(
                PARTIAL_EVENTS_COUNT_COLUMN,
                140,
                SWT.RIGHT,
                PARTIAL_COUNT_COLUMN_TIP,
                new ColumnLabelProvider() {
                    @Override
                    public String getText(Object element) {
                        TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element;
                        if (!HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
                            return TmfStatisticsFormatter.toColumnData(node, StatsColumn.PARTIAL);
                        }
                        return EMPTY_STRING;
                    }

                },
                new ViewerComparator() {
                    @Override
                    public int compare(Viewer viewer, Object e1, Object e2) {
                        TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1;
                        TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2;

                        return (int) (n1.getValues().getPartial() - n2.getValues().getPartial());
                    }
                },
                new ITmfColumnPercentageProvider() {
                    @Override
                    public double getPercentage(TmfStatisticsTreeNode node) {
                        TmfStatisticsTreeNode top = node.getTop();
                        return (top == null || top.getValues().getPartial() == 0) ?
                                0 : (double) (node.getValues().getPartial()) / top.getValues().getPartial();
                    }
                }));

        /* Dummy column used to "fix" the display on Linux (using GTK) */
        builder.add(new TmfBaseColumnData(EMPTY_STRING, 1, SWT.RIGHT, EMPTY_STRING,
                new ColumnLabelProvider() {
                    @Override
                    public String getText(Object element) {
                        return EMPTY_STRING;
                    }
                },
                new ViewerComparator(),
                new ITmfColumnPercentageProvider() {
                    @Override
                    public double getPercentage(TmfStatisticsTreeNode node) {
                        return 0;
                    }
                }));

        fColumnData = builder.build();
    }

    /**
     * Return a list of the column created for the view
     *
     * @return columns list
     */
    public List<TmfBaseColumnData> getColumnData() {
        return fColumnData;
    }

}

Back to the top