1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
21 import org
.eclipse
.jface
.action
.Action
;
22 import org
.eclipse
.jface
.viewers
.ILabelProviderListener
;
23 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
24 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
25 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
26 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
27 import org
.eclipse
.jface
.viewers
.ITreeViewerListener
;
28 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
29 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.TreeExpansionEvent
;
31 import org
.eclipse
.jface
.viewers
.TreeViewer
;
32 import org
.eclipse
.jface
.viewers
.Viewer
;
33 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.ITmfImageConstants
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.dialogs
.TimeGraphFilterDialog
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.custom
.SashForm
;
41 import org
.eclipse
.swt
.events
.ControlAdapter
;
42 import org
.eclipse
.swt
.events
.ControlEvent
;
43 import org
.eclipse
.swt
.events
.MouseEvent
;
44 import org
.eclipse
.swt
.events
.MouseTrackAdapter
;
45 import org
.eclipse
.swt
.events
.MouseWheelListener
;
46 import org
.eclipse
.swt
.events
.PaintEvent
;
47 import org
.eclipse
.swt
.events
.PaintListener
;
48 import org
.eclipse
.swt
.events
.SelectionAdapter
;
49 import org
.eclipse
.swt
.events
.SelectionEvent
;
50 import org
.eclipse
.swt
.graphics
.Image
;
51 import org
.eclipse
.swt
.graphics
.Point
;
52 import org
.eclipse
.swt
.layout
.FillLayout
;
53 import org
.eclipse
.swt
.widgets
.Composite
;
54 import org
.eclipse
.swt
.widgets
.Display
;
55 import org
.eclipse
.swt
.widgets
.Event
;
56 import org
.eclipse
.swt
.widgets
.Listener
;
57 import org
.eclipse
.swt
.widgets
.Slider
;
58 import org
.eclipse
.swt
.widgets
.Tree
;
59 import org
.eclipse
.swt
.widgets
.TreeColumn
;
60 import org
.eclipse
.swt
.widgets
.TreeItem
;
63 * Time graph "combo" view (with the list/tree on the left and the gantt chart
67 * @author Patrick Tasse
69 public class TimeGraphCombo
extends Composite
{
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 private static final Object FILLER
= new Object();
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
82 private TreeViewer fTreeViewer
;
85 private TimeGraphViewer fTimeGraphViewer
;
87 // The top-level input (children excluded)
88 private List
<?
extends ITimeGraphEntry
> fTopInput
;
90 // All the inputs (children included)
91 private List
<?
extends ITimeGraphEntry
> fAllInput
;
93 // The selection listener map
94 private final HashMap
<ITimeGraphSelectionListener
, SelectionListenerWrapper
> fSelectionListenerMap
= new HashMap
<ITimeGraphSelectionListener
, SelectionListenerWrapper
>();
96 // The map of viewer filters
97 private final Map
<ViewerFilter
, ViewerFilter
> fViewerFilterMap
= new HashMap
<ViewerFilter
, ViewerFilter
>();
99 // Flag to block the tree selection changed listener when triggered by the time graph combo
100 private boolean fInhibitTreeSelection
= false;
102 // Number of filler rows used by the tree content provider
103 private int fNumFillerRows
;
105 // Calculated item height for Linux workaround
106 private int fLinuxItemHeight
= 0;
108 // The button that opens the filter dialog
109 private Action showFilterAction
;
112 private TimeGraphFilterDialog fFilterDialog
;
114 // The filter generated from the filter dialog
115 private RawViewerFilter fFilter
;
117 // ------------------------------------------------------------------------
119 // ------------------------------------------------------------------------
122 * The TreeContentProviderWrapper is used to insert filler items after
123 * the elements of the tree's real content provider.
125 private class TreeContentProviderWrapper
implements ITreeContentProvider
{
126 private final ITreeContentProvider contentProvider
;
128 public TreeContentProviderWrapper(ITreeContentProvider contentProvider
) {
129 this.contentProvider
= contentProvider
;
133 public void dispose() {
134 contentProvider
.dispose();
138 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
139 contentProvider
.inputChanged(viewer
, oldInput
, newInput
);
143 public Object
[] getElements(Object inputElement
) {
144 Object
[] elements
= contentProvider
.getElements(inputElement
);
145 // add filler elements to ensure alignment with time analysis viewer
146 Object
[] oElements
= Arrays
.copyOf(elements
, elements
.length
+ fNumFillerRows
, new Object
[0].getClass());
147 for (int i
= 0; i
< fNumFillerRows
; i
++) {
148 oElements
[elements
.length
+ i
] = FILLER
;
154 public Object
[] getChildren(Object parentElement
) {
155 if (parentElement
instanceof ITimeGraphEntry
) {
156 return contentProvider
.getChildren(parentElement
);
158 return new Object
[0];
162 public Object
getParent(Object element
) {
163 if (element
instanceof ITimeGraphEntry
) {
164 return contentProvider
.getParent(element
);
170 public boolean hasChildren(Object element
) {
171 if (element
instanceof ITimeGraphEntry
) {
172 return contentProvider
.hasChildren(element
);
179 * The TreeLabelProviderWrapper is used to intercept the filler items
180 * from the calls to the tree's real label provider.
182 private class TreeLabelProviderWrapper
implements ITableLabelProvider
{
183 private final ITableLabelProvider labelProvider
;
185 public TreeLabelProviderWrapper(ITableLabelProvider labelProvider
) {
186 this.labelProvider
= labelProvider
;
190 public void addListener(ILabelProviderListener listener
) {
191 labelProvider
.addListener(listener
);
195 public void dispose() {
196 labelProvider
.dispose();
200 public boolean isLabelProperty(Object element
, String property
) {
201 if (element
instanceof ITimeGraphEntry
) {
202 return labelProvider
.isLabelProperty(element
, property
);
208 public void removeListener(ILabelProviderListener listener
) {
209 labelProvider
.removeListener(listener
);
213 public Image
getColumnImage(Object element
, int columnIndex
) {
214 if (element
instanceof ITimeGraphEntry
) {
215 return labelProvider
.getColumnImage(element
, columnIndex
);
221 public String
getColumnText(Object element
, int columnIndex
) {
222 if (element
instanceof ITimeGraphEntry
) {
223 return labelProvider
.getColumnText(element
, columnIndex
);
231 * The SelectionListenerWrapper is used to intercept the filler items from
232 * the time graph combo's real selection listener, and to prevent double
233 * notifications from being sent when selection changes in both tree and
234 * time graph at the same time.
236 private class SelectionListenerWrapper
implements ISelectionChangedListener
, ITimeGraphSelectionListener
{
237 private final ITimeGraphSelectionListener listener
;
238 private ITimeGraphEntry selection
= null;
240 public SelectionListenerWrapper(ITimeGraphSelectionListener listener
) {
241 this.listener
= listener
;
245 public void selectionChanged(SelectionChangedEvent event
) {
246 if (fInhibitTreeSelection
) {
249 Object element
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
250 if (element
instanceof ITimeGraphEntry
) {
251 ITimeGraphEntry entry
= (ITimeGraphEntry
) element
;
252 if (entry
!= selection
) {
254 listener
.selectionChanged(new TimeGraphSelectionEvent(event
.getSource(), selection
));
260 public void selectionChanged(TimeGraphSelectionEvent event
) {
261 ITimeGraphEntry entry
= event
.getSelection();
262 if (entry
!= selection
) {
264 listener
.selectionChanged(new TimeGraphSelectionEvent(event
.getSource(), selection
));
270 * The ViewerFilterWrapper is used to intercept the filler items from
271 * the time graph combo's real ViewerFilters. These filler items should
274 private class ViewerFilterWrapper
extends ViewerFilter
{
276 ViewerFilter fWrappedFilter
;
278 ViewerFilterWrapper(ViewerFilter filter
) {
280 this.fWrappedFilter
= filter
;
284 public boolean select(Viewer viewer
, Object parentElement
, Object element
) {
285 if (element
instanceof ITimeGraphEntry
) {
286 return fWrappedFilter
.select(viewer
, parentElement
, element
);
294 * This filter simply keeps a list of elements that should be shown
295 * All the other elements will be filtered
297 private class RawViewerFilter
extends ViewerFilter
{
299 private List
<Object
> fNonFiltered
= new ArrayList
<Object
>();
301 public void setNonFiltered(List
<Object
> objects
) {
302 fNonFiltered
= objects
;
305 public List
<Object
> getNonFiltered() {
310 public boolean select(Viewer viewer
, Object parentElement
, Object element
) {
311 return fNonFiltered
.contains(element
);
315 // ------------------------------------------------------------------------
317 // ------------------------------------------------------------------------
320 * Constructs a new instance of this class given its parent
321 * and a style value describing its behavior and appearance.
323 * @param parent a widget which will be the parent of the new instance (cannot be null)
324 * @param style the style of widget to construct
326 public TimeGraphCombo(Composite parent
, int style
) {
327 super(parent
, style
);
328 setLayout(new FillLayout());
330 final SashForm sash
= new SashForm(this, SWT
.NONE
);
332 fTreeViewer
= new TreeViewer(sash
, SWT
.FULL_SELECTION
| SWT
.H_SCROLL
);
333 final Tree tree
= fTreeViewer
.getTree();
334 tree
.setHeaderVisible(true);
335 tree
.setLinesVisible(true);
337 fTimeGraphViewer
= new TimeGraphViewer(sash
, SWT
.NONE
);
338 fTimeGraphViewer
.setItemHeight(getItemHeight(tree
));
339 fTimeGraphViewer
.setHeaderHeight(tree
.getHeaderHeight());
340 fTimeGraphViewer
.setBorderWidth(tree
.getBorderWidth());
341 fTimeGraphViewer
.setNameWidthPref(0);
343 fFilter
= new RawViewerFilter();
346 fFilterDialog
= new TimeGraphFilterDialog(getShell());
348 // Feature in Windows. The tree vertical bar reappears when
349 // the control is resized so we need to hide it again.
350 // Bug in Linux. The tree header height is 0 in constructor,
351 // so we need to reset it later when the control is resized.
352 tree
.addControlListener(new ControlAdapter() {
355 public void controlResized(ControlEvent e
) {
358 tree
.getVerticalBar().setEnabled(false);
359 // this can trigger controlResized recursively
360 tree
.getVerticalBar().setVisible(false);
363 fTimeGraphViewer
.setHeaderHeight(tree
.getHeaderHeight());
367 // ensure synchronization of expanded items between tree and time graph
368 fTreeViewer
.addTreeListener(new ITreeViewerListener() {
370 public void treeCollapsed(TreeExpansionEvent event
) {
371 fTimeGraphViewer
.setExpandedState((ITimeGraphEntry
) event
.getElement(), false);
372 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
373 if (treeItems
.size() == 0) {
376 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
377 tree
.setTopItem(treeItem
);
381 public void treeExpanded(TreeExpansionEvent event
) {
382 fTimeGraphViewer
.setExpandedState((ITimeGraphEntry
) event
.getElement(), true);
383 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
384 if (treeItems
.size() == 0) {
387 final TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
388 // queue the top item update because the tree can change its top item
389 // autonomously immediately after the listeners have been notified
390 getDisplay().asyncExec(new Runnable() {
393 tree
.setTopItem(treeItem
);
398 // ensure synchronization of expanded items between tree and time graph
399 fTimeGraphViewer
.addTreeListener(new ITimeGraphTreeListener() {
401 public void treeCollapsed(TimeGraphTreeExpansionEvent event
) {
402 fTreeViewer
.setExpandedState(event
.getEntry(), false);
406 public void treeExpanded(TimeGraphTreeExpansionEvent event
) {
407 fTreeViewer
.setExpandedState(event
.getEntry(), true);
411 // prevent mouse button from selecting a filler tree item
412 tree
.addListener(SWT
.MouseDown
, new Listener() {
414 public void handleEvent(Event event
) {
415 TreeItem treeItem
= tree
.getItem(new Point(event
.x
, event
.y
));
416 if (treeItem
== null || treeItem
.getData() == FILLER
) {
418 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
419 if (treeItems
.size() == 0) {
420 fTreeViewer
.setSelection(new StructuredSelection());
421 fTimeGraphViewer
.setSelection(null);
424 // this prevents from scrolling up when selecting
425 // the partially visible tree item at the bottom
426 tree
.select(treeItems
.get(treeItems
.size() - 1));
427 fTreeViewer
.setSelection(new StructuredSelection());
428 fTimeGraphViewer
.setSelection(null);
433 // prevent mouse wheel from scrolling down into filler tree items
434 tree
.addListener(SWT
.MouseWheel
, new Listener() {
436 public void handleEvent(Event event
) {
438 Slider scrollBar
= fTimeGraphViewer
.getVerticalBar();
439 fTimeGraphViewer
.setTopIndex(scrollBar
.getSelection() - event
.count
);
440 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
441 if (treeItems
.size() == 0) {
444 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
445 tree
.setTopItem(treeItem
);
449 // prevent key stroke from selecting a filler tree item
450 tree
.addListener(SWT
.KeyDown
, new Listener() {
452 public void handleEvent(Event event
) {
453 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
454 if (treeItems
.size() == 0) {
455 fTreeViewer
.setSelection(new StructuredSelection());
459 if (event
.keyCode
== SWT
.ARROW_DOWN
) {
460 int index
= Math
.min(fTimeGraphViewer
.getSelectionIndex() + 1, treeItems
.size() - 1);
461 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) treeItems
.get(index
).getData());
463 } else if (event
.keyCode
== SWT
.PAGE_DOWN
) {
464 int height
= tree
.getSize().y
- tree
.getHeaderHeight() - tree
.getHorizontalBar().getSize().y
;
465 int countPerPage
= height
/ getItemHeight(tree
);
466 int index
= Math
.min(fTimeGraphViewer
.getSelectionIndex() + countPerPage
- 1, treeItems
.size() - 1);
467 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) treeItems
.get(index
).getData());
469 } else if (event
.keyCode
== SWT
.END
) {
470 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) treeItems
.get(treeItems
.size() - 1).getData());
473 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
474 tree
.setTopItem(treeItem
);
475 if (fTimeGraphViewer
.getSelectionIndex() >= 0) {
476 fTreeViewer
.setSelection(new StructuredSelection(fTimeGraphViewer
.getSelection()));
478 fTreeViewer
.setSelection(new StructuredSelection());
483 // ensure alignment of top item between tree and time graph
484 fTimeGraphViewer
.getTimeGraphControl().addControlListener(new ControlAdapter() {
486 public void controlResized(ControlEvent e
) {
487 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
488 if (treeItems
.size() == 0) {
491 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
492 tree
.setTopItem(treeItem
);
496 // ensure synchronization of selected item between tree and time graph
497 fTreeViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
499 public void selectionChanged(SelectionChangedEvent event
) {
500 if (fInhibitTreeSelection
) {
503 if (event
.getSelection() instanceof IStructuredSelection
) {
504 Object selection
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
505 if (selection
instanceof ITimeGraphEntry
) {
506 fTimeGraphViewer
.setSelection((ITimeGraphEntry
) selection
);
508 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
509 if (treeItems
.size() == 0) {
512 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
513 tree
.setTopItem(treeItem
);
518 // ensure synchronization of selected item between tree and time graph
519 fTimeGraphViewer
.addSelectionListener(new ITimeGraphSelectionListener() {
521 public void selectionChanged(TimeGraphSelectionEvent event
) {
522 ITimeGraphEntry entry
= fTimeGraphViewer
.getSelection();
523 fInhibitTreeSelection
= true; // block the tree selection changed listener
525 StructuredSelection selection
= new StructuredSelection(entry
);
526 fTreeViewer
.setSelection(selection
);
528 fTreeViewer
.setSelection(new StructuredSelection());
530 fInhibitTreeSelection
= false;
531 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
532 if (treeItems
.size() == 0) {
535 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
536 tree
.setTopItem(treeItem
);
540 // ensure alignment of top item between tree and time graph
541 fTimeGraphViewer
.getVerticalBar().addSelectionListener(new SelectionAdapter() {
543 public void widgetSelected(SelectionEvent e
) {
544 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
545 if (treeItems
.size() == 0) {
548 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
549 tree
.setTopItem(treeItem
);
553 // ensure alignment of top item between tree and time graph
554 fTimeGraphViewer
.getTimeGraphControl().addMouseWheelListener(new MouseWheelListener() {
556 public void mouseScrolled(MouseEvent e
) {
557 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
558 if (treeItems
.size() == 0) {
561 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
562 tree
.setTopItem(treeItem
);
566 // ensure the tree has focus control when mouse is over it if the time graph had control
567 fTreeViewer
.getControl().addMouseTrackListener(new MouseTrackAdapter() {
569 public void mouseEnter(MouseEvent e
) {
570 if (fTimeGraphViewer
.getTimeGraphControl().isFocusControl()) {
571 fTreeViewer
.getControl().setFocus();
576 // ensure the time graph has focus control when mouse is over it if the tree had control
577 fTimeGraphViewer
.getTimeGraphControl().addMouseTrackListener(new MouseTrackAdapter() {
579 public void mouseEnter(MouseEvent e
) {
580 if (fTreeViewer
.getControl().isFocusControl()) {
581 fTimeGraphViewer
.getTimeGraphControl().setFocus();
585 fTimeGraphViewer
.getTimeGraphScale().addMouseTrackListener(new MouseTrackAdapter() {
587 public void mouseEnter(MouseEvent e
) {
588 if (fTreeViewer
.getControl().isFocusControl()) {
589 fTimeGraphViewer
.getTimeGraphControl().setFocus();
594 // The filler rows are required to ensure alignment when the tree does not have a
595 // visible horizontal scroll bar. The tree does not allow its top item to be set
596 // to a value that would cause blank space to be drawn at the bottom of the tree.
597 fNumFillerRows
= Display
.getDefault().getBounds().height
/ getItemHeight(tree
);
599 sash
.setWeights(new int[] { 1, 1 });
602 // ------------------------------------------------------------------------
604 // ------------------------------------------------------------------------
607 * Returns this time graph combo's tree viewer.
609 * @return the tree viewer
611 public TreeViewer
getTreeViewer() {
616 * Returns this time graph combo's time graph viewer.
618 * @return the time graph viewer
620 public TimeGraphViewer
getTimeGraphViewer() {
621 return fTimeGraphViewer
;
625 * Callback for the show filter action
629 public void showFilterDialog() {
630 if(fTopInput
!= null) {
631 fFilterDialog
.setInput(fTopInput
.toArray(new ITimeGraphEntry
[0]));
632 fFilterDialog
.setTitle(Messages
.TmfTimeFilterDialog_WINDOW_TITLE
);
633 fFilterDialog
.setMessage(Messages
.TmfTimeFilterDialog_MESSAGE
);
634 fFilterDialog
.setInitialElementSelections(fFilter
.getNonFiltered());
635 fFilterDialog
.setExpandedElements(fAllInput
.toArray());
636 fFilterDialog
.create();
637 fFilterDialog
.open();
638 // Process selected elements
639 if (fFilterDialog
.getResult() != null) {
640 fInhibitTreeSelection
= true;
641 fFilter
.setNonFiltered(new ArrayList
<Object
>(Arrays
.asList(fFilterDialog
.getResult())));
642 fTreeViewer
.refresh();
643 fTreeViewer
.expandAll();
644 fTimeGraphViewer
.refresh();
645 fInhibitTreeSelection
= false;
646 // Reset selection to first entry
647 if (fFilterDialog
.getResult().length
> 0) {
648 setSelection((ITimeGraphEntry
) fFilterDialog
.getResult()[0]);
655 * Get the show filter action.
657 * @return The Action object
660 public Action
getShowFilterAction() {
661 if (showFilterAction
== null) {
663 showFilterAction
= new Action() {
669 showFilterAction
.setText(Messages
.TmfTimeGraphCombo_FilterActionNameText
);
670 showFilterAction
.setToolTipText(Messages
.TmfTimeGraphCombo_FilterActionToolTipText
);
671 // TODO find a nice, distinctive icon
672 showFilterAction
.setImageDescriptor(Activator
.getDefault().getImageDescripterFromPath(ITmfImageConstants
.IMG_UI_FILTERS
));
675 return showFilterAction
;
678 // ------------------------------------------------------------------------
680 // ------------------------------------------------------------------------
683 * @see org.eclipse.swt.widgets.Control#redraw()
686 public void redraw() {
687 fTimeGraphViewer
.getControl().redraw();
691 // ------------------------------------------------------------------------
693 // ------------------------------------------------------------------------
696 * Sets the tree content provider used by this time graph combo.
698 * @param contentProvider the tree content provider
700 public void setTreeContentProvider(ITreeContentProvider contentProvider
) {
701 fTreeViewer
.setContentProvider(new TreeContentProviderWrapper(contentProvider
));
705 * Sets the tree label provider used by this time graph combo.
707 * @param labelProvider the tree label provider
709 public void setTreeLabelProvider(ITableLabelProvider labelProvider
) {
710 fTreeViewer
.setLabelProvider(new TreeLabelProviderWrapper(labelProvider
));
714 * Sets the tree content provider used by the filter dialog
716 * @param contentProvider the tree content provider
719 public void setFilterContentProvider(ITreeContentProvider contentProvider
) {
720 fFilterDialog
.setContentProvider(contentProvider
);
724 * Sets the tree label provider used by the filter dialog
726 * @param labelProvider the tree label provider
729 public void setFilterLabelProvider(ITableLabelProvider labelProvider
) {
730 fFilterDialog
.setLabelProvider(labelProvider
);
734 * Sets the tree columns for this time graph combo.
736 * @param columnNames the tree column names
738 public void setTreeColumns(String
[] columnNames
) {
739 final Tree tree
= fTreeViewer
.getTree();
740 for (String columnName
: columnNames
) {
741 TreeColumn column
= new TreeColumn(tree
, SWT
.LEFT
);
742 column
.setText(columnName
);
748 * Sets the tree columns for this time graph combo's filter dialog.
750 * @param columnNames the tree column names
753 public void setFilterColumns(String
[] columnNames
) {
754 fFilterDialog
.setColumnNames(columnNames
);
758 * Sets the time graph provider used by this time graph combo.
760 * @param timeGraphProvider the time graph provider
762 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
763 fTimeGraphViewer
.setTimeGraphProvider(timeGraphProvider
);
767 * Sets or clears the input for this time graph combo.
768 * The input array should only contain top-level elements.
770 * @param input the input of this time graph combo, or <code>null</code> if none
772 public void setInput(ITimeGraphEntry
[] input
) {
773 fTopInput
= new ArrayList
<ITimeGraphEntry
>(Arrays
.asList(input
));
774 fAllInput
= listAllInputs(fTopInput
);
775 fFilter
.setNonFiltered(new ArrayList
<Object
>(fAllInput
));
776 fInhibitTreeSelection
= true;
777 fTreeViewer
.setInput(input
);
778 for (SelectionListenerWrapper listenerWrapper
: fSelectionListenerMap
.values()) {
779 listenerWrapper
.selection
= null;
781 fInhibitTreeSelection
= false;
782 fTreeViewer
.expandAll();
783 fTreeViewer
.getTree().getVerticalBar().setEnabled(false);
784 fTreeViewer
.getTree().getVerticalBar().setVisible(false);
785 fTimeGraphViewer
.setItemHeight(getItemHeight(fTreeViewer
.getTree()));
786 fTimeGraphViewer
.setInput(input
);
790 * @param filter The filter object to be attached to the view
793 public void addFilter(ViewerFilter filter
) {
794 ViewerFilter wrapper
= new ViewerFilterWrapper(filter
);
795 fTreeViewer
.addFilter(wrapper
);
796 fTimeGraphViewer
.addFilter(wrapper
);
797 fViewerFilterMap
.put(filter
, wrapper
);
801 * @param filter The filter object to be removed from the view
804 public void removeFilter(ViewerFilter filter
) {
805 ViewerFilter wrapper
= fViewerFilterMap
.get(filter
);
806 fTreeViewer
.removeFilter(wrapper
);
807 fTimeGraphViewer
.removeFilter(wrapper
);
808 fViewerFilterMap
.remove(filter
);
812 * Refreshes this time graph completely with information freshly obtained from its model.
814 public void refresh() {
815 fInhibitTreeSelection
= true;
816 fTreeViewer
.refresh();
817 fTimeGraphViewer
.refresh();
818 fInhibitTreeSelection
= false;
822 * Adds a listener for selection changes in this time graph combo.
824 * @param listener a selection listener
826 public void addSelectionListener(ITimeGraphSelectionListener listener
) {
827 SelectionListenerWrapper listenerWrapper
= new SelectionListenerWrapper(listener
);
828 fTreeViewer
.addSelectionChangedListener(listenerWrapper
);
829 fSelectionListenerMap
.put(listener
, listenerWrapper
);
830 fTimeGraphViewer
.addSelectionListener(listenerWrapper
);
834 * Removes the given selection listener from this time graph combo.
836 * @param listener a selection changed listener
838 public void removeSelectionListener(ITimeGraphSelectionListener listener
) {
839 SelectionListenerWrapper listenerWrapper
= fSelectionListenerMap
.remove(listener
);
840 fTreeViewer
.removeSelectionChangedListener(listenerWrapper
);
841 fTimeGraphViewer
.removeSelectionListener(listenerWrapper
);
845 * Sets the current selection for this time graph combo.
847 * @param selection the new selection
849 public void setSelection(ITimeGraphEntry selection
) {
850 fTimeGraphViewer
.setSelection(selection
);
851 fInhibitTreeSelection
= true; // block the tree selection changed listener
852 if (selection
!= null) {
853 StructuredSelection structuredSelection
= new StructuredSelection(selection
);
854 fTreeViewer
.setSelection(structuredSelection
);
856 fTreeViewer
.setSelection(new StructuredSelection());
858 fInhibitTreeSelection
= false;
859 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(fTreeViewer
.getTree());
860 if (treeItems
.size() == 0) {
863 TreeItem treeItem
= treeItems
.get(fTimeGraphViewer
.getTopIndex());
864 fTreeViewer
.getTree().setTopItem(treeItem
);
868 * Set the expanded state of an entry
871 * The entry to expand/collapse
873 * True for expanded, false for collapsed
877 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
878 fTimeGraphViewer
.setExpandedState(entry
, expanded
);
879 fTreeViewer
.setExpandedState(entry
, expanded
);
883 * Collapses all nodes of the viewer's tree, starting with the root.
887 public void collapseAll() {
888 fTimeGraphViewer
.collapseAll();
889 fTreeViewer
.collapseAll();
893 * Expands all nodes of the viewer's tree, starting with the root.
897 public void expandAll() {
898 fTimeGraphViewer
.expandAll();
899 fTreeViewer
.expandAll();
902 // ------------------------------------------------------------------------
904 // ------------------------------------------------------------------------
906 private ArrayList
<TreeItem
> getVisibleExpandedItems(Tree tree
) {
907 ArrayList
<TreeItem
> items
= new ArrayList
<TreeItem
>();
908 for (TreeItem item
: tree
.getItems()) {
909 if (item
.getData() == FILLER
) {
913 if (item
.getExpanded()) {
914 items
.addAll(getVisibleExpandedItems(item
));
920 private ArrayList
<TreeItem
> getVisibleExpandedItems(TreeItem treeItem
) {
921 ArrayList
<TreeItem
> items
= new ArrayList
<TreeItem
>();
922 for (TreeItem item
: treeItem
.getItems()) {
924 if (item
.getExpanded()) {
925 items
.addAll(getVisibleExpandedItems(item
));
932 * Explores the list of top-level inputs and returns all the inputs
934 * @param inputs The top-level inputs
935 * @return All the inputs
937 private List
<?
extends ITimeGraphEntry
> listAllInputs(List
<?
extends ITimeGraphEntry
> inputs
) {
938 ArrayList
<ITimeGraphEntry
> items
= new ArrayList
<ITimeGraphEntry
>();
939 for (ITimeGraphEntry entry
: inputs
) {
941 if (entry
.hasChildren()) {
942 items
.addAll(listAllInputs(entry
.getChildren()));
948 private int getItemHeight(final Tree tree
) {
950 * Bug in Linux. The method getItemHeight doesn't always return the correct value.
952 if (fLinuxItemHeight
>= 0 && System
.getProperty("os.name").contains("Linux")) { //$NON-NLS-1$ //$NON-NLS-2$
953 if (fLinuxItemHeight
!= 0) {
954 return fLinuxItemHeight
;
956 ArrayList
<TreeItem
> treeItems
= getVisibleExpandedItems(tree
);
957 if (treeItems
.size() > 1) {
958 final TreeItem treeItem0
= treeItems
.get(0);
959 final TreeItem treeItem1
= treeItems
.get(1);
960 PaintListener paintListener
= new PaintListener() {
962 public void paintControl(PaintEvent e
) {
963 tree
.removePaintListener(this);
964 int y0
= treeItem0
.getBounds().y
;
965 int y1
= treeItem1
.getBounds().y
;
966 int itemHeight
= y1
- y0
;
967 if (itemHeight
> 0) {
968 fLinuxItemHeight
= itemHeight
;
969 fTimeGraphViewer
.setItemHeight(itemHeight
);
973 tree
.addPaintListener(paintListener
);
976 fLinuxItemHeight
= -1; // Not Linux, don't perform os.name check anymore
978 return tree
.getItemHeight();