Use the NonNull utility methods where we can
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / timegraph / AbstractTimeGraphView.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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.Arrays;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.CopyOnWriteArrayList;
29
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.NullProgressMonitor;
32 import org.eclipse.jdt.annotation.NonNull;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.eclipse.jface.action.Action;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.action.IStatusLineManager;
37 import org.eclipse.jface.action.IToolBarManager;
38 import org.eclipse.jface.action.Separator;
39 import org.eclipse.jface.viewers.AbstractTreeViewer;
40 import org.eclipse.jface.viewers.ILabelProvider;
41 import org.eclipse.jface.viewers.ILabelProviderListener;
42 import org.eclipse.jface.viewers.ISelectionProvider;
43 import org.eclipse.jface.viewers.ITableLabelProvider;
44 import org.eclipse.jface.viewers.ITreeContentProvider;
45 import org.eclipse.jface.viewers.TreeViewer;
46 import org.eclipse.jface.viewers.Viewer;
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.graphics.Image;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.swt.widgets.Display;
51 import org.eclipse.swt.widgets.TreeColumn;
52 import org.eclipse.tracecompass.tmf.core.signal.TmfRangeSynchSignal;
53 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
54 import org.eclipse.tracecompass.tmf.core.signal.TmfTimeSynchSignal;
55 import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
56 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
57 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
58 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
59 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
60 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
61 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
62 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
63 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
64 import org.eclipse.tracecompass.tmf.ui.TmfUiRefreshHandler;
65 import org.eclipse.tracecompass.tmf.ui.views.TmfView;
66 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphContentProvider;
67 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
68 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphRangeListener;
69 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphSelectionListener;
70 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener;
71 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphCombo;
72 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
73 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphRangeUpdateEvent;
74 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent;
75 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphViewer;
76 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
77 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
78 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
79 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
80 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
81 import org.eclipse.ui.IActionBars;
82
83 /**
84 * An abstract view all time graph views can inherit
85 *
86 * This view contains either a time graph viewer, or a time graph combo which is
87 * divided between a tree viewer on the left and a time graph viewer on the right.
88 *
89 * @since 2.1
90 */
91 public abstract class AbstractTimeGraphView extends TmfView {
92
93 /** Constant indicating that all levels of the time graph should be expanded */
94 protected static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS;
95
96 /**
97 * Redraw state enum
98 */
99 private enum State {
100 IDLE, BUSY, PENDING
101 }
102
103 // ------------------------------------------------------------------------
104 // Fields
105 // ------------------------------------------------------------------------
106
107 /** The timegraph wrapper */
108 private ITimeGraphWrapper fTimeGraphWrapper;
109
110 /** The selected trace */
111 private ITmfTrace fTrace;
112
113 /** The timegraph entry list */
114 private List<TimeGraphEntry> fEntryList;
115
116 /** The trace to entry list hash map */
117 private final Map<ITmfTrace, List<TimeGraphEntry>> fEntryListMap = new HashMap<>();
118
119 /** The trace to build thread hash map */
120 private final Map<ITmfTrace, BuildThread> fBuildThreadMap = new HashMap<>();
121
122 /** The start time */
123 private long fStartTime;
124
125 /** The end time */
126 private long fEndTime;
127
128 /** The display width */
129 private final int fDisplayWidth;
130
131 /** The zoom thread */
132 private ZoomThread fZoomThread;
133
134 /** The next resource action */
135 private Action fNextResourceAction;
136
137 /** The previous resource action */
138 private Action fPreviousResourceAction;
139
140 /** A comparator class */
141 private Comparator<ITimeGraphEntry> fEntryComparator = null;
142
143 /** The redraw state used to prevent unnecessary queuing of display runnables */
144 private State fRedrawState = State.IDLE;
145
146 /** The redraw synchronization object */
147 private final Object fSyncObj = new Object();
148
149 /** The presentation provider for this view */
150 private final TimeGraphPresentationProvider fPresentation;
151
152 /** The tree column label array, or null if combo is not used */
153 private String[] fColumns;
154
155 /** The tree label provider, or null if combo is not used */
156 private TreeLabelProvider fLabelProvider = null;
157
158 /** The relative weight of the sash, ignored if combo is not used */
159 private int[] fWeight = { 1, 1 };
160
161 /** The filter column label array, or null if filter is not used */
162 private String[] fFilterColumns;
163
164 /** The pack done flag */
165 private boolean fPackDone = false;
166
167 /** The filter label provider, or null if filter is not used */
168 private TreeLabelProvider fFilterLabelProvider;
169
170 private int fAutoExpandLevel = ALL_LEVELS;
171
172 // ------------------------------------------------------------------------
173 // Classes
174 // ------------------------------------------------------------------------
175
176 private interface ITimeGraphWrapper {
177
178 void setTimeGraphProvider(TimeGraphPresentationProvider fPresentation);
179
180 TimeGraphViewer getTimeGraphViewer();
181
182 void addSelectionListener(ITimeGraphSelectionListener iTimeGraphSelectionListener);
183
184 ISelectionProvider getSelectionProvider();
185
186 void setFocus();
187
188 boolean isDisposed();
189
190 void refresh();
191
192 void setInput(Object input);
193
194 Object getInput();
195
196 void redraw();
197
198 void update();
199
200 void setAutoExpandLevel(int level);
201
202 }
203
204 private class TimeGraphViewerWrapper implements ITimeGraphWrapper {
205 private TimeGraphViewer viewer;
206
207 private TimeGraphViewerWrapper(Composite parent, int style) {
208 viewer = new TimeGraphViewer(parent, style);
209 }
210
211 @Override
212 public void setTimeGraphProvider(TimeGraphPresentationProvider timeGraphProvider) {
213 viewer.setTimeGraphProvider(timeGraphProvider);
214 }
215
216 @Override
217 public TimeGraphViewer getTimeGraphViewer() {
218 return viewer;
219 }
220
221 @Override
222 public void addSelectionListener(ITimeGraphSelectionListener listener) {
223 viewer.addSelectionListener(listener);
224 }
225
226 @Override
227 public ISelectionProvider getSelectionProvider() {
228 return viewer.getSelectionProvider();
229 }
230
231 @Override
232 public void setFocus() {
233 viewer.setFocus();
234 }
235
236 @Override
237 public boolean isDisposed() {
238 return viewer.getControl().isDisposed();
239 }
240
241 @Override
242 public void setInput(Object input) {
243 viewer.setInput(input);
244 }
245
246 @Override
247 public Object getInput() {
248 return viewer.getInput();
249 }
250
251 @Override
252 public void refresh() {
253 viewer.refresh();
254 }
255
256 @Override
257 public void redraw() {
258 viewer.getControl().redraw();
259 }
260
261 @Override
262 public void update() {
263 viewer.getControl().update();
264 }
265
266 @Override
267 public void setAutoExpandLevel(int level) {
268 viewer.setAutoExpandLevel(level);
269 }
270 }
271
272 private class TimeGraphComboWrapper implements ITimeGraphWrapper {
273 private TimeGraphCombo combo;
274
275 private TimeGraphComboWrapper(Composite parent, int style) {
276 combo = new TimeGraphCombo(parent, style, fWeight);
277 }
278
279 @Override
280 public void setTimeGraphProvider(TimeGraphPresentationProvider timeGraphProvider) {
281 combo.setTimeGraphProvider(timeGraphProvider);
282 }
283
284 @Override
285 public TimeGraphViewer getTimeGraphViewer() {
286 return combo.getTimeGraphViewer();
287 }
288
289 @Override
290 public void addSelectionListener(ITimeGraphSelectionListener listener) {
291 combo.addSelectionListener(listener);
292 }
293
294 @Override
295 public ISelectionProvider getSelectionProvider() {
296 return combo.getTreeViewer();
297 }
298
299 @Override
300 public void setFocus() {
301 combo.setFocus();
302 }
303
304 @Override
305 public boolean isDisposed() {
306 return combo.isDisposed();
307 }
308
309 @Override
310 public void setInput(Object input) {
311 combo.setInput(input);
312 }
313
314 @Override
315 public Object getInput() {
316 return combo.getInput();
317 }
318
319 @Override
320 public void refresh() {
321 combo.refresh();
322 }
323
324 @Override
325 public void redraw() {
326 combo.redraw();
327 }
328
329 @Override
330 public void update() {
331 combo.update();
332 }
333
334 @Override
335 public void setAutoExpandLevel(int level) {
336 combo.setAutoExpandLevel(level);
337 }
338
339 TimeGraphCombo getTimeGraphCombo() {
340 return combo;
341 }
342
343 TreeViewer getTreeViewer() {
344 return combo.getTreeViewer();
345 }
346
347 IAction getShowFilterAction() {
348 return combo.getShowFilterAction();
349 }
350 }
351
352 private class TreeContentProvider implements ITreeContentProvider {
353
354 @Override
355 public void dispose() {
356 }
357
358 @Override
359 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
360 }
361
362 @Override
363 public ITimeGraphEntry[] getElements(Object inputElement) {
364 if (inputElement != null) {
365 try {
366 return ((List<?>) inputElement).toArray(new ITimeGraphEntry[0]);
367 } catch (ClassCastException e) {
368 }
369 }
370 return new ITimeGraphEntry[0];
371 }
372
373 @Override
374 public Object[] getChildren(Object parentElement) {
375 ITimeGraphEntry entry = (ITimeGraphEntry) parentElement;
376 List<? extends ITimeGraphEntry> children = entry.getChildren();
377 return children.toArray(new ITimeGraphEntry[children.size()]);
378 }
379
380 @Override
381 public Object getParent(Object element) {
382 ITimeGraphEntry entry = (ITimeGraphEntry) element;
383 return entry.getParent();
384 }
385
386 @Override
387 public boolean hasChildren(Object element) {
388 ITimeGraphEntry entry = (ITimeGraphEntry) element;
389 return entry.hasChildren();
390 }
391
392 }
393
394 private class TimeGraphContentProvider implements ITimeGraphContentProvider {
395
396 @Override
397 public ITimeGraphEntry[] getElements(Object inputElement) {
398 if (inputElement != null) {
399 try {
400 return ((List<?>) inputElement).toArray(new ITimeGraphEntry[0]);
401 } catch (ClassCastException e) {
402 }
403 }
404 return new ITimeGraphEntry[0];
405 }
406
407 }
408
409 /**
410 * Base class to provide the labels for the tree viewer. Views extending
411 * this class typically need to override the getColumnText method if they
412 * have more than one column to display
413 */
414 protected static class TreeLabelProvider implements ITableLabelProvider, ILabelProvider {
415
416 @Override
417 public void addListener(ILabelProviderListener listener) {
418 }
419
420 @Override
421 public void dispose() {
422 }
423
424 @Override
425 public boolean isLabelProperty(Object element, String property) {
426 return false;
427 }
428
429 @Override
430 public void removeListener(ILabelProviderListener listener) {
431 }
432
433 @Override
434 public Image getColumnImage(Object element, int columnIndex) {
435 return null;
436 }
437
438 @Override
439 public String getColumnText(Object element, int columnIndex) {
440 TimeGraphEntry entry = (TimeGraphEntry) element;
441 if (columnIndex == 0) {
442 return entry.getName();
443 }
444 return new String();
445 }
446
447 /**
448 * @since 3.2
449 */
450 @Override
451 public Image getImage(Object element) {
452 return null;
453 }
454
455 /**
456 * @since 3.2
457 */
458 @Override
459 public String getText(Object element) {
460 TimeGraphEntry entry = (TimeGraphEntry) element;
461 return entry.getName();
462 }
463
464 }
465
466 private class BuildThread extends Thread {
467 private final @NonNull ITmfTrace fBuildTrace;
468 private final @NonNull ITmfTrace fParentTrace;
469 private final @NonNull IProgressMonitor fMonitor;
470
471 public BuildThread(final @NonNull ITmfTrace trace, final @NonNull ITmfTrace parentTrace, final String name) {
472 super(name + " build"); //$NON-NLS-1$
473 fBuildTrace = trace;
474 fParentTrace = parentTrace;
475 fMonitor = new NullProgressMonitor();
476 }
477
478 @Override
479 public void run() {
480 buildEventList(fBuildTrace, fParentTrace, fMonitor);
481 synchronized (fBuildThreadMap) {
482 fBuildThreadMap.remove(fBuildTrace);
483 }
484 }
485
486 public void cancel() {
487 fMonitor.setCanceled(true);
488 }
489 }
490
491 private class ZoomThread extends Thread {
492 private final @NonNull List<TimeGraphEntry> fZoomEntryList;
493 private final long fZoomStartTime;
494 private final long fZoomEndTime;
495 private final long fResolution;
496 private final @NonNull IProgressMonitor fMonitor;
497
498 public ZoomThread(@NonNull List<TimeGraphEntry> entryList, long startTime, long endTime, String name) {
499 super(name + " zoom"); //$NON-NLS-1$
500 fZoomEntryList = entryList;
501 fZoomStartTime = startTime;
502 fZoomEndTime = endTime;
503 fResolution = Math.max(1, (fZoomEndTime - fZoomStartTime) / fDisplayWidth);
504 fMonitor = new NullProgressMonitor();
505 }
506
507 @Override
508 public void run() {
509 for (TimeGraphEntry entry : fZoomEntryList) {
510 if (fMonitor.isCanceled()) {
511 return;
512 }
513 if (entry == null) {
514 break;
515 }
516 zoom(entry, fMonitor);
517 }
518 /* Refresh the arrows when zooming */
519 List<ILinkEvent> events = getLinkList(fZoomStartTime, fZoomEndTime, fResolution, fMonitor);
520 if (events != null) {
521 fTimeGraphWrapper.getTimeGraphViewer().setLinks(events);
522 redraw();
523 }
524 }
525
526 private void zoom(@NonNull TimeGraphEntry entry, @NonNull IProgressMonitor monitor) {
527 if (fZoomStartTime <= fStartTime && fZoomEndTime >= fEndTime) {
528 entry.setZoomedEventList(null);
529 } else {
530 List<ITimeEvent> zoomedEventList = getEventList(entry, fZoomStartTime, fZoomEndTime, fResolution, monitor);
531 if (zoomedEventList != null) {
532 entry.setZoomedEventList(zoomedEventList);
533 }
534 }
535 redraw();
536 for (ITimeGraphEntry child : entry.getChildren()) {
537 if (fMonitor.isCanceled()) {
538 return;
539 }
540 if (child instanceof TimeGraphEntry) {
541 zoom((TimeGraphEntry) child, monitor);
542 }
543 }
544 }
545
546 public void cancel() {
547 fMonitor.setCanceled(true);
548 }
549 }
550
551 // ------------------------------------------------------------------------
552 // Constructors
553 // ------------------------------------------------------------------------
554
555 /**
556 * Constructs a time graph view that contains either a time graph viewer or
557 * a time graph combo.
558 *
559 * By default, the view uses a time graph viewer. To use a time graph combo,
560 * the subclass constructor must call {@link #setTreeColumns(String[])} and
561 * {@link #setTreeLabelProvider(TreeLabelProvider)}.
562 *
563 * @param id
564 * The id of the view
565 * @param pres
566 * The presentation provider
567 */
568 public AbstractTimeGraphView(String id, TimeGraphPresentationProvider pres) {
569 super(id);
570 fPresentation = pres;
571 fDisplayWidth = Display.getDefault().getBounds().width;
572 }
573
574 // ------------------------------------------------------------------------
575 // Getters and setters
576 // ------------------------------------------------------------------------
577
578 /**
579 * Getter for the time graph combo
580 *
581 * @return The time graph combo, or null if combo is not used
582 */
583 protected TimeGraphCombo getTimeGraphCombo() {
584 if (fTimeGraphWrapper instanceof TimeGraphComboWrapper) {
585 return ((TimeGraphComboWrapper) fTimeGraphWrapper).getTimeGraphCombo();
586 }
587 return null;
588 }
589
590 /**
591 * Getter for the time graph viewer
592 *
593 * @return The time graph viewer
594 */
595 protected TimeGraphViewer getTimeGraphViewer() {
596 return fTimeGraphWrapper.getTimeGraphViewer();
597 }
598
599 /**
600 * Getter for the presentation provider
601 *
602 * @return The time graph presentation provider
603 * @since 3.0
604 */
605 protected ITimeGraphPresentationProvider2 getPresentationProvider() {
606 return fPresentation;
607 }
608
609 /**
610 * Sets the tree column labels.
611 * This should be called from the constructor.
612 *
613 * @param columns
614 * The array of tree column labels
615 */
616 protected void setTreeColumns(final String[] columns) {
617 fColumns = columns;
618 }
619
620 /**
621 * Sets the tree label provider.
622 * This should be called from the constructor.
623 *
624 * @param tlp
625 * The tree label provider
626 */
627 protected void setTreeLabelProvider(final TreeLabelProvider tlp) {
628 fLabelProvider = tlp;
629 }
630
631 /**
632 * Sets the relative weight of each part of the time graph combo.
633 * This should be called from the constructor.
634 *
635 * @param weights
636 * The array (length 2) of relative weights of each part of the combo
637 */
638 protected void setWeight(final int[] weights) {
639 fWeight = weights;
640 }
641
642 /**
643 * Sets the filter column labels.
644 * This should be called from the constructor.
645 *
646 * @param filterColumns
647 * The array of filter column labels
648 */
649 protected void setFilterColumns(final String[] filterColumns) {
650 fFilterColumns = filterColumns;
651 }
652
653 /**
654 * Sets the filter label provider.
655 * This should be called from the constructor.
656 *
657 * @param labelProvider
658 * The filter label provider
659 *
660 * @since 3.0
661 */
662 protected void setFilterLabelProvider(final TreeLabelProvider labelProvider) {
663 fFilterLabelProvider = labelProvider;
664 }
665
666 /**
667 * Gets the display width
668 *
669 * @return the display width
670 */
671 protected int getDisplayWidth() {
672 return fDisplayWidth;
673 }
674
675 /**
676 * Gets the comparator for the entries
677 *
678 * @return The entry comparator
679 */
680 protected Comparator<ITimeGraphEntry> getEntryComparator() {
681 return fEntryComparator;
682 }
683
684 /**
685 * Sets the comparator class for the entries
686 *
687 * @param comparator
688 * A comparator object
689 */
690 protected void setEntryComparator(final Comparator<ITimeGraphEntry> comparator) {
691 fEntryComparator = comparator;
692 }
693
694 /**
695 * Gets the trace displayed in the view
696 *
697 * @return The trace
698 */
699 protected ITmfTrace getTrace() {
700 return fTrace;
701 }
702
703 /**
704 * Gets the start time
705 *
706 * @return The start time
707 */
708 protected long getStartTime() {
709 return fStartTime;
710 }
711
712 /**
713 * Sets the start time
714 *
715 * @param time
716 * The start time
717 */
718 protected void setStartTime(long time) {
719 fStartTime = time;
720 }
721
722 /**
723 * Gets the end time
724 *
725 * @return The end time
726 */
727 protected long getEndTime() {
728 return fEndTime;
729 }
730
731 /**
732 * Sets the end time
733 *
734 * @param time
735 * The end time
736 */
737 protected void setEndTime(long time) {
738 fEndTime = time;
739 }
740
741 /**
742 * Sets the auto-expand level to be used for the input of the view. The
743 * value 0 means that there is no auto-expand; 1 means that top-level
744 * elements are expanded, but not their children; 2 means that top-level
745 * elements are expanded, and their children, but not grand-children; and so
746 * on.
747 * <p>
748 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
749 * </p>
750 *
751 * @param level
752 * non-negative level, or <code>ALL_LEVELS</code> to expand all
753 * levels of the tree
754 */
755 protected void setAutoExpandLevel(int level) {
756 fAutoExpandLevel = level;
757 ITimeGraphWrapper tgWrapper = fTimeGraphWrapper;
758 if (tgWrapper != null) {
759 tgWrapper.setAutoExpandLevel(level);
760 }
761 }
762
763 /**
764 * Gets the entry list for a trace
765 *
766 * @param trace
767 * the trace
768 *
769 * @return the entry list map
770 * @since 3.0
771 */
772 protected List<TimeGraphEntry> getEntryList(ITmfTrace trace) {
773 synchronized (fEntryListMap) {
774 return fEntryListMap.get(trace);
775 }
776 }
777
778 /**
779 * Adds a trace entry list to the entry list map
780 *
781 * @param trace
782 * the trace to add
783 * @param list
784 * the list of time graph entries
785 */
786 protected void putEntryList(ITmfTrace trace, List<TimeGraphEntry> list) {
787 synchronized (fEntryListMap) {
788 fEntryListMap.put(trace, new CopyOnWriteArrayList<>(list));
789 }
790 }
791
792 /**
793 * Adds a list of entries to a trace's entry list
794 *
795 * @param trace
796 * the trace
797 * @param list
798 * the list of time graph entries to add
799 * @since 3.0
800 */
801 protected void addToEntryList(ITmfTrace trace, List<TimeGraphEntry> list) {
802 synchronized (fEntryListMap) {
803 List<TimeGraphEntry> entryList = fEntryListMap.get(trace);
804 if (entryList == null) {
805 fEntryListMap.put(trace, new CopyOnWriteArrayList<>(list));
806 } else {
807 entryList.addAll(list);
808 }
809 }
810 }
811
812 /**
813 * Removes a list of entries from a trace's entry list
814 *
815 * @param trace
816 * the trace
817 * @param list
818 * the list of time graph entries to remove
819 * @since 3.0
820 */
821 protected void removeFromEntryList(ITmfTrace trace, List<TimeGraphEntry> list) {
822 synchronized (fEntryListMap) {
823 List<TimeGraphEntry> entryList = fEntryListMap.get(trace);
824 if (entryList != null) {
825 entryList.removeAll(list);
826 }
827 }
828 }
829
830 /**
831 * Text for the "next" button
832 *
833 * @return The "next" button text
834 */
835 protected String getNextText() {
836 return Messages.AbstractTimeGraphtView_NextText;
837 }
838
839 /**
840 * Tooltip for the "next" button
841 *
842 * @return Tooltip for the "next" button
843 */
844 protected String getNextTooltip() {
845 return Messages.AbstractTimeGraphView_NextTooltip;
846 }
847
848 /**
849 * Text for the "Previous" button
850 *
851 * @return The "Previous" button text
852 */
853 protected String getPrevText() {
854 return Messages.AbstractTimeGraphView_PreviousText;
855 }
856
857 /**
858 * Tooltip for the "previous" button
859 *
860 * @return Tooltip for the "previous" button
861 */
862 protected String getPrevTooltip() {
863 return Messages.AbstractTimeGraphView_PreviousTooltip;
864 }
865
866 // ------------------------------------------------------------------------
867 // ViewPart
868 // ------------------------------------------------------------------------
869
870 @Override
871 public void createPartControl(Composite parent) {
872 if (fColumns == null || fLabelProvider == null) {
873 fTimeGraphWrapper = new TimeGraphViewerWrapper(parent, SWT.NONE);
874 TimeGraphViewer viewer = fTimeGraphWrapper.getTimeGraphViewer();
875 viewer.setTimeGraphContentProvider(new TimeGraphContentProvider());
876 } else {
877 TimeGraphComboWrapper wrapper = new TimeGraphComboWrapper(parent, SWT.NONE);
878 fTimeGraphWrapper = wrapper;
879 TimeGraphCombo combo = wrapper.getTimeGraphCombo();
880 combo.setTreeContentProvider(new TreeContentProvider());
881 combo.setTreeLabelProvider(fLabelProvider);
882 combo.setTreeColumns(fColumns);
883 combo.setFilterContentProvider(new TreeContentProvider());
884 combo.setFilterLabelProvider(fFilterLabelProvider);
885 combo.setFilterColumns(fFilterColumns);
886 combo.setTimeGraphContentProvider(new TimeGraphContentProvider());
887 }
888
889 fTimeGraphWrapper.setTimeGraphProvider(fPresentation);
890 fTimeGraphWrapper.setAutoExpandLevel(fAutoExpandLevel);
891
892 fTimeGraphWrapper.getTimeGraphViewer().addRangeListener(new ITimeGraphRangeListener() {
893 @Override
894 public void timeRangeUpdated(TimeGraphRangeUpdateEvent event) {
895 final long startTime = event.getStartTime();
896 final long endTime = event.getEndTime();
897 TmfTimeRange range = new TmfTimeRange(new TmfNanoTimestamp(startTime), new TmfNanoTimestamp(endTime));
898 broadcast(new TmfRangeSynchSignal(AbstractTimeGraphView.this, range));
899 startZoomThread(startTime, endTime);
900 }
901 });
902
903 fTimeGraphWrapper.getTimeGraphViewer().addTimeListener(new ITimeGraphTimeListener() {
904 @Override
905 public void timeSelected(TimeGraphTimeEvent event) {
906 TmfNanoTimestamp startTime = new TmfNanoTimestamp(event.getBeginTime());
907 TmfNanoTimestamp endTime = new TmfNanoTimestamp(event.getEndTime());
908 broadcast(new TmfTimeSynchSignal(AbstractTimeGraphView.this, startTime, endTime));
909 }
910 });
911
912 fTimeGraphWrapper.getTimeGraphViewer().setTimeFormat(TimeFormat.CALENDAR);
913
914 IStatusLineManager statusLineManager = getViewSite().getActionBars().getStatusLineManager();
915 fTimeGraphWrapper.getTimeGraphViewer().getTimeGraphControl().setStatusLineManager(statusLineManager);
916
917 // View Action Handling
918 makeActions();
919 contributeToActionBars();
920
921 ITmfTrace trace = getActiveTrace();
922 if (trace != null) {
923 traceSelected(new TmfTraceSelectedSignal(this, trace));
924 }
925
926 // make selection available to other views
927 getSite().setSelectionProvider(fTimeGraphWrapper.getSelectionProvider());
928 }
929
930 @Override
931 public void setFocus() {
932 fTimeGraphWrapper.setFocus();
933 }
934
935 // ------------------------------------------------------------------------
936 // Signal handlers
937 // ------------------------------------------------------------------------
938
939 /**
940 * Handler for the trace opened signal.
941 *
942 * @param signal
943 * The incoming signal
944 * @since 2.0
945 */
946 @TmfSignalHandler
947 public void traceOpened(TmfTraceOpenedSignal signal) {
948 fTrace = signal.getTrace();
949 loadTrace();
950 }
951
952 /**
953 * Handler for the trace selected signal
954 *
955 * @param signal
956 * The incoming signal
957 */
958 @TmfSignalHandler
959 public void traceSelected(final TmfTraceSelectedSignal signal) {
960 if (signal.getTrace() == fTrace) {
961 return;
962 }
963 fTrace = signal.getTrace();
964
965 loadTrace();
966 }
967
968 /**
969 * Trace is closed: clear the data structures and the view
970 *
971 * @param signal
972 * the signal received
973 */
974 @TmfSignalHandler
975 public void traceClosed(final TmfTraceClosedSignal signal) {
976 synchronized (fBuildThreadMap) {
977 for (ITmfTrace trace : getTracesToBuild(signal.getTrace())) {
978 BuildThread buildThread = fBuildThreadMap.remove(trace);
979 if (buildThread != null) {
980 buildThread.cancel();
981 }
982 }
983 }
984 synchronized (fEntryListMap) {
985 fEntryListMap.remove(signal.getTrace());
986 }
987 if (signal.getTrace() == fTrace) {
988 fTrace = null;
989 fStartTime = 0;
990 fEndTime = 0;
991 if (fZoomThread != null) {
992 fZoomThread.cancel();
993 }
994 refresh();
995 }
996 }
997
998 /**
999 * Handler for the time synch signal
1000 *
1001 * @param signal
1002 * The signal that's received
1003 */
1004 @TmfSignalHandler
1005 public void synchToTime(final TmfTimeSynchSignal signal) {
1006 if (signal.getSource() == this || fTrace == null) {
1007 return;
1008 }
1009 final long beginTime = signal.getBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1010 final long endTime = signal.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1011
1012 Display.getDefault().asyncExec(new Runnable() {
1013 @Override
1014 public void run() {
1015 if (fTimeGraphWrapper.isDisposed()) {
1016 return;
1017 }
1018 if (beginTime == endTime) {
1019 fTimeGraphWrapper.getTimeGraphViewer().setSelectedTime(beginTime, true);
1020 } else {
1021 fTimeGraphWrapper.getTimeGraphViewer().setSelectionRange(beginTime, endTime);
1022 }
1023 startZoomThread(fTimeGraphWrapper.getTimeGraphViewer().getTime0(), fTimeGraphWrapper.getTimeGraphViewer().getTime1());
1024
1025 synchingToTime(beginTime);
1026 }
1027 });
1028 }
1029
1030 /**
1031 * Handler for the range synch signal
1032 *
1033 * @param signal
1034 * The signal that's received
1035 */
1036 @TmfSignalHandler
1037 public void synchToRange(final TmfRangeSynchSignal signal) {
1038 if (signal.getSource() == this || fTrace == null) {
1039 return;
1040 }
1041 if (signal.getCurrentRange().getIntersection(fTrace.getTimeRange()) == null) {
1042 return;
1043 }
1044 final long startTime = signal.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1045 final long endTime = signal.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1046 Display.getDefault().asyncExec(new Runnable() {
1047 @Override
1048 public void run() {
1049 if (fTimeGraphWrapper.isDisposed()) {
1050 return;
1051 }
1052 fTimeGraphWrapper.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
1053 startZoomThread(startTime, endTime);
1054 }
1055 });
1056 }
1057
1058 /**
1059 * @param signal the format of the timestamps was updated.
1060 * @since 2.1
1061 */
1062 @TmfSignalHandler
1063 public void updateTimeFormat( final TmfTimestampFormatUpdateSignal signal){
1064 fTimeGraphWrapper.refresh();
1065 }
1066
1067 // ------------------------------------------------------------------------
1068 // Internal
1069 // ------------------------------------------------------------------------
1070
1071 private void loadTrace() {
1072 synchronized (fEntryListMap) {
1073 fEntryList = fEntryListMap.get(fTrace);
1074 if (fEntryList == null) {
1075 rebuild();
1076 } else {
1077 fStartTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1078 fEndTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1079 refresh();
1080 }
1081 }
1082 }
1083
1084 /**
1085 * Forces a rebuild of the entries list, even if entries already exist for this trace
1086 * @since 3.0
1087 */
1088 protected void rebuild() {
1089 setStartTime(Long.MAX_VALUE);
1090 setEndTime(Long.MIN_VALUE);
1091 refresh();
1092 ITmfTrace viewTrace = fTrace;
1093 if (viewTrace == null) {
1094 return;
1095 }
1096 synchronized (fBuildThreadMap) {
1097 for (ITmfTrace trace : getTracesToBuild(viewTrace)) {
1098 if (trace == null) {
1099 break;
1100 }
1101 BuildThread buildThread = new BuildThread(trace, viewTrace, getName());
1102 fBuildThreadMap.put(trace, buildThread);
1103 buildThread.start();
1104 }
1105 }
1106 }
1107
1108 /**
1109 * Method called when synching to a given timestamp. Inheriting classes can
1110 * perform actions here to update the view at the given timestamp.
1111 *
1112 * @param time
1113 * The currently selected time
1114 */
1115 protected void synchingToTime(long time) {
1116
1117 }
1118
1119 /**
1120 * Return the list of traces whose data or analysis results will be used to
1121 * populate the view. By default, if the trace is an experiment, the traces
1122 * under it will be returned, otherwise, the trace itself is returned.
1123 *
1124 * A build thread will be started for each trace returned by this method,
1125 * some of which may receive events in live streaming mode.
1126 *
1127 * @param trace
1128 * The trace associated with this view
1129 * @return List of traces with data to display
1130 * @since 3.0
1131 */
1132 protected @NonNull Iterable<ITmfTrace> getTracesToBuild(@NonNull ITmfTrace trace) {
1133 return checkNotNull(Arrays.asList(TmfTraceManager.getTraceSet(trace)));
1134 }
1135
1136 /**
1137 * Build the entries list to show in this time graph
1138 *
1139 * Called from the BuildThread
1140 *
1141 * @param trace
1142 * The trace being built
1143 * @param parentTrace
1144 * The parent of the trace set, or the trace itself
1145 * @param monitor
1146 * The progress monitor object
1147 * @since 3.0
1148 */
1149 protected abstract void buildEventList(@NonNull ITmfTrace trace, @NonNull ITmfTrace parentTrace, @NonNull IProgressMonitor monitor);
1150
1151 /**
1152 * Gets the list of event for an entry in a given timerange
1153 *
1154 * @param entry
1155 * The entry to get events for
1156 * @param startTime
1157 * Start of the time range
1158 * @param endTime
1159 * End of the time range
1160 * @param resolution
1161 * The resolution
1162 * @param monitor
1163 * The progress monitor object
1164 * @return The list of events for the entry
1165 */
1166 protected abstract @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry entry,
1167 long startTime, long endTime, long resolution,
1168 @NonNull IProgressMonitor monitor);
1169
1170 /**
1171 * Gets the list of links (displayed as arrows) for a trace in a given
1172 * timerange. Default implementation returns an empty list.
1173 *
1174 * @param startTime
1175 * Start of the time range
1176 * @param endTime
1177 * End of the time range
1178 * @param resolution
1179 * The resolution
1180 * @param monitor
1181 * The progress monitor object
1182 * @return The list of link events
1183 * @since 2.1
1184 */
1185 protected @Nullable List<ILinkEvent> getLinkList(long startTime, long endTime,
1186 long resolution, @NonNull IProgressMonitor monitor) {
1187 return new ArrayList<>();
1188 }
1189
1190
1191 /**
1192 * Refresh the display
1193 */
1194 protected void refresh() {
1195 TmfUiRefreshHandler.getInstance().queueUpdate(this, new Runnable() {
1196 @Override
1197 public void run() {
1198 if (fTimeGraphWrapper.isDisposed()) {
1199 return;
1200 }
1201 boolean hasEntries = false;
1202 synchronized (fEntryListMap) {
1203 fEntryList = fEntryListMap.get(fTrace);
1204 if (fEntryList == null) {
1205 fEntryList = new CopyOnWriteArrayList<>();
1206 } else if (fEntryComparator != null) {
1207 List<TimeGraphEntry> list = new ArrayList<>(fEntryList);
1208 Collections.sort(list, fEntryComparator);
1209 fEntryList.clear();
1210 fEntryList.addAll(list);
1211 }
1212 hasEntries = fEntryList.size() != 0;
1213 }
1214 if (fEntryList != fTimeGraphWrapper.getInput()) {
1215 fTimeGraphWrapper.setInput(fEntryList);
1216 } else {
1217 fTimeGraphWrapper.refresh();
1218 }
1219 fTimeGraphWrapper.getTimeGraphViewer().setTimeBounds(fStartTime, fEndTime);
1220
1221 long selectionBeginTime = fTrace == null ? 0 : fTraceManager.getSelectionBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1222 long selectionEndTime = fTrace == null ? 0 : fTraceManager.getSelectionEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1223 long startTime = fTrace == null ? 0 : fTraceManager.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1224 long endTime = fTrace == null ? 0 : fTraceManager.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
1225 startTime = Math.max(startTime, fStartTime);
1226 endTime = Math.min(endTime, fEndTime);
1227 fTimeGraphWrapper.getTimeGraphViewer().setSelectionRange(selectionBeginTime, selectionEndTime);
1228 fTimeGraphWrapper.getTimeGraphViewer().setStartFinishTime(startTime, endTime);
1229
1230 if (fTimeGraphWrapper instanceof TimeGraphComboWrapper && !fPackDone) {
1231 for (TreeColumn column : ((TimeGraphComboWrapper) fTimeGraphWrapper).getTreeViewer().getTree().getColumns()) {
1232 column.pack();
1233 }
1234 if (hasEntries) {
1235 fPackDone = true;
1236 }
1237 }
1238
1239 startZoomThread(startTime, endTime);
1240 }
1241 });
1242 }
1243
1244 /**
1245 * Redraw the canvas
1246 */
1247 protected void redraw() {
1248 synchronized (fSyncObj) {
1249 if (fRedrawState == State.IDLE) {
1250 fRedrawState = State.BUSY;
1251 } else {
1252 fRedrawState = State.PENDING;
1253 return;
1254 }
1255 }
1256 Display.getDefault().asyncExec(new Runnable() {
1257 @Override
1258 public void run() {
1259 if (fTimeGraphWrapper.isDisposed()) {
1260 return;
1261 }
1262 fTimeGraphWrapper.redraw();
1263 fTimeGraphWrapper.update();
1264 synchronized (fSyncObj) {
1265 if (fRedrawState == State.PENDING) {
1266 fRedrawState = State.IDLE;
1267 redraw();
1268 } else {
1269 fRedrawState = State.IDLE;
1270 }
1271 }
1272 }
1273 });
1274 }
1275
1276 private void startZoomThread(long startTime, long endTime) {
1277 if (fZoomThread != null) {
1278 fZoomThread.cancel();
1279 }
1280 final List<TimeGraphEntry> entryList = fEntryList;
1281 if (entryList == null) {
1282 return;
1283 }
1284 fZoomThread = new ZoomThread(entryList, startTime, endTime, getName());
1285 fZoomThread.start();
1286 }
1287
1288 private void makeActions() {
1289 fPreviousResourceAction = fTimeGraphWrapper.getTimeGraphViewer().getPreviousItemAction();
1290 fPreviousResourceAction.setText(getPrevText());
1291 fPreviousResourceAction.setToolTipText(getPrevTooltip());
1292 fNextResourceAction = fTimeGraphWrapper.getTimeGraphViewer().getNextItemAction();
1293 fNextResourceAction.setText(getNextText());
1294 fNextResourceAction.setToolTipText(getNextTooltip());
1295 }
1296
1297 private void contributeToActionBars() {
1298 IActionBars bars = getViewSite().getActionBars();
1299 fillLocalToolBar(bars.getToolBarManager());
1300 }
1301
1302 /**
1303 * Add actions to local tool bar manager
1304 *
1305 * @param manager the tool bar manager
1306 */
1307 protected void fillLocalToolBar(IToolBarManager manager) {
1308 if (fTimeGraphWrapper instanceof TimeGraphComboWrapper) {
1309 if (fFilterColumns != null && fFilterLabelProvider != null && fFilterColumns.length > 0) {
1310 manager.add(((TimeGraphComboWrapper) fTimeGraphWrapper).getShowFilterAction());
1311 }
1312 }
1313 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getShowLegendAction());
1314 manager.add(new Separator());
1315 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getResetScaleAction());
1316 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getPreviousEventAction());
1317 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getNextEventAction());
1318 manager.add(fPreviousResourceAction);
1319 manager.add(fNextResourceAction);
1320 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getZoomInAction());
1321 manager.add(fTimeGraphWrapper.getTimeGraphViewer().getZoomOutAction());
1322 manager.add(new Separator());
1323 }
1324 }
This page took 0.075779 seconds and 5 git commands to generate.