tmf: Make entry comparator apply recursively in AbstractTimeGraphView
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / timegraph / AbstractTimeGraphView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
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 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated signal handling
12 * Geneviève Bastien - Move code to provide base classes for time graph view
13 * Marc-Andre Laperle - Add time zone preference
14 * Geneviève Bastien - Add event links between entries
15 *******************************************************************************/
16
17 package org.eclipse.tracecompass.tmf.ui.views.timegraph;
18
19 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.concurrent.CopyOnWriteArrayList;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.core.resources.IMarker;
35 import org.eclipse.core.resources.IMarkerDelta;
36 import org.eclipse.core.resources.IResource;
37 import org.eclipse.core.resources.IResourceChangeEvent;
38 import org.eclipse.core.resources.IResourceChangeListener;
39 import org.eclipse.core.resources.IWorkspaceRunnable;
40 import org.eclipse.core.resources.ResourcesPlugin;
41 import org.eclipse.core.runtime.CoreException;
42 import org.eclipse.core.runtime.IProgressMonitor;
43 import org.eclipse.core.runtime.NullProgressMonitor;
44 import org.eclipse.jdt.annotation.NonNull;
45 import org.eclipse.jdt.annotation.Nullable;
46 import org.eclipse.jface.action.Action;
47 import org.eclipse.jface.action.IAction;
48 import org.eclipse.jface.action.IMenuManager;
49 import org.eclipse.jface.action.IStatusLineManager;
50 import org.eclipse.jface.action.IToolBarManager;
51 import org.eclipse.jface.action.Separator;
52 import org.eclipse.jface.viewers.AbstractTreeViewer;
53 import org.eclipse.jface.viewers.ILabelProvider;
54 import org.eclipse.jface.viewers.ILabelProviderListener;
55 import org.eclipse.jface.viewers.ISelectionProvider;
56 import org.eclipse.jface.viewers.ITableLabelProvider;
57 import org.eclipse.jface.viewers.ITreeContentProvider;
58 import org.eclipse.jface.viewers.TreeViewer;
59 import org.eclipse.jface.viewers.ViewerFilter;
60 import org.eclipse.osgi.util.NLS;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.graphics.Color;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.widgets.Composite;
65 import org.eclipse.swt.widgets.Display;
66 import org.eclipse.swt.widgets.TreeColumn;
67 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
68 import org.eclipse.tracecompass.tmf.core.resources.ITmfMarker;
69 import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
70 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
71 import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
72 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
73 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
74 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
75 import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
76 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
77 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
78 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
79 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
80 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceAdapterManager;
81 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext;
82 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
83 import org.eclipse.tracecompass.tmf.ui.TmfUiRefreshHandler;
84 import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo;
85 import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned;
86 import org.eclipse.tracecompass.tmf.ui.views.TmfView;
87 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphBookmarkListener;
88 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphContentProvider;
89 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
90 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphRangeListener;
91 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphSelectionListener;
92 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener;
93 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphBookmarkEvent;
94 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphCombo;
95 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphContentProvider;
96 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
97 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphRangeUpdateEvent;
98 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent;
99 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphViewer;
100 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
101 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent;
102 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEventSource;
103 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
104 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
105 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.MarkerEvent;
106 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
107 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
108 import org.eclipse.ui.IActionBars;
109
110 /**
111 * An abstract view all time graph views can inherit
112 *
113 * This view contains either a time graph viewer, or a time graph combo which is
114 * divided between a tree viewer on the left and a time graph viewer on the right.
115 */
116 public abstract class AbstractTimeGraphView extends TmfView implements ITmfTimeAligned, IResourceChangeListener {
117
118 /** Constant indicating that all levels of the time graph should be expanded */
119 protected static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS;
120
121 private static final Pattern RGBA_PATTERN = Pattern.compile("RGBA \\{(\\d+), (\\d+), (\\d+), (\\d+)\\}"); //$NON-NLS-1$
122
123 /**
124 * Redraw state enum
125 */
126 private enum State {
127 IDLE, BUSY, PENDING
128 }
129
130 // ------------------------------------------------------------------------
131 // Fields
132 // ------------------------------------------------------------------------
133
134 /** The timegraph wrapper */
135 private ITimeGraphWrapper fTimeGraphWrapper;
136
137 /** The selected trace */
138 private ITmfTrace fTrace;
139
140 /** The selected trace editor file*/
141 private IFile fEditorFile;
142
143 /** The timegraph entry list */
144 private List<TimeGraphEntry> fEntryList;
145
146 /** The trace to entry list hash map */
147 private final Map<ITmfTrace, List<TimeGraphEntry>> fEntryListMap = new HashMap<>();
148
149 /** The trace to filters hash map */
150 private final Map<ITmfTrace, ViewerFilter[]> fFiltersMap = new HashMap<>();
151
152 /** The trace to marker event sources hash map */
153 private final Map<ITmfTrace, List<IMarkerEventSource>> fMarkerEventSourcesMap = new HashMap<>();
154
155 /** The trace to build thread hash map */
156 private final Map<ITmfTrace, BuildThread> fBuildThreadMap = new HashMap<>();
157
158 /** The start time */
159 private long fStartTime = SWT.DEFAULT;
160
161 /** The end time */
162 private long fEndTime = SWT.DEFAULT;
163
164 /** The display width */
165 private final int fDisplayWidth;
166
167 /** The zoom thread */
168 private ZoomThread fZoomThread;
169
170 /** The next resource action */
171 private Action fNextResourceAction;
172
173 /** The previous resource action */
174 private Action fPreviousResourceAction;
175
176 /** A comparator class */
177 private Comparator<ITimeGraphEntry> fEntryComparator = null;
178
179 /** The redraw state used to prevent unnecessary queuing of display runnables */
180 private State fRedrawState = State.IDLE;
181
182 /** The redraw synchronization object */
183 private final Object fSyncObj = new Object();
184
185 /** The presentation provider for this view */
186 private final TimeGraphPresentationProvider fPresentation;
187
188 /** The tree column label array, or null if combo is not used */
189 private String[] fColumns;
190
191 /** The tree label provider, or null if combo is not used */
192 private TreeLabelProvider fLabelProvider = null;
193
194 /** The time graph content provider */
195 private @NonNull ITimeGraphContentProvider fTimeGraphContentProvider = new TimeGraphContentProvider();
196
197 /** The relative weight of the sash, ignored if combo is not used */
198 private int[] fWeight = { 1, 3 };
199
200 /** The filter column label array, or null if filter is not used */
201 private String[] fFilterColumns;
202
203 /** The pack done flag */
204 private boolean fPackDone = false;
205
206 /** The filter label provider, or null if filter is not used */
207 private TreeLabelProvider fFilterLabelProvider;
208
209 private int fAutoExpandLevel = ALL_LEVELS;
210
211 /** The list of color resources created by this view */
212 private final List<Color> fColors = new ArrayList<>();
213
214 // ------------------------------------------------------------------------
215 // Classes
216 // ------------------------------------------------------------------------
217
218 private interface ITimeGraphWrapper {
219
220 void setTimeGraphContentProvider(ITimeGraphContentProvider timeGraphContentProvider);
221
222 void setTimeGraphPresentationProvider(TimeGraphPresentationProvider timeGraphPresentationProvider);
223
224 TimeGraphViewer getTimeGraphViewer();
225
226 void addSelectionListener(ITimeGraphSelectionListener listener);
227
228 ISelectionProvider getSelectionProvider();
229
230 void setFocus();
231
232 boolean isDisposed();
233
234 void refresh();
235
236 void setInput(Object input);
237
238 Object getInput();
239
240 void setFilters(ViewerFilter[] filters);
241
242 ViewerFilter[] getFilters();
243
244 void redraw();
245
246 void update();
247
248 void setAutoExpandLevel(int level);
249
250 void setFilterColumns(String[] columnNames);
251
252 void setFilterContentProvider(ITreeContentProvider contentProvider);
253
254 void setFilterLabelProvider(ITableLabelProvider labelProvider);
255
256 IAction getShowFilterDialogAction();
257
258 void performAlign(int offset, int width);
259
260 TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo();
261
262 int getAvailableWidth(int requestedOffset);
263 }
264
265 private class TimeGraphViewerWrapper implements ITimeGraphWrapper {
266 private TimeGraphViewer viewer;
267
268 private TimeGraphViewerWrapper(Composite parent, int style) {
269 viewer = new TimeGraphViewer(parent, style);
270 }
271
272 @Override
273 public void setTimeGraphContentProvider(ITimeGraphContentProvider timeGraphContentProvider) {
274 viewer.setTimeGraphContentProvider(timeGraphContentProvider);
275 }
276
277 @Override
278 public void setTimeGraphPresentationProvider(TimeGraphPresentationProvider timeGraphPresentationProvider) {
279 viewer.setTimeGraphProvider(timeGraphPresentationProvider);
280 }
281
282 @Override
283 public TimeGraphViewer getTimeGraphViewer() {
284 return viewer;
285 }
286
287 @Override
288 public void addSelectionListener(ITimeGraphSelectionListener listener) {
289 viewer.addSelectionListener(listener);
290 }
291
292 @Override
293 public ISelectionProvider getSelectionProvider() {
294 return viewer.getSelectionProvider();
295 }
296
297 @Override
298 public void setFocus() {
299 viewer.setFocus();
300 }
301
302 @Override
303 public boolean isDisposed() {
304 return viewer.getControl().isDisposed();
305 }
306
307 @Override
308 public void setInput(Object input) {
309 viewer.setInput(input);
310 }
311
312 @Override
313 public Object getInput() {
314 return viewer.getInput();
315 }
316
317 @Override
318 public void setFilterColumns(String[] columnNames) {
319 viewer.setFilterColumns(columnNames);
320 }
321
322 @Override
323 public void setFilterContentProvider(ITreeContentProvider contentProvider) {
324 viewer.setFilterContentProvider(contentProvider);
325 }
326
327 @Override
328 public void setFilterLabelProvider(ITableLabelProvider labelProvider) {
329 viewer.setFilterLabelProvider(labelProvider);
330 }
331
332 @Override
333 public void setFilters(ViewerFilter[] filters) {
334 viewer.setFilters(filters);
335 }
336
337 @Override
338 public ViewerFilter[] getFilters() {
339 return viewer.getFilters();
340 }
341
342 @Override
343 public IAction getShowFilterDialogAction() {
344 return viewer.getShowFilterDialogAction();
345 }
346
347 @Override
348 public void refresh() {
349 viewer.refresh();
350 }
351
352 @Override
353 public void redraw() {
354 viewer.getControl().redraw();
355 }
356
357 @Override
358 public void update() {
359 viewer.getControl().update();
360 }
361
362 @Override
363 public void setAutoExpandLevel(int level) {
364 viewer.setAutoExpandLevel(level);
365 }
366
367 @Override
368 public void performAlign(int offset, int width) {
369 viewer.performAlign(offset, width);
370 }
371
372 @Override
373 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
374 return viewer.getTimeViewAlignmentInfo();
375 }
376
377 @Override
378 public int getAvailableWidth(int requestedOffset) {
379 return viewer.getAvailableWidth(requestedOffset);
380 }
381 }
382
383 private class TimeGraphComboWrapper implements ITimeGraphWrapper {
384 private TimeGraphCombo combo;
385
386 private TimeGraphComboWrapper(Composite parent, int style) {
387 combo = new TimeGraphCombo(parent, style, fWeight);
388 }
389
390 @Override
391 public void setTimeGraphContentProvider(ITimeGraphContentProvider timeGraphContentProvider) {
392 combo.setTimeGraphContentProvider(timeGraphContentProvider);
393 }
394
395 @Override
396 public void setTimeGraphPresentationProvider(TimeGraphPresentationProvider timeGraphPresentationProvider) {
397 combo.setTimeGraphProvider(timeGraphPresentationProvider);
398 }
399
400 @Override
401 public TimeGraphViewer getTimeGraphViewer() {
402 return combo.getTimeGraphViewer();
403 }
404
405 @Override
406 public void addSelectionListener(ITimeGraphSelectionListener listener) {
407 combo.addSelectionListener(listener);
408 }
409
410 @Override
411 public ISelectionProvider getSelectionProvider() {
412 return combo.getTreeViewer();
413 }
414
415 @Override
416 public void setFocus() {
417 combo.setFocus();
418 }
419
420 @Override
421 public boolean isDisposed() {
422 return combo.isDisposed();
423 }
424
425 @Override
426 public void setInput(Object input) {
427 combo.setInput(input);
428 }
429
430 @Override
431 public Object getInput() {
432 return combo.getInput();
433 }
434
435 @Override
436 public void setFilterColumns(String[] columnNames) {
437 combo.setFilterColumns(columnNames);
438 }
439
440 @Override
441 public void setFilterContentProvider(ITreeContentProvider contentProvider) {
442 combo.setFilterContentProvider(contentProvider);
443 }
444
445 @Override
446 public void setFilterLabelProvider(ITableLabelProvider labelProvider) {
447 combo.setFilterLabelProvider(labelProvider);
448 }
449
450 @Override
451 public void setFilters(ViewerFilter[] filters) {
452 combo.setFilters(filters);
453 }
454
455 @Override
456 public ViewerFilter[] getFilters() {
457 return combo.getFilters();
458 }
459
460 @Override
461 public IAction getShowFilterDialogAction() {
462 return combo.getShowFilterDialogAction();
463 }
464
465 @Override
466 public void refresh() {
467 combo.refresh();
468 }
469
470 @Override
471 public void redraw() {
472 combo.redraw();
473 }
474
475 @Override
476 public void update() {
477 combo.update();
478 }
479
480 @Override
481 public void setAutoExpandLevel(int level) {
482 combo.setAutoExpandLevel(level);
483 }
484
485 TimeGraphCombo getTimeGraphCombo() {
486 return combo;
487 }
488
489 TreeViewer getTreeViewer() {
490 return combo.getTreeViewer();
491 }
492
493 @Override
494 public void performAlign(int offset, int width) {
495 combo.performAlign(offset, width);
496 }
497
498 @Override
499 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
500 return combo.getTimeViewAlignmentInfo();
501 }
502
503 @Override
504 public int getAvailableWidth(int requestedOffset) {
505 return combo.getAvailableWidth(requestedOffset);
506 }
507 }
508
509 /**
510 * Base class to provide the labels for the tree viewer. Views extending
511 * this class typically need to override the getColumnText method if they
512 * have more than one column to display
513 */
514 protected static class TreeLabelProvider implements ITableLabelProvider, ILabelProvider {
515
516 @Override
517 public void addListener(ILabelProviderListener listener) {
518 }
519
520 @Override
521 public void dispose() {
522 }
523
524 @Override
525 public boolean isLabelProperty(Object element, String property) {
526 return false;
527 }
528
529 @Override
530 public void removeListener(ILabelProviderListener listener) {
531 }
532
533 @Override
534 public Image getColumnImage(Object element, int columnIndex) {
535 return null;
536 }
537
538 @Override
539 public String getColumnText(Object element, int columnIndex) {
540 TimeGraphEntry entry = (TimeGraphEntry) element;
541 if (columnIndex == 0) {
542 return entry.getName();
543 }
544 return new String();
545 }
546
547 @Override
548 public Image getImage(Object element) {
549 return null;
550 }
551
552 @Override
553 public String getText(Object element) {
554 TimeGraphEntry entry = (TimeGraphEntry) element;
555 return entry.getName();
556 }
557
558 }
559
560 private class BuildThread extends Thread {
561 private final @NonNull ITmfTrace fBuildTrace;
562 private final @NonNull ITmfTrace fParentTrace;
563 private final @NonNull IProgressMonitor fMonitor;
564
565 public BuildThread(final @NonNull ITmfTrace trace, final @NonNull ITmfTrace parentTrace, final String name) {
566 super(name + " build"); //$NON-NLS-1$
567 fBuildTrace = trace;
568 fParentTrace = parentTrace;
569 fMonitor = new NullProgressMonitor();
570 }
571
572 @Override
573 public void run() {
574 buildEventList(fBuildTrace, fParentTrace, fMonitor);
575 synchronized (fBuildThreadMap) {
576 fBuildThreadMap.remove(fBuildTrace);
577 }
578 }
579
580 public void cancel() {
581 fMonitor.setCanceled(true);
582 }
583 }
584
585 /**
586 * Zoom thread
587 * @since 1.1
588 */
589 protected abstract class ZoomThread extends Thread {
590 private final long fZoomStartTime;
591 private final long fZoomEndTime;
592 private final long fResolution;
593 private final @NonNull IProgressMonitor fMonitor;
594
595 /**
596 * Constructor
597 *
598 * @param startTime
599 * the start time
600 * @param endTime
601 * the end time
602 * @param resolution
603 * the resolution
604 */
605 public ZoomThread(long startTime, long endTime, long resolution) {
606 super(AbstractTimeGraphView.this.getName() + " zoom"); //$NON-NLS-1$
607 fZoomStartTime = startTime;
608 fZoomEndTime = endTime;
609 fResolution = resolution;
610 fMonitor = new NullProgressMonitor();
611 }
612
613 /**
614 * @return the zoom start time
615 */
616 public long getZoomStartTime() {
617 return fZoomStartTime;
618 }
619
620 /**
621 * @return the zoom end time
622 */
623 public long getZoomEndTime() {
624 return fZoomEndTime;
625 }
626
627 /**
628 * @return the resolution
629 */
630 public long getResolution() {
631 return fResolution;
632 }
633
634 /**
635 * @return the monitor
636 */
637 public @NonNull IProgressMonitor getMonitor() {
638 return fMonitor;
639 }
640
641 /**
642 * Cancel the zoom thread
643 */
644 public void cancel() {
645 fMonitor.setCanceled(true);
646 }
647 }
648
649 private class ZoomThreadByEntry extends ZoomThread {
650 private final @NonNull List<TimeGraphEntry> fZoomEntryList;
651
652 public ZoomThreadByEntry(@NonNull List<TimeGraphEntry> entryList, long startTime, long endTime, long resolution) {
653 super(startTime, endTime, resolution);
654 fZoomEntryList = entryList;
655 }
656
657 @Override
658 public void run() {
659 for (TimeGraphEntry entry : fZoomEntryList) {
660 if (getMonitor().isCanceled()) {
661 return;
662 }
663 if (entry == null) {
664 break;
665 }
666 zoom(entry, getMonitor());
667 }
668 /* Refresh the arrows when zooming */
669 List<ILinkEvent> events = getLinkList(getZoomStartTime(), getZoomEndTime(), getResolution(), getMonitor());
670 if (events != null) {
671 fTimeGraphWrapper.getTimeGraphViewer().setLinks(events);
672 redraw();
673 }
674 /* Refresh the view-specific markers when zooming */
675 List<IMarkerEvent> markers = new ArrayList<>(getViewMarkerList(getZoomStartTime(), getZoomEndTime(), getResolution(), getMonitor()));
676 /* Refresh the trace-specific markers when zooming */
677 markers.addAll(getTraceMarkerList(getZoomStartTime(), getZoomEndTime(), getResolution(), getMonitor()));
678 fTimeGraphWrapper.getTimeGraphViewer().setMarkers(markers);
679 redraw();
680 }
681
682 private void zoom(@NonNull TimeGraphEntry entry, @NonNull IProgressMonitor monitor) {
683 if (getZoomStartTime() <= fStartTime && getZoomEndTime() >= fEndTime) {
684 entry.setZoomedEventList(null);
685 } else {
686 List<ITimeEvent> zoomedEventList = getEventList(entry, getZoomStartTime(), getZoomEndTime(), getResolution(), monitor);
687 if (zoomedEventList != null) {
688 entry.setZoomedEventList(zoomedEventList);
689 }
690 }
691 redraw();
692 for (ITimeGraphEntry child : entry.getChildren()) {
693 if (monitor.isCanceled()) {
694 return;
695 }
696 if (child instanceof TimeGraphEntry) {
697 zoom((TimeGraphEntry) child, monitor);
698 }
699 }
700 }
701
702 }
703
704 // ------------------------------------------------------------------------
705 // Constructors
706 // ------------------------------------------------------------------------
707
708 /**
709 * Constructs a time graph view that contains either a time graph viewer or
710 * a time graph combo.
711 *
712 * By default, the view uses a time graph viewer. To use a time graph combo,
713 * the subclass constructor must call {@link #setTreeColumns(String[])} and
714 * {@link #setTreeLabelProvider(TreeLabelProvider)}.
715 *
716 * @param id
717 * The id of the view
718 * @param pres
719 * The presentation provider
720 */
721 public AbstractTimeGraphView(String id, TimeGraphPresentationProvider pres) {
722 super(id);
723 fPresentation = pres;
724 fDisplayWidth = Display.getDefault().getBounds().width;
725 }
726
727 // ------------------------------------------------------------------------
728 // Getters and setters
729 // ------------------------------------------------------------------------
730
731 /**
732 * Getter for the time graph combo
733 *
734 * @return The time graph combo, or null if combo is not used
735 */
736 protected TimeGraphCombo getTimeGraphCombo() {
737 if (fTimeGraphWrapper instanceof TimeGraphComboWrapper) {
738 return ((TimeGraphComboWrapper) fTimeGraphWrapper).getTimeGraphCombo();
739 }
740 return null;
741 }
742
743 /**
744 * Getter for the time graph viewer
745 *
746 * @return The time graph viewer
747 */
748 protected TimeGraphViewer getTimeGraphViewer() {
749 return fTimeGraphWrapper.getTimeGraphViewer();
750 }
751
752 /**
753 * Getter for the presentation provider
754 *
755 * @return The time graph presentation provider
756 */
757 protected ITimeGraphPresentationProvider2 getPresentationProvider() {
758 return fPresentation;
759 }
760
761 /**
762 * Sets the tree column labels.
763 * This should be called from the constructor.
764 *
765 * @param columns
766 * The array of tree column labels
767 */
768 protected void setTreeColumns(final String[] columns) {
769 fColumns = columns;
770 }
771
772 /**
773 * Sets the tree label provider.
774 * This should be called from the constructor.
775 *
776 * @param tlp
777 * The tree label provider
778 */
779 protected void setTreeLabelProvider(final TreeLabelProvider tlp) {
780 fLabelProvider = tlp;
781 }
782
783 /**
784 * Sets the time graph content provider. This should be called from the
785 * constructor.
786 *
787 * @param tgcp
788 * The time graph content provider
789 * @since 1.0
790 */
791 protected void setTimeGraphContentProvider(final @NonNull ITimeGraphContentProvider tgcp) {
792 fTimeGraphContentProvider = tgcp;
793 }
794
795 /**
796 * Sets the relative weight of each part of the time graph combo.
797 * This should be called from the constructor.
798 *
799 * @param weights
800 * The array (length 2) of relative weights of each part of the combo
801 */
802 protected void setWeight(final int[] weights) {
803 fWeight = weights;
804 }
805
806 /**
807 * Sets the filter column labels.
808 * This should be called from the constructor.
809 *
810 * @param filterColumns
811 * The array of filter column labels
812 */
813 protected void setFilterColumns(final String[] filterColumns) {
814 fFilterColumns = filterColumns;
815 }
816
817 /**
818 * Sets the filter label provider.
819 * This should be called from the constructor.
820 *
821 * @param labelProvider
822 * The filter label provider
823 */
824 protected void setFilterLabelProvider(final TreeLabelProvider labelProvider) {
825 fFilterLabelProvider = labelProvider;
826 }
827
828 /**
829 * Gets the display width
830 *
831 * @return the display width
832 */
833 protected int getDisplayWidth() {
834 return fDisplayWidth;
835 }
836
837 /**
838 * Gets the comparator for the entries
839 *
840 * @return The entry comparator
841 */
842 protected Comparator<ITimeGraphEntry> getEntryComparator() {
843 return fEntryComparator;
844 }
845
846 /**
847 * Sets the comparator class for the entries.
848 * <p>
849 * This comparator will apply recursively to entries that implement
850 * {@link TimeGraphEntry#sortChildren(Comparator)}.
851 *
852 * @param comparator
853 * A comparator object
854 */
855 protected void setEntryComparator(final Comparator<ITimeGraphEntry> comparator) {
856 fEntryComparator = comparator;
857 }
858
859 /**
860 * Gets the trace displayed in the view
861 *
862 * @return The trace
863 */
864 protected ITmfTrace getTrace() {
865 return fTrace;
866 }
867
868 /**
869 * Gets the start time
870 *
871 * @return The start time
872 */
873 protected long getStartTime() {
874 return fStartTime;
875 }
876
877 /**
878 * Sets the start time
879 *
880 * @param time
881 * The start time
882 */
883 protected void setStartTime(long time) {
884 fStartTime = time;
885 }
886
887 /**
888 * Gets the end time
889 *
890 * @return The end time
891 */
892 protected long getEndTime() {
893 return fEndTime;
894 }
895
896 /**
897 * Sets the end time
898 *
899 * @param time
900 * The end time
901 */
902 protected void setEndTime(long time) {
903 fEndTime = time;
904 }
905
906 /**
907 * Sets the auto-expand level to be used for the input of the view. The
908 * value 0 means that there is no auto-expand; 1 means that top-level
909 * elements are expanded, but not their children; 2 means that top-level
910 * elements are expanded, and their children, but not grand-children; and so
911 * on.
912 * <p>
913 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
914 * </p>
915 *
916 * @param level
917 * non-negative level, or <code>ALL_LEVELS</code> to expand all
918 * levels of the tree
919 */
920 protected void setAutoExpandLevel(int level) {
921 fAutoExpandLevel = level;
922 ITimeGraphWrapper tgWrapper = fTimeGraphWrapper;
923 if (tgWrapper != null) {
924 tgWrapper.setAutoExpandLevel(level);
925 }
926 }
927
928 /**
929 * Gets the entry list for a trace
930 *
931 * @param trace
932 * the trace
933 *
934 * @return the entry list map
935 */
936 protected List<TimeGraphEntry> getEntryList(ITmfTrace trace) {
937 synchronized (fEntryListMap) {
938 return fEntryListMap.get(trace);
939 }
940 }
941
942 /**
943 * Adds a trace entry list to the entry list map
944 *
945 * @param trace
946 * the trace to add
947 * @param list
948 * the list of time graph entries
949 */
950 protected void putEntryList(ITmfTrace trace, List<TimeGraphEntry> list) {
951 synchronized (fEntryListMap) {
952 fEntryListMap.put(trace, new CopyOnWriteArrayList<>(list));
953 }
954 }
955
956 /**
957 * Adds a list of entries to a trace's entry list
958 *
959 * @param trace
960 * the trace
961 * @param list
962 * the list of time graph entries to add
963 */
964 protected void addToEntryList(ITmfTrace trace, List<TimeGraphEntry> list) {
965 synchronized (fEntryListMap) {
966 List<TimeGraphEntry> entryList = fEntryListMap.get(trace);
967 if (entryList == null) {
968 fEntryListMap.put(trace, new CopyOnWriteArrayList<>(list));
969 } else {
970 entryList.addAll(list);
971 }
972 }
973 }
974
975 /**
976 * Removes a list of entries from a trace's entry list
977 *
978 * @param trace
979 * the trace
980 * @param list
981 * the list of time graph entries to remove
982 */
983 protected void removeFromEntryList(ITmfTrace trace, List<TimeGraphEntry> list) {
984 synchronized (fEntryListMap) {
985 List<TimeGraphEntry> entryList = fEntryListMap.get(trace);
986 if (entryList != null) {
987 entryList.removeAll(list);
988 }
989 }
990 }
991
992 /**
993 * Text for the "next" button
994 *
995 * @return The "next" button text
996 */
997 protected String getNextText() {
998 return Messages.AbstractTimeGraphtView_NextText;
999 }
1000
1001 /**
1002 * Tooltip for the "next" button
1003 *
1004 * @return Tooltip for the "next" button
1005 */
1006 protected String getNextTooltip() {
1007 return Messages.AbstractTimeGraphView_NextTooltip;
1008 }
1009
1010 /**
1011 * Text for the "Previous" button
1012 *
1013 * @return The "Previous" button text
1014 */
1015 protected String getPrevText() {
1016 return Messages.AbstractTimeGraphView_PreviousText;
1017 }
1018
1019 /**
1020 * Tooltip for the "previous" button
1021 *
1022 * @return Tooltip for the "previous" button
1023 */
1024 protected String getPrevTooltip() {
1025 return Messages.AbstractTimeGraphView_PreviousTooltip;
1026 }
1027
1028 // ------------------------------------------------------------------------
1029 // ViewPart
1030 // ------------------------------------------------------------------------
1031
1032 @Override
1033 public void createPartControl(Composite parent) {
1034 super.createPartControl(parent);
1035 if (fColumns == null || fLabelProvider == null) {
1036 fTimeGraphWrapper = new TimeGraphViewerWrapper(parent, SWT.NONE);
1037 } else {
1038 TimeGraphComboWrapper wrapper = new TimeGraphComboWrapper(parent, SWT.NONE);
1039 fTimeGraphWrapper = wrapper;
1040 TimeGraphCombo combo = wrapper.getTimeGraphCombo();
1041 combo.setTreeContentProvider(fTimeGraphContentProvider);
1042 combo.setTreeLabelProvider(fLabelProvider);
1043 combo.setTreeColumns(fColumns);
1044 }
1045 fTimeGraphWrapper.setTimeGraphContentProvider(fTimeGraphContentProvider);
1046 fTimeGraphWrapper.setFilterContentProvider(fTimeGraphContentProvider);
1047 fTimeGraphWrapper.setFilterLabelProvider(fFilterLabelProvider);
1048 fTimeGraphWrapper.setFilterColumns(fFilterColumns);
1049
1050 fTimeGraphWrapper.setTimeGraphPresentationProvider(fPresentation);
1051 fTimeGraphWrapper.setAutoExpandLevel(fAutoExpandLevel);
1052
1053 fTimeGraphWrapper.getTimeGraphViewer().addRangeListener(new ITimeGraphRangeListener() {
1054 @Override
1055 public void timeRangeUpdated(TimeGraphRangeUpdateEvent event) {
1056 final long startTime = event.getStartTime();
1057 final long endTime = event.getEndTime();
1058 TmfTimeRange range = new TmfTimeRange(new TmfNanoTimestamp(startTime), new TmfNanoTimestamp(endTime));
1059 broadcast(new TmfWindowRangeUpdatedSignal(AbstractTimeGraphView.this, range));
1060 startZoomThread(startTime, endTime);
1061 }
1062 });
1063
1064 fTimeGraphWrapper.getTimeGraphViewer().addTimeListener(new ITimeGraphTimeListener() {
1065 @Override
1066 public void timeSelected(TimeGraphTimeEvent event) {
1067 TmfNanoTimestamp startTime = new TmfNanoTimestamp(event.getBeginTime());
1068 TmfNanoTimestamp endTime = new TmfNanoTimestamp(event.getEndTime());
1069 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractTimeGraphView.this, startTime, endTime));
1070 }
1071 });
1072
1073 fTimeGraphWrapper.getTimeGraphViewer().addBookmarkListener(new ITimeGraphBookmarkListener() {
1074 @Override
1075 public void bookmarkAdded(final TimeGraphBookmarkEvent event) {
1076 try {
1077 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
1078 @Override
1079 public void run(IProgressMonitor monitor) throws CoreException {
1080 IMarkerEvent bookmark = event.getBookmark();
1081 IMarker marker = fEditorFile.createMarker(IMarker.BOOKMARK);
1082 marker.setAttribute(IMarker.MESSAGE, bookmark.getLabel());
1083 marker.setAttribute(ITmfMarker.MARKER_TIME, Long.toString(bookmark.getTime()));
1084 if (bookmark.getDuration() > 0) {
1085 marker.setAttribute(ITmfMarker.MARKER_DURATION, Long.toString(bookmark.getDuration()));
1086 marker.setAttribute(IMarker.LOCATION,
1087 NLS.bind(org.eclipse.tracecompass.internal.tmf.ui.Messages.TmfMarker_LocationTimeRange,
1088 new TmfNanoTimestamp(bookmark.getTime()),
1089 new TmfNanoTimestamp(bookmark.getTime() + bookmark.getDuration())));
1090 } else {
1091 marker.setAttribute(IMarker.LOCATION,
1092 NLS.bind(org.eclipse.tracecompass.internal.tmf.ui.Messages.TmfMarker_LocationTime,
1093 new TmfNanoTimestamp(bookmark.getTime())));
1094 }
1095 marker.setAttribute(ITmfMarker.MARKER_COLOR, bookmark.getColor().getRGBA().toString());
1096 }
1097 }, null);
1098 } catch (CoreException e) {
1099 Activator.getDefault().logError(e.getMessage());
1100 }
1101 }
1102
1103 @Override
1104 public void bookmarkRemoved(TimeGraphBookmarkEvent event) {
1105 try {
1106 IMarkerEvent bookmark = event.getBookmark();
1107 IMarker[] markers = fEditorFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO);
1108 for (IMarker marker : markers) {
1109 if (bookmark.getLabel().equals(marker.getAttribute(IMarker.MESSAGE)) &&
1110 Long.toString(bookmark.getTime()).equals(marker.getAttribute(ITmfMarker.MARKER_TIME, (String) null)) &&
1111 Long.toString(bookmark.getDuration()).equals(marker.getAttribute(ITmfMarker.MARKER_DURATION, Long.toString(0))) &&
1112 bookmark.getColor().getRGBA().toString().equals(marker.getAttribute(ITmfMarker.MARKER_COLOR))) {
1113 marker.delete();
1114 break;
1115 }
1116 }
1117 } catch (CoreException e) {
1118 Activator.getDefault().logError(e.getMessage());
1119 }
1120 }
1121 });
1122
1123 fTimeGraphWrapper.getTimeGraphViewer().setTimeFormat(TimeFormat.CALENDAR);
1124
1125 IStatusLineManager statusLineManager = getViewSite().getActionBars().getStatusLineManager();
1126 fTimeGraphWrapper.getTimeGraphViewer().getTimeGraphControl().setStatusLineManager(statusLineManager);
1127
1128 // View Action Handling
1129 makeActions();
1130 contributeToActionBars();
1131
1132 ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
1133 if (trace != null) {
1134 traceSelected(new TmfTraceSelectedSignal(this, trace));
1135 }
1136
1137 // make selection available to other views
1138 getSite().setSelectionProvider(fTimeGraphWrapper.getSelectionProvider());
1139
1140 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
1141 }
1142
1143 @Override
1144 public void setFocus() {
1145 fTimeGraphWrapper.setFocus();
1146 }
1147
1148 @Override
1149 public void dispose() {
1150 super.dispose();
1151 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
1152 }
1153
1154 /**
1155 * @since 2.0
1156 */
1157 @Override
1158 public void resourceChanged(final IResourceChangeEvent event) {
1159 for (final IMarkerDelta delta : event.findMarkerDeltas(IMarker.BOOKMARK, false)) {
1160 if (delta.getResource().equals(fEditorFile)) {
1161 fTimeGraphWrapper.getTimeGraphViewer().setBookmarks(refreshBookmarks(fEditorFile));
1162 redraw();
1163 return;
1164 }
1165 }
1166 }
1167
1168 private List<IMarkerEvent> refreshBookmarks(final IFile editorFile) {
1169 List<IMarkerEvent> bookmarks = new ArrayList<>();
1170 for (Color color : fColors) {
1171 color.dispose();
1172 }
1173 fColors.clear();
1174 if (editorFile == null || !editorFile.exists()) {
1175 return bookmarks;
1176 }
1177 try {
1178 IMarker[] markers = editorFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO);
1179 for (IMarker marker : markers) {
1180 String label = marker.getAttribute(IMarker.MESSAGE, (String) null);
1181 String time = marker.getAttribute(ITmfMarker.MARKER_TIME, (String) null);
1182 String duration = marker.getAttribute(ITmfMarker.MARKER_DURATION, Long.toString(0));
1183 String rgba = marker.getAttribute(ITmfMarker.MARKER_COLOR, (String) null);
1184 if (label != null && time != null && rgba != null) {
1185 Matcher matcher = RGBA_PATTERN.matcher(rgba);
1186 if (matcher.matches()) {
1187 try {
1188 int red = Integer.valueOf(matcher.group(1));
1189 int green = Integer.valueOf(matcher.group(2));
1190 int blue = Integer.valueOf(matcher.group(3));
1191 int alpha = Integer.valueOf(matcher.group(4));
1192 Color color = new Color(Display.getDefault(), red, green, blue, alpha);
1193 fColors.add(color);
1194 bookmarks.add(new MarkerEvent(null, Long.valueOf(time), Long.valueOf(duration), IMarkerEvent.BOOKMARKS, color, label, true));
1195 } catch (NumberFormatException e) {
1196 Activator.getDefault().logError(e.getMessage());
1197 }
1198 }
1199 }
1200 }
1201 } catch (CoreException e) {
1202 Activator.getDefault().logError(e.getMessage());
1203 }
1204 return bookmarks;
1205 }
1206
1207 // ------------------------------------------------------------------------
1208 // Signal handlers
1209 // ------------------------------------------------------------------------
1210
1211 /**
1212 * Handler for the trace opened signal.
1213 *
1214 * @param signal
1215 * The incoming signal
1216 */
1217 @TmfSignalHandler
1218 public void traceOpened(TmfTraceOpenedSignal signal) {
1219 loadTrace(signal.getTrace());
1220 }
1221
1222 /**
1223 * Handler for the trace selected signal
1224 *
1225 * @param signal
1226 * The incoming signal
1227 */
1228 @TmfSignalHandler
1229 public void traceSelected(final TmfTraceSelectedSignal signal) {
1230 if (signal.getTrace() == fTrace) {
1231 return;
1232 }
1233 loadTrace(signal.getTrace());
1234 }
1235
1236 /**
1237 * Trace is closed: clear the data structures and the view
1238 *
1239 * @param signal
1240 * the signal received
1241 */
1242 @TmfSignalHandler
1243 public void traceClosed(final TmfTraceClosedSignal signal) {
1244 synchronized (fBuildThreadMap) {
1245 for (ITmfTrace trace : getTracesToBuild(signal.getTrace())) {
1246 BuildThread buildThread = fBuildThreadMap.remove(trace);
1247 if (buildThread != null) {
1248 buildThread.cancel();
1249 }
1250 }
1251 }
1252 fMarkerEventSourcesMap.remove(signal.getTrace());
1253 synchronized (fEntryListMap) {
1254 fEntryListMap.remove(signal.getTrace());
1255 }
1256 fFiltersMap.remove(signal.getTrace());
1257 if (signal.getTrace() == fTrace) {
1258 fTrace = null;
1259 fEditorFile = null;
1260 fStartTime = SWT.DEFAULT;
1261 fEndTime = SWT.DEFAULT;
1262 if (fZoomThread != null) {
1263 fZoomThread.cancel();
1264 fZoomThread = null;
1265 }
1266 refresh();
1267 }
1268 }
1269
1270 /**
1271 * Handler for the selection range signal.
1272 *
1273 * @param signal
1274 * The signal that's received
1275 * @since 1.0
1276 */
1277 @TmfSignalHandler
1278 public void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal) {
1279 if (signal.getSource() == this || fTrace == null) {
1280 return;
1281 }
1282 final long beginTime = signal.getBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1283 final long endTime = signal.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1284
1285 Display.getDefault().asyncExec(new Runnable() {
1286 @Override
1287 public void run() {
1288 if (fTimeGraphWrapper.isDisposed()) {
1289 return;
1290 }
1291 if (beginTime == endTime) {
1292 fTimeGraphWrapper.getTimeGraphViewer().setSelectedTime(beginTime, true);
1293 } else {
1294 fTimeGraphWrapper.getTimeGraphViewer().setSelectionRange(beginTime, endTime, true);
1295 }
1296 synchingToTime(fTimeGraphWrapper.getTimeGraphViewer().getSelectionBegin());
1297 }
1298 });
1299 }
1300
1301 /**
1302 * Handler for the window range signal.
1303 *
1304 * @param signal
1305 * The signal that's received
1306 * @since 1.0
1307 */
1308 @TmfSignalHandler
1309 public void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal) {
1310 if (signal.getSource() == this || fTrace == null) {
1311 return;
1312 }
1313 if (signal.getCurrentRange().getIntersection(fTrace.getTimeRange()) == null) {
1314 return;
1315 }
1316 final long startTime = signal.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1317 final long endTime = signal.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1318 Display.getDefault().asyncExec(new Runnable() {
1319 @Override
1320 public void run() {
1321 if (fTimeGraphWrapper.isDisposed()) {
1322 return;
1323 }
1324 fTimeGraphWrapper.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
1325 startZoomThread(startTime, endTime);
1326 }
1327 });
1328 }
1329
1330 /**
1331 * @param signal the format of the timestamps was updated.
1332 */
1333 @TmfSignalHandler
1334 public void updateTimeFormat( final TmfTimestampFormatUpdateSignal signal){
1335 fTimeGraphWrapper.refresh();
1336 }
1337
1338 // ------------------------------------------------------------------------
1339 // Internal
1340 // ------------------------------------------------------------------------
1341
1342 private void loadTrace(final ITmfTrace trace) {
1343 if (fZoomThread != null) {
1344 fZoomThread.cancel();
1345 fZoomThread = null;
1346 }
1347 if (fTrace != null) {
1348 /* save the filters of the previous trace */
1349 fFiltersMap.put(fTrace, fTimeGraphWrapper.getFilters());
1350 }
1351 fTrace = trace;
1352 fEditorFile = TmfTraceManager.getInstance().getTraceEditorFile(trace);
1353 synchronized (fEntryListMap) {
1354 fEntryList = fEntryListMap.get(fTrace);
1355 if (fEntryList == null) {
1356 rebuild();
1357 } else {
1358 fStartTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1359 fEndTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1360 refresh();
1361 }
1362 }
1363 }
1364
1365 /**
1366 * Forces a rebuild of the entries list, even if entries already exist for this trace
1367 */
1368 protected void rebuild() {
1369 setStartTime(Long.MAX_VALUE);
1370 setEndTime(Long.MIN_VALUE);
1371 refresh();
1372 ITmfTrace viewTrace = fTrace;
1373 if (viewTrace == null) {
1374 return;
1375 }
1376 List<IMarkerEventSource> markerEventSources = new ArrayList<>();
1377 synchronized (fBuildThreadMap) {
1378 for (ITmfTrace trace : getTracesToBuild(viewTrace)) {
1379 if (trace == null) {
1380 break;
1381 }
1382 markerEventSources.addAll(TmfTraceAdapterManager.getAdapters(trace, IMarkerEventSource.class));
1383 BuildThread buildThread = new BuildThread(trace, viewTrace, getName());
1384 fBuildThreadMap.put(trace, buildThread);
1385 buildThread.start();
1386 }
1387 }
1388 fMarkerEventSourcesMap.put(viewTrace, markerEventSources);
1389 }
1390
1391 /**
1392 * Method called when synching to a given timestamp. Inheriting classes can
1393 * perform actions here to update the view at the given timestamp.
1394 *
1395 * @param time
1396 * The currently selected time
1397 */
1398 protected void synchingToTime(long time) {
1399
1400 }
1401
1402 /**
1403 * Return the list of traces whose data or analysis results will be used to
1404 * populate the view. By default, if the trace is an experiment, the traces
1405 * under it will be returned, otherwise, the trace itself is returned.
1406 *
1407 * A build thread will be started for each trace returned by this method,
1408 * some of which may receive events in live streaming mode.
1409 *
1410 * @param trace
1411 * The trace associated with this view
1412 * @return List of traces with data to display
1413 */
1414 protected @NonNull Iterable<ITmfTrace> getTracesToBuild(@NonNull ITmfTrace trace) {
1415 return TmfTraceManager.getTraceSet(trace);
1416 }
1417
1418 /**
1419 * Build the entries list to show in this time graph
1420 *
1421 * Called from the BuildThread
1422 *
1423 * @param trace
1424 * The trace being built
1425 * @param parentTrace
1426 * The parent of the trace set, or the trace itself
1427 * @param monitor
1428 * The progress monitor object
1429 */
1430 protected abstract void buildEventList(@NonNull ITmfTrace trace, @NonNull ITmfTrace parentTrace, @NonNull IProgressMonitor monitor);
1431
1432 /**
1433 * Gets the list of event for an entry in a given timerange
1434 *
1435 * @param entry
1436 * The entry to get events for
1437 * @param startTime
1438 * Start of the time range
1439 * @param endTime
1440 * End of the time range
1441 * @param resolution
1442 * The resolution
1443 * @param monitor
1444 * The progress monitor object
1445 * @return The list of events for the entry
1446 */
1447 protected abstract @Nullable List<@NonNull ITimeEvent> getEventList(@NonNull TimeGraphEntry entry,
1448 long startTime, long endTime, long resolution,
1449 @NonNull IProgressMonitor monitor);
1450
1451 /**
1452 * Gets the list of links (displayed as arrows) for a trace in a given
1453 * timerange. Default implementation returns an empty list.
1454 *
1455 * @param startTime
1456 * Start of the time range
1457 * @param endTime
1458 * End of the time range
1459 * @param resolution
1460 * The resolution
1461 * @param monitor
1462 * The progress monitor object
1463 * @return The list of link events
1464 */
1465 protected @Nullable List<@NonNull ILinkEvent> getLinkList(long startTime, long endTime,
1466 long resolution, @NonNull IProgressMonitor monitor) {
1467 return new ArrayList<>();
1468 }
1469
1470 /**
1471 * Gets the list of view-specific marker categories. Default implementation
1472 * returns an empty list.
1473 *
1474 * @return The list of marker categories
1475 * @since 2.0
1476 */
1477 protected @NonNull List<String> getViewMarkerCategories() {
1478 return new ArrayList<>();
1479 }
1480
1481 /**
1482 * Gets the list of view-specific markers for a trace in a given time range.
1483 * Default implementation returns an empty list.
1484 *
1485 * @param startTime
1486 * Start of the time range
1487 * @param endTime
1488 * End of the time range
1489 * @param resolution
1490 * The resolution
1491 * @param monitor
1492 * The progress monitor object
1493 * @return The list of marker events
1494 * @since 2.0
1495 */
1496 protected @NonNull List<IMarkerEvent> getViewMarkerList(long startTime, long endTime,
1497 long resolution, @NonNull IProgressMonitor monitor) {
1498 return new ArrayList<>();
1499 }
1500
1501 /**
1502 * Gets the list of trace-specific markers for a trace in a given time range.
1503 *
1504 * @param startTime
1505 * Start of the time range
1506 * @param endTime
1507 * End of the time range
1508 * @param resolution
1509 * The resolution
1510 * @param monitor
1511 * The progress monitor object
1512 * @return The list of marker events
1513 * @since 2.0
1514 */
1515 protected @NonNull List<IMarkerEvent> getTraceMarkerList(long startTime, long endTime,
1516 long resolution, @NonNull IProgressMonitor monitor) {
1517 List<IMarkerEvent> markers = new ArrayList<>();
1518 for (IMarkerEventSource markerEventSource : getMarkerEventSources(fTrace)) {
1519 for (String category : markerEventSource.getMarkerCategories()) {
1520 if (monitor.isCanceled()) {
1521 break;
1522 }
1523 markers.addAll(markerEventSource.getMarkerList(checkNotNull(category), startTime, endTime, resolution, monitor));
1524 }
1525 }
1526 return markers;
1527 }
1528
1529 /**
1530 * Get the list of current marker categories.
1531 *
1532 * @return The list of marker categories
1533 * @since 2.0
1534 */
1535 private @NonNull List<String> getMarkerCategories() {
1536 Set<String> categories = new HashSet<>(getViewMarkerCategories());
1537 for (IMarkerEventSource markerEventSource : getMarkerEventSources(fTrace)) {
1538 categories.addAll(markerEventSource.getMarkerCategories());
1539 }
1540 return new ArrayList<>(categories);
1541 }
1542
1543 /**
1544 * Gets the list of marker event sources for a given trace.
1545 *
1546 * @param trace
1547 * The trace
1548 * @return The list of marker event sources
1549 * @since 2.0
1550 */
1551 private @NonNull List<IMarkerEventSource> getMarkerEventSources(ITmfTrace trace) {
1552 List<IMarkerEventSource> markerEventSources = fMarkerEventSourcesMap.get(trace);
1553 if (markerEventSources == null) {
1554 markerEventSources = checkNotNull(Collections.<IMarkerEventSource>emptyList());
1555 }
1556 return markerEventSources;
1557 }
1558
1559 /**
1560 * Refresh the display
1561 */
1562 protected void refresh() {
1563 final boolean zoomThread = Thread.currentThread() instanceof ZoomThread;
1564 TmfUiRefreshHandler.getInstance().queueUpdate(this, new Runnable() {
1565 @Override
1566 public void run() {
1567 if (fTimeGraphWrapper.isDisposed()) {
1568 return;
1569 }
1570 boolean hasEntries = false;
1571 synchronized (fEntryListMap) {
1572 fEntryList = fEntryListMap.get(fTrace);
1573 if (fEntryList == null) {
1574 fEntryList = new CopyOnWriteArrayList<>();
1575 } else if (fEntryComparator != null) {
1576 List<TimeGraphEntry> list = new ArrayList<>(fEntryList);
1577 Collections.sort(list, fEntryComparator);
1578 for (ITimeGraphEntry entry : list) {
1579 sortChildren(entry, fEntryComparator);
1580 }
1581 fEntryList.clear();
1582 fEntryList.addAll(list);
1583 }
1584 hasEntries = fEntryList.size() != 0;
1585 }
1586 if (fEntryList != fTimeGraphWrapper.getInput()) {
1587 fTimeGraphWrapper.setInput(fEntryList);
1588 /* restore the previously saved filters, if any */
1589 fTimeGraphWrapper.setFilters(fFiltersMap.get(fTrace));
1590 fTimeGraphWrapper.getTimeGraphViewer().setLinks(null);
1591 fTimeGraphWrapper.getTimeGraphViewer().setBookmarks(refreshBookmarks(fEditorFile));
1592 fTimeGraphWrapper.getTimeGraphViewer().setMarkerCategories(getMarkerCategories());
1593 fTimeGraphWrapper.getTimeGraphViewer().setMarkers(null);
1594 } else {
1595 fTimeGraphWrapper.refresh();
1596 }
1597 long startBound = (fStartTime == Long.MAX_VALUE ? SWT.DEFAULT : fStartTime);
1598 long endBound = (fEndTime == Long.MIN_VALUE ? SWT.DEFAULT : fEndTime);
1599 fTimeGraphWrapper.getTimeGraphViewer().setTimeBounds(startBound, endBound);
1600
1601 TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext();
1602 long selectionBeginTime = fTrace == null ? SWT.DEFAULT : ctx.getSelectionRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1603 long selectionEndTime = fTrace == null ? SWT.DEFAULT : ctx.getSelectionRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1604 long startTime = fTrace == null ? SWT.DEFAULT : ctx.getWindowRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1605 long endTime = fTrace == null ? SWT.DEFAULT : ctx.getWindowRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1606 startTime = (fStartTime == Long.MAX_VALUE ? SWT.DEFAULT : Math.max(startTime, fStartTime));
1607 endTime = (fEndTime == Long.MIN_VALUE ? SWT.DEFAULT : Math.min(endTime, fEndTime));
1608 fTimeGraphWrapper.getTimeGraphViewer().setSelectionRange(selectionBeginTime, selectionEndTime, false);
1609 fTimeGraphWrapper.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
1610
1611 if (fTimeGraphWrapper instanceof TimeGraphComboWrapper && !fPackDone) {
1612 for (TreeColumn column : ((TimeGraphComboWrapper) fTimeGraphWrapper).getTreeViewer().getTree().getColumns()) {
1613 column.pack();
1614 }
1615 if (hasEntries) {
1616 fPackDone = true;
1617 }
1618 }
1619
1620 if (!zoomThread) {
1621 startZoomThread(startTime, endTime);
1622 }
1623 }
1624 });
1625 }
1626
1627 /**
1628 * Redraw the canvas
1629 */
1630 protected void redraw() {
1631 synchronized (fSyncObj) {
1632 if (fRedrawState == State.IDLE) {
1633 fRedrawState = State.BUSY;
1634 } else {
1635 fRedrawState = State.PENDING;
1636 return;
1637 }
1638 }
1639 Display.getDefault().asyncExec(new Runnable() {
1640 @Override
1641 public void run() {
1642 if (fTimeGraphWrapper.isDisposed()) {
1643 return;
1644 }
1645 fTimeGraphWrapper.redraw();
1646 fTimeGraphWrapper.update();
1647 synchronized (fSyncObj) {
1648 if (fRedrawState == State.PENDING) {
1649 fRedrawState = State.IDLE;
1650 redraw();
1651 } else {
1652 fRedrawState = State.IDLE;
1653 }
1654 }
1655 }
1656 });
1657 }
1658
1659 private static void sortChildren(ITimeGraphEntry entry, Comparator<ITimeGraphEntry> comparator) {
1660 if (entry instanceof TimeGraphEntry) {
1661 ((TimeGraphEntry) entry).sortChildren(comparator);
1662 }
1663 for (ITimeGraphEntry child : entry.getChildren()) {
1664 sortChildren(child, comparator);
1665 }
1666 }
1667
1668 /**
1669 * Start or restart the zoom thread.
1670 *
1671 * @param startTime
1672 * the zoom start time
1673 * @param endTime
1674 * the zoom end time
1675 * @since 2.0
1676 */
1677 protected void startZoomThread(long startTime, long endTime) {
1678 boolean restart = false;
1679 if (fZoomThread != null) {
1680 fZoomThread.cancel();
1681 if (fZoomThread.fZoomStartTime == startTime && fZoomThread.fZoomEndTime == endTime) {
1682 restart = true;
1683 }
1684 }
1685 long resolution = Math.max(1, (endTime - startTime) / fDisplayWidth);
1686 fZoomThread = createZoomThread(startTime, endTime, resolution, restart);
1687 if (fZoomThread != null) {
1688 fZoomThread.start();
1689 }
1690 }
1691
1692 /**
1693 * Create a zoom thread.
1694 *
1695 * @param startTime
1696 * the zoom start time
1697 * @param endTime
1698 * the zoom end time
1699 * @param resolution
1700 * the resolution
1701 * @param restart
1702 * true if restarting zoom for the same time range
1703 * @return a zoom thread
1704 * @since 1.1
1705 */
1706 protected @Nullable ZoomThread createZoomThread(long startTime, long endTime, long resolution, boolean restart) {
1707 final List<TimeGraphEntry> entryList = fEntryList;
1708 if (entryList == null) {
1709 return null;
1710 }
1711 return new ZoomThreadByEntry(entryList, startTime, endTime, resolution);
1712 }
1713
1714 private void makeActions() {
1715 fPreviousResourceAction = fTimeGraphWrapper.getTimeGraphViewer().getPreviousItemAction();
1716 fPreviousResourceAction.setText(getPrevText());
1717 fPreviousResourceAction.setToolTipText(getPrevTooltip());
1718 fNextResourceAction = fTimeGraphWrapper.getTimeGraphViewer().getNextItemAction();
1719 fNextResourceAction.setText(getNextText());
1720 fNextResourceAction.setToolTipText(getNextTooltip());
1721 }
1722
1723 private void contributeToActionBars() {
1724 IActionBars bars = getViewSite().getActionBars();
1725 fillLocalToolBar(bars.getToolBarManager());
1726 fillLocalMenu(bars.getMenuManager());
1727 }
1728
1729 /**
1730 * Add actions to local tool bar manager
1731 *
1732 * @param manager the tool bar manager
1733 */
1734 protected void fillLocalToolBar(IToolBarManager manager) {
1735 if (fFilterColumns != null && fFilterLabelProvider != null && fFilterColumns.length > 0) {
1736 manager.add(fTimeGraphWrapper.getShowFilterDialogAction());
1737 }
1738 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getShowLegendAction());
1739 manager.add(new Separator());
1740 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getResetScaleAction());
1741 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getPreviousEventAction());
1742 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getNextEventAction());
1743 manager.add(new Separator());
1744 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getToggleBookmarkAction());
1745 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getPreviousMarkerAction());
1746 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getNextMarkerAction());
1747 manager.add(new Separator());
1748 manager.add(fPreviousResourceAction);
1749 manager.add(fNextResourceAction);
1750 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getZoomInAction());
1751 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getZoomOutAction());
1752 manager.add(new Separator());
1753 }
1754
1755 /**
1756 * Add actions to local menu manager
1757 *
1758 * @param manager the tool bar manager
1759 * @since 2.0
1760 */
1761 protected void fillLocalMenu(IMenuManager manager) {
1762 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getMarkersMenu());
1763 }
1764
1765 /**
1766 * @since 1.0
1767 */
1768 @Override
1769 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
1770 if (fTimeGraphWrapper == null) {
1771 return null;
1772 }
1773 return fTimeGraphWrapper.getTimeViewAlignmentInfo();
1774 }
1775
1776 /**
1777 * @since 1.0
1778 */
1779 @Override
1780 public int getAvailableWidth(int requestedOffset) {
1781 if (fTimeGraphWrapper == null) {
1782 return 0;
1783 }
1784 return fTimeGraphWrapper.getAvailableWidth(requestedOffset);
1785 }
1786
1787 /**
1788 * @since 1.0
1789 */
1790 @Override
1791 public void performAlign(int offset, int width) {
1792 if (fTimeGraphWrapper != null) {
1793 fTimeGraphWrapper.performAlign(offset, width);
1794 }
1795 }
1796 }
This page took 0.092499 seconds and 6 git commands to generate.