Class TableViewer
- All Implemented Interfaces:
IInputProvider
,IInputSelectionProvider
,IPostSelectionProvider
,ISelectionProvider
- Direct Known Subclasses:
CheckboxTableViewer
Table
control.
This class is not intended to be subclassed outside the viewer framework. It is designed to be instantiated with a pre-existing SWT table control and configured with a domain-specific content provider, table label provider, element filter (optional), and element sorter (optional).
Label providers for table viewers must implement either the
ITableLabelProvider
or the ILabelProvider
interface
(see TableViewer.setLabelProvider
for more details).
As of 3.1 the TableViewer now supports the SWT.VIRTUAL flag. If the
underlying table is SWT.VIRTUAL, the content provider may implement ILazyContentProvider
instead of IStructuredContentProvider
. Note
that in this case, the viewer does not support sorting or filtering. Also
note that in this case, the Widget based APIs may return null if the element
is not specified or not created yet.
Users of SWT.VIRTUAL should also avoid using getItems() from the Table within the TreeViewer as this does not necessarily generate a callback for the TreeViewer to populate the items. It also has the side effect of creating all of the items thereby eliminating the performance improvements of SWT.VIRTUAL.
Users setting up an editable table 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.Viewer
WIDGET_DATA_KEY
-
Constructor Summary
ConstructorDescriptionTableViewer
(Composite parent) Creates a table viewer on a newly-created table control under the given parent.TableViewer
(Composite parent, int style) Creates a table viewer on a newly-created table control under the given parent.TableViewer
(Table table) Creates a table viewer on the given table control. -
Method Summary
Modifier and TypeMethodDescriptionprotected ColumnViewerEditor
Creates the viewer editor used for editing cell contents.protected void
doClear
(int index) Clears the item at the given zero-relative index in the receiver.protected void
Clears all the items in the receiver.protected void
Deselects all selected items in the receiver.protected Widget
doFindItem
(Object element) Returns the widget in this viewer's control which represent the given element.protected Widget
doGetColumn
(int index) Returns the column at the given, zero-relative index in the receiver.protected int
Returns the number of columns contained in the receiver.protected Item
doGetItem
(int index) Returns the item at the given, zero-relative index in the receiver.protected int
Returns the number of items contained in the receiver.protected Item[]
Returns a (possibly empty) array of TableItems which are the items in the receiver.protected Item[]
Returns an array ofItem
that are currently selected in the receiver.protected int[]
Returns the zero-relative indices of the items which are currently selected in the receiver.protected int
Searches the receiver's list starting at the first item (index 0) until an item is found that is equal to the argument, and returns the index of that item.protected void
doRemove
(int[] indices) Removes the items from the receiver's list at the given zero-relative indices.protected void
doRemove
(int start, int end) Removes the items from the receiver which are between the given zero-relative start and end indices (inclusive).protected void
Removes all of the items from the receiver.protected void
doResetItem
(Item item) Resets the given item in the receiver.protected void
doSelect
(int[] indices) Selects the items at the given zero-relative indices in the receiver.protected void
doSetItemCount
(int count) Sets the number of items contained in the receiver.protected void
doSetSelection
(int[] indices) Selects the items at the given zero-relative indices in the receiver.protected void
doSetSelection
(Item[] items) Sets the receiver's selection to be the given array of items.protected void
doShowItem
(Item item) Shows the item.protected void
Shows the selection.Returns the primary control associated with this viewer.protected Item
Returns theItem
at the given widget-relative coordinates, ornull
if there is no item at the given coordinates.getTable()
Returns this table viewer's table control.protected ViewerRow
getViewerRowFromItem
(Widget item) Returns aViewerRow
associated with the given row widget.protected ViewerRow
internalCreateNewRowPart
(int style, int rowIndex) Create a new row with style at indexvoid
refresh
(boolean updateLabels, boolean reveal) Refreshes this viewer with information freshly obtained from this viewer's model.void
Refreshes this viewer starting with the given element.void
Removes the given elements from this table viewer.void
setSelection
(ISelection selection, boolean reveal) Sets a new selection for this viewer and optionally makes it visible.Methods inherited from class org.eclipse.jface.viewers.AbstractTableViewer
add, add, assertContentProviderType, clear, contains, doFindInputItem, doUpdateItem, getColumnViewerOwner, getElementAt, getLabelProvider, getRawChildren, getSelectionFromWidget, handleDispose, hookControl, indexForElement, inputChanged, insert, internalRefresh, internalRefresh, remove, replace, reveal, setContentProvider, setItemCount, setSelectionToWidget
Methods inherited from class org.eclipse.jface.viewers.ColumnViewer
applyEditorValue, cancelEditing, checkBusy, disassociate, editElement, firePostSelectionChanged, getCell, getCellEditors, getCellModifier, getColumnProperties, getColumnViewerEditor, getItem, getLabelProvider, getSortedChildren, getViewerRow, handleDoubleSelect, 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, associate, buildLabel, equals, filter, findItem, findItems, fireDoubleClick, fireOpen, getColorAndFontCollector, getComparator, getComparer, getFilteredChildren, getFilters, getRoot, getSelection, getSorter, getStructuredSelection, handleInvalidSelection, handleLabelProviderChanged, handleOpen, handlePostSelect, handleSelect, hasFilters, internalUpdate, mapElement, needsRefilter, preservingSelection, refresh, refresh, refreshItem, removeDoubleClickListener, removeFilter, removeOpenListener, removePostSelectionChangedListener, resetFilters, setComparator, setComparer, setFilters, setInput, setSelectionToWidget, setSorter, setUseHashlookup, testFindItem, testFindItems, unmapElement, unmapElement, update, updateItem, usingElementMap
Methods inherited from class org.eclipse.jface.viewers.ContentViewer
getContentProvider, getInput, labelProviderChanged
Methods inherited from class org.eclipse.jface.viewers.Viewer
addHelpListener, addSelectionChangedListener, fireHelpRequested, fireSelectionChanged, getData, handleHelpRequest, removeHelpListener, removeSelectionChangedListener, scrollDown, scrollUp, 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
-
TableViewer
Creates a table viewer on a newly-created table control under the given parent. The table 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. The table has no columns.- Parameters:
parent
- the parent control
-
TableViewer
Creates a table viewer on a newly-created table control under the given parent. The table control is created using the given style bits. The viewer has no input, no content provider, a default label provider, no sorter, and no filters. The table has no columns.- Parameters:
parent
- the parent controlstyle
- SWT style bits
-
TableViewer
Creates a table viewer on the given table control. The viewer has no input, no content provider, a default label provider, no sorter, and no filters.- Parameters:
table
- the table control
-
-
Method Details
-
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
-
getTable
Returns this table viewer's table control.- Returns:
- the table control
-
createViewerEditor
Description copied from class:ColumnViewer
Creates the viewer editor used for editing cell contents. To be implemented by subclasses.- Specified by:
createViewerEditor
in classColumnViewer
- Returns:
- the editor, or
null
if this viewer does not support editing cell contents.
-
setSelection
Sets a new selection for this viewer and optionally makes it visible. The TableViewer implementation of this method is inefficient for the ILazyContentProvider as lookup is done by indices rather than elements and may require population of the entire table in worse case.
Use Table#setSelection(int[] indices) and Table#showSelection() if you wish to set selection more efficiently when using a ILazyContentProvider.
- Overrides:
setSelection
in classStructuredViewer
- Parameters:
selection
- the new selectionreveal
-true
if the selection is to be made visible, andfalse
otherwise- See Also:
-
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.- Specified by:
getViewerRowFromItem
in classColumnViewer
- Parameters:
item
- the row widget- Returns:
- ViewerRow a viewer row object
-
internalCreateNewRowPart
Create a new row with style at index- Specified by:
internalCreateNewRowPart
in classAbstractTableViewer
- Parameters:
style
- the row's stylerowIndex
- index of the new row- Returns:
- ViewerRow
- Since:
- 3.3
-
getItemAt
Description copied from class:ColumnViewer
Returns theItem
at the given widget-relative coordinates, ornull
if there is no item at the given coordinates.- Specified by:
getItemAt
in classColumnViewer
- Parameters:
p
- the widget-relative coordinates- Returns:
- the
Item
at the coordinates ornull
if there is no item at the given coordinates
-
doGetItemCount
protected int doGetItemCount()Description copied from class:AbstractTableViewer
Returns the number of items contained in the receiver.- Specified by:
doGetItemCount
in classAbstractTableViewer
- Returns:
- the number of items
-
doIndexOf
Description copied from class:AbstractTableViewer
Searches the receiver's list starting at the first item (index 0) until an item is found that is equal to the argument, and returns the index of that item. If no item is found, returns -1.- Specified by:
doIndexOf
in classAbstractTableViewer
- Parameters:
item
- the search item- Returns:
- the index of the item
-
doSetItemCount
protected void doSetItemCount(int count) Description copied from class:AbstractTableViewer
Sets the number of items contained in the receiver.- Specified by:
doSetItemCount
in classAbstractTableViewer
- Parameters:
count
- the number of items
-
doGetItems
Description copied from class:AbstractTableViewer
Returns a (possibly empty) array of TableItems which are the items in the receiver.- Specified by:
doGetItems
in classAbstractTableViewer
- Returns:
- the items in the receiver
-
doGetColumnCount
protected int doGetColumnCount()Description copied from class:ColumnViewer
Returns the number of columns contained in the receiver. If no columns were created by the programmer, this value is zero, despite the fact that visually, one column of items may be visible. This occurs when the programmer uses the column viewer like a list, adding elements but never creating a column.- Specified by:
doGetColumnCount
in classColumnViewer
- Returns:
- the number of columns
-
doGetColumn
Description copied from class:AbstractTableViewer
Returns the column at the given, zero-relative index in the receiver. Throws an exception if the index is out of range. Columns are returned in the order that they were created. If no TableColumns were created by the programmer, this method will throw ERROR_INVALID_RANGE despite the fact that a single column of data may be visible in the table. This occurs when the programmer uses the table like a list, adding items but never creating a column.- Specified by:
doGetColumn
in classAbstractTableViewer
- Parameters:
index
- the index of the column to return- Returns:
- the column at the given index
-
doGetItem
Description copied from class:AbstractTableViewer
Returns the item at the given, zero-relative index in the receiver. Throws an exception if the index is out of range.- Specified by:
doGetItem
in classAbstractTableViewer
- Parameters:
index
- the index of the item to return- Returns:
- the item at the given index
-
doGetSelection
Description copied from class:AbstractTableViewer
Returns an array ofItem
that are currently selected in the receiver. The order of the items is unspecified. An empty array indicates that no items are selected.- Specified by:
doGetSelection
in classAbstractTableViewer
- Returns:
- an array representing the selection
-
doGetSelectionIndices
protected int[] doGetSelectionIndices()Description copied from class:AbstractTableViewer
Returns the zero-relative indices of the items which are currently selected in the receiver. The order of the indices is unspecified. The array is empty if no items are selected.- Specified by:
doGetSelectionIndices
in classAbstractTableViewer
- Returns:
- an array representing the selection
-
doClearAll
protected void doClearAll()Description copied from class:AbstractTableViewer
Clears all the items in the receiver. The text, icon and other attributes of the items are set to their default values. If the table was created with theSWT.VIRTUAL
style, these attributes are requested again as needed.- Specified by:
doClearAll
in classAbstractTableViewer
-
doResetItem
Description copied from class:AbstractTableViewer
Resets the given item in the receiver. The text, icon and other attributes of the item are set to their default values.- Specified by:
doResetItem
in classAbstractTableViewer
- Parameters:
item
- the item to reset
-
doRemove
protected void doRemove(int start, int end) Description copied from class:AbstractTableViewer
Removes the items from the receiver which are between the given zero-relative start and end indices (inclusive).- Specified by:
doRemove
in classAbstractTableViewer
- Parameters:
start
- the start of the rangeend
- the end of the range
-
doRemoveAll
protected void doRemoveAll()Description copied from class:AbstractTableViewer
Removes all of the items from the receiver.- Specified by:
doRemoveAll
in classAbstractTableViewer
-
doRemove
protected void doRemove(int[] indices) Description copied from class:AbstractTableViewer
Removes the items from the receiver's list at the given zero-relative indices.- Specified by:
doRemove
in classAbstractTableViewer
- Parameters:
indices
- the array of indices of the items
-
doShowItem
Description copied from class:AbstractTableViewer
Shows the item. If the item is already showing in the receiver, this method simply returns. Otherwise, the items are scrolled until the item is visible.- Specified by:
doShowItem
in classAbstractTableViewer
- Parameters:
item
- the item to be shown
-
doDeselectAll
protected void doDeselectAll()Description copied from class:AbstractTableViewer
Deselects all selected items in the receiver.- Specified by:
doDeselectAll
in classAbstractTableViewer
-
doSetSelection
Description copied from class:AbstractTableViewer
Sets the receiver's selection to be the given array of items. The current selection is cleared before the new items are selected, and if necessary the receiver is scrolled to make the new selection visible.Items that are not in the receiver are ignored. If the receiver is single-select and multiple items are specified, then all items are ignored.
- Specified by:
doSetSelection
in classAbstractTableViewer
- Parameters:
items
- the array of items
-
doShowSelection
protected void doShowSelection()Description copied from class:AbstractTableViewer
Shows the selection. If the selection is already showing in the receiver, this method simply returns. Otherwise, the items are scrolled until the selection is visible.- Specified by:
doShowSelection
in classAbstractTableViewer
-
doSetSelection
protected void doSetSelection(int[] indices) Description copied from class:AbstractTableViewer
Selects the items at the given zero-relative indices in the receiver. The current selection is cleared before the new items are selected, and if necessary the receiver is scrolled to make the new selection visible.Indices that are out of range and duplicate indices are ignored. If the receiver is single-select and multiple indices are specified, then all indices are ignored.
- Specified by:
doSetSelection
in classAbstractTableViewer
- Parameters:
indices
- the indices of the items to select
-
doClear
protected void doClear(int index) Description copied from class:AbstractTableViewer
Clears the item at the given zero-relative index in the receiver. The text, icon and other attributes of the item are set to the default value. If the table was created with theSWT.VIRTUAL
style, these attributes are requested again as needed.- Specified by:
doClear
in classAbstractTableViewer
- Parameters:
index
- the index of the item to clear- See Also:
-
doSelect
protected void doSelect(int[] indices) Description copied from class:AbstractTableViewer
Selects the items at the given zero-relative indices in the receiver. The current selection is not cleared before the new items are selected.If the item at a given index is not selected, it is selected. If the item at a given index was already selected, it remains selected. Indices that are out of range and duplicate indices are ignored. If the receiver is single-select and multiple indices are specified, then all indices are ignored.
- Specified by:
doSelect
in classAbstractTableViewer
- Parameters:
indices
- the array of indices for the items to select
-
refresh
Refreshes this viewer starting with the given element. Labels are updated as described inrefresh(boolean updateLabels)
. The methods attempts to preserve the selection.Unlike the
update
methods, this handles structural changes to the given element (e.g. addition or removal of children). If only the given element needs updating, it is more efficient to use theupdate
methods.Subclasses who can provide this feature can open this method for the public
- Parameters:
element
- the elementupdateLabels
-true
to update labels for existing elements,false
to only update labels as needed, assuming that labels for existing elements are unchanged.reveal
-true
to make the preserved selection visible afterwards- Since:
- 3.3
-
refresh
public void refresh(boolean updateLabels, boolean reveal) Refreshes this viewer with information freshly obtained from this viewer's model. IfupdateLabels
istrue
then labels for otherwise unaffected elements are updated as well. Otherwise, it assumes labels for existing elements are unchanged, and labels are only obtained as needed (for example, for new elements).Calling
refresh(true)
has the same effect asrefresh()
.Note that the implementation may still obtain labels for existing elements even if
updateLabels
is false. The intent is simply to allow optimization where possible.- Parameters:
updateLabels
-true
to update labels for existing elements,false
to only update labels as needed, assuming that labels for existing elements are unchanged.reveal
-true
to make the preserved selection visible afterwards- Since:
- 3.3
-
remove
Description copied from class:AbstractTableViewer
Removes the given elements from this table viewer. The selection is updated if required.This method should be called (by the content provider) when elements have been removed from the model, in order to cause the viewer to accurately reflect the model. This method only affects the viewer, not the model.
- Overrides:
remove
in classAbstractTableViewer
- Parameters:
elements
- the elements to remove
-
doFindItem
Description copied from class:StructuredViewer
Returns the widget in this viewer's control which represent the given element. This method searches all the children of the input element.This method is internal to the framework; subclassers should not call this method.
- Overrides:
doFindItem
in classAbstractTableViewer
- Parameters:
element
- the element to find the representing widget for- Returns:
- the corresponding widget, or
null
if none
-