Class WorkspaceAction

All Implemented Interfaces:
IAction, ISelectionChangedListener
Direct Known Subclasses:
BuildAction, CloseResourceAction, OpenResourceAction, RefreshAction, RenameResourceAction, ScrubLocalAction

public abstract class WorkspaceAction extends SelectionListenerAction
The abstract superclass for actions which invoke commands implemented in org.eclipse.core.* on a set of selected resources. It iterates over all selected resources; errors are collected and displayed to the user via a problems dialog at the end of the operation. User requests to cancel the operation are passed along to the core.

Subclasses must implement the following methods:

  • invokeOperation - to perform the operation on one of the selected resources
  • getOperationMessage - to furnish a title for the progress dialog

Subclasses may override the following methods:

  • shouldPerformResourcePruning - reimplement to turn off
  • updateSelection - extend to refine enablement criteria
  • getProblemsTitle - reimplement to furnish a title for the problems dialog
  • getProblemsMessage - reimplement to furnish a message for the problems dialog
  • run - extend to
  • Constructor Details

    • WorkspaceAction

      @Deprecated protected WorkspaceAction(Shell shell, String text)
      Creates a new action with the given text.
      Parameters:
      shell - the shell (for the modal progress dialog and error messages)
      text - the string used as the text for the action, or null if there is no text
    • WorkspaceAction

      protected WorkspaceAction(IShellProvider provider, String text)
      Creates a new action with the given text.
      Parameters:
      provider - the shell provider (for the modal progress dialog and error messages)
      text - the string used as the text for the action, or null if there is no text
      Since:
      3.4
  • Method Details

    • getOperationMessage

      protected abstract String getOperationMessage()
      Returns the string to display for this action's operation.

      Note that this hook method is invoked in a non-UI thread.

      Subclasses must implement this method.

      Returns:
      the message
      Since:
      3.1
    • getProblemsMessage

      protected String getProblemsMessage()
      Returns the string to display for this action's problems dialog.

      The WorkspaceAction implementation of this method returns a vague message (localized counterpart of something like "The following problems occurred."). Subclasses may reimplement to provide something more suited to the particular action.

      Returns:
      the problems message
      Since:
      3.1
    • getProblemsTitle

      protected String getProblemsTitle()
      Returns the title for this action's problems dialog.

      The WorkspaceAction implementation of this method returns a generic title (localized counterpart of "Problems"). Subclasses may reimplement to provide something more suited to the particular action.

      Returns:
      the problems dialog title
      Since:
      3.1
    • invokeOperation

      @Deprecated protected void invokeOperation(IResource resource, IProgressMonitor monitor) throws CoreException
      Deprecated.
      Since 3.3, subclasses should instead implement the method createOperation(IStatus[]) and provide an empty implementation for this method.
      Performs this action's operation on each of the selected resources, reporting progress to, and fielding cancel requests from, the given progress monitor.

      Note that this method is invoked in a non-UI thread.

      Subclasses must implement this method.

      Parameters:
      resource - one of the selected resources
      monitor - a progress monitor
      Throws:
      CoreException - if the operation fails
      Since:
      3.1
    • run

      public void run()
      The CoreWrapperAction implementation of this IAction method uses a ProgressMonitorDialog to run the operation. The operation calls execute (which, in turn, calls invokeOperation). Afterwards, any CoreExceptions encountered while running the operation are reported to the user via a problems dialog.

      Subclasses may extend this method.

      Specified by:
      run in interface IAction
      Overrides:
      run in class Action
      See Also:
    • shouldPerformResourcePruning

      protected boolean shouldPerformResourcePruning()
      Returns whether this action should attempt to optimize the resources being operated on. This kind of pruning makes sense when the operation has depth infinity semantics (when the operation is applied explicitly to a resource then it is also applied implicitly to all the resource's descendents).

      The WorkspaceAction implementation of this method returns true. Subclasses should reimplement to return false if pruning is not required.

      Returns:
      true if pruning should be performed, and false if pruning is not desired
      Since:
      3.1
    • updateSelection

      protected boolean updateSelection(IStructuredSelection selection)
      The WorkspaceAction implementation of this SelectionListenerAction method ensures that this action is disabled if any of the selected resources are inaccessible. Subclasses may extend to react to selection changes; however, if the super method returns false, the overriding method should also return false.
      Overrides:
      updateSelection in class BaseSelectionListenerAction
      Parameters:
      selection - the new selection
      Returns:
      true if the action should be enabled for this selection, and false otherwise
    • getActionResources

      protected List<? extends IResource> getActionResources()
      Returns the elements that the action is to be performed on. By default return the selected resources.

      Subclasses may override this method.

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

      public void runInBackground(ISchedulingRule rule)
      Run the action in the background rather than with the progress dialog.
      Parameters:
      rule - The rule to apply to the background job or null if there isn't one.
    • runInBackground

      public void runInBackground(ISchedulingRule rule, Object jobFamily)
      Run the action in the background rather than with the progress dialog.
      Parameters:
      rule - The rule to apply to the background job or null if there isn't one.
      jobFamily - a single family that the job should belong to or null if none.
      Since:
      3.1
    • runInBackground

      public void runInBackground(ISchedulingRule rule, Object[] jobFamilies)
      Run the action in the background rather than with the progress dialog.
      Parameters:
      rule - The rule to apply to the background job or null if there isn't one.
      jobFamilies - the families the job should belong to or null if none.
      Since:
      3.1
    • createOperation

      protected IRunnableWithProgress createOperation(IStatus[] errorStatus)
      Returns the operation to perform when this action runs. The returned operation must be an IRunnableWithProgress that will perform the action's work. The default implementation returns an operation that will iterate over the selected resources and call invokeOperation(IResource, IProgressMonitor) for each resource. Subclasses must either implement invokeOperation(IResource, IProgressMonitor) or override this method to provide a different operation. Subclasses typically override this method when an undoable operation is to be provided.
      Parameters:
      errorStatus - an array of error status objects to which the result of running the operation should be added.
      Returns:
      the operation to perform when this action runs.
      Since:
      3.3