Eclipse Platform
2.0

org.eclipse.ui.actions
Class SelectionListenerAction

java.lang.Object
  |
  +--org.eclipse.jface.action.Action
        |
        +--org.eclipse.ui.actions.SelectionListenerAction
All Implemented Interfaces:
IAction, ISelectionChangedListener
Direct Known Subclasses:
AddBookmarkAction, CopyProjectAction, CopyResourceAction, CreateFileAction, CreateFolderAction, DeleteResourceAction, ExportResourcesAction, ImportResourcesAction, OpenSystemEditorAction, WorkspaceAction

public abstract class SelectionListenerAction
extends Action
implements ISelectionChangedListener

The abstract superclass for actions that listen to selection change events. This implementation tracks the current selection (see getStructuredSelection) and provides a convenient place to monitor selection changes that could affect the availability of the action.

Subclasses must implement the following IAction method:

Subclasses may extend the updateSelection method to update the action determine its availability based on the current selection.

The object instantiating the subclass is responsible for registering the instance with a selection provider. Alternatively, the object can notify the subclass instance directly of a selection change using the methods:


Field Summary
 
Fields inherited from interface org.eclipse.jface.action.IAction
AS_CHECK_BOX, AS_DROP_DOWN_MENU, AS_PUSH_BUTTON, CHECKED, DESCRIPTION, ENABLED, IMAGE, TEXT, TOOL_TIP_TEXT
 
Constructor Summary
protected SelectionListenerAction(String text)
          Creates a new action with the given text.
 
Method Summary
protected  List getSelectedNonResources()
          Returns the elements in the current selection that are not IResources.
protected  List getSelectedResources()
          Returns the elements in the current selection that are IResources.
 IStructuredSelection getStructuredSelection()
          Returns the current structured selection in the workbench, or an empty selection if nothing is selected or if selection does not include objects (for example, raw text).
protected  boolean resourceIsType(IResource resource, int resourceMask)
          Returns whether the type of the given resource is among those in the given resource type mask.
 void selectionChanged(IStructuredSelection selection)
          Notifies this action that the given structured selection has changed.
 void selectionChanged(SelectionChangedEvent event)
          The SelectionListenerAction implementation of this ISelectionChangedListener method calls selectionChanged(IStructuredSelection) assuming the selection is a structured one.
protected  boolean selectionIsOfType(int resourceMask)
          Returns whether the current selection consists entirely of resources whose types are among those in the given resource type mask.
protected  boolean updateSelection(IStructuredSelection selection)
          Updates this action in response to the given selection.
 
Methods inherited from class org.eclipse.jface.action.Action
addPropertyChangeListener, convertAccelerator, convertAccelerator, findKeyCode, findKeyString, findModifier, findModifierString, firePropertyChange, firePropertyChange, getAccelerator, getActionDefinitionId, getDescription, getDisabledImageDescriptor, getHelpListener, getHoverImageDescriptor, getId, getImageDescriptor, getMenuCreator, getStyle, getText, getToolTipText, isChecked, isEnabled, removeAcceleratorText, removePropertyChangeListener, run, runWithEvent, setAccelerator, setActionDefinitionId, setChecked, setDescription, setDisabledImageDescriptor, setEnabled, setHelpListener, setHoverImageDescriptor, setId, setImageDescriptor, setMenuCreator, setText, setToolTipText
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SelectionListenerAction

protected SelectionListenerAction(String text)
Creates a new action with the given text.

Parameters:
text - the string used as the text for the action, or null if these is no text
Method Detail

getSelectedNonResources

protected List getSelectedNonResources()
Returns the elements in the current selection that are not IResources.

Returns:
list of elements (element type: Object)

getSelectedResources

protected List getSelectedResources()
Returns the elements in the current selection that are IResources.

Returns:
list of resource elements (element type: IResource)

getStructuredSelection

public IStructuredSelection getStructuredSelection()
Returns the current structured selection in the workbench, or an empty selection if nothing is selected or if selection does not include objects (for example, raw text).

Returns:
the current structured selection in the workbench

resourceIsType

protected boolean resourceIsType(IResource resource,
                                 int resourceMask)
Returns whether the type of the given resource is among those in the given resource type mask.

Parameters:
resource - the resource
resourceMask - a bitwise OR of resource types: IResource.{FILE, FOLDER, PROJECT, ROOT}
Returns:
true if the resource type matches, and false otherwise
See Also:
IResource

selectionChanged

public final void selectionChanged(IStructuredSelection selection)
Notifies this action that the given structured selection has changed.

The SelectionListenerAction implementation of this method records the given selection for future reference and calls updateSelection, updating the enable state of this action based on the outcome. Subclasses should override updateSelection to react to selection changes.

Parameters:
selection - the new selection

selectionChanged

public final void selectionChanged(SelectionChangedEvent event)
The SelectionListenerAction implementation of this ISelectionChangedListener method calls selectionChanged(IStructuredSelection) assuming the selection is a structured one. Subclasses should override the updateSelection method to react to selection changes.

Specified by:
selectionChanged in interface ISelectionChangedListener
Parameters:
event - event object describing the change

selectionIsOfType

protected boolean selectionIsOfType(int resourceMask)
Returns whether the current selection consists entirely of resources whose types are among those in the given resource type mask.

Parameters:
resourceMask - a bitwise OR of resource types: IResource.{FILE, FOLDER, PROJECT, ROOT}
Returns:
true if all resources in the current selection are of the specified types or if the current selection is empty, and false if some elements are resources of a different type or not resources
See Also:
IResource

updateSelection

protected boolean updateSelection(IStructuredSelection selection)
Updates this action in response to the given selection.

The SelectionListenerAction implementation of this method returns true. Subclasses may extend to react to selection changes; however, if the super method returns false, the overriding method must also return false.

Parameters:
selection - the new selection
Returns:
true if the action should be enabled for this selection, and false otherwise

Eclipse Platform
2.0

Copyright (c) IBM Corp. and others 2000, 2002. All Rights Reserved.