public class TextFileChange extends TextChange
TextChange
that operates on a IFile
.
As of 3.1 the content stamp managed by a text file change maps to the modification
stamp of its underlying IFile
. Undoing a text file change will
roll back the modification stamp of a resource to its original value using
the new API IResource.revertModificationStamp(long)
The class should be subclassed by clients which need to perform special operation when acquiring or releasing a document.
Modifier and Type | Field and Description |
---|---|
static int |
FORCE_SAVE
Flag (value 2) indicating that the file is to be saved after the change has been applied.
|
static int |
KEEP_SAVE_STATE
Flag (value 1) indicating that the file's save state has to be kept.
|
static int |
LEAVE_DIRTY
Flag (value 4) indicating that the file will not be saved after the change has been applied.
|
Constructor and Description |
---|
TextFileChange(String name,
IFile file)
Creates a new
TextFileChange for the given file. |
Modifier and Type | Method and Description |
---|---|
protected IDocument |
acquireDocument(IProgressMonitor pm)
Acquires a reference to the document to be changed by this text
change.
|
protected void |
commit(IDocument document,
IProgressMonitor pm)
Commits the document acquired via a call to
aquireDocument . |
protected Change |
createUndoChange(UndoEdit edit)
Hook to create an undo change for the given undo edit.
|
protected Change |
createUndoChange(UndoEdit edit,
ContentStamp stampToRestore)
Hook to create an undo change for the given undo edit and content stamp.
|
void |
dispose()
Disposes this change.
|
Object[] |
getAffectedObjects()
Returns the elements affected by this change or
null if
the affected elements cannot be determined. |
IFile |
getFile()
Returns the
IFile this change is working on. |
Object |
getModifiedElement()
Returns the element modified by this
Change . |
int |
getSaveMode()
Returns the save state set via
setSaveMode(int) . |
void |
initializeValidationData(IProgressMonitor monitor)
Hook method to initialize some internal state to provide an adequate answer
for the
isValid method. |
protected boolean |
isDocumentAcquired()
Is the document currently acquired?
|
protected boolean |
isDocumentModified()
Has the document been modified since it has been first acquired by the change?
|
RefactoringStatus |
isValid(IProgressMonitor monitor)
Verifies that this change object is still valid and can be executed by calling
perform . |
protected boolean |
needsSaving()
Does the text file change need saving?
|
protected UndoEdit |
performEdits(IDocument document)
Executes the text edits on the given document.
|
protected void |
releaseDocument(IDocument document,
IProgressMonitor pm)
Releases the document acquired via a call to
aquireDocument . |
void |
setSaveMode(int saveMode)
Sets the save state.
|
addEdit, addTextEditChangeGroup, addTextEditGroup, getCurrentContent, getCurrentContent, getCurrentDocument, getEdit, getPreviewContent, getPreviewContent, getPreviewContent, getPreviewDocument, getPreviewEdit, getPreviewEdits, getTextEditChangeGroups, perform, setEdit, setKeepPreviewEdits
addChangeGroup, getChangeGroups, getKeepPreviewEdits, getName, getTextType, hasOneGroupCategory, setEnabled, setTextType
getAdapter, getDescriptor, getParent, isEnabled, setEnabledShallow
public static final int KEEP_SAVE_STATE
public static final int FORCE_SAVE
public static final int LEAVE_DIRTY
public void setSaveMode(int saveMode)
KEEP_SAVE_STATE
,
FORCE_SAVE
or LEAVE_DIRTY
.saveMode
- indicating how save is handled when the document
gets committedpublic int getSaveMode()
setSaveMode(int)
.public IFile getFile()
IFile
this change is working on.protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore)
edit
- the UndoEdit
to create an undo change forstampToRestore
- the content stamp to restore when the undo
edit is executed.null
if no undo change can
be created. Returning null
results in the fact that
the whole change tree can't be undone. So returning null
is only recommended if an exception occurred during creating the
undo change.public Object getModifiedElement()
Change
. The method may return
null
if the change isn't related to an element.getModifiedElement
in class Change
public Object[] getAffectedObjects()
Change
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.
getAffectedObjects
in class Change
null
if
the affected elements cannot be determinedpublic void initializeValidationData(IProgressMonitor monitor)
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.
initializeValidationData
in class Change
monitor
- a progress monitorpublic RefactoringStatus isValid(IProgressMonitor monitor) throws CoreException
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.
isValid
in class Change
monitor
- a progress monitor.CoreException
- if an error occurred during validation check. The change
is to be treated as invalid if an exception occurspublic void dispose()
initializeValidationData
.
Subclasses may override this method.
protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException
TextChange.releaseDocument(IDocument, IProgressMonitor)
.
The method releaseDocument
must be called as many times as
aquireDocument
has been called.
acquireDocument
in class TextChange
pm
- a progress monitorCoreException
- if the document can't be acquiredprotected void commit(IDocument document, IProgressMonitor pm) throws CoreException
aquireDocument
. It is up to the implementors of this method to decide what committing
a document means. Typically, the content of the document is written back to the file
system.
The implementation of this method only commits the underlying buffer if
needsSaving()
and isDocumentModified()
returns true
.
commit
in class TextChange
document
- the document to commitpm
- a progress monitorCoreException
- if the document can't be committedprotected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException
aquireDocument
.releaseDocument
in class TextChange
document
- the document to releasepm
- a progress monitorCoreException
- if the document can't be releasedprotected final Change createUndoChange(UndoEdit edit)
createUndoChange
in class TextChange
edit
- the UndoEdit
to create an undo change fornull
if no undo change can
be created. Returning null
results in the fact that
the whole change tree can't be undone. So returning null
is only recommended if an exception occurred during the creation of the
undo change.protected UndoEdit performEdits(IDocument document) throws BadLocationException, MalformedTreeException
TextChange
super.performEdits(document)
.performEdits
in class TextChange
document
- the documentBadLocationException
- is thrown if one of the edits in the
tree can't be executed. The state of the document is undefined if this
exception is thrown.MalformedTreeException
- is thrown if the edit tree isn't
in a valid state. This exception is thrown before any edit is executed.
So the document is still in its original state.protected boolean isDocumentAcquired()
true
if the document is currently acquired,
false
otherwiseprotected boolean isDocumentModified()
false
is returned if the document has not been acquired yet, or has been released
already.protected boolean needsSaving()
The implementation of this method returns true
if the
FORCE_SAVE
flag is enabled, or the underlying file is not
dirty and KEEP_SAVE_STATE
is enabled.
true
if it needs saving according to its dirty
state and the save mode flags, false
otherwise
Copyright (c) 2000, 2015 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.