tmf: Bug 490372: Hide PieCharts when trace has only 1 event type
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / viewers / statistics / TmfStatisticsViewer.java
CommitLineData
cfd22ad0 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2012, 2015 Ericsson
cfd22ad0
MD
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial API and implementation
89c06060 11 * Alexandre Montplaisir - Port to ITmfStatistics provider
0fcf3b09 12 * Patrick Tasse - Support selection range
12d85f6c 13 * Bernd Hufmann - Fix range selection updates
cfd22ad0
MD
14 *******************************************************************************/
15
1743f395 16package org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics;
cfd22ad0 17
c14c0757 18import java.util.Collection;
de83d1ab 19import java.util.HashMap;
cfd22ad0 20import java.util.List;
89c06060 21import java.util.Map;
853e2dc8 22import java.util.Map.Entry;
cfd22ad0 23
de83d1ab 24import org.eclipse.core.runtime.jobs.Job;
378e6b92 25import org.eclipse.jface.viewers.StructuredSelection;
cfd22ad0
MD
26import org.eclipse.jface.viewers.TreeViewer;
27import org.eclipse.jface.viewers.TreeViewerColumn;
28import org.eclipse.jface.viewers.Viewer;
29import org.eclipse.jface.viewers.ViewerComparator;
cfd22ad0 30import org.eclipse.swt.SWT;
cdf994ef 31import org.eclipse.swt.custom.SashForm;
cfd22ad0
MD
32import org.eclipse.swt.events.SelectionAdapter;
33import org.eclipse.swt.events.SelectionEvent;
34import org.eclipse.swt.graphics.Color;
35import org.eclipse.swt.graphics.Cursor;
cfd22ad0
MD
36import org.eclipse.swt.widgets.Composite;
37import org.eclipse.swt.widgets.Control;
38import org.eclipse.swt.widgets.Display;
39import org.eclipse.swt.widgets.Event;
40import org.eclipse.swt.widgets.Listener;
853e2dc8
BH
41import org.eclipse.tracecompass.internal.tmf.ui.viewers.piecharts.TmfPieChartViewer;
42import org.eclipse.tracecompass.internal.tmf.ui.viewers.piecharts.model.TmfPieChartStatisticsModel;
43import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfBaseColumnData;
44import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfBaseColumnDataProvider;
45import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfStatisticsFormatter;
46import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
47import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfStatisticsTreeManager;
48import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode;
49import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfTreeContentProvider;
2bdf0193
AM
50import org.eclipse.tracecompass.tmf.core.component.TmfComponent;
51import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
97c71024 52import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
cdf994ef 53import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
2bdf0193 54import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
2bdf0193
AM
55import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsModule;
56import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
57import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
58import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
21852dfa 59import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext;
2bdf0193 60import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
b8585c7c 61import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
5c5fa260 62import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
853e2dc8 63import org.eclipse.tracecompass.tmf.ui.TmfUiRefreshHandler;
2bdf0193 64import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer;
cfd22ad0
MD
65
66/**
67 * A basic viewer to display statistics in the statistics view.
68 *
8b60cb37
MD
69 * It is linked to a single ITmfTrace until its disposal.
70 *
cfd22ad0 71 * @author Mathieu Denis
cfd22ad0 72 */
05627bda 73public class TmfStatisticsViewer extends TmfViewer {
cfd22ad0 74
87f83123
AM
75 /** The actual tree viewer to display */
76 private TreeViewer fTreeViewer;
05627bda 77
87f83123
AM
78 /** Update range synchronization object */
79 private final Object fStatisticsRangeUpdateSyncObj = new Object();
73fbf6be 80
cdf994ef
ACL
81 /** The statistics tree linked to this viewer */
82 private TmfStatisticsTree fStatisticsData;
83
87f83123
AM
84 /** The trace that is displayed by this viewer */
85 private ITmfTrace fTrace;
89c06060 86
87f83123 87 /** Indicates to process all events */
05627bda
MD
88 private boolean fProcessAll;
89
87f83123 90 /** View instance counter (for multiple statistics views) */
cfd22ad0
MD
91 private static int fCountInstance = 0;
92
87f83123 93 /** Number of this instance. Used as an instance ID. */
cfd22ad0
MD
94 private int fInstanceNb;
95
87f83123 96 /** Object to store the cursor while waiting for the trace to load */
cfd22ad0
MD
97 private Cursor fWaitCursor = null;
98
99 /**
05627bda
MD
100 * Counts the number of times waitCursor() has been called. It avoids
101 * removing the waiting cursor, since there may be multiple requests running
102 * at the same time.
103 */
104 private int fWaitCursorCount = 0;
105
87f83123 106 /** Tells to send a time range request when the trace gets updated. */
05627bda
MD
107 private boolean fSendRangeRequest = true;
108
de83d1ab
MAL
109 private final Map<ITmfTrace, Job> fUpdateJobsPartial = new HashMap<>();
110 private final Map<ITmfTrace, Job> fUpdateJobsGlobal = new HashMap<>();
111
cdf994ef
ACL
112 private TmfPieChartViewer fPieChartViewer;
113
114 private SashForm fSash;
de83d1ab 115
cdf994ef 116 private TmfPieChartStatisticsModel fPieChartModel;
de83d1ab 117
05627bda
MD
118 /**
119 * Create a basic statistics viewer. To be used in conjunction with
120 * {@link TmfStatisticsViewer#init(Composite, String, ITmfTrace)}
121 *
122 * @param parent
123 * The parent composite that will hold the viewer
124 * @param viewerName
125 * The name that will be assigned to this viewer
126 * @param trace
127 * The trace that is displayed by this viewer
128 * @see TmfComponent
129 */
130 public TmfStatisticsViewer(Composite parent, String viewerName, ITmfTrace trace) {
131 init(parent, viewerName, trace);
132 }
133
134 /**
135 * Initialize the statistics viewer.
cfd22ad0
MD
136 *
137 * @param parent
05627bda
MD
138 * The parent component of the viewer.
139 * @param viewerName
140 * The name to give to the viewer.
141 * @param trace
142 * The trace that will be displayed by the viewer.
cfd22ad0 143 */
05627bda
MD
144 public void init(Composite parent, String viewerName, ITmfTrace trace) {
145 super.init(parent, viewerName);
cfd22ad0
MD
146 // Increment a counter to make sure the tree ID is unique.
147 fCountInstance++;
148 fInstanceNb = fCountInstance;
05627bda
MD
149 fTrace = trace;
150
faa38350 151 // The viewer will process all events if he is assigned to an experiment
05627bda
MD
152 fProcessAll = (trace instanceof TmfExperiment);
153
154 initContent(parent);
8b60cb37 155 initInput();
05627bda
MD
156 }
157
05627bda
MD
158 @Override
159 public void dispose() {
160 super.dispose();
161 if (fWaitCursor != null) {
162 fWaitCursor.dispose();
163 }
8b60cb37 164
de83d1ab
MAL
165 for (Job j : fUpdateJobsGlobal.values()) {
166 j.cancel();
167 }
168
169 for (Job j : fUpdateJobsPartial.values()) {
170 j.cancel();
171 }
172
66792052 173 // Clean the model for this viewer
36033ff0 174 TmfStatisticsTreeManager.removeStatTreeRoot(getTreeID());
cdf994ef 175 fPieChartViewer.reinitializeCharts();
05627bda
MD
176 }
177
1c0de632
AM
178 // ------------------------------------------------------------------------
179 // Signal handlers
180 // ------------------------------------------------------------------------
89c06060 181
05627bda 182 /**
faa38350 183 * Handles the signal about new trace range.
05627bda
MD
184 *
185 * @param signal
faa38350 186 * The trace range updated signal
05627bda
MD
187 */
188 @TmfSignalHandler
faa38350
PT
189 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
190 ITmfTrace trace = signal.getTrace();
05627bda 191 // validate
faa38350 192 if (!isListeningTo(trace)) {
05627bda
MD
193 return;
194 }
195
3c934968
MD
196 synchronized (fStatisticsRangeUpdateSyncObj) {
197 // Sends the time range request only once from this method.
198 if (fSendRangeRequest) {
199 fSendRangeRequest = false;
21852dfa
AM
200
201 TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext();
202 TmfTimeRange timeRange = ctx.getSelectionRange();
0fcf3b09 203 requestTimeRangeData(trace, timeRange);
3c934968 204 }
05627bda 205 }
faa38350 206 requestData(trace, signal.getRange());
05627bda
MD
207 }
208
0fcf3b09
PT
209 /**
210 * Handles the time synch updated signal. It updates the time range
211 * statistics.
212 *
213 * @param signal
214 * Contains the information about the new selected time range.
97c71024 215 * @since 1.0
0fcf3b09
PT
216 */
217 @TmfSignalHandler
97c71024 218 public void timeSynchUpdated(TmfSelectionRangeUpdatedSignal signal) {
faa38350
PT
219 if (fTrace == null) {
220 return;
221 }
0fcf3b09
PT
222 ITmfTimestamp begin = signal.getBeginTime();
223 ITmfTimestamp end = signal.getEndTime();
224 TmfTimeRange timeRange = new TmfTimeRange(begin, end);
225 requestTimeRangeData(fTrace, timeRange);
05627bda
MD
226 }
227
1c0de632
AM
228 // ------------------------------------------------------------------------
229 // Class methods
230 // ------------------------------------------------------------------------
231
05627bda
MD
232 /*
233 * Returns the primary control associated with this viewer.
234 *
235 * @return the SWT control which displays this viewer's content
236 */
237 @Override
238 public Control getControl() {
cdf994ef 239 return fSash;
05627bda
MD
240 }
241
242 /**
243 * Get the input of the viewer.
244 *
245 * @return an object representing the input of the statistics viewer.
246 */
247 public Object getInput() {
248 return fTreeViewer.getInput();
249 }
250
05627bda
MD
251 /**
252 * This method can be overridden to implement another way of representing
253 * the statistics data and to retrieve the information for display.
254 *
255 * @return a TmfStatisticsData object.
256 */
36033ff0 257 public TmfStatisticsTree getStatisticData() {
05627bda 258 if (fStatisticsData == null) {
36033ff0 259 fStatisticsData = new TmfStatisticsTree();
05627bda
MD
260 }
261 return fStatisticsData;
262 }
263
cdf994ef
ACL
264 /**
265 * @return the model of the piecharts in this viewer
266 * @since 2.0
267 */
1904e6a4 268 public TmfPieChartStatisticsModel getPieChartModel() {
cdf994ef
ACL
269 if (fPieChartModel == null) {
270 fPieChartModel = new TmfPieChartStatisticsModel();
1904e6a4
ACL
271 }
272 return fPieChartModel;
cdf994ef
ACL
273 }
274
05627bda
MD
275 /**
276 * Returns a unique ID based on name to be associated with the statistics
277 * tree for this viewer. For a same name, it will always return the same ID.
278 *
279 * @return a unique statistics tree ID.
280 */
281 public String getTreeID() {
282 return getName() + fInstanceNb;
283 }
284
285 @Override
286 public void refresh() {
287 final Control viewerControl = getControl();
288 // Ignore update if disposed
289 if (viewerControl.isDisposed()) {
290 return;
291 }
cdf994ef
ACL
292 refreshTree();
293 refreshPieCharts(true, true);
294 }
295
296 /**
297 * Only refreshes the Tree viewer
298 */
1904e6a4 299 private void refreshTree() {
cdf994ef
ACL
300 final Control viewerControl = getControl();
301 // Ignore update if disposed
302 if (viewerControl.isDisposed()) {
303 return;
304 }
05627bda 305
cdf994ef 306 Display.getDefault().asyncExec(new Runnable() {
05627bda
MD
307 @Override
308 public void run() {
309 if (!viewerControl.isDisposed()) {
310 fTreeViewer.refresh();
311 }
312 }
313 });
314 }
315
cdf994ef
ACL
316 /**
317 * Only refreshes the piecharts depending on the parameters
1904e6a4
ACL
318 *
319 * @param refreshGlobal
320 * if we have to refresh the global piechart
321 * @param refreshSelection
322 * if we have to refresh the selection piechart
cdf994ef
ACL
323 * @since 2.0
324 */
325 protected void refreshPieCharts(final boolean refreshGlobal, final boolean refreshSelection) {
326 final Control viewerControl = getControl();
327 // Ignore update if disposed
328 if (viewerControl.isDisposed()) {
329 return;
330 }
331
853e2dc8
BH
332 TmfPieChartStatisticsModel pieChartModel = getPieChartModel();
333 if (pieChartModel == null || pieChartModel.getPieChartGlobalModel() == null) {
334 return;
335 }
336 /* If there's only one event type, don't show any piechart */
337 boolean moreThanOne = false;
338 for (Entry<ITmfTrace, Map<String, Long>> entry : pieChartModel.getPieChartGlobalModel().entrySet()) {
339 if(entry.getValue() != null && entry.getValue().size() > 1) {
340 moreThanOne = true;
341 break;
342 }
343 }
344
345 setPieChartsVisible(moreThanOne);
346
cdf994ef
ACL
347 Display.getDefault().asyncExec(new Runnable() {
348 @Override
349 public void run() {
350 if (!viewerControl.isDisposed()) {
351 fPieChartViewer.refresh(refreshGlobal, refreshSelection);
352 }
353 }
354 });
355 }
356
3c934968
MD
357 /**
358 * Will force a request on the partial event count if one is needed.
359 */
360 public void sendPartialRequestOnNextUpdate() {
361 synchronized (fStatisticsRangeUpdateSyncObj) {
362 fSendRangeRequest = true;
363 }
364 }
365
05627bda
MD
366 /**
367 * Focus on the statistics tree of the viewer
368 */
369 public void setFocus() {
370 fTreeViewer.getTree().setFocus();
371 }
372
05627bda
MD
373 /**
374 * Cancels the request if it is not already completed
375 *
376 * @param request
377 * The request to be canceled
378 */
fd3f1eff 379 protected void cancelOngoingRequest(ITmfEventRequest request) {
05627bda
MD
380 if (request != null && !request.isCompleted()) {
381 request.cancel();
382 }
383 }
384
385 /**
386 * This method can be overridden to change the representation of the data in
387 * the columns.
388 *
b3a26928 389 * @return An object of type {@link TmfBaseColumnDataProvider}.
05627bda 390 */
b3a26928 391 protected TmfBaseColumnDataProvider getColumnDataProvider() {
05627bda
MD
392 return new TmfBaseColumnDataProvider();
393 }
cfd22ad0 394
05627bda
MD
395 /**
396 * Initialize the content that will be drawn in this viewer
397 *
398 * @param parent
399 * The parent of the control to create
400 */
401 protected void initContent(Composite parent) {
cdf994ef 402
cfd22ad0 403 final List<TmfBaseColumnData> columnDataList = getColumnDataProvider().getColumnData();
cfd22ad0 404
1904e6a4 405 fSash = new SashForm(parent, SWT.HORIZONTAL);
cdf994ef
ACL
406
407 fTreeViewer = new TreeViewer(fSash, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
408 fPieChartViewer = new TmfPieChartViewer(fSash);
378e6b92
ACL
409 fPieChartViewer.addEventTypeSelectionListener(new Listener() {
410
411 @Override
412 public void handleEvent(Event event) {
413 String eventTypeName = event.text;
414 if (getStatisticData().getRootNode() == null ||
378e6b92
ACL
415 fTreeViewer.getTree() == null) {
416 return;
417 }
418 /* Get all the nodes corresponding to the event name */
419 List<TmfStatisticsTreeNode> nodes = (List<TmfStatisticsTreeNode>) getStatisticData().getRootNode().findChildren(eventTypeName, true);
420 if (nodes.isEmpty()) {
421 /* Shouldn't happen, except for when selecting "Others" */
422 return;
423 }
424 /* Only select the first in the collection */
425 fTreeViewer.setSelection(new StructuredSelection(nodes.get(0)), true);
426 }
427 });
428
429 /* Make sure the sash is split in 2 equal parts */
430 fSash.setWeights(new int[] { 100, 100 });
cdf994ef 431
cfd22ad0
MD
432 fTreeViewer.setContentProvider(new TmfTreeContentProvider());
433 fTreeViewer.getTree().setHeaderVisible(true);
434 fTreeViewer.setUseHashlookup(true);
435
436 // Creates the columns defined by the column data provider
437 for (final TmfBaseColumnData columnData : columnDataList) {
438 final TreeViewerColumn treeColumn = new TreeViewerColumn(fTreeViewer, columnData.getAlignment());
439 treeColumn.getColumn().setText(columnData.getHeader());
440 treeColumn.getColumn().setWidth(columnData.getWidth());
441 treeColumn.getColumn().setToolTipText(columnData.getTooltip());
442
df2b3dbb
VP
443 // If is dummy column
444 if (columnData == columnDataList.get(TmfBaseColumnDataProvider.StatsColumn.DUMMY.getIndex())) {
445 treeColumn.getColumn().setResizable(false);
446 }
447
448 // A comparator is defined.
449 if (columnData.getComparator() != null) {
cfd22ad0
MD
450 // Adds a listener on the columns header for sorting purpose.
451 treeColumn.getColumn().addSelectionListener(new SelectionAdapter() {
452
453 private ViewerComparator reverseComparator;
454
455 @Override
456 public void widgetSelected(SelectionEvent e) {
457 // Initializes the reverse comparator once.
458 if (reverseComparator == null) {
459 reverseComparator = new ViewerComparator() {
460 @Override
461 public int compare(Viewer viewer, Object e1, Object e2) {
462 return -1 * columnData.getComparator().compare(viewer, e1, e2);
463 }
464 };
465 }
466
467 if (fTreeViewer.getTree().getSortDirection() == SWT.UP
468 || fTreeViewer.getTree().getSortColumn() != treeColumn.getColumn()) {
469 /*
df2b3dbb
VP
470 * Puts the descendant order if the old order was up
471 * or if the selected column has changed.
cfd22ad0
MD
472 */
473 fTreeViewer.setComparator(columnData.getComparator());
474 fTreeViewer.getTree().setSortDirection(SWT.DOWN);
475 } else {
476 /*
477 * Puts the ascendant ordering if the selected
478 * column hasn't changed.
479 */
480 fTreeViewer.setComparator(reverseComparator);
481 fTreeViewer.getTree().setSortDirection(SWT.UP);
482 }
483 fTreeViewer.getTree().setSortColumn(treeColumn.getColumn());
484 }
485 });
486 }
487 treeColumn.setLabelProvider(columnData.getLabelProvider());
488 }
489
df2b3dbb 490 // Handler that will draw the percentages and the bar charts.
cfd22ad0
MD
491 fTreeViewer.getTree().addListener(SWT.EraseItem, new Listener() {
492 @Override
493 public void handleEvent(Event event) {
494 if (columnDataList.get(event.index).getPercentageProvider() != null) {
df2b3dbb 495
cfd22ad0
MD
496 TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) event.item.getData();
497
df2b3dbb
VP
498 // If node is hidden, exit immediately.
499 if (TmfBaseColumnDataProvider.HIDDEN_FOLDER_LEVELS.contains(node.getName())) {
cfd22ad0
MD
500 return;
501 }
502
1904e6a4
ACL
503 // Otherwise, get percentage and draw bar and text if
504 // applicable.
df2b3dbb
VP
505 double percentage = columnDataList.get(event.index).getPercentageProvider().getPercentage(node);
506
507 // The item is selected.
508 if ((event.detail & SWT.SELECTED) > 0) {
1904e6a4
ACL
509 // Draws our own background to avoid overwriting the
510 // bar.
cfd22ad0
MD
511 event.gc.fillRectangle(event.x, event.y, event.width, event.height);
512 event.detail &= ~SWT.SELECTED;
513 }
514
df2b3dbb
VP
515 // Drawing the percentage text
516 // if events are present in top node
517 // and the current node is not the top node
518 // and if is total or partial events column.
519 // If not, exit the method.
520 if (!((event.index == TmfBaseColumnDataProvider.StatsColumn.TOTAL.getIndex() || event.index == TmfBaseColumnDataProvider.StatsColumn.PARTIAL.getIndex())
521 && node != node.getTop())) {
522 return;
523 }
524
525 long eventValue = event.index == TmfBaseColumnDataProvider.StatsColumn.TOTAL.getIndex() ?
526 node.getTop().getValues().getTotal() : node.getTop().getValues().getPartial();
527
528 if (eventValue != 0) {
529
530 int oldAlpha = event.gc.getAlpha();
531 Color oldForeground = event.gc.getForeground();
532 Color oldBackground = event.gc.getBackground();
533
534 // Bar to draw
535 if (percentage != 0) {
536 /*
537 * Draws a transparent gradient rectangle from the
538 * color of foreground and background.
539 */
540 int barWidth = (int) ((fTreeViewer.getTree().getColumn(event.index).getWidth() - 8) * percentage);
541 event.gc.setAlpha(64);
542 event.gc.setForeground(event.item.getDisplay().getSystemColor(SWT.COLOR_BLUE));
543 event.gc.setBackground(event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
544 event.gc.fillGradientRectangle(event.x, event.y, barWidth, event.height, true);
545 event.gc.drawRectangle(event.x, event.y, barWidth, event.height);
546
547 // Restore old values
548 event.gc.setBackground(oldBackground);
549 event.gc.setAlpha(oldAlpha);
550 event.detail &= ~SWT.BACKGROUND;
551
552 }
553
554 String percentageText = TmfStatisticsFormatter.toPercentageText(percentage);
555 String absoluteNumberText = TmfStatisticsFormatter.toColumnData(node, TmfBaseColumnDataProvider.StatsColumn.getColumn(event.index));
556
557 if (event.width > event.gc.stringExtent(percentageText).x + event.gc.stringExtent(absoluteNumberText).x) {
558 int textHeight = event.gc.stringExtent(percentageText).y;
559 event.gc.setForeground(event.item.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
560 event.gc.drawText(percentageText, event.x, event.y + (event.height - textHeight) / 2, true);
561 }
562
563 // Restores old values
564 event.gc.setForeground(oldForeground);
565
566 }
cfd22ad0
MD
567 }
568 }
df2b3dbb 569
cfd22ad0
MD
570 });
571
572 // Initializes the comparator parameters
573 fTreeViewer.setComparator(columnDataList.get(0).getComparator());
574 fTreeViewer.getTree().setSortColumn(fTreeViewer.getTree().getColumn(0));
575 fTreeViewer.getTree().setSortDirection(SWT.DOWN);
576 }
577
8b60cb37
MD
578 /**
579 * Initializes the input for the tree viewer.
8b60cb37
MD
580 */
581 protected void initInput() {
582 String treeID = getTreeID();
faa38350 583 TmfStatisticsTreeNode statisticsTreeNode;
36033ff0 584 if (TmfStatisticsTreeManager.containsTreeRoot(treeID)) {
faa38350
PT
585 // The statistics root is already present
586 statisticsTreeNode = TmfStatisticsTreeManager.getStatTreeRoot(treeID);
8b60cb37
MD
587
588 // Checks if the trace is already in the statistics tree.
faa38350 589 int numNodeTraces = statisticsTreeNode.getNbChildren();
8b60cb37 590
c14c0757
GB
591 Collection<ITmfTrace> traces = TmfTraceManager.getTraceSet(fTrace);
592 int numTraces = traces.size();
8b60cb37
MD
593
594 if (numTraces == numNodeTraces) {
595 boolean same = true;
596 /*
597 * Checks if the experiment contains the same traces as when
598 * previously selected.
599 */
c14c0757
GB
600 for (ITmfTrace trace : traces) {
601 String traceName = trace.getName();
faa38350 602 if (!statisticsTreeNode.containsChild(traceName)) {
8b60cb37
MD
603 same = false;
604 break;
605 }
606 }
607
608 if (same) {
609 // No need to reload data, all traces are already loaded
faa38350 610 fTreeViewer.setInput(statisticsTreeNode);
8b60cb37
MD
611 return;
612 }
613 // Clears the old content to start over
faa38350 614 statisticsTreeNode.reset();
8b60cb37
MD
615 }
616 } else {
617 // Creates a new tree
faa38350 618 statisticsTreeNode = TmfStatisticsTreeManager.addStatsTreeRoot(treeID, getStatisticData());
8b60cb37
MD
619 }
620
621 // Sets the input to a clean data model
faa38350 622 fTreeViewer.setInput(statisticsTreeNode);
cdf994ef
ACL
623 /* Set a new model for the piecharts and keep a reference */
624 fPieChartViewer.setInput(getPieChartModel());
8b60cb37
MD
625 }
626
cfd22ad0 627 /**
faa38350 628 * Tells if the viewer is listening to a trace.
cfd22ad0 629 *
1c0de632 630 * @param trace
05627bda
MD
631 * The trace that the viewer may be listening
632 * @return true if the viewer is listening to the trace, false otherwise
cfd22ad0 633 */
1c0de632
AM
634 protected boolean isListeningTo(ITmfTrace trace) {
635 if (fProcessAll || trace == fTrace) {
05627bda 636 return true;
cfd22ad0 637 }
05627bda 638 return false;
cfd22ad0
MD
639 }
640
641 /**
faa38350 642 * Called when an trace request has been completed successfully.
cfd22ad0 643 *
05627bda
MD
644 * @param global
645 * Tells if the request is a global or time range (partial)
646 * request.
cfd22ad0 647 */
05627bda 648 protected void modelComplete(boolean global) {
cdf994ef 649 refreshTree();
05627bda 650 waitCursor(false);
cfd22ad0
MD
651 }
652
653 /**
faa38350 654 * Sends the request to the trace for the whole trace
cfd22ad0 655 *
faa38350
PT
656 * @param trace
657 * The trace used to send the request
8b260d9f 658 * @param timeRange
faa38350 659 * The range to request to the trace
cfd22ad0 660 */
faa38350
PT
661 protected void requestData(final ITmfTrace trace, final TmfTimeRange timeRange) {
662 buildStatisticsTree(trace, timeRange, true);
cfd22ad0
MD
663 }
664
665 /**
faa38350 666 * Sends the time range request from the trace
cfd22ad0 667 *
faa38350
PT
668 * @param trace
669 * The trace used to send the request
8b260d9f 670 * @param timeRange
faa38350 671 * The range to request to the trace
cfd22ad0 672 */
faa38350 673 protected void requestTimeRangeData(final ITmfTrace trace, final TmfTimeRange timeRange) {
faa38350 674 buildStatisticsTree(trace, timeRange, false);
89c06060
AM
675 }
676
677 /**
df2b3dbb
VP
678 * Requests all the data of the trace to the state system which contains
679 * information about the statistics.
89c06060 680 *
df2b3dbb
VP
681 * Since the viewer may be listening to multiple traces, it may receive an
682 * experiment rather than a single trace. The filtering is done with the
89c06060
AM
683 * method {@link #isListeningTo(String trace)}.
684 *
faa38350
PT
685 * @param trace
686 * The trace for which a request must be done
89c06060
AM
687 * @param timeRange
688 * The time range that will be requested to the state system
689 * @param isGlobal
690 * Tells if the request is for the global event count or the
691 * partial one.
692 */
d6b46913 693 private void buildStatisticsTree(final ITmfTrace trace, final TmfTimeRange timeRange, final boolean isGlobal) {
36033ff0 694 final TmfStatisticsTree statsData = TmfStatisticsTreeManager.getStatTree(getTreeID());
89c06060
AM
695 if (statsData == null) {
696 return;
697 }
698
de83d1ab
MAL
699 Map<ITmfTrace, Job> updateJobs;
700 if (isGlobal) {
701 updateJobs = fUpdateJobsGlobal;
de83d1ab
MAL
702 } else {
703 updateJobs = fUpdateJobsPartial;
de83d1ab 704 }
89c06060 705
1d83ed07 706 for (ITmfTrace aTrace : TmfTraceManager.getTraceSet(trace)) {
de83d1ab
MAL
707 if (!isListeningTo(aTrace)) {
708 continue;
709 }
d6b46913 710
de83d1ab 711 /* Retrieve the statistics object */
b8585c7c 712 final TmfStatisticsModule statsMod = TmfTraceUtils.getAnalysisModuleOfClass(aTrace, TmfStatisticsModule.class, TmfStatisticsModule.ID);
de83d1ab
MAL
713 if (statsMod == null) {
714 /* No statistics module available for this trace */
715 continue;
716 }
55954069 717
de83d1ab
MAL
718 Job job = updateJobs.get(aTrace);
719 if (job == null) {
cdf994ef 720 job = new StatisticsUpdateJob("Statistics update", aTrace, isGlobal, timeRange, statsMod, this); //$NON-NLS-1$
de83d1ab
MAL
721 updateJobs.put(aTrace, job);
722 job.setSystem(true);
723 job.schedule();
89c06060 724 }
89c06060 725 }
cfd22ad0
MD
726 }
727
853e2dc8
BH
728 private void setPieChartsVisible(final boolean visible) {
729 if (fPieChartViewer.isDisposed()) {
730 return;
731 }
732 TmfUiRefreshHandler.getInstance().queueUpdate(fPieChartViewer, new Runnable() {
733 @Override
734 public void run() {
735 if (!fPieChartViewer.isDisposed()) {
736 fPieChartViewer.setVisible(visible);
737 fPieChartViewer.getParent().layout();
738 }
739 }
740 });
741 }
742
cfd22ad0 743 /**
05627bda 744 * Resets the number of events within the time range
cfd22ad0 745 */
05627bda 746 protected void resetTimeRangeValue() {
36033ff0 747 TmfStatisticsTreeNode treeModelRoot = TmfStatisticsTreeManager.getStatTreeRoot(getTreeID());
05627bda
MD
748 if (treeModelRoot != null && treeModelRoot.hasChildren()) {
749 treeModelRoot.resetTimeRangeValue();
750 }
cfd22ad0
MD
751 }
752
753 /**
df2b3dbb
VP
754 * When the trace is loading the cursor will be different so the user knows
755 * that the processing is not finished yet.
05627bda
MD
756 *
757 * Calls to this method are stacked.
cfd22ad0 758 *
05627bda 759 * @param waitRequested
cfd22ad0 760 * Indicates if we need to show the waiting cursor, or the
05627bda 761 * default one.
cfd22ad0 762 */
05627bda 763 protected void waitCursor(final boolean waitRequested) {
cfd22ad0
MD
764 if ((fTreeViewer == null) || (fTreeViewer.getTree().isDisposed())) {
765 return;
766 }
767
05627bda 768 boolean needsUpdate = false;
cfd22ad0 769 Display display = fTreeViewer.getControl().getDisplay();
05627bda
MD
770 if (waitRequested) {
771 fWaitCursorCount++;
772 if (fWaitCursor == null) { // The cursor hasn't been initialized yet
773 fWaitCursor = new Cursor(display, SWT.CURSOR_WAIT);
774 }
775 if (fWaitCursorCount == 1) { // The cursor is not in waiting mode
776 needsUpdate = true;
777 }
778 } else {
779 if (fWaitCursorCount > 0) { // The cursor is in waiting mode
780 fWaitCursorCount--;
781 if (fWaitCursorCount == 0) { // No more reason to wait
782 // Put back the default cursor
783 needsUpdate = true;
784 }
785 }
cfd22ad0
MD
786 }
787
05627bda
MD
788 if (needsUpdate) {
789 // Performs the updates on the UI thread
790 display.asyncExec(new Runnable() {
791 @Override
792 public void run() {
793 if ((fTreeViewer != null)
794 && (!fTreeViewer.getTree().isDisposed())) {
795 Cursor cursor = null; // indicates default
796 if (waitRequested) {
797 cursor = fWaitCursor;
798 }
799 fTreeViewer.getControl().setCursor(cursor);
cfd22ad0 800 }
cfd22ad0 801 }
05627bda
MD
802 });
803 }
cfd22ad0 804 }
cdf994ef
ACL
805
806 /**
1904e6a4
ACL
807 * @param isGlobal
808 * if the job to remove is global or partial
809 * @param jobTrace
810 * The trace
cdf994ef
ACL
811 */
812 void removeFromJobs(boolean isGlobal, ITmfTrace jobTrace) {
813 Map<ITmfTrace, Job> updateJobs = isGlobal ? fUpdateJobsGlobal : fUpdateJobsPartial;
814 Job job = updateJobs.remove(jobTrace);
815 if (job != null) {
816 job.cancel();
817 }
818 }
cfd22ad0 819}
This page took 0.126986 seconds and 5 git commands to generate.