tmf: Fix possible overflows in TmfXmlTimestampCondition
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / analysis / timing / ui / views / segmentstore / statistics / AbstractSegmentStoreStatisticsViewer.java
CommitLineData
ce8319b6 1/*******************************************************************************
658401c8 2 * Copyright (c) 2015, 2016 Ericsson
ce8319b6
BH
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
5b901f94 12package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics;
ce8319b6
BH
13
14import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
91deb4e1 16import java.text.Format;
ce8319b6
BH
17import java.util.ArrayList;
18import java.util.List;
ce8319b6
BH
19
20import org.eclipse.jdt.annotation.Nullable;
a1e4b7e8
BH
21import org.eclipse.jface.action.Action;
22import org.eclipse.jface.action.IAction;
23import org.eclipse.jface.action.IMenuManager;
24import org.eclipse.jface.action.MenuManager;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.IStructuredSelection;
27import org.eclipse.jface.viewers.TreeViewer;
ce8319b6
BH
28import org.eclipse.jface.viewers.Viewer;
29import org.eclipse.jface.viewers.ViewerComparator;
a0acb044 30import org.eclipse.swt.SWT;
ce8319b6 31import org.eclipse.swt.widgets.Composite;
a1e4b7e8 32import org.eclipse.swt.widgets.Menu;
5b901f94 33import org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics.SegmentStoreStatistics;
91deb4e1 34import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.SubSecondTimeWithUnitFormat;
658401c8 35import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator;
ce8319b6
BH
36import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
37import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
a1e4b7e8
BH
38import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
39import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
ce8319b6
BH
40import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
41import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
42import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
43import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
44import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
45
46/**
a0acb044
MK
47 * An abstract tree viewer implementation for displaying segment store
48 * statistics
ce8319b6
BH
49 *
50 * @author Bernd Hufmann
51 *
52 */
3507ca88 53public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer {
ce8319b6 54
91deb4e1 55 private static final Format FORMATTER = new SubSecondTimeWithUnitFormat();
ce8319b6 56
a0acb044
MK
57 @Nullable
58 private TmfAbstractAnalysisModule fModule;
a1e4b7e8 59 private MenuManager fTablePopupMenuManager;
ce8319b6
BH
60
61 private static final String[] COLUMN_NAMES = new String[] {
53f46dc0
BH
62 checkNotNull(Messages.SegmentStoreStatistics_LevelLabel),
63 checkNotNull(Messages.SegmentStoreStatistics_Statistics_MinLabel),
64 checkNotNull(Messages.SegmentStoreStatistics_MaxLabel),
a0acb044 65 checkNotNull(Messages.SegmentStoreStatistics_AverageLabel),
8f6e9344
JCK
66 checkNotNull(Messages.SegmentStoreStatisticsViewer_StandardDeviation),
67 checkNotNull(Messages.SegmentStoreStatisticsViewer_Count)
ce8319b6
BH
68 };
69
70 /**
71 * Constructor
72 *
73 * @param parent
74 * the parent composite
75 */
3507ca88 76 public AbstractSegmentStoreStatisticsViewer(Composite parent) {
ce8319b6 77 super(parent, false);
53f46dc0 78 setLabelProvider(new SegmentStoreStatisticsLabelProvider());
a1e4b7e8
BH
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);
88 }
89 }
90 });
91 Menu tablePopup = fTablePopupMenuManager.createContextMenu(getTreeViewer().getTree());
92 getTreeViewer().getTree().setMenu(tablePopup);
ce8319b6
BH
93 }
94
53f46dc0
BH
95 /** Provides label for the Segment Store tree viewer cells */
96 protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider {
ce8319b6
BH
97
98 @Override
99 public String getColumnText(@Nullable Object element, int columnIndex) {
a0acb044 100 String value = ""; //$NON-NLS-1$
ce8319b6
BH
101 if (element instanceof HiddenTreeViewerEntry) {
102 if (columnIndex == 0) {
103 value = ((HiddenTreeViewerEntry) element).getName();
104 }
53f46dc0
BH
105 } else if (element instanceof SegmentStoreStatisticsEntry) {
106 SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element;
1198ba6e 107 if (columnIndex == 0) {
0e4f957e 108 return String.valueOf(entry.getName());
1198ba6e
BH
109 }
110 if (entry.getEntry().getNbSegments() > 0) {
111 if (columnIndex == 1) {
112 value = toFormattedString(entry.getEntry().getMin());
ce8319b6 113 } else if (columnIndex == 2) {
1198ba6e 114 value = String.valueOf(toFormattedString(entry.getEntry().getMax()));
ce8319b6 115 } else if (columnIndex == 3) {
1198ba6e 116 value = String.valueOf(toFormattedString(entry.getEntry().getAverage()));
a0acb044
MK
117 } else if (columnIndex == 4) {
118 value = String.valueOf(toFormattedString(entry.getEntry().getStdDev()));
8f6e9344
JCK
119 } else if (columnIndex == 5) {
120 value = String.valueOf(entry.getEntry().getNbSegments());
ce8319b6
BH
121 }
122 }
123 }
124 return checkNotNull(value);
125 }
126 }
127
128 /**
129 * Creates the statistics analysis module
130 *
131 * @return the statistics analysis module
132 */
a0acb044
MK
133 @Nullable
134 protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule();
ce8319b6
BH
135
136 /**
137 * Gets the statistics analysis module
a0acb044 138 *
ce8319b6
BH
139 * @return the statistics analysis module
140 */
a0acb044
MK
141 @Nullable
142 public TmfAbstractAnalysisModule getStatisticsAnalysisModule() {
ce8319b6
BH
143 return fModule;
144 }
145
146 @Override
147 protected ITmfTreeColumnDataProvider getColumnDataProvider() {
148 return new ITmfTreeColumnDataProvider() {
149
150 @Override
4c4e2816 151 public List<@Nullable TmfTreeColumnData> getColumnData() {
ce8319b6 152 /* All columns are sortable */
4c4e2816 153 List<@Nullable TmfTreeColumnData> columns = new ArrayList<>();
ce8319b6 154 TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]);
a0acb044 155 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
156 column.setComparator(new ViewerComparator() {
157 @Override
158 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
159 if ((e1 == null) || (e2 == null)) {
160 return 0;
161 }
162
53f46dc0
BH
163 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
164 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
165
166 return n1.getName().compareTo(n2.getName());
167
168 }
169 });
170 columns.add(column);
171 column = new TmfTreeColumnData(COLUMN_NAMES[1]);
a0acb044 172 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
173 column.setComparator(new ViewerComparator() {
174 @Override
175 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
176 if ((e1 == null) || (e2 == null)) {
177 return 0;
178 }
179
53f46dc0
BH
180 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
181 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
182
183 return Long.compare(n1.getEntry().getMin(), n2.getEntry().getMin());
184
185 }
186 });
187 columns.add(column);
188 column = new TmfTreeColumnData(COLUMN_NAMES[2]);
a0acb044 189 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
190 column.setComparator(new ViewerComparator() {
191 @Override
192 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
193 if ((e1 == null) || (e2 == null)) {
194 return 0;
195 }
196
53f46dc0
BH
197 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
198 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
199
200 return Long.compare(n1.getEntry().getMax(), n2.getEntry().getMax());
201
202 }
203 });
204 columns.add(column);
205 column = new TmfTreeColumnData(COLUMN_NAMES[3]);
a0acb044 206 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
207 column.setComparator(new ViewerComparator() {
208 @Override
209 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
210 if ((e1 == null) || (e2 == null)) {
211 return 0;
212 }
213
53f46dc0
BH
214 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
215 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
216
217 return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage());
218
219 }
220 });
221 columns.add(column);
a0acb044
MK
222 column = new TmfTreeColumnData(COLUMN_NAMES[4]);
223 column.setAlignment(SWT.RIGHT);
224 column.setComparator(new ViewerComparator() {
225 @Override
226 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
227 if ((e1 == null) || (e2 == null)) {
228 return 0;
229 }
230
231 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
232 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6 233
a0acb044
MK
234 return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev());
235
236 }
237 });
238 columns.add(column);
8f6e9344
JCK
239 column = new TmfTreeColumnData(COLUMN_NAMES[5]);
240 column.setAlignment(SWT.RIGHT);
241 column.setComparator(new ViewerComparator() {
242 @Override
243 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
244 if ((e1 == null) || (e2 == null)) {
245 return 0;
246 }
247
248 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
249 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
250
251 return Long.compare(n1.getEntry().getNbSegments(), n2.getEntry().getNbSegments());
252
253 }
254 });
255 columns.add(column);
a0acb044
MK
256 column = new TmfTreeColumnData(""); //$NON-NLS-1$
257 columns.add(column);
ce8319b6
BH
258 return columns;
259 }
260
261 };
262 }
263
ce8319b6
BH
264 @Override
265 public void initializeDataSource() {
0b9fc6da
JCK
266 ITmfTrace trace = getTrace();
267 if (trace != null) {
268 TmfAbstractAnalysisModule module = createStatisticsAnalysiModule();
269 if (module == null) {
270 return;
271 }
272 try {
273 module.setTrace(trace);
274 module.schedule();
275 fModule = module;
276 } catch (TmfAnalysisException e) {
277 Activator.getDefault().logError("Error initializing statistics analysis module", e); //$NON-NLS-1$
278 }
ce8319b6
BH
279 }
280 }
281
a1e4b7e8
BH
282 /**
283 * Method to add commands to the context sensitive menu.
284 * @param manager
285 * the menu manager
286 * @param sel
287 * the current selection
288 */
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) {
294 @Override
295 public void run() {
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)));
299 }
300 };
301
302 IAction gotoEndTime = new Action(Messages.SegmentStoreStatisticsViewer_GotoMaxAction) {
303 @Override
304 public void run() {
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)));
308 }
309 };
310
311 manager.add(gotoStartTime);
312 manager.add(gotoEndTime);
313 }
314 }
315
ce8319b6
BH
316 /**
317 * Formats a double value string
318 *
319 * @param value
320 * a value to format
321 * @return formatted value
322 */
323 protected static String toFormattedString(double value) {
a0acb044
MK
324 // The cast to long is needed because the formatter cannot truncate the
325 // number.
0e4f957e 326 String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$
ce8319b6
BH
327 return percentageString;
328 }
329
330 /**
331 * Class for defining an entry in the statistics tree.
332 */
53f46dc0 333 protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry {
3507ca88 334
5b901f94 335 private final SegmentStoreStatistics fEntry;
ce8319b6
BH
336
337 /**
338 * Constructor
339 *
340 * @param name
341 * name of entry
342 *
343 * @param entry
53f46dc0 344 * segment store statistics object
ce8319b6 345 */
53f46dc0 346 public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) {
ce8319b6
BH
347 super(name);
348 fEntry = entry;
349 }
350
351 /**
352 * Gets the statistics object
353 *
354 * @return statistics object
355 */
53f46dc0 356 public SegmentStoreStatistics getEntry() {
0e4f957e 357 return fEntry;
ce8319b6
BH
358 }
359
360 }
361
362 /**
363 * Class to define a level in the tree that doesn't have any values.
364 */
53f46dc0 365 protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry {
ce8319b6
BH
366 /**
367 * Constructor
368 *
369 * @param name
370 * the name of the level
371 */
372 public HiddenTreeViewerEntry(String name) {
53f46dc0 373 super(name, new SegmentStoreStatistics());
ce8319b6
BH
374 }
375 }
376
377}
This page took 0.052875 seconds and 5 git commands to generate.