Class DocumentRangeNode
- All Implemented Interfaces:
IDocumentRange
,IEditableContent
,IEditableContentExtension
,IEncodedStreamContentAccessor
,IStreamContentAccessor
,IStructureComparator
,IAdaptable
- Direct Known Subclasses:
StructureRootNode
DocumentRangeNodes
are created while parsing the document and represent
a semantic entity (e.g. a Java class or method).
As a consequence of the parsing a DocumentRangeNode
maps to a range
of characters in the document.
Since a DocumentRangeNode
implements the IStructureComparator
and IStreamContentAccessor
interfaces it can be used as input to the
differencing engine. This makes it possible to perform
a structural diff on a document and have the nodes and leaves of the compare easily map
to character ranges within the document.
Clients need to be aware that this node registers position updaters with the document
using IDocument.addPosition(String, Position)
with the category set to
IDocumentRange.RANGE_CATEGORY
. The StructureDiffViewer
will
remove the category when the nodes are no longer being used. Other clients
must do the same.
Subclasses may add additional state collected while parsing the document.
- See Also:
-
Field Summary
Fields inherited from interface org.eclipse.compare.contentmergeviewer.IDocumentRange
RANGE_CATEGORY
-
Constructor Summary
ConstructorDescriptionDocumentRangeNode
(int typeCode, String id, IDocument document, int start, int length) Creates a newDocumentRangeNode
for the given range within the specified document.DocumentRangeNode
(DocumentRangeNode parent, int typeCode, String id, IDocument document, int start, int length) Creates a newDocumentRangeNode
for the given range within the specified document. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addChild
(DocumentRangeNode node) Adds the given node as a child.boolean
Implementation based ongetID
.<T> T
getAdapter
(Class<T> adapter) ImplementIAdaptable.getAdapter(Class)
in order to provide anISharedDocumentAdapter
that provides the proper look up key based on the input from which this structure node was created.Returns the position that has been set withsetAppendPosition
.Returns the name of a charset encoding to be used when decoding this stream accessor's contents into characters.Object[]
Returns an iterator for all children of this object ornull
if there are no children.Returns an openInputStream
for this object which can be used to retrieve the object's content.Returns the underlying document.getId()
Returns this node's id.Return the parent of this node ornull
if the node doesn't have a parent or the parent is not known.getRange()
Returns a position that specifies a subrange in the underlying document, ornull
if this document range spans the whole underlying document.int
Returns the type code of this node.int
hashCode()
Implementation based ongetID
.protected void
internalSetContents
(byte[] content) Method that is invoked fromsetContent(byte[])
.boolean
If this node has a parent, return the editability of the parent.boolean
Return whether the typed element being displayed is read-only. a read-only element will require a call to validateEdit before the element can be modified on disk.protected void
nodeChanged
(DocumentRangeNode node) Method that should be invoked whenever the contents of this node are changed. the change is propagated to the parent if there is one.replace
(ITypedElement child, ITypedElement other) This method is called on a parent to add or remove a child, or to copy the contents of a child.void
setAppendPosition
(int pos) Sets a position within the document range that can be used to (legally) insert text without breaking the syntax of the document.void
setContent
(byte[] content) Default implementation that callsinternalSetContents(byte[])
and thennodeChanged(DocumentRangeNode)
.void
Sets this node's id.void
setLength
(int length) Sets the length of the range of this node.validateEdit
(Shell shell) If the element is read-only, this method should be called to attempt to make it writable.
-
Constructor Details
-
DocumentRangeNode
Creates a newDocumentRangeNode
for the given range within the specified document. ThetypeCode
is uninterpreted client data. The ID is used when comparing two nodes with each other: i.e. the differencing engine performs a content compare on two nodes if their IDs are equal.- Parameters:
typeCode
- a type code for this nodeid
- an identifier for this nodedocument
- document on which this node is based onstart
- start position of range within documentlength
- length of range
-
DocumentRangeNode
public DocumentRangeNode(DocumentRangeNode parent, int typeCode, String id, IDocument document, int start, int length) Creates a newDocumentRangeNode
for the given range within the specified document. ThetypeCode
is uninterpreted client data. The ID is used when comparing two nodes with each other: i.e. the differencing engine performs a content compare on two nodes if their IDs are equal.- Parameters:
parent
- the parent nodetypeCode
- a type code for this nodeid
- an identifier for this nodedocument
- document on which this node is based onstart
- start position of range within documentlength
- length of range- Since:
- 3.3
-
-
Method Details
-
getDocument
Description copied from interface:IDocumentRange
Returns the underlying document.- Specified by:
getDocument
in interfaceIDocumentRange
- Returns:
- the underlying document
-
getRange
Description copied from interface:IDocumentRange
Returns a position that specifies a subrange in the underlying document, ornull
if this document range spans the whole underlying document.- Specified by:
getRange
in interfaceIDocumentRange
- Returns:
- a position that specifies a subrange in the underlying document, or
null
-
getTypeCode
public int getTypeCode()Returns the type code of this node. The type code is uninterpreted client data which can be set in the constructor.- Returns:
- the type code of this node
-
getId
Returns this node's id. It is used inequals
andhashcode
.- Returns:
- the node's id
-
setId
Sets this node's id. It is used inequals
andhashcode
.- Parameters:
id
- the new id for this node
-
addChild
Adds the given node as a child.- Parameters:
node
- the node to add as a child
-
getChildren
Description copied from interface:IStructureComparator
Returns an iterator for all children of this object ornull
if there are no children.- Specified by:
getChildren
in interfaceIStructureComparator
- Returns:
- an array with all children of this object, or an empty array if there are no children
-
setLength
public void setLength(int length) Sets the length of the range of this node.- Parameters:
length
- the length of the range
-
setAppendPosition
public void setAppendPosition(int pos) Sets a position within the document range that can be used to (legally) insert text without breaking the syntax of the document.E.g. when parsing a Java document the "append position" of a
DocumentRangeNode
representing a Java class could be the character position just before the closing bracket. Inserting the text of a new method there would not disturb the syntax of the class.- Parameters:
pos
- the character position within the underlying document where text can be legally inserted
-
getAppendPosition
Returns the position that has been set withsetAppendPosition
. IfsetAppendPosition
hasn't been called, the position after the last character of this range is returned. This method will returnnull
if the position could not be registered with the document.- Returns:
- a position where text can be legally inserted
-
equals
Implementation based ongetID
.- Specified by:
equals
in interfaceIStructureComparator
- Overrides:
equals
in classObject
- Parameters:
other
- the object to compare thisDocumentRangeNode
against.- Returns:
true
if theDocumentRangeNodes
are equal;false
otherwise.- See Also:
-
hashCode
public int hashCode()Implementation based ongetID
. -
getContents
Description copied from interface:IStreamContentAccessor
Returns an openInputStream
for this object which can be used to retrieve the object's content. The client is responsible for closing the stream when finished. Returnsnull
if this object has no streamable contents.- Specified by:
getContents
in interfaceIStreamContentAccessor
- Returns:
- an input stream containing the contents of this object
-
isEditable
public boolean isEditable()If this node has a parent, return the editability of the parent. Otherwise returntrue
. Subclasses may override.- Specified by:
isEditable
in interfaceIEditableContent
- Returns:
true
if this object can be modified- See Also:
-
replace
Description copied from interface:IEditableContent
This method is called on a parent to add or remove a child, or to copy the contents of a child. What to do is encoded in the two arguments as follows:add: dest == null src != null remove: dest != null src == null copy: dest != null src != null - Specified by:
replace
in interfaceIEditableContent
- Parameters:
child
- the existing child of this object to be replaced; ifnull
a new child can be added.other
- the new child to be added or replaced; ifnull
an existing child can be removed.- Returns:
- the argument
dest
-
setContent
public void setContent(byte[] content) Default implementation that callsinternalSetContents(byte[])
and thennodeChanged(DocumentRangeNode)
. Subclasses may override but should then callnodeChanged(DocumentRangeNode)
after the contents have been set.- Specified by:
setContent
in interfaceIEditableContent
- Parameters:
content
- this new contents replaces the old contents- See Also:
-
internalSetContents
protected void internalSetContents(byte[] content) Method that is invoked fromsetContent(byte[])
. By default, this method does nothing. Subclasses may override.- Parameters:
content
- the new content- Since:
- 3.3
-
getCharset
Description copied from interface:IEncodedStreamContentAccessor
Returns the name of a charset encoding to be used when decoding this stream accessor's contents into characters. Returnsnull
if a proper encoding cannot be determined.Note: this method does not check whether the result is a supported charset name. Callers should be prepared to handle
UnsupportedEncodingException
where this charset is used.- Specified by:
getCharset
in interfaceIEncodedStreamContentAccessor
- Returns:
- the name of a charset, or
null
- See Also:
-
nodeChanged
Method that should be invoked whenever the contents of this node are changed. the change is propagated to the parent if there is one.- Parameters:
node
- the node that has changed.- Since:
- 3.3
-
getAdapter
ImplementIAdaptable.getAdapter(Class)
in order to provide anISharedDocumentAdapter
that provides the proper look up key based on the input from which this structure node was created. The proper shared document adapter is obtained by callinggetAdapter(Class)
on this node's parent if there is one.- Specified by:
getAdapter
in interfaceIAdaptable
- Type Parameters:
T
- the class type- Parameters:
adapter
- the adapter class to look up- Returns:
- the object adapted to the given class or
null
- Since:
- 3.3
- See Also:
-
isReadOnly
public boolean isReadOnly()Description copied from interface:IEditableContentExtension
Return whether the typed element being displayed is read-only. a read-only element will require a call to validateEdit before the element can be modified on disk.- Specified by:
isReadOnly
in interfaceIEditableContentExtension
- Returns:
- whether the typed element is read-only
-
validateEdit
Description copied from interface:IEditableContentExtension
If the element is read-only, this method should be called to attempt to make it writable.- Specified by:
validateEdit
in interfaceIEditableContentExtension
- Parameters:
shell
- a shell used to prompt the user if required.- Returns:
- a status object that is
OK
if things are fine, otherwise a status describing reasons why modifying the given files is not reasonable. A status with a severity ofCANCEL
is returned if the validation was canceled, indicating the edit should not proceed.
-
getParentNode
Return the parent of this node ornull
if the node doesn't have a parent or the parent is not known.- Returns:
- the parent of this node or
null
-