1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.statistics
;
14 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
16 import java
.text
.Format
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 import org
.eclipse
.jface
.action
.Action
;
22 import org
.eclipse
.jface
.action
.IAction
;
23 import org
.eclipse
.jface
.action
.IMenuManager
;
24 import org
.eclipse
.jface
.action
.MenuManager
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.TreeViewer
;
28 import org
.eclipse
.jface
.viewers
.Viewer
;
29 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Menu
;
33 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.statistics
.SegmentStoreStatistics
;
34 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.SubSecondTimeWithUnitFormat
;
35 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.Activator
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.AbstractTmfTreeViewer
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.ITmfTreeColumnDataProvider
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeColumnData
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeViewerEntry
;
47 * An abstract tree viewer implementation for displaying segment store
50 * @author Bernd Hufmann
53 public abstract class AbstractSegmentStoreStatisticsViewer
extends AbstractTmfTreeViewer
{
55 private static final Format FORMATTER
= new SubSecondTimeWithUnitFormat();
58 private TmfAbstractAnalysisModule fModule
;
59 private MenuManager fTablePopupMenuManager
;
61 private static final String
[] COLUMN_NAMES
= new String
[] {
62 checkNotNull(Messages
.SegmentStoreStatistics_LevelLabel
),
63 checkNotNull(Messages
.SegmentStoreStatistics_Statistics_MinLabel
),
64 checkNotNull(Messages
.SegmentStoreStatistics_MaxLabel
),
65 checkNotNull(Messages
.SegmentStoreStatistics_AverageLabel
),
66 checkNotNull(Messages
.SegmentStoreStatisticsViewer_StandardDeviation
),
67 checkNotNull(Messages
.SegmentStoreStatisticsViewer_Count
)
74 * the parent composite
76 public AbstractSegmentStoreStatisticsViewer(Composite parent
) {
78 setLabelProvider(new SegmentStoreStatisticsLabelProvider());
79 fTablePopupMenuManager
= new MenuManager();
80 fTablePopupMenuManager
.setRemoveAllWhenShown(true);
81 fTablePopupMenuManager
.addMenuListener(manager
-> {
82 TreeViewer viewer
= getTreeViewer();
83 ISelection selection
= viewer
.getSelection();
84 if (selection
instanceof IStructuredSelection
) {
85 IStructuredSelection sel
= (IStructuredSelection
) selection
;
86 if (manager
!= null) {
87 appendToTablePopupMenu(manager
, sel
);
91 Menu tablePopup
= fTablePopupMenuManager
.createContextMenu(getTreeViewer().getTree());
92 getTreeViewer().getTree().setMenu(tablePopup
);
95 /** Provides label for the Segment Store tree viewer cells */
96 protected static class SegmentStoreStatisticsLabelProvider
extends TreeLabelProvider
{
99 public String
getColumnText(@Nullable Object element
, int columnIndex
) {
100 String value
= ""; //$NON-NLS-1$
101 if (element
instanceof HiddenTreeViewerEntry
) {
102 if (columnIndex
== 0) {
103 value
= ((HiddenTreeViewerEntry
) element
).getName();
105 } else if (element
instanceof SegmentStoreStatisticsEntry
) {
106 SegmentStoreStatisticsEntry entry
= (SegmentStoreStatisticsEntry
) element
;
107 if (columnIndex
== 0) {
108 return String
.valueOf(entry
.getName());
110 if (entry
.getEntry().getNbSegments() > 0) {
111 if (columnIndex
== 1) {
112 value
= toFormattedString(entry
.getEntry().getMin());
113 } else if (columnIndex
== 2) {
114 value
= String
.valueOf(toFormattedString(entry
.getEntry().getMax()));
115 } else if (columnIndex
== 3) {
116 value
= String
.valueOf(toFormattedString(entry
.getEntry().getAverage()));
117 } else if (columnIndex
== 4) {
118 value
= String
.valueOf(toFormattedString(entry
.getEntry().getStdDev()));
119 } else if (columnIndex
== 5) {
120 value
= String
.valueOf(entry
.getEntry().getNbSegments());
124 return checkNotNull(value
);
129 * Creates the statistics analysis module
131 * @return the statistics analysis module
134 protected abstract TmfAbstractAnalysisModule
createStatisticsAnalysiModule();
137 * Gets the statistics analysis module
139 * @return the statistics analysis module
142 public TmfAbstractAnalysisModule
getStatisticsAnalysisModule() {
147 protected ITmfTreeColumnDataProvider
getColumnDataProvider() {
148 return new ITmfTreeColumnDataProvider() {
151 public List
<@Nullable TmfTreeColumnData
> getColumnData() {
152 /* All columns are sortable */
153 List
<@Nullable TmfTreeColumnData
> columns
= new ArrayList
<>();
154 TmfTreeColumnData column
= new TmfTreeColumnData(COLUMN_NAMES
[0]);
155 column
.setAlignment(SWT
.RIGHT
);
156 column
.setComparator(new ViewerComparator() {
158 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
159 if ((e1
== null) || (e2
== null)) {
163 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
164 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
166 return n1
.getName().compareTo(n2
.getName());
171 column
= new TmfTreeColumnData(COLUMN_NAMES
[1]);
172 column
.setAlignment(SWT
.RIGHT
);
173 column
.setComparator(new ViewerComparator() {
175 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
176 if ((e1
== null) || (e2
== null)) {
180 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
181 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
183 return Long
.compare(n1
.getEntry().getMin(), n2
.getEntry().getMin());
188 column
= new TmfTreeColumnData(COLUMN_NAMES
[2]);
189 column
.setAlignment(SWT
.RIGHT
);
190 column
.setComparator(new ViewerComparator() {
192 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
193 if ((e1
== null) || (e2
== null)) {
197 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
198 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
200 return Long
.compare(n1
.getEntry().getMax(), n2
.getEntry().getMax());
205 column
= new TmfTreeColumnData(COLUMN_NAMES
[3]);
206 column
.setAlignment(SWT
.RIGHT
);
207 column
.setComparator(new ViewerComparator() {
209 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
210 if ((e1
== null) || (e2
== null)) {
214 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
215 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
217 return Double
.compare(n1
.getEntry().getAverage(), n2
.getEntry().getAverage());
222 column
= new TmfTreeColumnData(COLUMN_NAMES
[4]);
223 column
.setAlignment(SWT
.RIGHT
);
224 column
.setComparator(new ViewerComparator() {
226 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
227 if ((e1
== null) || (e2
== null)) {
231 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
232 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
234 return Double
.compare(n1
.getEntry().getStdDev(), n2
.getEntry().getStdDev());
239 column
= new TmfTreeColumnData(COLUMN_NAMES
[5]);
240 column
.setAlignment(SWT
.RIGHT
);
241 column
.setComparator(new ViewerComparator() {
243 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
244 if ((e1
== null) || (e2
== null)) {
248 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
249 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
251 return Long
.compare(n1
.getEntry().getNbSegments(), n2
.getEntry().getNbSegments());
256 column
= new TmfTreeColumnData(""); //$NON-NLS-1$
265 public void initializeDataSource() {
266 ITmfTrace trace
= getTrace();
268 TmfAbstractAnalysisModule module
= createStatisticsAnalysiModule();
269 if (module
== null) {
273 module
.setTrace(trace
);
276 } catch (TmfAnalysisException e
) {
277 Activator
.getDefault().logError("Error initializing statistics analysis module", e
); //$NON-NLS-1$
283 * Method to add commands to the context sensitive menu.
287 * the current selection
289 protected void appendToTablePopupMenu(IMenuManager manager
, IStructuredSelection sel
) {
290 Object element
= sel
.getFirstElement();
291 if ((element
instanceof SegmentStoreStatisticsEntry
) && !(element
instanceof HiddenTreeViewerEntry
)) {
292 final SegmentStoreStatisticsEntry segment
= (SegmentStoreStatisticsEntry
) element
;
293 IAction gotoStartTime
= new Action(Messages
.SegmentStoreStatisticsViewer_GotoMinAction
) {
296 long start
= segment
.getEntry().getMinSegment().getStart();
297 long end
= segment
.getEntry().getMinSegment().getEnd();
298 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreStatisticsViewer
.this, TmfTimestamp
.fromNanos(start
), TmfTimestamp
.fromNanos(end
)));
302 IAction gotoEndTime
= new Action(Messages
.SegmentStoreStatisticsViewer_GotoMaxAction
) {
305 long start
= segment
.getEntry().getMaxSegment().getStart();
306 long end
= segment
.getEntry().getMaxSegment().getEnd();
307 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreStatisticsViewer
.this, TmfTimestamp
.fromNanos(start
), TmfTimestamp
.fromNanos(end
)));
311 manager
.add(gotoStartTime
);
312 manager
.add(gotoEndTime
);
317 * Formats a double value string
321 * @return formatted value
323 protected static String
toFormattedString(double value
) {
324 // The cast to long is needed because the formatter cannot truncate the
326 String percentageString
= String
.format("%s", FORMATTER
.format(value
)); //$NON-NLS-1$
327 return percentageString
;
331 * Class for defining an entry in the statistics tree.
333 protected class SegmentStoreStatisticsEntry
extends TmfTreeViewerEntry
{
335 private final SegmentStoreStatistics fEntry
;
344 * segment store statistics object
346 public SegmentStoreStatisticsEntry(String name
, SegmentStoreStatistics entry
) {
352 * Gets the statistics object
354 * @return statistics object
356 public SegmentStoreStatistics
getEntry() {
363 * Class to define a level in the tree that doesn't have any values.
365 protected class HiddenTreeViewerEntry
extends SegmentStoreStatisticsEntry
{
370 * the name of the level
372 public HiddenTreeViewerEntry(String name
) {
373 super(name
, new SegmentStoreStatistics());