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
ConstructorsConstructorDescriptionTableViewer(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 ColumnViewerEditorCreates the viewer editor used for editing cell contents.protected voiddoClear(int index) Clears the item at the given zero-relative index in the receiver.protected voidClears all the items in the receiver.protected voidDeselects all selected items in the receiver.protected WidgetdoFindItem(Object element) Returns the widget in this viewer's control which represent the given element.protected WidgetdoGetColumn(int index) Returns the column at the given, zero-relative index in the receiver.protected intReturns the number of columns contained in the receiver.protected ItemdoGetItem(int index) Returns the item at the given, zero-relative index in the receiver.protected intReturns 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 ofItemthat are currently selected in the receiver.protected int[]Returns the zero-relative indices of the items which are currently selected in the receiver.protected intSearches 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 voiddoRemove(int[] indices) Removes the items from the receiver's list at the given zero-relative indices.protected voiddoRemove(int start, int end) Removes the items from the receiver which are between the given zero-relative start and end indices (inclusive).protected voidRemoves all of the items from the receiver.protected voiddoResetItem(Item item) Resets the given item in the receiver.protected voiddoSelect(int[] indices) Selects the items at the given zero-relative indices in the receiver.protected voiddoSetItemCount(int count) Sets the number of items contained in the receiver.protected voiddoSetSelection(int[] indices) Selects the items at the given zero-relative indices in the receiver.protected voiddoSetSelection(Item[] items) Sets the receiver's selection to be the given array of items.protected voiddoShowItem(Item item) Shows the item.protected voidShows the selection.Returns the primary control associated with this viewer.protected ItemReturns theItemat the given widget-relative coordinates, ornullif there is no item at the given coordinates.getTable()Returns this table viewer's table control.protected ViewerRowgetViewerRowFromItem(Widget item) Returns aViewerRowassociated with the given row widget.protected ViewerRowinternalCreateNewRowPart(int style, int rowIndex) Create a new row with style at indexvoidrefresh(boolean updateLabels, boolean reveal) Refreshes this viewer with information freshly obtained from this viewer's model.voidRefreshes this viewer starting with the given element.voidRemoves the given elements from this table viewer.voidsetSelection(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, setSelectionToWidgetMethods 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, updateSelectionMethods 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, usingElementMapMethods inherited from class org.eclipse.jface.viewers.ContentViewer
getContentProvider, getInput, labelProviderChangedMethods inherited from class org.eclipse.jface.viewers.Viewer
addHelpListener, addSelectionChangedListener, fireHelpRequested, fireSelectionChanged, getData, handleHelpRequest, removeHelpListener, removeSelectionChangedListener, scrollDown, scrollUp, setData, setSelectionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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:ViewerReturns the primary control associated with this viewer.- Specified by:
getControlin 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:ColumnViewerCreates the viewer editor used for editing cell contents. To be implemented by subclasses.- Specified by:
createViewerEditorin classColumnViewer- Returns:
- the editor, or
nullif 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:
setSelectionin classStructuredViewer- Parameters:
selection- the new selectionreveal-trueif the selection is to be made visible, andfalseotherwise- See Also:
-
getViewerRowFromItem
Description copied from class:ColumnViewerReturns aViewerRowassociated 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:
getViewerRowFromItemin classColumnViewer- Parameters:
item- the row widget- Returns:
- ViewerRow a viewer row object
-
internalCreateNewRowPart
Create a new row with style at index- Specified by:
internalCreateNewRowPartin classAbstractTableViewer- Parameters:
style- the row's stylerowIndex- index of the new row- Returns:
- ViewerRow
- Since:
- 3.3
-
getItemAt
Description copied from class:ColumnViewerReturns theItemat the given widget-relative coordinates, ornullif there is no item at the given coordinates.- Specified by:
getItemAtin classColumnViewer- Parameters:
p- the widget-relative coordinates- Returns:
- the
Itemat the coordinates ornullif there is no item at the given coordinates
-
doGetItemCount
protected int doGetItemCount()Description copied from class:AbstractTableViewerReturns the number of items contained in the receiver.- Specified by:
doGetItemCountin classAbstractTableViewer- Returns:
- the number of items
-
doIndexOf
Description copied from class:AbstractTableViewerSearches 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:
doIndexOfin classAbstractTableViewer- Parameters:
item- the search item- Returns:
- the index of the item
-
doSetItemCount
protected void doSetItemCount(int count) Description copied from class:AbstractTableViewerSets the number of items contained in the receiver.- Specified by:
doSetItemCountin classAbstractTableViewer- Parameters:
count- the number of items
-
doGetItems
Description copied from class:AbstractTableViewerReturns a (possibly empty) array of TableItems which are the items in the receiver.- Specified by:
doGetItemsin classAbstractTableViewer- Returns:
- the items in the receiver
-
doGetColumnCount
protected int doGetColumnCount()Description copied from class:ColumnViewerReturns 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:
doGetColumnCountin classColumnViewer- Returns:
- the number of columns
-
doGetColumn
Description copied from class:AbstractTableViewerReturns 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:
doGetColumnin classAbstractTableViewer- Parameters:
index- the index of the column to return- Returns:
- the column at the given index
-
doGetItem
Description copied from class:AbstractTableViewerReturns the item at the given, zero-relative index in the receiver. Throws an exception if the index is out of range.- Specified by:
doGetItemin classAbstractTableViewer- Parameters:
index- the index of the item to return- Returns:
- the item at the given index
-
doGetSelection
Description copied from class:AbstractTableViewerReturns an array ofItemthat are currently selected in the receiver. The order of the items is unspecified. An empty array indicates that no items are selected.- Specified by:
doGetSelectionin classAbstractTableViewer- Returns:
- an array representing the selection
-
doGetSelectionIndices
protected int[] doGetSelectionIndices()Description copied from class:AbstractTableViewerReturns 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:
doGetSelectionIndicesin classAbstractTableViewer- Returns:
- an array representing the selection
-
doClearAll
protected void doClearAll()Description copied from class:AbstractTableViewerClears 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.VIRTUALstyle, these attributes are requested again as needed.- Specified by:
doClearAllin classAbstractTableViewer
-
doResetItem
Description copied from class:AbstractTableViewerResets the given item in the receiver. The text, icon and other attributes of the item are set to their default values.- Specified by:
doResetItemin classAbstractTableViewer- Parameters:
item- the item to reset
-
doRemove
protected void doRemove(int start, int end) Description copied from class:AbstractTableViewerRemoves the items from the receiver which are between the given zero-relative start and end indices (inclusive).- Specified by:
doRemovein classAbstractTableViewer- Parameters:
start- the start of the rangeend- the end of the range
-
doRemoveAll
protected void doRemoveAll()Description copied from class:AbstractTableViewerRemoves all of the items from the receiver.- Specified by:
doRemoveAllin classAbstractTableViewer
-
doRemove
protected void doRemove(int[] indices) Description copied from class:AbstractTableViewerRemoves the items from the receiver's list at the given zero-relative indices.- Specified by:
doRemovein classAbstractTableViewer- Parameters:
indices- the array of indices of the items
-
doShowItem
Description copied from class:AbstractTableViewerShows 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:
doShowItemin classAbstractTableViewer- Parameters:
item- the item to be shown
-
doDeselectAll
protected void doDeselectAll()Description copied from class:AbstractTableViewerDeselects all selected items in the receiver.- Specified by:
doDeselectAllin classAbstractTableViewer
-
doSetSelection
Description copied from class:AbstractTableViewerSets 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:
doSetSelectionin classAbstractTableViewer- Parameters:
items- the array of items
-
doShowSelection
protected void doShowSelection()Description copied from class:AbstractTableViewerShows 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:
doShowSelectionin classAbstractTableViewer
-
doSetSelection
protected void doSetSelection(int[] indices) Description copied from class:AbstractTableViewerSelects 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:
doSetSelectionin classAbstractTableViewer- Parameters:
indices- the indices of the items to select
-
doClear
protected void doClear(int index) Description copied from class:AbstractTableViewerClears 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.VIRTUALstyle, these attributes are requested again as needed.- Specified by:
doClearin classAbstractTableViewer- Parameters:
index- the index of the item to clear- See Also:
-
doSelect
protected void doSelect(int[] indices) Description copied from class:AbstractTableViewerSelects 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:
doSelectin 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
updatemethods, 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 theupdatemethods.Subclasses who can provide this feature can open this method for the public
- Parameters:
element- the elementupdateLabels-trueto update labels for existing elements,falseto only update labels as needed, assuming that labels for existing elements are unchanged.reveal-trueto 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. IfupdateLabelsistruethen 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
updateLabelsis false. The intent is simply to allow optimization where possible.- Parameters:
updateLabels-trueto update labels for existing elements,falseto only update labels as needed, assuming that labels for existing elements are unchanged.reveal-trueto make the preserved selection visible afterwards- Since:
- 3.3
-
remove
Description copied from class:AbstractTableViewerRemoves 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:
removein classAbstractTableViewer- Parameters:
elements- the elements to remove
-
doFindItem
Description copied from class:StructuredViewerReturns 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:
doFindItemin classAbstractTableViewer- Parameters:
element- the element to find the representing widget for- Returns:
- the corresponding widget, or
nullif none
-