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
.internal
.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
.viewers
.Viewer
;
22 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.SubSecondTimeWithUnitFormat
;
25 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.segmentstore
.statistics
.SegmentStoreStatistics
;
26 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.Activator
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
30 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.AbstractTmfTreeViewer
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.ITmfTreeColumnDataProvider
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeColumnData
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeViewerEntry
;
36 * An abstract tree viewer implementation for displaying segment store statistics
38 * @author Bernd Hufmann
41 public abstract class AbstractSegmentStoreStatisticsViewer
extends AbstractTmfTreeViewer
{
43 private static final Format FORMATTER
= new SubSecondTimeWithUnitFormat();
45 @Nullable private TmfAbstractAnalysisModule fModule
;
47 private static final String
[] COLUMN_NAMES
= new String
[] {
48 checkNotNull(Messages
.SegmentStoreStatistics_LevelLabel
),
49 checkNotNull(Messages
.SegmentStoreStatistics_Statistics_MinLabel
),
50 checkNotNull(Messages
.SegmentStoreStatistics_MaxLabel
),
51 checkNotNull(Messages
.SegmentStoreStatistics_AverageLabel
)
58 * the parent composite
60 public AbstractSegmentStoreStatisticsViewer(Composite parent
) {
62 setLabelProvider(new SegmentStoreStatisticsLabelProvider());
65 /** Provides label for the Segment Store tree viewer cells */
66 protected static class SegmentStoreStatisticsLabelProvider
extends TreeLabelProvider
{
69 public String
getColumnText(@Nullable Object element
, int columnIndex
) {
70 String value
= ""; //$NON-NLS-1$
71 if (element
instanceof HiddenTreeViewerEntry
) {
72 if (columnIndex
== 0) {
73 value
= ((HiddenTreeViewerEntry
) element
).getName();
75 } else if (element
instanceof SegmentStoreStatisticsEntry
) {
76 SegmentStoreStatisticsEntry entry
= (SegmentStoreStatisticsEntry
) element
;
77 if (columnIndex
== 0) {
78 return checkNotNull(String
.valueOf(entry
.getName()));
80 if (entry
.getEntry().getNbSegments() > 0) {
81 if (columnIndex
== 1) {
82 value
= toFormattedString(entry
.getEntry().getMin());
83 } else if (columnIndex
== 2) {
84 value
= String
.valueOf(toFormattedString(entry
.getEntry().getMax()));
85 } else if (columnIndex
== 3) {
86 value
= String
.valueOf(toFormattedString(entry
.getEntry().getAverage()));
90 return checkNotNull(value
);
95 * Creates the statistics analysis module
97 * @return the statistics analysis module
99 @Nullable protected abstract TmfAbstractAnalysisModule
createStatisticsAnalysiModule();
102 * Gets the statistics analysis module
103 * @return the statistics analysis module
105 @Nullable public TmfAbstractAnalysisModule
getStatisticsAnalysisModule() {
110 protected ITmfTreeColumnDataProvider
getColumnDataProvider() {
111 return new ITmfTreeColumnDataProvider() {
114 public List
<@Nullable TmfTreeColumnData
> getColumnData() {
115 /* All columns are sortable */
116 List
<@Nullable TmfTreeColumnData
> columns
= new ArrayList
<>();
117 TmfTreeColumnData column
= new TmfTreeColumnData(COLUMN_NAMES
[0]);
118 column
.setComparator(new ViewerComparator() {
120 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
121 if ((e1
== null) || (e2
== null)) {
125 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
126 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
128 return n1
.getName().compareTo(n2
.getName());
133 column
= new TmfTreeColumnData(COLUMN_NAMES
[1]);
134 column
.setComparator(new ViewerComparator() {
136 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
137 if ((e1
== null) || (e2
== null)) {
141 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
142 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
144 return Long
.compare(n1
.getEntry().getMin(), n2
.getEntry().getMin());
149 column
= new TmfTreeColumnData(COLUMN_NAMES
[2]);
150 column
.setComparator(new ViewerComparator() {
152 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
153 if ((e1
== null) || (e2
== null)) {
157 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
158 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
160 return Long
.compare(n1
.getEntry().getMax(), n2
.getEntry().getMax());
165 column
= new TmfTreeColumnData(COLUMN_NAMES
[3]);
166 column
.setComparator(new ViewerComparator() {
168 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
169 if ((e1
== null) || (e2
== null)) {
173 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
174 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
176 return Double
.compare(n1
.getEntry().getAverage(), n2
.getEntry().getAverage());
190 public void initializeDataSource() {
191 /* Should not be called while trace is still null */
192 ITmfTrace trace
= checkNotNull(getTrace());
193 TmfAbstractAnalysisModule module
= createStatisticsAnalysiModule();
194 if (module
== null) {
198 module
.setTrace(trace
);
201 } catch (TmfAnalysisException e
) {
202 Activator
.getDefault().logError("Error initializing statistics analysis module", e
); //$NON-NLS-1$
207 * Formats a double value string
211 * @return formatted value
213 protected static String
toFormattedString(double value
) {
214 // The cast to long is needed because the formatter cannot truncate the number.
215 String percentageString
= checkNotNull(String
.format("%s", FORMATTER
.format(value
))); //$NON-NLS-1$
216 return percentageString
;
220 * Class for defining an entry in the statistics tree.
222 protected class SegmentStoreStatisticsEntry
extends TmfTreeViewerEntry
{
224 private SegmentStoreStatistics fEntry
;
233 * segment store statistics object
235 public SegmentStoreStatisticsEntry(String name
, SegmentStoreStatistics entry
) {
241 * Gets the statistics object
243 * @return statistics object
245 public SegmentStoreStatistics
getEntry() {
246 return checkNotNull(fEntry
);
252 * Class to define a level in the tree that doesn't have any values.
254 protected class HiddenTreeViewerEntry
extends SegmentStoreStatisticsEntry
{
259 * the name of the level
261 public HiddenTreeViewerEntry(String name
) {
262 super(name
, new SegmentStoreStatistics());