1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson, others
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
10 * Patrick Tasse - Initial API and implementation
11 * François Rajotte - Filter implementation
12 * Geneviève Bastien - Add event links between entries
13 * Christian Mansky - Add check active / uncheck inactive buttons
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.HashMap
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
28 import org
.eclipse
.jface
.viewers
.ILabelProviderListener
;
29 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
32 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
33 import org
.eclipse
.jface
.viewers
.ITreeViewerListener
;
34 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
35 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
36 import org
.eclipse
.jface
.viewers
.TreeExpansionEvent
;
37 import org
.eclipse
.jface
.viewers
.TreeViewer
;
38 import org
.eclipse
.jface
.viewers
.Viewer
;
39 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.custom
.SashForm
;
42 import org
.eclipse
.swt
.events
.ControlAdapter
;
43 import org
.eclipse
.swt
.events
.ControlEvent
;
44 import org
.eclipse
.swt
.events
.DisposeEvent
;
45 import org
.eclipse
.swt
.events
.DisposeListener
;
46 import org
.eclipse
.swt
.events
.MouseEvent
;
47 import org
.eclipse
.swt
.events
.MouseTrackAdapter
;
48 import org
.eclipse
.swt
.events
.MouseWheelListener
;
49 import org
.eclipse
.swt
.events
.PaintEvent
;
50 import org
.eclipse
.swt
.events
.PaintListener
;
51 import org
.eclipse
.swt
.events
.SelectionAdapter
;
52 import org
.eclipse
.swt
.events
.SelectionEvent
;
53 import org
.eclipse
.swt
.graphics
.Font
;
54 import org
.eclipse
.swt
.graphics
.FontData
;
55 import org
.eclipse
.swt
.graphics
.Image
;
56 import org
.eclipse
.swt
.graphics
.Point
;
57 import org
.eclipse
.swt
.graphics
.Rectangle
;
58 import org
.eclipse
.swt
.layout
.FillLayout
;
59 import org
.eclipse
.swt
.layout
.GridLayout
;
60 import org
.eclipse
.swt
.widgets
.Composite
;
61 import org
.eclipse
.swt
.widgets
.Control
;
62 import org
.eclipse
.swt
.widgets
.Display
;
63 import org
.eclipse
.swt
.widgets
.Event
;
64 import org
.eclipse
.swt
.widgets
.Listener
;
65 import org
.eclipse
.swt
.widgets
.Sash
;
66 import org
.eclipse
.swt
.widgets
.Slider
;
67 import org
.eclipse
.swt
.widgets
.Tree
;
68 import org
.eclipse
.swt
.widgets
.TreeColumn
;
69 import org
.eclipse
.swt
.widgets
.TreeItem
;
70 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.dialogs
.ITimeGraphEntryActiveProvider
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.dialogs
.ShowFilterDialogAction
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphColorScheme
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphControl
;
81 import com
.google
.common
.collect
.Iterables
;
84 * Time graph "combo" view (with the list/tree on the left and the gantt chart
87 * @author Patrick Tasse
89 public class TimeGraphCombo
extends Composite
{
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
95 /** Constant indicating that all levels of the time graph should be expanded */
96 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
98 private static final Object FILLER
= new Object();
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
104 /** The tree viewer */
105 private TreeViewer fTreeViewer
;
107 /** The time viewer */
108 private @NonNull TimeGraphViewer fTimeGraphViewer
;
110 /** The selection listener map */
111 private final Map
<ITimeGraphSelectionListener
, SelectionListenerWrapper
> fSelectionListenerMap
= new HashMap
<>();
113 /** The map of viewer filters to viewer filter wrappers */
114 private final Map
<@NonNull ViewerFilter
, @NonNull ViewerFilter
> fViewerFilterMap
= new HashMap
<>();
117 * Flag to block the tree selection changed listener when triggered by the
120 private boolean fInhibitTreeSelection
= false;
122 /** Number of filler rows used by the tree content provider */
123 private int fNumFillerRows
;
125 /** Calculated item height for Linux workaround */
126 private int fLinuxItemHeight
= 0;
128 /** The action that opens the filter dialog */
129 private ShowFilterDialogAction fShowFilterDialogAction
;
131 /** Default weight of each part of the sash */
132 private static final int[] DEFAULT_WEIGHTS
= { 1, 1 };
134 /** List of all expanded items whose parents are also expanded */
135 private List
<TreeItem
> fVisibleExpandedItems
= null;
137 private Listener fSashDragListener
;
138 private SashForm fSashForm
;
140 private final boolean fScrollBarsInTreeWorkaround
;
142 private Font fTreeFont
;
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * The TimeGraphViewerExtension is used to set appropriate values and to
150 * override methods that could be called directly by the user and that must
151 * be handled by the time graph combo.
153 private class TimeGraphViewerExtension
extends TimeGraphViewer
{
155 private TimeGraphViewerExtension(Composite parent
, int style
, Tree tree
) {
156 super(parent
, style
);
157 setItemHeight(TimeGraphCombo
.this.getItemHeight(tree
, true));
158 setHeaderHeight(tree
.getHeaderHeight());
159 setBorderWidth(tree
.getBorderWidth());
164 public ShowFilterDialogAction
getShowFilterDialogAction() {
165 return TimeGraphCombo
.this.getShowFilterDialogAction();
169 protected TimeGraphControl
createTimeGraphControl(Composite composite
, TimeGraphColorScheme colors
) {
170 return new TimeGraphControl(composite
, colors
) {
172 public void verticalZoom(boolean zoomIn
) {
173 TimeGraphCombo
.this.verticalZoom(zoomIn
);
177 public void resetVerticalZoom() {
178 TimeGraphCombo
.this.resetVerticalZoom();
185 * The TreeContentProviderWrapper is used to insert filler items after
186 * the elements of the tree's real content provider.
188 private class TreeContentProviderWrapper
implements ITreeContentProvider
{
189 private final ITreeContentProvider contentProvider
;
191 public TreeContentProviderWrapper(ITreeContentProvider contentProvider
) {
192 this.contentProvider
= contentProvider
;
196 public void dispose() {
197 contentProvider
.dispose();
201 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
202 contentProvider
.inputChanged(viewer
, oldInput
, newInput
);
206 public Object
[] getElements(Object inputElement
) {
207 Object
[] elements
= contentProvider
.getElements(inputElement
);
208 // add filler elements to ensure alignment with time analysis viewer
209 Object
[] oElements
= Arrays
.copyOf(elements
, elements
.length
+ fNumFillerRows
, Object
[].class);
210 for (int i
= 0; i
< fNumFillerRows
; i
++) {
211 oElements
[elements
.length
+ i
] = FILLER
;
217 public Object
[] getChildren(Object parentElement
) {
218 if (parentElement
instanceof ITimeGraphEntry
) {
219 return contentProvider
.getChildren(parentElement
);
221 return new Object
[0];
225 public Object
getParent(Object element
) {
226 if (element
instanceof ITimeGraphEntry
) {
227 return contentProvider
.getParent(element
);
233 public boolean hasChildren(Object element
) {
234 if (element
instanceof ITimeGraphEntry
) {
235 return contentProvider
.hasChildren(element
);
242 * The TreeLabelProviderWrapper is used to intercept the filler items
243 * from the calls to the tree's real label provider.
245 private class TreeLabelProviderWrapper
implements ITableLabelProvider
{
246 private final ITableLabelProvider labelProvider
;
248 public TreeLabelProviderWrapper(ITableLabelProvider labelProvider
) {
249 this.labelProvider
= labelProvider
;
253 public void addListener(ILabelProviderListener listener
) {
254 labelProvider
.addListener(listener
);
258 public void dispose() {
259 labelProvider
.dispose();
263 public boolean isLabelProperty(Object element
, String property
) {
264 if (element
instanceof ITimeGraphEntry
) {
265 return labelProvider
.isLabelProperty(element
, property
);
271 public void removeListener(ILabelProviderListener listener
) {
272 labelProvider
.removeListener(listener
);
276 public Image
getColumnImage(Object element
, int columnIndex
) {
277 if (element
instanceof ITimeGraphEntry
) {
278 return labelProvider
.getColumnImage(element
, columnIndex
);
284 public String
getColumnText(Object element
, int columnIndex
) {
285 if (element
instanceof ITimeGraphEntry
) {
286 return labelProvider
.getColumnText(element
, columnIndex
);
294 * The SelectionListenerWrapper is used to intercept the filler items from
295 * the time graph combo's real selection listener, and to prevent double
296 * notifications from being sent when selection changes in both tree and
297 * time graph at the same time.
299 private class SelectionListenerWrapper
implements ISelectionChangedListener
, ITimeGraphSelectionListener
{
300 private final ITimeGraphSelectionListener listener
;
301 private ITimeGraphEntry selection
= null;
303 public SelectionListenerWrapper(ITimeGraphSelectionListener listener
) {
304 this.listener
= listener
;
308 public void selectionChanged(SelectionChangedEvent event
) {
309 if (fInhibitTreeSelection
) {
312 Object element
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
313 if (element
instanceof ITimeGraphEntry
) {
314 ITimeGraphEntry entry
= (ITimeGraphEntry
) element
;
315 if (entry
!= selection
) {
317 listener
.selectionChanged(new TimeGraphSelectionEvent(event
.getSource(), selection
));
323 public void selectionChanged(TimeGraphSelectionEvent event
) {
324 ITimeGraphEntry entry
= event
.getSelection();
325 if (entry
!= selection
) {
327 listener
.selectionChanged(new TimeGraphSelectionEvent(event
.getSource(), selection
));
333 * The ViewerFilterWrapper is used to intercept the filler items from
334 * the time graph combo's real ViewerFilters. These filler items should
337 private class ViewerFilterWrapper
extends ViewerFilter
{
339 private ViewerFilter fWrappedFilter
;
341 ViewerFilterWrapper(ViewerFilter filter
) {
343 this.fWrappedFilter
= filter
;
347 public boolean select(Viewer viewer
, Object parentElement
, Object element
) {
348 if (element
instanceof ITimeGraphEntry
) {
349 return fWrappedFilter
.select(viewer
, parentElement
, element
);
356 // ------------------------------------------------------------------------
358 // ------------------------------------------------------------------------
361 * Constructs a new instance of this class given its parent
362 * and a style value describing its behavior and appearance.
364 * @param parent a widget which will be the parent of the new instance (cannot be null)
365 * @param style the style of widget to construct
367 public TimeGraphCombo(Composite parent
, int style
) {
368 this(parent
, style
, DEFAULT_WEIGHTS
);
372 * Constructs a new instance of this class given its parent and a style
373 * value describing its behavior and appearance.
376 * a widget which will be the parent of the new instance (cannot
379 * the style of widget to construct
381 * The array (length 2) of relative weights of each side of the sash form
383 public TimeGraphCombo(Composite parent
, int style
, int[] weights
) {
384 super(parent
, style
);
385 setLayout(new FillLayout());
387 fSashForm
= new SashForm(this, SWT
.NONE
);
390 * In Windows, SWT.H_SCROLL | SWT.NO_SCROLL is not properly supported,
391 * both scroll bars are always created. See Tree.checkStyle: "Even when
392 * WS_HSCROLL or WS_VSCROLL is not specified, Windows creates trees and
393 * tables with scroll bars."
395 fScrollBarsInTreeWorkaround
= "win32".equals(SWT
.getPlatform()); //$NON-NLS-1$
397 int scrollBarStyle
= fScrollBarsInTreeWorkaround ? SWT
.H_SCROLL
: SWT
.H_SCROLL
| SWT
.NO_SCROLL
;
399 fTreeViewer
= new TreeViewer(fSashForm
, SWT
.FULL_SELECTION
| scrollBarStyle
);
400 fTreeViewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
401 final Tree tree
= fTreeViewer
.getTree();
402 tree
.setHeaderVisible(true);
403 tree
.setLinesVisible(true);
405 fTimeGraphViewer
= new TimeGraphViewerExtension(fSashForm
, SWT
.NONE
, tree
);
407 if (fScrollBarsInTreeWorkaround
) {
408 // Feature in Windows. The tree vertical bar reappears when
409 // the control is resized so we need to hide it again.
410 tree
.addControlListener(new ControlAdapter() {
411 private int depth
= 0;
414 public void controlResized(ControlEvent e
) {
417 tree
.getVerticalBar().setEnabled(false);
418 // this can trigger controlResized recursively
419 tree
.getVerticalBar().setVisible(false);
425 // Bug in Linux. The tree header height is 0 in constructor,
426 // so we need to reset it later when the control is painted.
427 // This work around used to be done on control resized but the header
428 // height was not initialized on the initial resize on GTK3.
429 tree
.addPaintListener(new PaintListener() {
431 public void paintControl(PaintEvent e
) {
432 int headerHeight
= tree
.getHeaderHeight();
433 if (headerHeight
> 0) {
434 fTimeGraphViewer
.setHeaderHeight(headerHeight
);
435 tree
.removePaintListener(this);
440 tree
.addDisposeListener(new DisposeListener() {
442 public void widgetDisposed(DisposeEvent e
) {
443 if (fTreeFont
!= null) {
449 // ensure synchronization of expanded items between tree and time graph
450 fTreeViewer
.addTreeListener(new ITreeViewerListener() {
452 public void treeCollapsed(TreeExpansionEvent event
) {
453 fTimeGraphViewer
.setExpandedState((ITimeGraphEntry
) event
.getElement(), false);
454 // queue the alignment update because the tree items may only be
455 // actually collapsed after the listeners have been notified
456 fVisibleExpandedItems
= null; // invalidate the cache
457 getDisplay().asyncExec(new Runnable() {
460 alignTreeItems(true);
465 public void treeExpanded(TreeExpansionEvent event
) {
466 ITimeGraphEntry entry
= (ITimeGraphEntry
) event
.getElement();
467 fTimeGraphViewer
.setExpandedState(entry
, true);
468 Set
<Object
> expandedElements
= new HashSet
<>(Arrays
.asList(fTreeViewer
.getExpandedElements()));
469 for (ITimeGraphEntry child
: entry
.getChildren()) {
470 if (child
.hasChildren()) {
471 boolean expanded
= expandedElements
.contains(child
);
472 fTimeGraphViewer
.setExpandedState(child
, expanded
);
475 // queue the alignment update because the tree items may only be
476 // actually expanded after the listeners have been notified
477 fVisibleExpandedItems
= null; // invalidate the cache
478 getDisplay().asyncExec(new Runnable() {
481 alignTreeItems(true);
486 // ensure synchronization of expanded items between tree and time graph
487 fTimeGraphViewer
.addTreeListener(new ITimeGraphTreeListener() {
489 public void treeCollapsed(TimeGraphTreeExpansionEvent event
) {
490 fTreeViewer
.setExpandedState(event
.getEntry(), false);
491 alignTreeItems(true);
495 public void treeExpanded(TimeGraphTreeExpansionEvent event
) {
496 ITimeGraphEntry entry
= event
.getEntry();
497 fTreeViewer
.setExpandedState(entry
, true);
498 Set
<Object
> expandedElements
= new HashSet
<>(Arrays
.asList(fTreeViewer
.getExpandedElements()));
499 for (ITimeGraphEntry child
: entry
.getChildren()) {
500 if (child
.hasChildren()) {
501 boolean expanded
= expandedElements
.contains(child
);
502 fTimeGraphViewer
.setExpandedState(child
, expanded
);
505 alignTreeItems(true);
509 // prevent mouse button from selecting a filler tree item
510 tree
.addListener(SWT
.MouseDown
, new Listener() {
512 public void handleEvent(Event event
) {
513 TreeItem treeItem
= tree
.getItem(new Point(event
.x
, event
.y
));
514 if (treeItem
== null || treeItem
.getData() == FILLER
) {
516 List
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
, false);
517 if (treeItems
.size() == 0) {
518 fTreeViewer
.setSelection(new StructuredSelection());
519 fTimeGraphViewer
.setSelection(null);
522 // this prevents from scrolling up when selecting
523 // the partially visible tree item at the bottom
524 tree
.select(treeItems
.get(treeItems
.size() - 1));
525 fTreeViewer
.setSelection(new StructuredSelection());
526 fTimeGraphViewer
.setSelection(null);
531 // prevent mouse wheel from scrolling down into filler tree items
532 tree
.addListener(SWT
.MouseWheel
, new Listener() {
534 public void handleEvent(Event event
) {
536 Slider scrollBar
= fTimeGraphViewer
.getVerticalBar();
537 fTimeGraphViewer
.setTopIndex(scrollBar
.getSelection() - event
.count
);
538 alignTreeItems(false);
542 // prevent key stroke from selecting a filler tree item
543 tree
.addListener(SWT
.KeyDown
, new Listener() {
545 public void handleEvent(Event event
) {
546 List
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
, false);
547 if (treeItems
.size() == 0) {
548 fTreeViewer
.setSelection(new StructuredSelection());
552 if (event
.keyCode
== SWT
.ARROW_DOWN
) {
553 int index
= Math
.min(fTimeGraphViewer
.getSelectionIndex() + 1, treeItems
.size() - 1);
554 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) treeItems
.get(index
).getData());
556 } else if (event
.keyCode
== SWT
.PAGE_DOWN
) {
557 int height
= tree
.getSize().y
- tree
.getHeaderHeight() - tree
.getHorizontalBar().getSize().y
;
558 int countPerPage
= height
/ getItemHeight(tree
, false);
559 int index
= Math
.min(fTimeGraphViewer
.getSelectionIndex() + countPerPage
- 1, treeItems
.size() - 1);
560 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) treeItems
.get(index
).getData());
562 } else if (event
.keyCode
== SWT
.END
) {
563 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) treeItems
.get(treeItems
.size() - 1).getData());
565 } else if ((event
.character
== '+' || event
.character
== '=') && ((event
.stateMask
& SWT
.CTRL
) != 0)) {
567 } else if (event
.character
== '-' && ((event
.stateMask
& SWT
.CTRL
) != 0)) {
569 } else if (event
.character
== '0' && ((event
.stateMask
& SWT
.CTRL
) != 0)) {
574 if (fTimeGraphViewer
.getSelectionIndex() >= 0) {
575 fTreeViewer
.setSelection(new StructuredSelection(fTimeGraphViewer
.getSelection()));
577 fTreeViewer
.setSelection(new StructuredSelection());
579 alignTreeItems(false);
583 // ensure alignment of top item between tree and time graph
584 fTimeGraphViewer
.getTimeGraphControl().addControlListener(new ControlAdapter() {
586 public void controlResized(ControlEvent e
) {
587 alignTreeItems(false);
591 // ensure synchronization of selected item between tree and time graph
592 fTreeViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
594 public void selectionChanged(SelectionChangedEvent event
) {
595 if (fInhibitTreeSelection
) {
598 if (event
.getSelection() instanceof IStructuredSelection
) {
599 Object selection
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
600 if (selection
instanceof ITimeGraphEntry
) {
601 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) selection
);
603 alignTreeItems(false);
608 // ensure synchronization of selected item between tree and time graph
609 fTimeGraphViewer
.addSelectionListener(new ITimeGraphSelectionListener() {
611 public void selectionChanged(TimeGraphSelectionEvent event
) {
612 ITimeGraphEntry entry
= fTimeGraphViewer
.getSelection();
613 fInhibitTreeSelection
= true; // block the tree selection changed listener
615 StructuredSelection selection
= new StructuredSelection(entry
);
616 fTreeViewer
.setSelection(selection
);
618 fTreeViewer
.setSelection(new StructuredSelection());
620 fInhibitTreeSelection
= false;
621 alignTreeItems(false);
625 // ensure alignment of top item between tree and time graph
626 fTimeGraphViewer
.getVerticalBar().addSelectionListener(new SelectionAdapter() {
628 public void widgetSelected(SelectionEvent e
) {
629 alignTreeItems(false);
633 // ensure alignment of top item between tree and time graph
634 fTimeGraphViewer
.getTimeGraphControl().addMouseWheelListener(new MouseWheelListener() {
636 public void mouseScrolled(MouseEvent e
) {
637 alignTreeItems(false);
641 // ensure the tree has focus control when mouse is over it if the time graph had control
642 fTreeViewer
.getControl().addMouseTrackListener(new MouseTrackAdapter() {
644 public void mouseEnter(MouseEvent e
) {
645 if (fTimeGraphViewer
.getTimeGraphControl().isFocusControl()) {
646 fTreeViewer
.getControl().setFocus();
651 // ensure the time graph has focus control when mouse is over it if the tree had control
652 fTimeGraphViewer
.getTimeGraphControl().addMouseTrackListener(new MouseTrackAdapter() {
654 public void mouseEnter(MouseEvent e
) {
655 if (fTreeViewer
.getControl().isFocusControl()) {
656 fTimeGraphViewer
.getTimeGraphControl().setFocus();
660 fTimeGraphViewer
.getTimeGraphScale().addMouseTrackListener(new MouseTrackAdapter() {
662 public void mouseEnter(MouseEvent e
) {
663 if (fTreeViewer
.getControl().isFocusControl()) {
664 fTimeGraphViewer
.getTimeGraphControl().setFocus();
669 // The filler rows are required to ensure alignment when the tree does not have a
670 // visible horizontal scroll bar. The tree does not allow its top item to be set
671 // to a value that would cause blank space to be drawn at the bottom of the tree.
672 fNumFillerRows
= Display
.getDefault().getBounds().height
/ getItemHeight(tree
, false);
674 fSashForm
.setWeights(weights
);
676 fTimeGraphViewer
.getTimeGraphControl().addPaintListener(new PaintListener() {
678 public void paintControl(PaintEvent e
) {
679 // Sashes in a SashForm are being created on layout so add the
680 // drag listener here
681 if (fSashDragListener
== null) {
682 for (Control control
: fSashForm
.getChildren()) {
683 if (control
instanceof Sash
) {
684 fSashDragListener
= new Listener() {
687 public void handleEvent(Event event
) {
688 sendTimeViewAlignmentChanged();
692 control
.removePaintListener(this);
693 control
.addListener(SWT
.Selection
, fSashDragListener
);
694 // There should be only one sash
703 private void verticalZoom(boolean zoomIn
) {
704 Tree tree
= fTreeViewer
.getTree();
705 FontData fontData
= tree
.getFont().getFontData()[0];
706 int height
= fontData
.getHeight() + (zoomIn ?
1 : -1);
710 fontData
.setHeight(height
);
711 if (fTreeFont
!= null) {
714 fTreeFont
= new Font(tree
.getDisplay(), fontData
);
715 tree
.setFont(fTreeFont
);
718 fTimeGraphViewer
.setHeaderHeight(tree
.getHeaderHeight());
719 fTimeGraphViewer
.setItemHeight(getItemHeight(tree
, true));
720 alignTreeItems(false);
723 private void resetVerticalZoom() {
724 Tree tree
= fTreeViewer
.getTree();
725 if (fTreeFont
!= null) {
732 fTimeGraphViewer
.setHeaderHeight(tree
.getHeaderHeight());
733 fTimeGraphViewer
.setItemHeight(getItemHeight(tree
, true));
734 alignTreeItems(false);
737 private void sendTimeViewAlignmentChanged() {
738 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(fSashForm
, getTimeViewAlignmentInfo()));
741 // ------------------------------------------------------------------------
743 // ------------------------------------------------------------------------
746 * Returns this time graph combo's tree viewer.
748 * @return the tree viewer
750 public TreeViewer
getTreeViewer() {
755 * Returns this time graph combo's time graph viewer.
757 * @return the time graph viewer
759 public @NonNull TimeGraphViewer
getTimeGraphViewer() {
760 return fTimeGraphViewer
;
764 * Get the show filter dialog action.
766 * @return The Action object
769 public ShowFilterDialogAction
getShowFilterDialogAction() {
770 if (fShowFilterDialogAction
== null) {
771 fShowFilterDialogAction
= new ShowFilterDialogAction(fTimeGraphViewer
) {
773 protected void addFilter(ViewerFilter filter
) {
774 /* add filter to the combo instead of the viewer */
775 TimeGraphCombo
.this.addFilter(filter
);
779 protected void removeFilter(ViewerFilter filter
) {
780 /* remove filter from the combo instead of the viewer */
781 TimeGraphCombo
.this.removeFilter(filter
);
785 protected void refresh() {
786 /* refresh the combo instead of the viewer */
787 TimeGraphCombo
.this.refresh();
791 return fShowFilterDialogAction
;
794 // ------------------------------------------------------------------------
796 // ------------------------------------------------------------------------
799 public void redraw() {
800 fTimeGraphViewer
.getControl().redraw();
805 public void update() {
806 fTimeGraphViewer
.getControl().update();
810 // ------------------------------------------------------------------------
812 // ------------------------------------------------------------------------
815 * Sets the tree content provider used by this time graph combo.
817 * @param contentProvider the tree content provider
819 public void setTreeContentProvider(ITreeContentProvider contentProvider
) {
820 fTreeViewer
.setContentProvider(new TreeContentProviderWrapper(contentProvider
));
824 * Sets the tree label provider used by this time graph combo.
826 * @param labelProvider the tree label provider
828 public void setTreeLabelProvider(ITableLabelProvider labelProvider
) {
829 fTreeViewer
.setLabelProvider(new TreeLabelProviderWrapper(labelProvider
));
833 * Sets the tree content provider used by the filter dialog
835 * @param contentProvider the tree content provider
837 public void setFilterContentProvider(ITreeContentProvider contentProvider
) {
838 getShowFilterDialogAction().getFilterDialog().setContentProvider(contentProvider
);
842 * Sets the tree label provider used by the filter dialog
844 * @param labelProvider the tree label provider
846 public void setFilterLabelProvider(ITableLabelProvider labelProvider
) {
847 getShowFilterDialogAction().getFilterDialog().setLabelProvider(labelProvider
);
851 * Adds a "check active" button used by the filter dialog
853 * @param activeProvider
854 * Additional button info specific to a certain view.
857 public void addTimeGraphFilterCheckActiveButton(ITimeGraphEntryActiveProvider activeProvider
) {
858 getShowFilterDialogAction().getFilterDialog().addTimeGraphFilterCheckActiveButton(activeProvider
);
862 * Adds an "uncheck inactive" button used by the filter dialog
864 * @param inactiveProvider
865 * Additional button info specific to a certain view.
868 public void addTimeGraphFilterUncheckInactiveButton(ITimeGraphEntryActiveProvider inactiveProvider
) {
869 getShowFilterDialogAction().getFilterDialog().addTimeGraphFilterUncheckInactiveButton(inactiveProvider
);
873 * Sets the tree columns for this time graph combo.
875 * @param columnNames the tree column names
877 public void setTreeColumns(String
[] columnNames
) {
878 final Tree tree
= fTreeViewer
.getTree();
879 for (String columnName
: columnNames
) {
880 TreeColumn column
= new TreeColumn(tree
, SWT
.LEFT
);
881 column
.setMoveable(true);
882 column
.setText(columnName
);
888 * Sets the tree columns for this time graph combo's filter dialog.
890 * @param columnNames the tree column names
892 public void setFilterColumns(String
[] columnNames
) {
893 getShowFilterDialogAction().getFilterDialog().setColumnNames(columnNames
);
897 * Sets the time graph content provider used by this time graph combo.
899 * @param timeGraphContentProvider
900 * the time graph content provider
902 public void setTimeGraphContentProvider(ITimeGraphContentProvider timeGraphContentProvider
) {
903 fTimeGraphViewer
.setTimeGraphContentProvider(timeGraphContentProvider
);
907 * Sets the time graph presentation provider used by this time graph combo.
909 * @param timeGraphProvider the time graph provider
911 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
912 fTimeGraphViewer
.setTimeGraphProvider(timeGraphProvider
);
916 * Sets or clears the input for this time graph combo.
918 * @param input the input of this time graph combo, or <code>null</code> if none
920 public void setInput(Object input
) {
921 fInhibitTreeSelection
= true;
922 fTreeViewer
.setInput(input
);
923 for (SelectionListenerWrapper listenerWrapper
: fSelectionListenerMap
.values()) {
924 listenerWrapper
.selection
= null;
926 fInhibitTreeSelection
= false;
927 if (fScrollBarsInTreeWorkaround
) {
928 fTreeViewer
.getTree().getVerticalBar().setEnabled(false);
929 fTreeViewer
.getTree().getVerticalBar().setVisible(false);
931 fTimeGraphViewer
.setInput(input
);
932 fTimeGraphViewer
.setItemHeight(getItemHeight(fTreeViewer
.getTree(), false));
933 // queue the alignment update because in Linux the item bounds are not
934 // set properly until the tree has been painted at least once
935 fVisibleExpandedItems
= null; // invalidate the cache
936 getDisplay().asyncExec(new Runnable() {
939 alignTreeItems(true);
944 * Gets the input for this time graph combo.
946 * @return The input of this time graph combo, or <code>null</code> if none
948 public Object
getInput() {
949 return fTreeViewer
.getInput();
953 * Sets or clears the list of links to display on this combo
955 * @param links the links to display in this time graph combo
957 public void setLinks(List
<ILinkEvent
> links
) {
958 fTimeGraphViewer
.setLinks(links
);
962 * @param filter The filter object to be attached to the view
964 public void addFilter(@NonNull ViewerFilter filter
) {
965 fInhibitTreeSelection
= true;
966 ViewerFilter wrapper
= new ViewerFilterWrapper(filter
);
967 fTreeViewer
.addFilter(wrapper
);
968 fTimeGraphViewer
.addFilter(filter
);
969 fViewerFilterMap
.put(filter
, wrapper
);
970 alignTreeItems(true);
971 fInhibitTreeSelection
= false;
975 * @param filter The filter object to be removed from the view
977 public void removeFilter(@NonNull ViewerFilter filter
) {
978 fInhibitTreeSelection
= true;
979 ViewerFilter wrapper
= fViewerFilterMap
.get(filter
);
980 fTreeViewer
.removeFilter(wrapper
);
981 fTimeGraphViewer
.removeFilter(filter
);
982 fViewerFilterMap
.remove(filter
);
983 alignTreeItems(true);
984 fInhibitTreeSelection
= false;
988 * Returns this viewer's filters.
990 * @return an array of viewer filters
993 public @NonNull ViewerFilter
[] getFilters() {
994 return fTimeGraphViewer
.getFilters();
998 * Sets the filters, replacing any previous filters, and triggers
999 * refiltering of the elements.
1002 * an array of viewer filters, or null
1005 public void setFilters(@NonNull ViewerFilter
[] filters
) {
1006 fInhibitTreeSelection
= true;
1007 fViewerFilterMap
.clear();
1008 if (filters
== null) {
1009 fTreeViewer
.resetFilters();
1011 for (ViewerFilter filter
: filters
) {
1012 ViewerFilter wrapper
= new ViewerFilterWrapper(filter
);
1013 fViewerFilterMap
.put(filter
, wrapper
);
1015 ViewerFilter
[] wrappers
= Iterables
.toArray(fViewerFilterMap
.values(), ViewerFilter
.class);
1016 fTreeViewer
.setFilters(wrappers
);
1018 fTimeGraphViewer
.setFilters(filters
);
1019 alignTreeItems(true);
1020 fInhibitTreeSelection
= false;
1024 * Refreshes this time graph completely with information freshly obtained from its model.
1026 public void refresh() {
1027 fInhibitTreeSelection
= true;
1028 Tree tree
= fTreeViewer
.getTree();
1030 tree
.setRedraw(false);
1031 fTreeViewer
.refresh();
1033 tree
.setRedraw(true);
1035 fTimeGraphViewer
.refresh();
1036 alignTreeItems(true);
1037 fInhibitTreeSelection
= false;
1041 * Adds a listener for selection changes in this time graph combo.
1043 * @param listener a selection listener
1045 public void addSelectionListener(ITimeGraphSelectionListener listener
) {
1046 SelectionListenerWrapper listenerWrapper
= new SelectionListenerWrapper(listener
);
1047 fTreeViewer
.addSelectionChangedListener(listenerWrapper
);
1048 fSelectionListenerMap
.put(listener
, listenerWrapper
);
1049 fTimeGraphViewer
.addSelectionListener(listenerWrapper
);
1053 * Removes the given selection listener from this time graph combo.
1055 * @param listener a selection changed listener
1057 public void removeSelectionListener(ITimeGraphSelectionListener listener
) {
1058 SelectionListenerWrapper listenerWrapper
= fSelectionListenerMap
.remove(listener
);
1059 fTreeViewer
.removeSelectionChangedListener(listenerWrapper
);
1060 fTimeGraphViewer
.removeSelectionListener(listenerWrapper
);
1064 * Sets the current selection for this time graph combo.
1066 * @param selection the new selection
1068 public void setSelection(ITimeGraphEntry selection
) {
1069 fTimeGraphViewer
.setSelection(selection
);
1070 fInhibitTreeSelection
= true; // block the tree selection changed listener
1071 if (selection
!= null) {
1072 StructuredSelection structuredSelection
= new StructuredSelection(selection
);
1073 fTreeViewer
.setSelection(structuredSelection
);
1075 fTreeViewer
.setSelection(new StructuredSelection());
1077 fInhibitTreeSelection
= false;
1078 alignTreeItems(false);
1082 * Sets the auto-expand level to be used for new entries discovered when
1083 * calling {@link #setInput(Object)} or {@link #refresh()}. The value 0
1084 * means that there is no auto-expand; 1 means that top-level entries are
1085 * expanded, but not their children; 2 means that top-level entries are
1086 * expanded, and their children, but not grand-children; and so on.
1088 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
1092 * non-negative level, or <code>ALL_LEVELS</code> to expand all
1093 * levels of the tree
1095 public void setAutoExpandLevel(int level
) {
1096 fTimeGraphViewer
.setAutoExpandLevel(level
);
1098 fTreeViewer
.setAutoExpandLevel(level
);
1100 fTreeViewer
.setAutoExpandLevel(level
+ 1);
1105 * Returns the auto-expand level.
1107 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
1108 * the tree are expanded automatically
1109 * @see #setAutoExpandLevel
1111 public int getAutoExpandLevel() {
1112 return fTimeGraphViewer
.getAutoExpandLevel();
1116 * Set the expanded state of an entry
1119 * The entry to expand/collapse
1121 * True for expanded, false for collapsed
1123 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
1124 fTimeGraphViewer
.setExpandedState(entry
, expanded
);
1125 fTreeViewer
.setExpandedState(entry
, expanded
);
1126 alignTreeItems(true);
1130 * Collapses all nodes of the viewer's tree, starting with the root.
1132 public void collapseAll() {
1133 fTimeGraphViewer
.collapseAll();
1134 fTreeViewer
.collapseAll();
1135 alignTreeItems(true);
1139 * Expands all nodes of the viewer's tree, starting with the root.
1141 public void expandAll() {
1142 fTimeGraphViewer
.expandAll();
1143 fTreeViewer
.expandAll();
1144 alignTreeItems(true);
1147 // ------------------------------------------------------------------------
1149 // ------------------------------------------------------------------------
1151 private List
<TreeItem
> getVisibleExpandedItems(Tree tree
, boolean refresh
) {
1152 if (fVisibleExpandedItems
== null || refresh
) {
1153 List
<TreeItem
> visibleExpandedItems
= new ArrayList
<>();
1154 addVisibleExpandedItems(visibleExpandedItems
, tree
.getItems());
1155 fVisibleExpandedItems
= visibleExpandedItems
;
1157 return fVisibleExpandedItems
;
1160 private void addVisibleExpandedItems(List
<TreeItem
> visibleExpandedItems
, TreeItem
[] items
) {
1161 for (TreeItem item
: items
) {
1162 Object data
= item
.getData();
1163 if (data
== FILLER
) {
1166 visibleExpandedItems
.add(item
);
1167 boolean expandedState
= fTimeGraphViewer
.getExpandedState((ITimeGraphEntry
) data
);
1168 if (item
.getExpanded() != expandedState
) {
1169 /* synchronize the expanded state of both viewers */
1170 fTreeViewer
.setExpandedState(data
, expandedState
);
1172 if (expandedState
) {
1173 addVisibleExpandedItems(visibleExpandedItems
, item
.getItems());
1178 private int getItemHeight(final Tree tree
, boolean force
) {
1180 * Bug in Linux. The method getItemHeight doesn't always return the correct value.
1182 if (fLinuxItemHeight
>= 0 && System
.getProperty("os.name").contains("Linux")) { //$NON-NLS-1$ //$NON-NLS-2$
1183 if (fLinuxItemHeight
!= 0 && !force
) {
1184 return fLinuxItemHeight
;
1187 if (getVisibleExpandedItems(tree
, true).size() > 1) {
1188 PaintListener paintListener
= new PaintListener() {
1190 public void paintControl(PaintEvent e
) {
1191 // get the treeItems here to have all items
1192 List
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
, true);
1193 if (treeItems
.size() < 2) {
1196 final TreeItem treeItem0
= treeItems
.get(0);
1197 final TreeItem treeItem1
= treeItems
.get(1);
1198 tree
.removePaintListener(this);
1199 int y0
= treeItem0
.getBounds().y
;
1200 int y1
= treeItem1
.getBounds().y
;
1201 int itemHeight
= y1
- y0
;
1202 if (itemHeight
> 0) {
1203 fLinuxItemHeight
= itemHeight
;
1204 fTimeGraphViewer
.setItemHeight(itemHeight
);
1208 tree
.addPaintListener(paintListener
);
1211 fLinuxItemHeight
= -1; // Not Linux, don't perform os.name check anymore
1213 return tree
.getItemHeight();
1216 private void alignTreeItems(boolean refreshExpandedItems
) {
1217 // align the tree top item with the time graph top item
1218 Tree tree
= fTreeViewer
.getTree();
1219 List
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
, refreshExpandedItems
);
1220 int topIndex
= fTimeGraphViewer
.getTopIndex();
1221 if (topIndex
>= treeItems
.size()) {
1224 TreeItem item
= treeItems
.get(topIndex
);
1225 tree
.setTopItem(item
);
1228 * In GTK3, the bounds of the tree items are only sure to be correct
1229 * after the tree has been painted.
1231 tree
.addPaintListener(new PaintListener() {
1233 public void paintControl(PaintEvent e
) {
1234 tree
.removePaintListener(this);
1238 * Bug in GTK. Calling setTopItem() can scroll to the wrong item
1239 * when the 'tree view' is dirty. Set it again once it is clean.
1241 if (SWT
.getPlatform().equals("gtk")) { //$NON-NLS-1$
1242 TreeItem topItem
= tree
.getTopItem();
1243 tree
.getDisplay().asyncExec(() -> {
1244 if (!tree
.isDisposed() && !topItem
.isDisposed()) {
1245 tree
.setTopItem(topItem
);
1251 /* Make sure the paint event is triggered. */
1255 private void doAlignTreeItems() {
1256 Tree tree
= fTreeViewer
.getTree();
1257 List
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
, false);
1258 int topIndex
= fTimeGraphViewer
.getTopIndex();
1259 if (topIndex
>= treeItems
.size()) {
1262 TreeItem item
= treeItems
.get(topIndex
);
1264 // get the first filler item so we can calculate the last item's height
1265 TreeItem fillerItem
= null;
1266 for (TreeItem treeItem
: fTreeViewer
.getTree().getItems()) {
1267 if (treeItem
.getData() == FILLER
) {
1268 fillerItem
= treeItem
;
1273 // ensure the time graph item heights are equal to the tree item heights
1274 int treeHeight
= fTreeViewer
.getTree().getBounds().height
;
1275 int index
= topIndex
;
1276 Rectangle bounds
= item
.getBounds();
1277 while (index
< treeItems
.size()) {
1278 if (bounds
.y
> treeHeight
) {
1281 TreeItem nextItem
= (index
+ 1 == treeItems
.size()) ? fillerItem
: treeItems
.get(index
+ 1);
1282 Rectangle nextBounds
= alignTreeItem(item
, bounds
, nextItem
);
1285 bounds
= nextBounds
;
1289 * When an item's height in the time graph changes, it is possible that
1290 * the time graph readjusts its top index to fill empty space at the
1291 * bottom of the viewer. Calling method setTopIndex() triggers this
1292 * adjustment, if needed. In that case, we need to make sure that the
1293 * newly visible items at the top of the viewer are also aligned.
1295 fTimeGraphViewer
.setTopIndex(topIndex
);
1296 if (fTimeGraphViewer
.getTopIndex() != topIndex
) {
1297 alignTreeItems(false);
1301 private Rectangle
alignTreeItem(TreeItem item
, Rectangle bounds
, TreeItem nextItem
) {
1303 * Bug in Linux. The method getBounds doesn't always return the correct height.
1304 * Use the difference of y position between items to calculate the height.
1306 Rectangle nextBounds
= nextItem
.getBounds();
1307 Integer itemHeight
= nextBounds
.y
- bounds
.y
;
1308 if (itemHeight
> 0) {
1309 ITimeGraphEntry entry
= (ITimeGraphEntry
) item
.getData();
1310 fTimeGraphViewer
.getTimeGraphControl().setItemHeight(entry
, itemHeight
);
1316 * Return the time alignment information
1318 * @return the time alignment information
1320 * @see ITmfTimeAligned
1324 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
1325 Point location
= fSashForm
.toDisplay(0, 0);
1326 int timeAxisOffset
= fTreeViewer
.getControl().getSize().x
+ fSashForm
.getSashWidth();
1327 return new TmfTimeViewAlignmentInfo(fSashForm
.getShell(), location
, timeAxisOffset
);
1331 * Return the available width for the time-axis.
1333 * @see ITmfTimeAligned
1335 * @param requestedOffset
1336 * the requested offset
1337 * @return the available width for the time-axis
1341 public int getAvailableWidth(int requestedOffset
) {
1342 int vBarWidth
= ((fTimeGraphViewer
.getVerticalBar() != null) && (fTimeGraphViewer
.getVerticalBar().isVisible())) ? fTimeGraphViewer
.getVerticalBar().getSize().x
: 0;
1343 int totalWidth
= fSashForm
.getBounds().width
;
1344 return Math
.min(totalWidth
, Math
.max(0, totalWidth
- requestedOffset
- vBarWidth
));
1348 * Perform the alignment operation.
1351 * the alignment offset
1353 * the alignment width
1355 * @see ITmfTimeAligned
1359 public void performAlign(int offset
, int width
) {
1360 int total
= fSashForm
.getBounds().width
;
1361 int timeAxisOffset
= Math
.min(offset
, total
);
1362 int width1
= Math
.max(0, timeAxisOffset
- fSashForm
.getSashWidth());
1363 int width2
= total
- timeAxisOffset
;
1364 if (width1
>= 0 && width2
> 0 || width1
> 0 && width2
>= 0) {
1365 fSashForm
.setWeights(new int[] { width1
, width2
});
1369 Composite composite
= fTimeGraphViewer
.getTimeAlignedComposite();
1370 GridLayout layout
= (GridLayout
) composite
.getLayout();
1371 int timeBasedControlsWidth
= composite
.getSize().x
;
1372 int marginSize
= timeBasedControlsWidth
- width
;
1373 layout
.marginRight
= Math
.max(0, marginSize
);