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