Class TreeViewer

All Implemented Interfaces:
IInputProvider, IInputSelectionProvider, IPostSelectionProvider, ISelectionProvider
Direct Known Subclasses:
CheckboxTreeViewer, CommonViewer, DiffTreeViewer

@NoExtend public class TreeViewer extends AbstractTreeViewer
A concrete viewer based on an SWT Tree control.

This class is not intended to be subclassed outside the viewer framework. It is designed to be instantiated with a pre-existing SWT tree control and configured with a domain-specific content provider, label provider, element filter (optional), and element sorter (optional).

As of 3.2, TreeViewer supports multiple equal elements (each with a different parent chain) in the tree. This support requires that clients enable the element map by calling setUseHashlookup(true).

Content providers for tree viewers must implement either the ITreeContentProvider interface, (as of 3.2) the ILazyTreeContentProvider interface, or (as of 3.3) the ILazyTreePathContentProvider. If the content provider is an ILazyTreeContentProvider or an ILazyTreePathContentProvider, the underlying Tree must be created using the SWT.VIRTUAL style bit, the tree viewer will not support sorting or filtering, and hash lookup must be enabled by calling StructuredViewer.setUseHashlookup(boolean).

Users setting up an editable tree with more than 1 column have to pass the SWT.FULL_SELECTION style bit

  • Constructor Details

    • TreeViewer

      public TreeViewer(Composite parent)
      Creates a tree viewer on a newly-created tree control under the given parent. The tree control is created using the SWT style bits MULTI, H_SCROLL, V_SCROLL, and BORDER. The viewer has no input, no content provider, a default label provider, no sorter, and no filters.
      Parameters:
      parent - the parent control
    • TreeViewer

      public TreeViewer(Composite parent, int style)
      Creates a tree viewer on a newly-created tree control under the given parent. The tree control is created using the given SWT style bits. The viewer has no input, no content provider, a default label provider, no sorter, and no filters.
      Parameters:
      parent - the parent control
      style - the SWT style bits used to create the tree.
    • TreeViewer

      public TreeViewer(Tree tree)
      Creates a tree viewer on the given tree control. The viewer has no input, no content provider, a default label provider, no sorter, and no filters.
      Parameters:
      tree - the tree control
  • Method Details

    • addTreeListener

      protected void addTreeListener(Control c, TreeListener listener)
      Description copied from class: AbstractTreeViewer
      Adds the given SWT tree listener to the given SWT control.
      Specified by:
      addTreeListener in class AbstractTreeViewer
      Parameters:
      c - the SWT control
      listener - the SWT tree listener
    • getColumnViewerOwner

      protected Widget getColumnViewerOwner(int columnIndex)
      Description copied from class: ColumnViewer
      Returns the column widget at the given column index.
      Overrides:
      getColumnViewerOwner in class AbstractTreeViewer
      Parameters:
      columnIndex - the column index
      Returns:
      Widget the column widget
    • getChildren

      protected Item[] getChildren(Widget o)
      Description copied from class: AbstractTreeViewer
      Returns the SWT child items for the given SWT widget.
      Specified by:
      getChildren in class AbstractTreeViewer
      Parameters:
      o - the widget
      Returns:
      the child items
    • getControl

      public Control getControl()
      Description copied from class: Viewer
      Returns the primary control associated with this viewer.
      Specified by:
      getControl in class Viewer
      Returns:
      the SWT control which displays this viewer's content
    • getExpanded

      protected boolean getExpanded(Item item)
      Description copied from class: AbstractTreeViewer
      Returns whether the given SWT item is expanded or collapsed.
      Specified by:
      getExpanded in class AbstractTreeViewer
      Parameters:
      item - the item
      Returns:
      true if the item is considered expanded and false if collapsed
    • getItemAt

      protected Item getItemAt(Point p)
      Description copied from class: AbstractTreeViewer
      This implementation of AbstractTreeViewer.getItemAt(Point) returns null to ensure API backwards compatibility. Subclasses should override.
      Overrides:
      getItemAt in class AbstractTreeViewer
      Parameters:
      p - the widget-relative coordinates
      Returns:
      the Item at the coordinates or null if there is no item at the given coordinates
    • getItemCount

      protected int getItemCount(Control widget)
      Description copied from class: AbstractTreeViewer
      Returns the number of child items of the given SWT control.
      Specified by:
      getItemCount in class AbstractTreeViewer
      Parameters:
      widget - the control
      Returns:
      the number of children
    • getItemCount

      protected int getItemCount(Item item)
      Description copied from class: AbstractTreeViewer
      Returns the number of child items of the given SWT item.
      Specified by:
      getItemCount in class AbstractTreeViewer
      Parameters:
      item - the item
      Returns:
      the number of children
    • getItems

      protected Item[] getItems(Item item)
      Description copied from class: AbstractTreeViewer
      Returns the child items of the given SWT item.
      Specified by:
      getItems in class AbstractTreeViewer
      Parameters:
      item - the item
      Returns:
      the child items
    • getLabelProvider

      public IBaseLabelProvider getLabelProvider()
      The tree viewer implementation of this Viewer framework method ensures that the given label provider is an instance of either ITableLabelProvider or ILabelProvider. If it is an ITableLabelProvider, then it provides a separate label text and image for each column. If it is an ILabelProvider, then it provides only the label text and image for the first column, and any remaining columns are blank.
      Overrides:
      getLabelProvider in class ContentViewer
      Returns:
      a label provider
    • getParentItem

      protected Item getParentItem(Item item)
      Description copied from class: AbstractTreeViewer
      Returns the parent item of the given item in the tree, or null if there is no parent item.
      Specified by:
      getParentItem in class AbstractTreeViewer
      Parameters:
      item - the item
      Returns:
      the parent item, or null if none
    • getSelection

      protected Item[] getSelection(Control widget)
      Description copied from class: AbstractTreeViewer
      Returns all selected items for the given SWT control.
      Specified by:
      getSelection in class AbstractTreeViewer
      Parameters:
      widget - the control
      Returns:
      the list of selected items
    • getTree

      public Tree getTree()
      Returns this tree viewer's tree control.
      Returns:
      the tree control
    • hookControl

      protected void hookControl(Control control)
      Description copied from class: ContentViewer
      Adds event listener hooks to the given control.

      All subclasses must call this method when their control is first established.

      The ContentViewer implementation of this method hooks dispose events for the given control. Subclasses may override if they need to add other control hooks; however, super.hookControl must be invoked.

      Overrides:
      hookControl in class AbstractTreeViewer
      Parameters:
      control - the control
    • createViewerEditor

      protected ColumnViewerEditor createViewerEditor()
      Description copied from class: AbstractTreeViewer
      This implementation of AbstractTreeViewer.createViewerEditor() returns null to ensure API backwards compatibility. Subclasses should override.
      Overrides:
      createViewerEditor in class AbstractTreeViewer
      Returns:
      the editor, or null if this viewer does not support editing cell contents.
    • newItem

      protected Item newItem(Widget parent, int flags, int ix)
      Description copied from class: AbstractTreeViewer
      Creates a new item.
      Specified by:
      newItem in class AbstractTreeViewer
      Parameters:
      parent - the parent widget
      flags - SWT style bits
      ix - if non-negative, indicates the position to insert the item into its parent
      Returns:
      the newly-created item
    • removeAll

      protected void removeAll(Control widget)
      Description copied from class: AbstractTreeViewer
      Removes all items from the given control.
      Specified by:
      removeAll in class AbstractTreeViewer
      Parameters:
      widget - the control
    • setExpanded

      protected void setExpanded(Item node, boolean expand)
      Description copied from class: AbstractTreeViewer
      Sets the expand state of the given item.
      Specified by:
      setExpanded in class AbstractTreeViewer
      Parameters:
      node - the item
      expand - the expand state of the item
    • setSelection

      protected void setSelection(List<Item> items)
      Description copied from class: AbstractTreeViewer
      Sets the selection to the given list of items.
      Specified by:
      setSelection in class AbstractTreeViewer
      Parameters:
      items - list of items (element type: org.eclipse.swt.widgets.Item)
    • showItem

      protected void showItem(Item item)
      Description copied from class: AbstractTreeViewer
      Shows the given item.
      Specified by:
      showItem in class AbstractTreeViewer
      Parameters:
      item - the item
    • getChild

      protected Item getChild(Widget widget, int index)
      Description copied from class: AbstractTreeViewer
      Get the child for the widget at index. Note that the default implementation is not very efficient and should be overridden if this class is implemented.
      Overrides:
      getChild in class AbstractTreeViewer
      Parameters:
      widget - the widget to check
      index - the index of the widget
      Returns:
      Item or null if widget is not a type that can contain items.
    • assertContentProviderType

      protected void assertContentProviderType(IContentProvider provider)
      Description copied from class: StructuredViewer
      Assert that the content provider is of one of the supported types.
      Overrides:
      assertContentProviderType in class AbstractTreeViewer
      Parameters:
      provider - content provider to check
    • getRawChildren

      protected Object[] getRawChildren(Object parent)
      Description copied from class: StructuredViewer
      Returns the children of the given parent without sorting and filtering them. The resulting array must not be modified, as it may come directly from the model's internal state.

      Returns an empty array if the given parent is null.

      Overrides:
      getRawChildren in class AbstractTreeViewer
      Parameters:
      parent - the parent element
      Returns:
      the child elements
    • setChildCount

      public void setChildCount(Object elementOrTreePath, int count)
      For a TreeViewer with a tree with the VIRTUAL style bit set, set the number of children of the given element or tree path. To set the number of children of the invisible root of the tree, you can pass the input object or an empty tree path.
      Parameters:
      elementOrTreePath - the element, or tree path
      count - new number of children
      Since:
      3.2
    • replace

      public void replace(Object parentElementOrTreePath, int index, Object element)
      For a TreeViewer with a tree with the VIRTUAL style bit set, replace the given parent's child at index with the given element. If the given parent is this viewer's input or an empty tree path, this will replace the root element at the given index.

      This method should be called by implementers of ILazyTreeContentProvider to populate this viewer.

      Parameters:
      parentElementOrTreePath - the parent of the element that should be updated, or the tree path to that parent
      index - the index in the parent's children
      element - the new element
      Since:
      3.2
      See Also:
    • isExpandable

      public boolean isExpandable(Object element)
      Description copied from class: AbstractTreeViewer
      Return whether the tree node representing the given element or path can be expanded. Clients should query expandability by path if the viewer's content provider is an ITreePathContentProvider.

      The default implementation of this framework method calls hasChildren on this viewer's content provider. It may be overridden if necessary.

      Overrides:
      isExpandable in class AbstractTreeViewer
      Parameters:
      element - the element or path
      Returns:
      true if the tree node representing the given element can be expanded, or false if not
      See Also:
    • getParentElement

      protected Object getParentElement(Object element)
      Description copied from class: AbstractTreeViewer
      This method takes a tree path or an element. If the argument is not a tree path, returns the parent of the given element or null if the parent is not known. If the argument is a tree path with more than one segment, returns its parent tree path, otherwise returns null.
      Overrides:
      getParentElement in class AbstractTreeViewer
      Parameters:
      element - the element or path to find parent for
      Returns:
      the parent element, or parent path, or null
    • internalAdd

      protected void internalAdd(Widget widget, Object parentElement, Object[] childElements)
      Description copied from class: AbstractTreeViewer
      Adds the given child elements to this viewer as children of the given parent element.

      EXPERIMENTAL. Not to be used except by JDT. This method was added to support JDT's explorations into grouping by working sets. This method cannot be removed without breaking binary backwards compatibility, but should not be called by clients.

      Overrides:
      internalAdd in class AbstractTreeViewer
      Parameters:
      widget - the widget for the parent element
      parentElement - the parent element
      childElements - the child elements to add
    • internalRefreshStruct

      protected void internalRefreshStruct(Widget widget, Object element, boolean updateLabels)
    • mapElement

      protected void mapElement(Object element, Widget item)
      Description copied from class: StructuredViewer
      Adds the element item pair to the element map.

      This method is internal to the framework; subclassers should not call this method.

      Overrides:
      mapElement in class StructuredViewer
      Parameters:
      element - the element
      item - the corresponding widget
    • getViewerRowFromItem

      protected ViewerRow getViewerRowFromItem(Widget item)
      Description copied from class: ColumnViewer
      Returns a ViewerRow associated with the given row widget. Implementations may re-use the same instance for different row widgets; callers can only use the viewer row locally and until the next call to this method.
      Overrides:
      getViewerRowFromItem in class AbstractTreeViewer
      Parameters:
      item - the row widget
      Returns:
      ViewerRow a viewer row object
    • internalInitializeTree

      protected void internalInitializeTree(Control widget)
      Description copied from class: AbstractTreeViewer
      Initializes the tree with root items, expanding to the appropriate level if necessary.
      Overrides:
      internalInitializeTree in class AbstractTreeViewer
      Parameters:
      widget - the tree control
    • updatePlus

      protected void updatePlus(Item item, Object element)
      Description copied from class: AbstractTreeViewer
      Updates the "+"/"-" icon of the tree node from the given element. It calls isExpandable to determine whether an element is expandable.
      Overrides:
      updatePlus in class AbstractTreeViewer
      Parameters:
      item - the item
      element - the element
    • remove

      public void remove(Object parentOrTreePath, int index)
      Removes the element at the specified index of the parent. The selection is updated if required.
      Parameters:
      parentOrTreePath - the parent element, the input element, or a tree path to the parent element
      index - child index
      Since:
      3.3
    • handleTreeExpand

      protected void handleTreeExpand(TreeEvent event)
      Description copied from class: AbstractTreeViewer
      Handles a tree expand event from the SWT widget.
      Overrides:
      handleTreeExpand in class AbstractTreeViewer
      Parameters:
      event - the SWT tree event
    • handleTreeCollapse

      protected void handleTreeCollapse(TreeEvent event)
      Description copied from class: AbstractTreeViewer
      Handles a tree collapse event from the SWT widget.
      Overrides:
      handleTreeCollapse in class AbstractTreeViewer
      Parameters:
      event - the SWT tree event
    • setContentProvider

      public void setContentProvider(IContentProvider provider)
      Sets the content provider used by this TreeViewer.

      Content providers for tree viewers must implement either ITreeContentProvider, or ITreePathContentProvider, or ILazyTreeContentProvider, or ILazyTreePathContentProvider.

      Overrides:
      setContentProvider in class AbstractTreeViewer
      Parameters:
      provider - the content provider
      See Also:
    • setHasChildren

      public void setHasChildren(Object elementOrTreePath, boolean hasChildren)
      For a TreeViewer with a tree with the VIRTUAL style bit set, inform the viewer about whether the given element or tree path has children. Avoid calling this method if the number of children has already been set.
      Parameters:
      elementOrTreePath - the element, or tree path
      hasChildren - the new state for the element or tree path
      Since:
      3.3
    • disassociate

      protected void disassociate(Item item)
      Description copied from class: AbstractTreeViewer
      The AbstractTreeViewer implementation of this method also recurses over children of the corresponding element.
      Overrides:
      disassociate in class AbstractTreeViewer
      Parameters:
      item - the widget
    • doGetColumnCount

      protected int doGetColumnCount()
      Description copied from class: AbstractTreeViewer
      Returns the number of columns of this viewer.

      Subclasses should overwrite this method, which has a default implementation (returning 0) for API backwards compatility reasons

      Overrides:
      doGetColumnCount in class AbstractTreeViewer
      Returns:
      the number of columns
    • setSelection

      public void setSelection(ISelection selection, boolean reveal)
      Sets a new selection for this viewer and optionally makes it visible.

      Currently the reveal parameter is not honored because Tree does not provide an API to only select an item without scrolling it into view

      Overrides:
      setSelection in class StructuredViewer
      Parameters:
      selection - the new selection
      reveal - true if the selection is to be made visible, and false otherwise
    • editElement

      public void editElement(Object element, int column)
      Description copied from class: ColumnViewer
      Starts editing the given element at the given column index.
      Overrides:
      editElement in class ColumnViewer
      Parameters:
      element - the model element
      column - the column index