public abstract class TextChange extends TextEditBasedChange
text edit tree
to a document. The text change manages the text edit tree.
Access to the document must be provided by concrete subclasses via the method
aquireDocument
,
commitDocument
, and
releaseDocument
.
A text change offers the ability to access the original content of
the document as well as creating a preview of the change. The edit
tree gets copied when creating any kind of preview. Therefore no region
updating on the original edit tree takes place when requesting a preview
(for more information on region updating see class TextEdit
.
If region tracking is required for a preview, it can be enabled via a call
to the method setKeepPreviewEdits
.
If enabled, the text change keeps the copied edit tree executed for the
preview, allowing clients to map an original edit to an executed edit. The
executed edit can then be used to determine its position in the preview.
Note: this class is not intended to be subclassed outside the refactoring framework.
Modifier | Constructor and Description |
---|---|
protected |
TextChange(String name)
Creates a new text change with the specified name.
|
Modifier and Type | Method and Description |
---|---|
protected abstract IDocument |
acquireDocument(IProgressMonitor pm)
Acquires a reference to the document to be changed by this text
change.
|
void |
addEdit(TextEdit edit)
Adds the given edit to the edit tree.
|
void |
addTextEditChangeGroup(TextEditChangeGroup group)
Adds a
text edit change group . |
void |
addTextEditGroup(TextEditGroup group)
Adds a
text edit group . |
protected abstract void |
commit(IDocument document,
IProgressMonitor pm)
Commits the document acquired via a call to
aquireDocument . |
protected abstract Change |
createUndoChange(UndoEdit edit)
Hook to create an undo change for the given undo edit.
|
String |
getCurrentContent(IProgressMonitor pm)
Returns the current content of the document this text
change is associated with.
|
String |
getCurrentContent(IRegion region,
boolean expandRegionToFullLine,
int surroundingLines,
IProgressMonitor pm)
Returns the current content of the text edit change clipped to a specific
region.
|
IDocument |
getCurrentDocument(IProgressMonitor pm)
Returns the document this text change is associated to.
|
TextEdit |
getEdit()
Returns the root text edit.
|
String |
getPreviewContent(IProgressMonitor pm)
Returns the preview content as a string.
|
String |
getPreviewContent(TextEditBasedChangeGroup[] changeGroups,
IRegion region,
boolean expandRegionToFullLine,
int surroundingLines,
IProgressMonitor pm)
Returns a preview of the text change clipped to a specific region.
|
String |
getPreviewContent(TextEditChangeGroup[] changeGroups,
IRegion region,
boolean expandRegionToFullLine,
int surroundingLines,
IProgressMonitor pm)
Returns a preview of the text change clipped to a specific region.
|
IDocument |
getPreviewDocument(IProgressMonitor pm)
Returns a document containing a preview of the text change.
|
TextEdit |
getPreviewEdit(TextEdit original)
Returns the edit that got executed during preview generation
instead of the given original.
|
TextEdit[] |
getPreviewEdits(TextEdit[] originals)
Returns the edits that were executed during preview generation
instead of the given array of original edits.
|
TextEditChangeGroup[] |
getTextEditChangeGroups()
Returns the
text edit change groups managed by this
text change. |
Change |
perform(IProgressMonitor pm)
Performs this change.
|
protected UndoEdit |
performEdits(IDocument document)
Executes the text edits on the given document.
|
protected abstract void |
releaseDocument(IDocument document,
IProgressMonitor pm)
Releases the document acquired via a call to
aquireDocument . |
void |
setEdit(TextEdit edit)
Sets the root text edit that should be applied to the
document represented by this text change.
|
void |
setKeepPreviewEdits(boolean keep)
Controls whether the text edit change should keep executed edits during
preview generation.
|
addChangeGroup, getChangeGroups, getKeepPreviewEdits, getName, getTextType, hasOneGroupCategory, setEnabled, setTextType
dispose, getAdapter, getAffectedObjects, getDescriptor, getModifiedElement, getParent, initializeValidationData, isEnabled, isValid, setEnabledShallow
protected TextChange(String name)
null
.
The text type of this text change is set to txt
.
name
- the name of the text changeTextEditBasedChange.setTextType(String)
public void setEdit(TextEdit edit)
edit
- the root text edit. The root text edit
can only be set once.public TextEdit getEdit()
null
if no root edit has been
setpublic void addTextEditGroup(TextEditGroup group)
text edit group
. This method is a convenience
method for calling change.addTextEditChangeGroup(new
TextEditChangeGroup(change, group));
.addTextEditGroup
in class TextEditBasedChange
group
- the text edit group to addpublic void addTextEditChangeGroup(TextEditChangeGroup group)
text edit change group
. Calling this method
requires that a root edit has been set via the method setEdit
. The edits managed by the given text edit change group must be part of
the change's root edit.group
- the text edit change group to addpublic TextEditChangeGroup[] getTextEditChangeGroups()
text edit change groups
managed by this
text change.public void addEdit(TextEdit edit) throws MalformedTreeException
edit
- the text edit to addMalformedTreeException
- if the edit can't be added. Reason
is that is overlaps with an already existing editprotected abstract IDocument acquireDocument(IProgressMonitor pm) throws CoreException
releaseDocument(IDocument, IProgressMonitor)
.
The method releaseDocument
must be called as many times as
aquireDocument
has been called.
pm
- a progress monitorCoreException
- if the document can't be acquiredprotected abstract 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.document
- the document to commitpm
- a progress monitorCoreException
- if the document can't be committedprotected abstract void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException
aquireDocument
.document
- the document to releasepm
- a progress monitorCoreException
- if the document can't be releasedprotected abstract Change createUndoChange(UndoEdit edit)
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.public 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.perform
in class Change
pm
- a progress monitornull
if no
undo is providedCoreException
- if an error occurred during change executionprotected UndoEdit performEdits(IDocument document) throws BadLocationException, MalformedTreeException
super.performEdits(document)
.document
- the documentMalformedTreeException
- 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.BadLocationException
- 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.public IDocument getCurrentDocument(IProgressMonitor pm) throws CoreException
The returned document must not be modified by the client. Doing so will result in an unexpected behavior when the change is performed.
pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if the document can't be acquiredpublic String getCurrentContent(IProgressMonitor pm) throws CoreException
getCurrentContent
in class TextEditBasedChange
pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if the content can't be accessedpublic String getCurrentContent(IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException
expandRegionToFullLine
is false
then the parameter region
determines the clipping.
expandRegionToFullLine
is true
then the region determined by the parameter region
is extended to cover full lines.
surroundingLines
> 0 then the given number
of surrounding lines is added. The value of surroundingLines
is only considered if expandRegionToFullLine
is true
getCurrentContent
in class TextEditBasedChange
region
- the starting region for the text to be returnedexpandRegionToFullLine
- if true
is passed the region
is extended to cover full linessurroundingLines
- the number of surrounding lines to be added to
the clipping region. Is only considered if expandRegionToFullLine
is true
pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if an exception occurs while accessing the current contentpublic TextEdit getPreviewEdit(TextEdit original)
setKeepPreviewEdits
is set to true
and that
a preview has been requested via one of the getPreview*
methods.
The method returns null
if the original isn't managed
by this text change.
original
- the original edit managed by this text changepublic TextEdit[] getPreviewEdits(TextEdit[] originals)
setKeepPreviewEdits
is set to true
and that a preview has been requested via one of the
getPreview*
methods.
The method returns an empty array if none of the original edits is managed by this text change.
originals
- an array of original edits managed by this text
changepublic IDocument getPreviewDocument(IProgressMonitor pm) throws CoreException
text edit change groups
.pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if the preview can't be createdpublic String getPreviewContent(IProgressMonitor pm) throws CoreException
getPreviewContent
in class TextEditBasedChange
pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if the preview can't be createdpublic String getPreviewContent(TextEditChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException
text edit change groups
.
The region is determined as follows:
expandRegionToFullLine
is false
then the parameter region
determines the clipping.
expandRegionToFullLine
is true
then the region determined by the parameter region
is extended to cover full lines.
surroundingLines
> 0 then the given number
of surrounding lines is added. The value of surroundingLines
is only considered if expandRegionToFullLine
is true
changeGroups
- a set of change groups for which a preview is to be
generatedregion
- the starting region for the clippingexpandRegionToFullLine
- if true
is passed the region
is extended to cover full linessurroundingLines
- the number of surrounding lines to be added to
the clipping region. Is only considered if expandRegionToFullLine
is true
pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if an exception occurs while generating the previewgetCurrentContent(IRegion, boolean, int, IProgressMonitor)
public String getPreviewContent(TextEditBasedChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException
text edit change groups
.
The region is determined as follows:
expandRegionToFullLine
is false
then the parameter region
determines the clipping.
expandRegionToFullLine
is true
then the region determined by the parameter region
is extended to cover full lines.
surroundingLines
> 0 then the given number
of surrounding lines is added. The value of surroundingLines
is only considered if expandRegionToFullLine
is true
getPreviewContent
in class TextEditBasedChange
changeGroups
- a set of change groups for which a preview is to be
generatedregion
- the starting region for the clippingexpandRegionToFullLine
- if true
is passed the region
is extended to cover full linessurroundingLines
- the number of surrounding lines to be added to
the clipping region. Is only considered if expandRegionToFullLine
is true
pm
- a progress monitor to report progress or null
if no progress reporting is desiredCoreException
- if an exception occurs while generating the previewgetCurrentContent(IRegion, boolean, int, IProgressMonitor)
public void setKeepPreviewEdits(boolean keep)
setKeepPreviewEdits
in class TextEditBasedChange
keep
- if true
executed preview edits are kept
Copyright (c) 2000, 2015 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.