Class Viewer
- java.lang.Object
-
- org.eclipse.jface.viewers.Viewer
-
- All Implemented Interfaces:
IInputProvider
,IInputSelectionProvider
,ISelectionProvider
- Direct Known Subclasses:
ContentViewer
,TextViewer
public abstract class Viewer extends Object implements IInputSelectionProvider
A viewer is a model-based adapter on a widget.A viewer can be created as an adapter on a pre-existing control (e.g., creating a
ListViewer
on an existingList
control). All viewers also provide a convenience constructor for creating the control.Implementing a concrete viewer typically involves the following steps:
- create SWT controls for viewer (in constructor) (optional)
- initialize SWT controls from input (inputChanged)
- define viewer-specific update methods
- support selections (
setSelection
,getSelection
)
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
WIDGET_DATA_KEY
Unique key for associating element data with widgets.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Viewer()
Creates a new viewer.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addHelpListener(HelpListener listener)
Adds a listener for help requests in this viewer.void
addSelectionChangedListener(ISelectionChangedListener listener)
Adds a listener for selection changes in this selection provider.protected void
fireHelpRequested(HelpEvent event)
Notifies any help listeners that help has been requested.protected void
fireSelectionChanged(SelectionChangedEvent event)
Notifies any selection changed listeners that the viewer's selection has changed.abstract Control
getControl()
Returns the primary control associated with this viewer.Object
getData(String key)
Returns the value of the property with the given name, ornull
if the property is not found.abstract Object
getInput()
Returns the input.abstract ISelection
getSelection()
Returns the current selection for this provider.protected void
handleHelpRequest(HelpEvent event)
Handles a help request from the underlying SWT control.protected void
inputChanged(Object input, Object oldInput)
Internal hook method called when the input to this viewer is initially set or subsequently changed.abstract void
refresh()
Refreshes this viewer completely with information freshly obtained from this viewer's model.void
removeHelpListener(HelpListener listener)
Removes the given help listener from this viewer.void
removeSelectionChangedListener(ISelectionChangedListener listener)
Removes the given selection change listener from this selection provider.Item
scrollDown(int x, int y)
Scrolls the viewer's control down by one item from the given display-relative coordinates.Item
scrollUp(int x, int y)
Scrolls the viewer's control up by one item from the given display-relative coordinates.void
setData(String key, Object value)
Sets the value of the property with the given name to the given value, or tonull
if the property is to be removed.abstract void
setInput(Object input)
Sets or clears the input for this viewer.void
setSelection(ISelection selection)
The viewer implementation of thisISelectionProvider
method make the new selection for this viewer without making it visible.abstract void
setSelection(ISelection selection, boolean reveal)
Sets a new selection for this viewer and optionally makes it visible.
-
-
-
Field Detail
-
WIDGET_DATA_KEY
protected static final String WIDGET_DATA_KEY
Unique key for associating element data with widgets.
-
-
Method Detail
-
addHelpListener
public void addHelpListener(HelpListener listener)
Adds a listener for help requests in this viewer. Has no effect if an identical listener is already registered.- Parameters:
listener
- a help listener
-
addSelectionChangedListener
public void addSelectionChangedListener(ISelectionChangedListener listener)
Description copied from interface:ISelectionProvider
Adds a listener for selection changes in this selection provider. Has no effect if an identical listener is already registered.- Specified by:
addSelectionChangedListener
in interfaceISelectionProvider
- Parameters:
listener
- a selection changed listener
-
fireHelpRequested
protected void fireHelpRequested(HelpEvent event)
Notifies any help listeners that help has been requested. Only listeners registered at the time this method is called are notified.- Parameters:
event
- a help event- See Also:
HelpListener.helpRequested(org.eclipse.swt.events.HelpEvent)
-
fireSelectionChanged
protected void fireSelectionChanged(SelectionChangedEvent event)
Notifies any selection changed listeners that the viewer's selection has changed. Only listeners registered at the time this method is called are notified.- Parameters:
event
- a selection changed event- See Also:
ISelectionChangedListener.selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
-
getControl
public abstract Control getControl()
Returns the primary control associated with this viewer.- Returns:
- the SWT control which displays this viewer's content
-
getData
public Object getData(String key)
Returns the value of the property with the given name, ornull
if the property is not found.The default implementation performs a (linear) search of an internal table. Overriding this method is generally not required if the number of different keys is small. If a more efficient representation of a viewer's properties is required, override both
getData
andsetData
.- Parameters:
key
- the property name- Returns:
- the property value, or
null
if the property is not found
-
getInput
public abstract Object getInput()
Description copied from interface:IInputProvider
Returns the input.- Specified by:
getInput
in interfaceIInputProvider
- Returns:
- the input object
-
getSelection
public abstract ISelection getSelection()
Description copied from interface:ISelectionProvider
Returns the current selection for this provider.- Specified by:
getSelection
in interfaceISelectionProvider
- Returns:
- the current selection
-
handleHelpRequest
protected void handleHelpRequest(HelpEvent event)
Handles a help request from the underlying SWT control. The default behavior is to fire a help request, with the event's data modified to hold this viewer.- Parameters:
event
- the event
-
inputChanged
protected void inputChanged(Object input, Object oldInput)
Internal hook method called when the input to this viewer is initially set or subsequently changed.The default implementation does nothing. Subclassers may override this method to do something when a viewer's input is set. A typical use is populate the viewer.
- Parameters:
input
- the new input of this viewer, ornull
if noneoldInput
- the old input element ornull
if there was previously no input
-
refresh
public abstract void refresh()
Refreshes this viewer completely with information freshly obtained from this viewer's model.
-
removeHelpListener
public void removeHelpListener(HelpListener listener)
Removes the given help listener from this viewer. Has no effect if an identical listener is not registered.- Parameters:
listener
- a help listener
-
removeSelectionChangedListener
public void removeSelectionChangedListener(ISelectionChangedListener listener)
Description copied from interface:ISelectionProvider
Removes the given selection change listener from this selection provider. Has no effect if an identical listener is not registered.- Specified by:
removeSelectionChangedListener
in interfaceISelectionProvider
- Parameters:
listener
- a selection changed listener
-
scrollDown
public Item scrollDown(int x, int y)
Scrolls the viewer's control down by one item from the given display-relative coordinates. Returns the newly revealed Item, ornull
if no scrolling occurred or if the viewer doesn't represent an item-based widget.- Parameters:
x
- horizontal coordinatey
- vertical coordinate- Returns:
- the item scrolled down to
-
scrollUp
public Item scrollUp(int x, int y)
Scrolls the viewer's control up by one item from the given display-relative coordinates. Returns the newly revealed Item, ornull
if no scrolling occurred or if the viewer doesn't represent an item-based widget.- Parameters:
x
- horizontal coordinatey
- vertical coordinate- Returns:
- the item scrolled up to
-
setData
public void setData(String key, Object value)
Sets the value of the property with the given name to the given value, or tonull
if the property is to be removed. If this viewer has such a property, its value is replaced; otherwise a new property is added.The default implementation records properties in an internal table which is searched linearly. Overriding this method is generally not required if the number of different keys is small. If a more efficient representation of a viewer's properties is required, override both
getData
andsetData
.- Parameters:
key
- the property namevalue
- the property value, ornull
if the property is not found
-
setInput
public abstract void setInput(Object input)
Sets or clears the input for this viewer.- Parameters:
input
- the input of this viewer, ornull
if none
-
setSelection
public void setSelection(ISelection selection)
The viewer implementation of thisISelectionProvider
method make the new selection for this viewer without making it visible.This method is equivalent to
setSelection(selection,false)
.Note that some implementations may not be able to set the selection without also revealing it, for example (as of 3.3) TreeViewer.
- Specified by:
setSelection
in interfaceISelectionProvider
- Parameters:
selection
- the new selection
-
setSelection
public abstract void setSelection(ISelection selection, boolean reveal)
Sets a new selection for this viewer and optionally makes it visible.Subclasses must implement this method.
- Parameters:
selection
- the new selectionreveal
-true
if the selection is to be made visible, andfalse
otherwise
-
-