Package org.eclipse.jface.viewers

## Class CheckboxTreeViewer

- java.lang.Object
- - org.eclipse.jface.viewers.Viewer
  - - org.eclipse.jface.viewers.ContentViewer
    - - org.eclipse.jface.viewers.StructuredViewer
      - - org.eclipse.jface.viewers.ColumnViewer
        - - org.eclipse.jface.viewers.AbstractTreeViewer
          - - org.eclipse.jface.viewers.TreeViewer
            - - org.eclipse.jface.viewers.CheckboxTreeViewer

All Implemented Interfaces:
  `ICheckable`, `IInputProvider`, `IInputSelectionProvider`, `IPostSelectionProvider`, `ISelectionProvider`

Direct Known Subclasses:
  `ContainerCheckedTreeViewer`

    public class CheckboxTreeViewer
    extends TreeViewer
    implements ICheckable

A concrete tree-structured viewer based on an SWT `Tree` control with checkboxes on each node.

This class supports setting an `ICheckStateProvider` to set the checkbox states. To see standard SWT behavior, view SWT Snippet274.

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).

Restriction:
  This class is not intended to be subclassed by clients.

### 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`

### Fields inherited from class org.eclipse.jface.viewers.Viewer

`WIDGET_DATA_KEY`

### Constructor Summary

Constructors
`CheckboxTreeViewer​(Composite parent)` Creates a tree viewer on a newly-created tree control under the given parent.
`CheckboxTreeViewer​(Composite parent, int style)` Creates a tree viewer on a newly-created tree control under the given parent.
`CheckboxTreeViewer​(Tree tree)` Creates a tree viewer on the given tree control.

### Method Summary

All Methods Instance Methods Concrete Methods Deprecated Methods
`void` `addCheckStateListener​(ICheckStateListener listener)` Adds a listener for changes to the checked state of elements in this viewer.
`protected void` `doUpdateItem​(Item item, Object element)` Copies the attributes of the given element into the given SWT item.
`protected void` `fireCheckStateChanged​(CheckStateChangedEvent event)` Notifies any check state listeners that the check state of an element has changed.
`boolean` `getChecked​(Object element)` Returns the checked state of the given element.
`Object[]` `getCheckedElements()` Returns a list of checked elements in this viewer's tree, including currently hidden ones that are marked as checked but are under a collapsed ancestor.
`boolean` `getGrayed​(Object element)` Returns the grayed state of the given element.
`Object[]` `getGrayedElements()` Returns a list of grayed elements in this viewer's tree, including currently hidden ones that are marked as grayed but are under a collapsed ancestor.
`protected void` `handleDoubleSelect​(SelectionEvent event)` Handles a double-click select event from the widget.
`protected void` `handleSelect​(SelectionEvent event)` Handles a select event from the widget.
`protected void` `preservingSelection​(Runnable updateCode)` Attempts to preserves the current selection across a run of the given code.
`void` `removeCheckStateListener​(ICheckStateListener listener)` Removes the given check state listener from this viewer.
`void` `setAllChecked​(boolean state)` Deprecated. as this method only checks or unchecks visible items is is recommended that `setSubtreeChecked(Object, boolean)` is used instead.
`boolean` `setChecked​(Object element, boolean state)` Sets the checked state for the given element in this viewer.
`protected void` `setCheckedChildren​(Item item, boolean state)` Sets the checked state for the children of the given item.
`void` `setCheckedElements​(Object[] elements)` Sets which elements are checked in this viewer's tree.
`void` `setCheckStateProvider​(ICheckStateProvider checkStateProvider)` Sets the `ICheckStateProvider` for this `CheckboxTreeViewer`.
`boolean` `setGrayChecked​(Object element, boolean state)` Check and gray the selection rather than calling both setGrayed and setChecked as an optimization.
`boolean` `setGrayed​(Object element, boolean state)` Sets the grayed state for the given element in this viewer.
`void` `setGrayedElements​(Object... elements)` Sets which elements are grayed in this viewer's tree.
`boolean` `setParentsGrayed​(Object element, boolean state)` Sets the grayed state for the given element and its parents in this viewer.
`boolean` `setSubtreeChecked​(Object element, boolean state)` Sets the checked state for the given element and its visible children in this viewer.

### Methods inherited from class org.eclipse.jface.viewers.TreeViewer

`addTreeListener, assertContentProviderType, createViewerEditor, disassociate, doGetColumnCount, editElement, getChild, getChildren, getColumnViewerOwner, getControl, getExpanded, getItemAt, getItemCount, getItemCount, getItems, getLabelProvider, getParentElement, getParentItem, getRawChildren, getSelection, getTree, getViewerRowFromItem, handleTreeCollapse, handleTreeExpand, hookControl, internalAdd, internalInitializeTree, internalRefreshStruct, isExpandable, mapElement, newItem, remove, removeAll, replace, setChildCount, setContentProvider, setExpanded, setHasChildren, setSelection, setSelection, showItem, updatePlus`

### Methods inherited from class org.eclipse.jface.viewers.AbstractTreeViewer

`add, add, addSelectionListener, addTreeListener, associate, buildLabel, collapseAll, collapseToLevel, createChildren, createTreeItem, doFindInputItem, doFindItem, doUpdateItem, expandAll, expandAll, expandToLevel, expandToLevel, expandToLevel, expandToLevel, fireTreeCollapsed, fireTreeExpanded, getAutoExpandLevel, getChildren, getExpandedElements, getExpandedState, getExpandedTreePaths, getNextItem, getPreviousItem, getSelection, getSelectionFromWidget, getSortedChildren, getStructuredSelection, getTreePathFromItem, getVisibleExpandedElements, indexForElement, inputChanged, insert, internalCollapseToLevel, internalExpand, internalExpandToLevel, internalFindItems, internalGetWidgetToSelect, internalIsInputOrEmptyPath, internalRefresh, internalRefresh, internalRefresh, internalRemove, internalRemove, isSameSelection, labelProviderChanged, remove, remove, remove, removeTreeListener, reveal, scrollDown, scrollUp, setAutoExpandLevel, setExpandedElements, setExpandedState, setExpandedTreePaths, setExpandPreCheckFilters, setSelectionToWidget, setSelectionToWidget, updateChildren`

### Methods inherited from class org.eclipse.jface.viewers.ColumnViewer

`applyEditorValue, cancelEditing, checkBusy, getCell, getCellEditors, getCellModifier, getColumnProperties, getColumnViewerEditor, getItem, getLabelProvider, getViewerRow, handleDispose, hookEditingSupport, isBusy, isCellEditorActive, refresh, refresh, setBusy, setCellEditors, setCellModifier, setColumnProperties, setColumnViewerEditor, setLabelProvider, triggerEditorActivationEvent, update`

### Methods inherited from class org.eclipse.jface.viewers.StructuredViewer

`addDoubleClickListener, addDragSupport, addDropSupport, addFilter, addOpenListener, addPostSelectionChangedListener, assertElementsNotNull, equals, filter, findItem, findItems, fireDoubleClick, fireOpen, firePostSelectionChanged, getColorAndFontCollector, getComparator, getComparer, getFilteredChildren, getFilters, getRoot, getSorter, handleInvalidSelection, handleLabelProviderChanged, handleOpen, handlePostSelect, hasFilters, internalUpdate, needsRefilter, refresh, refresh, refreshItem, removeDoubleClickListener, removeFilter, removeOpenListener, removePostSelectionChangedListener, resetFilters, setComparator, setComparer, setFilters, setInput, setSorter, setUseHashlookup, testFindItem, testFindItems, unmapAllElements, unmapElement, unmapElement, update, updateItem, updateSelection, 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 Detail

#### CheckboxTreeViewer

    public CheckboxTreeViewer​(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: `CHECK` and `BORDER`. The viewer has no input, no content provider, a default label provider, no sorter, and no filters.

Parameters:
  `parent` - the parent control

#### CheckboxTreeViewer

    public CheckboxTreeViewer​(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, plus the `CHECK` style bit. 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

#### CheckboxTreeViewer

    public CheckboxTreeViewer​(Tree tree)

Creates a tree viewer on the given tree control. The `SWT.CHECK` style bit must be set 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 Detail

#### addCheckStateListener

    public void addCheckStateListener​(ICheckStateListener listener)

Description copied from interface: `ICheckable`
Adds a listener for changes to the checked state of elements in this viewer. Has no effect if an identical listener is already registered.

Specified by:
  `addCheckStateListener` in interface `ICheckable`
Parameters:
  `listener` - a check state listener

#### setCheckStateProvider

    public void setCheckStateProvider​(ICheckStateProvider checkStateProvider)

Sets the `ICheckStateProvider` for this `CheckboxTreeViewer`. The check state provider will supply the logic for deciding whether the check box associated with each item should be checked, grayed or unchecked.

Parameters:
  `checkStateProvider` - The provider.
Since:
  3.5

#### doUpdateItem

    protected void doUpdateItem​(Item item,
                                Object element)

Description copied from class: `AbstractTreeViewer`
Copies the attributes of the given element into the given SWT item.

Overrides:
  `doUpdateItem` in class `AbstractTreeViewer`
Parameters:
  `item` - the SWT item
  `element` - the element

#### fireCheckStateChanged

    protected void fireCheckStateChanged​(CheckStateChangedEvent event)

Notifies any check state listeners that the check state of an element has changed. Only listeners registered at the time this method is called are notified.

Parameters:
  `event` - a check state changed event
See Also:
  `ICheckStateListener.checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)`

#### getChecked

    public boolean getChecked​(Object element)

Description copied from interface: `ICheckable`
Returns the checked state of the given element.

Specified by:
  `getChecked` in interface `ICheckable`
Parameters:
  `element` - the element
Returns:
  `true` if the element is checked, and `false` if not checked

#### getCheckedElements

    public Object[] getCheckedElements()

Returns a list of checked elements in this viewer's tree, including currently hidden ones that are marked as checked but are under a collapsed ancestor.

This method is typically used when preserving the interesting state of a viewer; `setCheckedElements` is used during the restore.

Returns:
  the array of checked elements
See Also:
  `setCheckedElements(java.lang.Object[])`

#### getGrayed

    public boolean getGrayed​(Object element)

Returns the grayed state of the given element.

Parameters:
  `element` - the element
Returns:
  `true` if the element is grayed, and `false` if not grayed

#### getGrayedElements

    public Object[] getGrayedElements()

Returns a list of grayed elements in this viewer's tree, including currently hidden ones that are marked as grayed but are under a collapsed ancestor.

This method is typically used when preserving the interesting state of a viewer; `setGrayedElements` is used during the restore.

Returns:
  the array of grayed elements
See Also:
  `setGrayedElements(java.lang.Object...)`

#### handleDoubleSelect

    protected void handleDoubleSelect​(SelectionEvent event)

Description copied from class: `StructuredViewer`
Handles a double-click select event from the widget.

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

Overrides:
  `handleDoubleSelect` in class `AbstractTreeViewer`
Parameters:
  `event` - the SWT selection event

#### handleSelect

    protected void handleSelect​(SelectionEvent event)

Description copied from class: `StructuredViewer`
Handles a select event from the widget.

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

Overrides:
  `handleSelect` in class `StructuredViewer`
Parameters:
  `event` - the SWT selection event

#### preservingSelection

    protected void preservingSelection​(Runnable updateCode)

Description copied from class: `StructuredViewer`
Attempts to preserves the current selection across a run of the given code. This method should not preserve the selection if {link #getPreserveSelection()} returns false.

The default implementation of this method:

- discovers the old selection (via `getSelection`)
- runs the given runnable
- attempts to restore the old selection (using `setSelectionToWidget`
- rediscovers the resulting selection (via `getSelection`)
- calls `handleInvalidSelection` if the resulting selection is different from the old selection

Overrides:
  `preservingSelection` in class `StructuredViewer`
Parameters:
  `updateCode` - the code to run see #getPreserveSelection()

#### removeCheckStateListener

    public void removeCheckStateListener​(ICheckStateListener listener)

Description copied from interface: `ICheckable`
Removes the given check state listener from this viewer. Has no effect if an identical listener is not registered.

Specified by:
  `removeCheckStateListener` in interface `ICheckable`
Parameters:
  `listener` - a check state listener

#### setChecked

    public boolean setChecked​(Object element,
                              boolean state)

Description copied from interface: `ICheckable`
Sets the checked state for the given element in this viewer. Does not fire events to check state listeners.

Specified by:
  `setChecked` in interface `ICheckable`
Parameters:
  `element` - the element
  `state` - `true` if the item should be checked, and `false` if it should be unchecked
Returns:
  `true` if the checked state could be set, and `false` otherwise

#### setCheckedChildren

    protected void setCheckedChildren​(Item item,
                                      boolean state)

Sets the checked state for the children of the given item.

Parameters:
  `item` - the item
  `state` - `true` if the item should be checked, and `false` if it should be unchecked
Since:
  3.11

#### setCheckedElements

    public void setCheckedElements​(Object[] elements)

Sets which elements are checked in this viewer's tree. The given list contains the elements that are to be checked; all other elements are to be unchecked. Does not fire events to check state listeners.

This method is typically used when restoring the interesting state of a viewer captured by an earlier call to `getCheckedElements`.

Parameters:
  `elements` - the array of checked elements
See Also:
  `getCheckedElements()`

#### setGrayed

    public boolean setGrayed​(Object element,
                             boolean state)

Sets the grayed state for the given element in this viewer.

Parameters:
  `element` - the element
  `state` - `true` if the item should be grayed, and `false` if it should be ungrayed
Returns:
  `true` if the gray state could be set, and `false` otherwise

#### setGrayChecked

    public boolean setGrayChecked​(Object element,
                                  boolean state)

Check and gray the selection rather than calling both setGrayed and setChecked as an optimization. Does not fire events to check state listeners.

Parameters:
  `element` - the item being checked
  `state` - a boolean indicating selection or deselection
Returns:
  boolean indicating success or failure.

#### setGrayedElements

    public void setGrayedElements​(Object... elements)

Sets which elements are grayed in this viewer's tree. The given list contains the elements that are to be grayed; all other elements are to be ungrayed.

This method is typically used when restoring the interesting state of a viewer captured by an earlier call to `getGrayedElements`.

Parameters:
  `elements` - the array of grayed elements
See Also:
  `getGrayedElements()`

#### setParentsGrayed

    public boolean setParentsGrayed​(Object element,
                                    boolean state)

Sets the grayed state for the given element and its parents in this viewer.

Parameters:
  `element` - the element
  `state` - `true` if the item should be grayed, and `false` if it should be ungrayed
Returns:
  `true` if the element is visible and the gray state could be set, and `false` otherwise
See Also:
  `setGrayed(java.lang.Object, boolean)`

#### setSubtreeChecked

    public boolean setSubtreeChecked​(Object element,
                                     boolean state)

Sets the checked state for the given element and its visible children in this viewer. Assumes that the element has been expanded before. To enforce that the item is expanded, call `expandToLevel` for the element. Does not fire events to check state listeners.

Parameters:
  `element` - the element
  `state` - `true` if the item should be checked, and `false` if it should be unchecked
Returns:
  `true` if the checked state could be set, and `false` otherwise

#### setAllChecked

    @Deprecated
    public void setAllChecked​(boolean state)

Deprecated.
as this method only checks or unchecks visible items is is recommended that `setSubtreeChecked(Object, boolean)` is used instead.
Sets to the given value the checked state for all elements in this viewer. Does not fire events to check state listeners. Assumes that the element has been expanded before. To enforce that the item is expanded, call `expandToLevel` for the element.

Parameters:
  `state` - `true` if the element should be checked, and `false` if it should be unchecked
Since:
  3.2
See Also:
  `setSubtreeChecked(Object, boolean)`