public abstract class Change extends Object implements IAdaptable
Change
object is typically created by
calling Refactoring.createChange(IProgressMonitor)
. This class should be
subclassed by clients wishing to provide new changes.
Changes are best executed by using a PerformChangeOperation
. If clients
execute a change directly then the following life cycle has to be honored:
initializeValidationData
has to be called.isValid
can be used to determine if a change
can still be applied to the workspace. If the method returns a RefactoringStatus
with a severity of FATAL then the change has to be
treated as invalid. Performing an invalid change isn't allowed and
results in an unspecified result. This method can be called multiple
times.
perform
can be called. A disabled change
must not be executed. The perform
method can only be called
once. After a change has been executed, only the method dispose
must be called.dispose
has to be called either after the
perform
method
has been called or if a change is no longer needed. The second case
for example occurs when the undo stack gets flushed and all change
objects managed by the undo stack are no longer needed. The method
dispose
is typically implemented to unregister listeners
registered during the
method initializeValidationData
. There is no guarantee
that initializeValidationData
, isValid
,
or perform
has been called before dispose
is called.
Change change= createChange(); try { change.initializeValidationData(pm); .... if (!change.isEnabled()) return; RefactoringStatus valid= change.isValid(new SubProgressMonitor(pm, 1)); if (valid.hasFatalError()) return; Change undo= change.perform(new SubProgressMonitor(pm, 1)); if (undo != null) { undo.initializeValidationData(new SubProgressMonitor(pm, 1)); // do something with the undo object } } finally { change.dispose(); }
It is important that implementors of this abstract class provide an adequate
implementation of isValid
and that they provide an undo change
via the return value of the method perform
. If no undo can be
provided then the perform
method is allowed to return null
. But
implementors should be aware that not providing an undo object for a change
object that is part of a larger change tree will result in the fact that for
the whole change tree no undo object will be present.
Changes which are returned as top-level changes (e.g. by Refactoring.createChange()
)
can optionally return a descriptor object of the refactoring which created this change object.
Clients may subclass this class.
Modifier | Constructor and Description |
---|---|
protected |
Change()
Constructs a new change object.
|
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Disposes this change.
|
<T> T |
getAdapter(Class<T> adapter)
Returns an object which is an instance of the given class
associated with this object.
|
Object[] |
getAffectedObjects()
Returns the elements affected by this change or
null if
the affected elements cannot be determined. |
ChangeDescriptor |
getDescriptor()
Returns a descriptor of this change.
|
abstract Object |
getModifiedElement()
Returns the element modified by this
Change . |
abstract String |
getName()
Returns the human readable name of this change.
|
Change |
getParent()
Returns the parent change.
|
abstract void |
initializeValidationData(IProgressMonitor pm)
Hook method to initialize some internal state to provide an adequate answer
for the
isValid method. |
boolean |
isEnabled()
Returns whether this change is enabled or not.
|
abstract RefactoringStatus |
isValid(IProgressMonitor pm)
Verifies that this change object is still valid and can be executed by calling
perform . |
abstract Change |
perform(IProgressMonitor pm)
Performs this change.
|
void |
setEnabled(boolean enabled)
Sets whether this change is enabled or not.
|
void |
setEnabledShallow(boolean enabled)
Sets the enablement state of this change in a shallow way.
|
public ChangeDescriptor getDescriptor()
Subclasses of changes created by
Refactoring.createChange(IProgressMonitor)
should override this
method to return a RefactoringChangeDescriptor
. A change tree
created by a particular refactoring is supposed to contain at most one
change which returns a refactoring descriptor. Refactorings usually
return an instance of CompositeChange
in their
Refactoring.createChange(IProgressMonitor)
method which
implements this method. The refactoring framework searches the change
tree top-down until a refactoring descriptor is found.
null
if this
change does not provide a change descriptor.public abstract String getName()
null
.public boolean isEnabled()
true
if the change is enabled; false
otherwise.public void setEnabled(boolean enabled)
enabled
- true
to enable this change;
false
otherwisepublic final void setEnabledShallow(boolean enabled)
enabled
- true
to enable this change;
false
otherwisepublic Change getParent()
null
if no
parent exists.public abstract void initializeValidationData(IProgressMonitor pm)
isValid
method. This method gets called after a change
or a whole change tree has been created.
Typically this method is implemented in one of the following ways:
dispose
.isValid
is called.
For example, a change object that manipulates the content of an IFile
could either listen to resource changes and detect that the file got changed or
it could remember some content stamp and compare it with the actual content stamp
when isValid
is called.
pm
- a progress monitorpublic abstract RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException
perform
. If a refactoring status with a severity of RefactoringStatus.FATAL
is returned then the change has to be treated as invalid
and can no longer be executed. Performing such a change produces an unspecified
result and will very likely throw an exception.
This method is also called by the UndoManager
to decide if
an undo or redo change is still valid and therefore can be executed.
pm
- a progress monitor.CoreException
- if an error occurred during validation check. The change
is to be treated as invalid if an exception occursOperationCanceledException
- if the validation check got canceledpublic abstract Change perform(IProgressMonitor pm) throws CoreException
IProgressMonitor.isCanceled()
since canceling a change tree in the
middle of its execution leaves the workspace in a half changed state.pm
- a progress monitornull
if no
undo is providedCoreException
- if an error occurred during change executionpublic void dispose()
initializeValidationData
.
Subclasses may override this method.
public abstract Object getModifiedElement()
Change
. The method may return
null
if the change isn't related to an element.public Object[] getAffectedObjects()
null
if
the affected elements cannot be determined. Returns an empty array
if the change doesn't modify any elements.
This default implementation returns null
to indicate that
the affected elements are unknown. Subclasses should reimplement this method
if they can compute the set of affected elements.
null
if
the affected elements cannot be determinedpublic <T> T getAdapter(Class<T> adapter)
IAdaptable
null
if
no such object can be found.
Clients may implement this method but should generally call Adapters.adapt(Object, Class, boolean)
rather than invoking it directly.
getAdapter
in interface IAdaptable
adapter
- the adapter class to look upnull
if this object does not
have an adapter for the given class
Copyright (c) 2000, 2018 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.