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
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
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 *******************************************************************************/
12 package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics;
13
14 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
16 import java.text.Format;
17 import java.util.ArrayList;
18 import java.util.List;
19
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;
45
46 /**
47 * An abstract tree viewer implementation for displaying segment store
48 * statistics
49 *
50 * @author Bernd Hufmann
51 *
52 */
53 public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer {
54
55 private static final Format FORMATTER = new SubSecondTimeWithUnitFormat();
56
57 @Nullable
58 private TmfAbstractAnalysisModule fModule;
59 private MenuManager fTablePopupMenuManager;
60
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)
68 };
69
70 /**
71 * Constructor
72 *
73 * @param parent
74 * the parent composite
75 */
76 public AbstractSegmentStoreStatisticsViewer(Composite parent) {
77 super(parent, false);
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);
88 }
89 }
90 });
91 Menu tablePopup = fTablePopupMenuManager.createContextMenu(getTreeViewer().getTree());
92 getTreeViewer().getTree().setMenu(tablePopup);
93 }
94
95 /** Provides label for the Segment Store tree viewer cells */
96 protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider {
97
98 @Override
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();
104 }
105 } else if (element instanceof SegmentStoreStatisticsEntry) {
106 SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element;
107 if (columnIndex == 0) {
108 return String.valueOf(entry.getName());
109 }
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());
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 */
133 @Nullable
134 protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule();
135
136 /**
137 * Gets the statistics analysis module
138 *
139 * @return the statistics analysis module
140 */
141 @Nullable
142 public TmfAbstractAnalysisModule getStatisticsAnalysisModule() {
143 return fModule;
144 }
145
146 @Override
147 protected ITmfTreeColumnDataProvider getColumnDataProvider() {
148 return new ITmfTreeColumnDataProvider() {
149
150 @Override
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() {
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
163 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
164 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
165
166 return n1.getName().compareTo(n2.getName());
167
168 }
169 });
170 columns.add(column);
171 column = new TmfTreeColumnData(COLUMN_NAMES[1]);
172 column.setAlignment(SWT.RIGHT);
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
180 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
181 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
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]);
189 column.setAlignment(SWT.RIGHT);
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
197 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
198 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
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]);
206 column.setAlignment(SWT.RIGHT);
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
214 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
215 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
216
217 return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage());
218
219 }
220 });
221 columns.add(column);
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;
233
234 return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev());
235
236 }
237 });
238 columns.add(column);
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);
256 column = new TmfTreeColumnData(""); //$NON-NLS-1$
257 columns.add(column);
258 return columns;
259 }
260
261 };
262 }
263
264 @Override
265 public void initializeDataSource() {
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 }
279 }
280 }
281
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
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) {
324 // The cast to long is needed because the formatter cannot truncate the
325 // number.
326 String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$
327 return percentageString;
328 }
329
330 /**
331 * Class for defining an entry in the statistics tree.
332 */
333 protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry {
334
335 private final SegmentStoreStatistics fEntry;
336
337 /**
338 * Constructor
339 *
340 * @param name
341 * name of entry
342 *
343 * @param entry
344 * segment store statistics object
345 */
346 public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) {
347 super(name);
348 fEntry = entry;
349 }
350
351 /**
352 * Gets the statistics object
353 *
354 * @return statistics object
355 */
356 public SegmentStoreStatistics getEntry() {
357 return fEntry;
358 }
359
360 }
361
362 /**
363 * Class to define a level in the tree that doesn't have any values.
364 */
365 protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry {
366 /**
367 * Constructor
368 *
369 * @param name
370 * the name of the level
371 */
372 public HiddenTreeViewerEntry(String name) {
373 super(name, new SegmentStoreStatistics());
374 }
375 }
376
377 }
This page took 0.048039 seconds and 6 git commands to generate.