Class AbstractWorkspaceOperation
- java.lang.Object
-
- org.eclipse.core.commands.operations.AbstractOperation
-
- org.eclipse.ui.ide.undo.AbstractWorkspaceOperation
-
- All Implemented Interfaces:
IAdvancedUndoableOperation,IAdvancedUndoableOperation2,IUndoableOperation
- Direct Known Subclasses:
CopyProjectOperation,CopyResourcesOperation,CreateFileOperation,CreateFolderOperation,CreateMarkersOperation,CreateProjectOperation,DeleteMarkersOperation,DeleteResourcesOperation,MoveProjectOperation,MoveResourcesOperation,UpdateMarkersOperation
public abstract class AbstractWorkspaceOperation extends AbstractOperation implements IAdvancedUndoableOperation, IAdvancedUndoableOperation2
An AbstractWorkspaceOperation represents an undoable operation that affects the workspace. It handles common workspace operation activities such as tracking which resources are affected by an operation, prompting the user when there are possible side effects of operations, building execution exceptions from core exceptions, etc. Clients may call the public API from a background thread.This class is not intended to be subclassed by clients.
- Since:
- 3.3
- Restriction:
- This class is not intended to be subclassed by clients.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaboutToNotify(OperationHistoryEvent event)An operation history notification about this operation is about to be sent to operation history listeners.protected voidappendDescriptiveText(StringBuffer text)Append any descriptive text to the specified string buffer to be shown in the receiver'stoString()text.booleancanExecute()Returns whether the operation can be executed in its current state.booleancanRedo()Returns whether the operation can be redone in its current state.booleancanUndo()Returns whether the operation can be undone in its current state.IStatuscomputeExecutionStatus(IProgressMonitor monitor)Return a status indicating the projected outcome of executing the receiver.IStatuscomputeRedoableStatus(IProgressMonitor monitor)Return a status indicating the projected outcome of redoing the receiver.IStatuscomputeUndoableStatus(IProgressMonitor monitor)Return a status indicating the projected outcome of undoing the receiver.protected abstract voiddoExecute(IProgressMonitor monitor, IAdaptable uiInfo)Perform the specific work involved in executing this operation.protected abstract voiddoUndo(IProgressMonitor monitor, IAdaptable uiInfo)Perform the specific work involved in undoing this operation.IStatusexecute(IProgressMonitor monitor, IAdaptable uiInfo)Execute the specified operation.Object[]getAffectedObjects()Return an array of objects that are affected by executing, undoing, or redoing this operation.protected IStatusgetErrorStatus(String message)Return an error status describing an invalid operation using the provided message.protected ISchedulingRulegetExecuteSchedulingRule()Return a scheduling rule appropriate for executing this operation.protected ISchedulingRulegetRedoSchedulingRule()Return a scheduling rule appropriate for redoing this operation.protected ShellgetShell(IAdaptable uiInfo)Return the shell described by the specified adaptable, or the active shell if no shell has been specified in the adaptable.protected ISchedulingRulegetUndoSchedulingRule()Return a scheduling rule appropriate for undoing this operation.protected IStatusgetWarningStatus(String message, int code)Return a warning status describing the warning state of an operation using the provided message and code.protected IWorkspacegetWorkspace()Return the workspace manipulated by this operation.protected IResourceRuleFactorygetWorkspaceRuleFactory()Return the workspace rule factory associated with this operation.protected booleanisValid()Return whether the proposed operation is valid.protected voidmarkInvalid()Mark this operation invalid due to some external change.IStatusredo(IProgressMonitor monitor, IAdaptable uiInfo)Redo the specified operation.protected booleanresourcesExist()Return whether the resources known by this operation currently exist.protected booleanresourcesIncludesProjects()Return whether the resources known by this operation contain any projects.booleanrunInBackground()Return a boolean that instructs whether the operation should be executed, undone, or redone in a background thread.voidsetModelProviderIds(String[] ids)Set the ids of any model providers for the resources involved.voidsetQuietCompute(boolean quiet)Set a boolean that instructs whether the computation of the receiver's execution, undo, or redo status should quietly compute status without consulting or prompting the user.protected voidsetTargetResources(IResource[] resources)Set the resources which are affected by this operationStringtoString()The string representation of this operation.IStatusundo(IProgressMonitor monitor, IAdaptable uiInfo)Undo the specified operation.protected booleanupdateResourceChangeDescriptionFactory(IResourceChangeDescriptionFactory factory, int operation)Update the provided resource change description factory so it can generate a resource delta describing the result of an undo or redo.-
Methods inherited from class org.eclipse.core.commands.operations.AbstractOperation
addContext, dispose, getContexts, getLabel, hasContext, removeContext, setLabel
-
-
-
-
Field Detail
-
EXECUTE
protected static int EXECUTE
-
UNDO
protected static int UNDO
-
REDO
protected static int REDO
-
resources
protected IResource[] resources
-
quietCompute
protected boolean quietCompute
-
-
Method Detail
-
setModelProviderIds
public void setModelProviderIds(String[] ids)
Set the ids of any model providers for the resources involved.- Parameters:
ids- the array of String model provider ids that provide models associated with the resources involved in this operation
-
setTargetResources
protected void setTargetResources(IResource[] resources)
Set the resources which are affected by this operation- Parameters:
resources- an array of resources
-
getWorkspace
protected IWorkspace getWorkspace()
Return the workspace manipulated by this operation.- Returns:
- the IWorkspace used by this operation.
-
getWorkspaceRuleFactory
protected IResourceRuleFactory getWorkspaceRuleFactory()
Return the workspace rule factory associated with this operation.- Returns:
- the IResourceRuleFactory associated with this operation.
-
markInvalid
protected void markInvalid()
Mark this operation invalid due to some external change. May be used by subclasses.
-
canExecute
public boolean canExecute()
Description copied from interface:IUndoableOperationReturns whether the operation can be executed in its current state.
Note: The computation for this method must be fast, as it is called frequently. If necessary, this method can be optimistic in its computation (returning true) and later perform more time-consuming computations during the actual execution of the operation, returning the appropriate status if the operation cannot actually execute at that time.
- Specified by:
canExecutein interfaceIUndoableOperation- Overrides:
canExecutein classAbstractOperation- Returns:
trueif the operation can be executed;falseotherwise.
-
canUndo
public boolean canUndo()
Description copied from interface:IUndoableOperationReturns whether the operation can be undone in its current state.
Note: The computation for this method must be fast, as it is called frequently. If necessary, this method can be optimistic in its computation (returning true) and later perform more time-consuming computations during the actual undo of the operation, returning the appropriate status if the operation cannot actually be undone at that time.
- Specified by:
canUndoin interfaceIUndoableOperation- Overrides:
canUndoin classAbstractOperation- Returns:
trueif the operation can be undone;falseotherwise.
-
canRedo
public boolean canRedo()
Description copied from interface:IUndoableOperationReturns whether the operation can be redone in its current state.
Note: The computation for this method must be fast, as it is called frequently. If necessary, this method can be optimistic in its computation (returning true) and later perform more time-consuming computations during the actual redo of the operation, returning the appropriate status if the operation cannot actually be redone at that time.
- Specified by:
canRedoin interfaceIUndoableOperation- Overrides:
canRedoin classAbstractOperation- Returns:
trueif the operation can be redone;falseotherwise.
-
execute
public IStatus execute(IProgressMonitor monitor, IAdaptable uiInfo) throws ExecutionException
Execute the specified operation. This implementation executes the operation in a workspace runnable and catches any CoreExceptions resulting from the operation. Unhandled CoreExceptions are propagated as ExecutionExceptions.- Specified by:
executein interfaceIUndoableOperation- Specified by:
executein classAbstractOperation- Parameters:
monitor- the progress monitor to use for the operationuiInfo- the IAdaptable (ornull) provided by the caller in order to supply UI information for prompting the user if necessary. When this parameter is notnull, it contains an adapter for the org.eclipse.swt.widgets.Shell.class- Returns:
- the IStatus of the execution. The status severity should be set
to
OKif the operation was successful, andERRORif it was not. Any other status is assumed to represent an incompletion of the execution. - Throws:
ExecutionException- if an exception occurred during execution.- See Also:
IUndoableOperation.execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
-
redo
public IStatus redo(IProgressMonitor monitor, IAdaptable uiInfo) throws ExecutionException
Redo the specified operation. This implementation redoes the operation in a workspace runnable and catches any CoreExceptions resulting from the operation. Unhandled CoreExceptions are propagated as ExecutionExceptions.- Specified by:
redoin interfaceIUndoableOperation- Specified by:
redoin classAbstractOperation- Parameters:
monitor- the progress monitor to use for the operationuiInfo- the IAdaptable (ornull) provided by the caller in order to supply UI information for prompting the user if necessary. When this parameter is notnull, it contains an adapter for the org.eclipse.swt.widgets.Shell.class- Returns:
- the IStatus of the redo. The status severity should be set to
OKif the operation was successful, andERRORif it was not. Any other status is assumed to represent an incompletion of the redo. - Throws:
ExecutionException- if an exception occurred during execution.- See Also:
IUndoableOperation.redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
-
undo
public IStatus undo(IProgressMonitor monitor, IAdaptable uiInfo) throws ExecutionException
Undo the specified operation. This implementation undoes the operation in a workspace runnable and catches any CoreExceptions resulting from the operation. Unhandled CoreExceptions are propagated as ExecutionExceptions.- Specified by:
undoin interfaceIUndoableOperation- Specified by:
undoin classAbstractOperation- Parameters:
monitor- the progress monitor to use for the operationuiInfo- the IAdaptable (ornull) provided by the caller in order to supply UI information for prompting the user if necessary. When this parameter is notnull, it contains an adapter for the org.eclipse.swt.widgets.Shell.class- Returns:
- the IStatus of the undo. The status severity should be set to
OKif the operation was successful, andERRORif it was not. Any other status is assumed to represent an incompletion of the undo. * - Throws:
ExecutionException- if an exception occurred during execution.- See Also:
IUndoableOperation.undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
-
doUndo
protected abstract void doUndo(IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException
Perform the specific work involved in undoing this operation.- Parameters:
monitor- the progress monitor to use for the operationuiInfo- the IAdaptable (ornull) provided by the caller in order to supply UI information for prompting the user if necessary. When this parameter is notnull, it contains an adapter for the org.eclipse.swt.widgets.Shell.class- Throws:
CoreException- propagates any CoreExceptions thrown from the resources API
-
doExecute
protected abstract void doExecute(IProgressMonitor monitor, IAdaptable uiInfo) throws CoreException
Perform the specific work involved in executing this operation.- Parameters:
monitor- the progress monitor to use for the operationuiInfo- the IAdaptable (ornull) provided by the caller in order to supply UI information for prompting the user if necessary. When this parameter is notnull, it contains an adapter for the org.eclipse.swt.widgets.Shell.class- Throws:
CoreException- propagates any CoreExceptions thrown from the resources API
-
isValid
protected boolean isValid()
Return whether the proposed operation is valid. The default implementation simply checks to see if the flag has been marked as invalid, relying on subclasses to mark the flag invalid when appropriate.- Returns:
- the validity flag
-
aboutToNotify
public void aboutToNotify(OperationHistoryEvent event)
Description copied from interface:IAdvancedUndoableOperationAn operation history notification about this operation is about to be sent to operation history listeners. Any preparation needed before listeners are notified about this operation should be performed here.
This method has been added to support legacy undo frameworks that are adapting to IUndoableOperation. Operations that previously relied on notification from their containing history or stack before any listeners are notified about changes to the operation should implement this interface.
- Specified by:
aboutToNotifyin interfaceIAdvancedUndoableOperation- Parameters:
event- the event that is about to be sent with the pending notification
-
getAffectedObjects
public Object[] getAffectedObjects()
Description copied from interface:IAdvancedUndoableOperationReturn an array of objects that are affected by executing, undoing, or redoing this operation. If it cannot be determined which objects are affected, return null.
- Specified by:
getAffectedObjectsin interfaceIAdvancedUndoableOperation- Returns:
- the array of Objects modified by this operation, or
nullif the affected objects cannot be determined.
-
computeExecutionStatus
public IStatus computeExecutionStatus(IProgressMonitor monitor)
Return a status indicating the projected outcome of executing the receiver. This method is not called by the operation history, but instead is used by clients (such as implementers ofIOperationApprover2) who wish to perform advanced validation of an operation before attempting to execute it. If an ERROR status is returned, the operation will not proceed and the user notified if deemed necessary by the caller. The validity flag on the operation should be marked as invalid. If an OK status is returned, the operation will proceed. The caller must interpret any other returned status severity, and may choose to prompt the user as to how to proceed. If there are multiple conditions that result in an ambiguous status severity, it is best for the implementor of this method to consult the user as to how to proceed for each one, and return an OK or ERROR status that accurately reflects the user's wishes, or to return a multi-status that accurately describes all of the issues at hand, so that the caller may potentially consult the user. (Note that the user should not be consulted at all if a client has calledsetQuietCompute(boolean)with a value oftrue.) This implementation computes the validity of execution by computing the resource delta that would be generated on execution, and checking whether any registered model providers are affected by the operation.- Specified by:
computeExecutionStatusin interfaceIAdvancedUndoableOperation2- Parameters:
monitor- the progress monitor to be used for computing the status- Returns:
- the status indicating the projected outcome of executing the receiver
- See Also:
IAdvancedUndoableOperation.computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor),setQuietCompute(boolean)
-
computeUndoableStatus
public IStatus computeUndoableStatus(IProgressMonitor monitor)
Return a status indicating the projected outcome of undoing the receiver. This method is not called by the operation history, but instead is used by clients (such as implementers ofIOperationApprover2) who wish to perform advanced validation of an operation before attempting to undo it. If an ERROR status is returned, the undo will not proceed and the user notified if deemed necessary by the caller. The validity flag on the operation should be marked as invalid. If an OK status is returned, the undo will proceed. The caller must interpret any other returned status severity, and may choose to prompt the user as to how to proceed. If there are multiple conditions that result in an ambiguous status severity, it is best for the implementor of this method to consult the user as to how to proceed for each one, and return an OK or ERROR status that accurately reflects the user's wishes, or to return a multi-status that accurately describes all of the issues at hand, so that the caller may potentially consult the user. (Note that the user should not be consulted at all if a client has calledsetQuietCompute(boolean)with a value oftrue.) This implementation computes the validity of undo by computing the resource delta that would be generated on undo, and checking whether any registered model providers are affected by the operation.- Specified by:
computeUndoableStatusin interfaceIAdvancedUndoableOperation- Parameters:
monitor- the progress monitor to be used for computing the status- Returns:
- the status indicating the projected outcome of undoing the receiver
- See Also:
IAdvancedUndoableOperation.computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor),setQuietCompute(boolean)
-
computeRedoableStatus
public IStatus computeRedoableStatus(IProgressMonitor monitor)
Return a status indicating the projected outcome of redoing the receiver. This method is not called by the operation history, but instead is used by clients (such as implementers ofIOperationApprover2) who wish to perform advanced validation of an operation before attempting to redo it. If an ERROR status is returned, the redo will not proceed and the user notified if deemed necessary by the caller. The validity flag on the operation should be marked as invalid. If an OK status is returned, the redo will proceed. The caller must interpret any other returned status severity, and may choose to prompt the user as to how to proceed. If there are multiple conditions that result in an ambiguous status severity, it is best for the implementor of this method to consult the user as to how to proceed for each one, and return an OK or ERROR status that accurately reflects the user's wishes, or to return a multi-status that accurately describes all of the issues at hand, so that the caller may potentially consult the user. (Note that the user should not be consulted at all if a client has calledsetQuietCompute(boolean)with a value oftrue.) This implementation computes the validity of redo by computing the resource delta that would be generated on redo, and checking whether any registered model providers are affected by the operation.- Specified by:
computeRedoableStatusin interfaceIAdvancedUndoableOperation- Parameters:
monitor- the progress monitor to be used for computing the status- Returns:
- the status indicating the projected outcome of redoing the receiver
- See Also:
IAdvancedUndoableOperation.computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor),setQuietCompute(boolean)
-
updateResourceChangeDescriptionFactory
protected boolean updateResourceChangeDescriptionFactory(IResourceChangeDescriptionFactory factory, int operation)
Update the provided resource change description factory so it can generate a resource delta describing the result of an undo or redo. Return a boolean indicating whether any update was done. The default implementation does not update the factory. Subclasses are expected to override this method to more specifically describe their modifications to the workspace.- Parameters:
factory- the factory to updateoperation- an integer indicating whether the change is part of an execute, undo, or redo- Returns:
- a boolean indicating whether the factory was updated.
-
getErrorStatus
protected IStatus getErrorStatus(String message)
Return an error status describing an invalid operation using the provided message.- Parameters:
message- the message to be used in the status, ornullif a generic message should be used- Returns:
- the error status
-
getWarningStatus
protected IStatus getWarningStatus(String message, int code)
Return a warning status describing the warning state of an operation using the provided message and code.- Parameters:
message- the message to be used in the status, ornullif a generic message should be usedcode- the integer code to be assigned to the status- Returns:
- the warning status
-
resourcesExist
protected boolean resourcesExist()
Return whether the resources known by this operation currently exist.- Returns:
trueif there are existing resources andfalseif there are no known resources or any one of them does not exist
-
resourcesIncludesProjects
protected boolean resourcesIncludesProjects()
Return whether the resources known by this operation contain any projects.- Returns:
trueif there is one or more projects known by this operation and false if there are no projects.
-
getExecuteSchedulingRule
protected ISchedulingRule getExecuteSchedulingRule()
Return a scheduling rule appropriate for executing this operation. The default implementation is to return a rule that locks out the entire workspace. Subclasses are encouraged to provide more specific rules that affect only their resources.- Returns:
- the scheduling rule to use when executing this operation, or
nullif there are no scheduling restrictions for this operation. - See Also:
IWorkspace.run(ICoreRunnable, ISchedulingRule, int, IProgressMonitor)
-
getUndoSchedulingRule
protected ISchedulingRule getUndoSchedulingRule()
Return a scheduling rule appropriate for undoing this operation. The default implementation is to return a rule that locks out the entire workspace. Subclasses are encouraged to provide more specific rules that affect only their resources.- Returns:
- the scheduling rule to use when undoing this operation, or
nullif there are no scheduling restrictions for this operation. - See Also:
IWorkspace.run(ICoreRunnable, ISchedulingRule, int, IProgressMonitor)
-
getRedoSchedulingRule
protected ISchedulingRule getRedoSchedulingRule()
Return a scheduling rule appropriate for redoing this operation. The default implementation considers the redo scheduling rule the same as the original execution scheduling rule.- Returns:
- the scheduling rule to use when redoing this operation, or
nullif there are no scheduling restrictions for this operation. - See Also:
IWorkspace.run(ICoreRunnable, ISchedulingRule, int, IProgressMonitor)
-
setQuietCompute
public void setQuietCompute(boolean quiet)
Description copied from interface:IAdvancedUndoableOperation2Set a boolean that instructs whether the computation of the receiver's execution, undo, or redo status should quietly compute status without consulting or prompting the user. The default value isfalse. This flag should only be set totruewhile the execution, undo, or redo status computations are being performed in the background, and should be restored tofalsewhen complete.If the status computation methods typically need to consult the user in order to determine the severity of a particular situation, the least severe status that could be chosen by the user should be returned when this flag is
true. This can help to prevent overzealous disposal of the operation history when an operation is in an ambiguous state. Typically, the status computation methods are invoked with this flag set tofalsejust before the actual execution, undo, or redo occurs, so the user can be consulted for the final outcome.- Specified by:
setQuietComputein interfaceIAdvancedUndoableOperation2- Parameters:
quiet-trueif it is inappropriate to consult or otherwise prompt the user while computing status, andfalseif the user may be prompted.- See Also:
IAdvancedUndoableOperation2.computeExecutionStatus(IProgressMonitor),IAdvancedUndoableOperation.computeUndoableStatus(IProgressMonitor),IAdvancedUndoableOperation.computeRedoableStatus(IProgressMonitor)
-
toString
public String toString()
Description copied from class:AbstractOperationThe string representation of this operation. Used for debugging purposes only. This string should not be shown to an end user.- Overrides:
toStringin classAbstractOperation- Returns:
- The string representation.
-
appendDescriptiveText
protected void appendDescriptiveText(StringBuffer text)
Append any descriptive text to the specified string buffer to be shown in the receiver'stoString()text.Note that this method is not intend to be subclassed by clients.
- Parameters:
text- the StringBuffer on which to append the text
-
getShell
protected Shell getShell(IAdaptable uiInfo)
Return the shell described by the specified adaptable, or the active shell if no shell has been specified in the adaptable.- Parameters:
uiInfo- the IAdaptable (ornull) provided by the caller in order to supply UI information for prompting the user if necessary. When this parameter is notnull, it contains an adapter for the org.eclipse.swt.widgets.Shell.class- Returns:
- the shell specified in the adaptable, or the active shell if no shell has been specified
-
runInBackground
public boolean runInBackground()
Description copied from interface:IAdvancedUndoableOperation2Return a boolean that instructs whether the operation should be executed, undone, or redone in a background thread.- Specified by:
runInBackgroundin interfaceIAdvancedUndoableOperation2- Returns:
trueif the operation should be run in the background,falseif it should not.
-
-