Class TreeViewer
- All Implemented Interfaces:
IInputProvider
,IInputSelectionProvider
,IPostSelectionProvider
,ISelectionProvider
- Direct Known Subclasses:
CheckboxTreeViewer
,CommonViewer
,DiffTreeViewer
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
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.jface.viewers.StructuredViewer
StructuredViewer.ColorAndFontCollector, StructuredViewer.ColorAndFontCollectorWithProviders
-
Field Summary
Fields inherited from class org.eclipse.jface.viewers.AbstractTreeViewer
ALL_LEVELS, NO_EXPAND
Fields inherited from class org.eclipse.jface.viewers.Viewer
WIDGET_DATA_KEY
-
Constructor Summary
ConstructorDescriptionTreeViewer
(Composite parent) Creates a tree viewer on a newly-created tree control under the given parent.TreeViewer
(Composite parent, int style) Creates a tree viewer on a newly-created tree control under the given parent.TreeViewer
(Tree tree) Creates a tree viewer on the given tree control. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addTreeListener
(Control c, TreeListener listener) Adds the given SWT tree listener to the given SWT control.protected void
assertContentProviderType
(IContentProvider provider) Assert that the content provider is of one of the supported types.protected ColumnViewerEditor
This implementation ofAbstractTreeViewer.createViewerEditor()
returns null to ensure API backwards compatibility.protected void
disassociate
(Item item) TheAbstractTreeViewer
implementation of this method also recurses over children of the corresponding element.protected int
Returns the number of columns of this viewer.void
editElement
(Object element, int column) Starts editing the given element at the given column index.protected Item
Get the child for the widget at index.protected Item[]
Returns the SWT child items for the given SWT widget.protected Widget
getColumnViewerOwner
(int columnIndex) Returns the column widget at the given column index.Returns the primary control associated with this viewer.protected boolean
getExpanded
(Item item) Returns whether the given SWT item is expanded or collapsed.protected Item
This implementation ofAbstractTreeViewer.getItemAt(Point)
returns null to ensure API backwards compatibility.protected int
getItemCount
(Control widget) Returns the number of child items of the given SWT control.protected int
getItemCount
(Item item) Returns the number of child items of the given SWT item.protected Item[]
Returns the child items of the given SWT item.The tree viewer implementation of thisViewer
framework method ensures that the given label provider is an instance of eitherITableLabelProvider
orILabelProvider
.protected Object
getParentElement
(Object element) This method takes a tree path or an element.protected Item
getParentItem
(Item item) Returns the parent item of the given item in the tree, ornull
if there is no parent item.protected Object[]
getRawChildren
(Object parent) Returns the children of the given parent without sorting and filtering them.protected Item[]
getSelection
(Control widget) Returns all selected items for the given SWT control.getTree()
Returns this tree viewer's tree control.protected ViewerRow
getViewerRowFromItem
(Widget item) Returns aViewerRow
associated with the given row widget.protected void
handleTreeCollapse
(TreeEvent event) Handles a tree collapse event from the SWT widget.protected void
handleTreeExpand
(TreeEvent event) Handles a tree expand event from the SWT widget.protected void
hookControl
(Control control) Adds event listener hooks to the given control.protected void
internalAdd
(Widget widget, Object parentElement, Object[] childElements) Adds the given child elements to this viewer as children of the given parent element.protected void
internalInitializeTree
(Control widget) Initializes the tree with root items, expanding to the appropriate level if necessary.protected void
internalRefreshStruct
(Widget widget, Object element, boolean updateLabels) boolean
isExpandable
(Object element) Return whether the tree node representing the given element or path can be expanded.protected void
mapElement
(Object element, Widget item) Adds the element item pair to the element map.protected Item
Creates a new item.void
Removes the element at the specified index of the parent.protected void
Removes all items from the given control.void
For a TreeViewer with a tree with the VIRTUAL style bit set, replace the given parent's child at index with the given element.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.void
setContentProvider
(IContentProvider provider) Sets the content provider used by thisTreeViewer
.protected void
setExpanded
(Item node, boolean expand) Sets the expand state of the given item.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.protected void
setSelection
(List<Item> items) Sets the selection to the given list of items.void
setSelection
(ISelection selection, boolean reveal) Sets a new selection for this viewer and optionally makes it visible.protected void
Shows the given item.protected void
updatePlus
(Item item, Object element) Updates the "+"/"-" icon of the tree node from the given element.Methods inherited from class org.eclipse.jface.viewers.AbstractTreeViewer
add, add, addSelectionListener, addTreeListener, associate, buildLabel, collapseAll, collapseToLevel, contains, createChildren, createTreeItem, doFindInputItem, doFindItem, doUpdateItem, doUpdateItem, expandAll, expandAll, expandToLevel, expandToLevel, expandToLevel, expandToLevel, fireTreeCollapsed, fireTreeExpanded, getAutoExpandLevel, getAutoExpandOnSingleChildLevels, getChildren, getExpandedElements, getExpandedState, getExpandedTreePaths, getNextItem, getPreviousItem, getSelection, getSelectionFromWidget, getSortedChildren, getStructuredSelection, getTreePathFromItem, getVisibleExpandedElements, handleDoubleSelect, indexForElement, inputChanged, insert, internalCollapseToLevel, internalExpand, internalExpandToLevel, internalFindItems, internalGetWidgetToSelect, internalIsInputOrEmptyPath, internalRefresh, internalRefresh, internalRefresh, internalRemove, internalRemove, isSameSelection, labelProviderChanged, remove, remove, remove, removeTreeListener, reveal, scrollDown, scrollUp, setAutoExpandLevel, setAutoExpandOnSingleChildLevels, setExpandedElements, setExpandedState, setExpandedStateWithAutoExpandOnSingleChild, setExpandedTreePaths, setExpandPreCheckFilters, setSelectionToWidget, setSelectionToWidget, updateChildren
Methods inherited from class org.eclipse.jface.viewers.ColumnViewer
applyEditorValue, cancelEditing, checkBusy, firePostSelectionChanged, getCell, getCellEditors, getCellModifier, getColumnProperties, getColumnViewerEditor, getItem, getLabelProvider, getViewerRow, handleDispose, hookEditingSupport, isBusy, isCellEditorActive, isExpandableNode, refresh, refresh, setBusy, setCellEditors, setCellModifier, setColumnProperties, setColumnViewerEditor, setDisplayIncrementally, setLabelProvider, triggerEditorActivationEvent, unmapAllElements, update, updateSelection
Methods inherited from class org.eclipse.jface.viewers.StructuredViewer
addDoubleClickListener, addDragSupport, addDropSupport, addFilter, addOpenListener, addPostSelectionChangedListener, assertElementsNotNull, equals, filter, findItem, findItems, fireDoubleClick, fireOpen, getColorAndFontCollector, getComparator, getComparer, getFilteredChildren, getFilters, getRoot, getSorter, handleInvalidSelection, handleLabelProviderChanged, handleOpen, handlePostSelect, handleSelect, hasFilters, internalUpdate, needsRefilter, preservingSelection, refresh, refresh, refreshItem, removeDoubleClickListener, removeFilter, removeOpenListener, removePostSelectionChangedListener, resetFilters, setComparator, setComparer, setFilters, setInput, setSorter, setUseHashlookup, testFindItem, testFindItems, unmapElement, unmapElement, update, updateItem, usingElementMap
Methods inherited from class org.eclipse.jface.viewers.ContentViewer
getContentProvider, getInput
Methods inherited from class org.eclipse.jface.viewers.Viewer
addHelpListener, addSelectionChangedListener, fireHelpRequested, fireSelectionChanged, getData, handleHelpRequest, removeHelpListener, removeSelectionChangedListener, setData, setSelection
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.jface.viewers.ISelectionProvider
addSelectionChangedListener, removeSelectionChangedListener, setSelection
-
Constructor Details
-
TreeViewer
Creates a tree viewer on a newly-created tree control under the given parent. The tree control is created using the SWT style bitsMULTI, H_SCROLL, V_SCROLL,
andBORDER
. The viewer has no input, no content provider, a default label provider, no sorter, and no filters.- Parameters:
parent
- the parent control
-
TreeViewer
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 controlstyle
- the SWT style bits used to create the tree.
-
TreeViewer
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
Description copied from class:AbstractTreeViewer
Adds the given SWT tree listener to the given SWT control.- Specified by:
addTreeListener
in classAbstractTreeViewer
- Parameters:
c
- the SWT controllistener
- the SWT tree listener
-
getColumnViewerOwner
Description copied from class:ColumnViewer
Returns the column widget at the given column index.- Overrides:
getColumnViewerOwner
in classAbstractTreeViewer
- Parameters:
columnIndex
- the column index- Returns:
- Widget the column widget
-
getChildren
Description copied from class:AbstractTreeViewer
Returns the SWT child items for the given SWT widget.- Specified by:
getChildren
in classAbstractTreeViewer
- Parameters:
o
- the widget- Returns:
- the child items
-
getControl
Description copied from class:Viewer
Returns the primary control associated with this viewer.- Specified by:
getControl
in classViewer
- Returns:
- the SWT control which displays this viewer's content
-
getExpanded
Description copied from class:AbstractTreeViewer
Returns whether the given SWT item is expanded or collapsed.- Specified by:
getExpanded
in classAbstractTreeViewer
- Parameters:
item
- the item- Returns:
true
if the item is considered expanded andfalse
if collapsed
-
getItemAt
Description copied from class:AbstractTreeViewer
This implementation ofAbstractTreeViewer.getItemAt(Point)
returns null to ensure API backwards compatibility. Subclasses should override.- Overrides:
getItemAt
in classAbstractTreeViewer
- Parameters:
p
- the widget-relative coordinates- Returns:
- the
Item
at the coordinates ornull
if there is no item at the given coordinates
-
getItemCount
Description copied from class:AbstractTreeViewer
Returns the number of child items of the given SWT control.- Specified by:
getItemCount
in classAbstractTreeViewer
- Parameters:
widget
- the control- Returns:
- the number of children
-
getItemCount
Description copied from class:AbstractTreeViewer
Returns the number of child items of the given SWT item.- Specified by:
getItemCount
in classAbstractTreeViewer
- Parameters:
item
- the item- Returns:
- the number of children
-
getItems
Description copied from class:AbstractTreeViewer
Returns the child items of the given SWT item.- Specified by:
getItems
in classAbstractTreeViewer
- Parameters:
item
- the item- Returns:
- the child items
-
getLabelProvider
The tree viewer implementation of thisViewer
framework method ensures that the given label provider is an instance of eitherITableLabelProvider
orILabelProvider
. If it is anITableLabelProvider
, then it provides a separate label text and image for each column. If it is anILabelProvider
, then it provides only the label text and image for the first column, and any remaining columns are blank.- Overrides:
getLabelProvider
in classContentViewer
- Returns:
- a label provider
-
getParentItem
Description copied from class:AbstractTreeViewer
Returns the parent item of the given item in the tree, ornull
if there is no parent item.- Specified by:
getParentItem
in classAbstractTreeViewer
- Parameters:
item
- the item- Returns:
- the parent item, or
null
if none
-
getSelection
Description copied from class:AbstractTreeViewer
Returns all selected items for the given SWT control.- Specified by:
getSelection
in classAbstractTreeViewer
- Parameters:
widget
- the control- Returns:
- the list of selected items
-
getTree
Returns this tree viewer's tree control.- Returns:
- the tree control
-
hookControl
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 classAbstractTreeViewer
- Parameters:
control
- the control
-
createViewerEditor
Description copied from class:AbstractTreeViewer
This implementation ofAbstractTreeViewer.createViewerEditor()
returns null to ensure API backwards compatibility. Subclasses should override.- Overrides:
createViewerEditor
in classAbstractTreeViewer
- Returns:
- the editor, or
null
if this viewer does not support editing cell contents.
-
newItem
Description copied from class:AbstractTreeViewer
Creates a new item.- Specified by:
newItem
in classAbstractTreeViewer
- Parameters:
parent
- the parent widgetflags
- SWT style bitsix
- if non-negative, indicates the position to insert the item into its parent- Returns:
- the newly-created item
-
removeAll
Description copied from class:AbstractTreeViewer
Removes all items from the given control.- Specified by:
removeAll
in classAbstractTreeViewer
- Parameters:
widget
- the control
-
setExpanded
Description copied from class:AbstractTreeViewer
Sets the expand state of the given item.- Specified by:
setExpanded
in classAbstractTreeViewer
- Parameters:
node
- the itemexpand
- the expand state of the item
-
setSelection
Description copied from class:AbstractTreeViewer
Sets the selection to the given list of items.- Specified by:
setSelection
in classAbstractTreeViewer
- Parameters:
items
- list of items (element type:org.eclipse.swt.widgets.Item
)
-
showItem
Description copied from class:AbstractTreeViewer
Shows the given item.- Specified by:
showItem
in classAbstractTreeViewer
- Parameters:
item
- the item
-
getChild
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 classAbstractTreeViewer
- Parameters:
widget
- the widget to checkindex
- the index of the widget- Returns:
- Item or
null
if widget is not a type that can contain items.
-
assertContentProviderType
Description copied from class:StructuredViewer
Assert that the content provider is of one of the supported types.- Overrides:
assertContentProviderType
in classAbstractTreeViewer
- Parameters:
provider
- content provider to check
-
getRawChildren
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 classAbstractTreeViewer
- Parameters:
parent
- the parent element- Returns:
- the child elements
-
setChildCount
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 pathcount
- new number of children- Since:
- 3.2
-
replace
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 parentindex
- the index in the parent's childrenelement
- the new element- Since:
- 3.2
- See Also:
-
isExpandable
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 anITreePathContentProvider
.The default implementation of this framework method calls
hasChildren
on this viewer's content provider. It may be overridden if necessary.- Overrides:
isExpandable
in classAbstractTreeViewer
- Parameters:
element
- the element or path- Returns:
true
if the tree node representing the given element can be expanded, orfalse
if not- See Also:
-
getParentElement
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 ornull
if the parent is not known. If the argument is a tree path with more than one segment, returns its parent tree path, otherwise returnsnull
.- Overrides:
getParentElement
in classAbstractTreeViewer
- Parameters:
element
- the element or path to find parent for- Returns:
- the parent element, or parent path, or
null
-
internalAdd
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 classAbstractTreeViewer
- Parameters:
widget
- the widget for the parent elementparentElement
- the parent elementchildElements
- the child elements to add
-
internalRefreshStruct
-
mapElement
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 classStructuredViewer
- Parameters:
element
- the elementitem
- the corresponding widget
-
getViewerRowFromItem
Description copied from class:ColumnViewer
Returns aViewerRow
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 classAbstractTreeViewer
- Parameters:
item
- the row widget- Returns:
- ViewerRow a viewer row object
-
internalInitializeTree
Description copied from class:AbstractTreeViewer
Initializes the tree with root items, expanding to the appropriate level if necessary.- Overrides:
internalInitializeTree
in classAbstractTreeViewer
- Parameters:
widget
- the tree control
-
updatePlus
Description copied from class:AbstractTreeViewer
Updates the "+"/"-" icon of the tree node from the given element. It callsisExpandable
to determine whether an element is expandable.- Overrides:
updatePlus
in classAbstractTreeViewer
- Parameters:
item
- the itemelement
- the element
-
remove
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 elementindex
- child index- Since:
- 3.3
-
handleTreeExpand
Description copied from class:AbstractTreeViewer
Handles a tree expand event from the SWT widget.- Overrides:
handleTreeExpand
in classAbstractTreeViewer
- Parameters:
event
- the SWT tree event
-
handleTreeCollapse
Description copied from class:AbstractTreeViewer
Handles a tree collapse event from the SWT widget.- Overrides:
handleTreeCollapse
in classAbstractTreeViewer
- Parameters:
event
- the SWT tree event
-
setContentProvider
Sets the content provider used by thisTreeViewer
.Content providers for tree viewers must implement either
ITreeContentProvider
, orITreePathContentProvider
, orILazyTreeContentProvider
, orILazyTreePathContentProvider
.- Overrides:
setContentProvider
in classAbstractTreeViewer
- Parameters:
provider
- the content provider- See Also:
-
setHasChildren
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 pathhasChildren
- the new state for the element or tree path- Since:
- 3.3
-
disassociate
Description copied from class:AbstractTreeViewer
TheAbstractTreeViewer
implementation of this method also recurses over children of the corresponding element.- Overrides:
disassociate
in classAbstractTreeViewer
- 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 classAbstractTreeViewer
- Returns:
- the number of columns
-
setSelection
Sets a new selection for this viewer and optionally makes it visible.Currently the
reveal
parameter is not honored becauseTree
does not provide an API to only select an item without scrolling it into view- Overrides:
setSelection
in classStructuredViewer
- Parameters:
selection
- the new selectionreveal
-true
if the selection is to be made visible, andfalse
otherwise
-
editElement
Description copied from class:ColumnViewer
Starts editing the given element at the given column index.- Overrides:
editElement
in classColumnViewer
- Parameters:
element
- the model elementcolumn
- the column index
-