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
ConstructorsConstructorDescriptionDocumentRangeNode(int typeCode, String id, IDocument document, int start, int length) Creates a newDocumentRangeNodefor the given range within the specified document.DocumentRangeNode(DocumentRangeNode parent, int typeCode, String id, IDocument document, int start, int length) Creates a newDocumentRangeNodefor the given range within the specified document. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChild(DocumentRangeNode node) Adds the given node as a child.booleanImplementation based ongetID.<T> TgetAdapter(Class<T> adapter) ImplementIAdaptable.getAdapter(Class)in order to provide anISharedDocumentAdapterthat 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 ornullif there are no children.Returns an openInputStreamfor 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 ornullif 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, ornullif this document range spans the whole underlying document.intReturns the type code of this node.inthashCode()Implementation based ongetID.protected voidinternalSetContents(byte[] content) Method that is invoked fromsetContent(byte[]).booleanIf this node has a parent, return the editability of the parent.booleanReturn 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 voidnodeChanged(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.voidsetAppendPosition(int pos) Sets a position within the document range that can be used to (legally) insert text without breaking the syntax of the document.voidsetContent(byte[] content) Default implementation that callsinternalSetContents(byte[])and thennodeChanged(DocumentRangeNode).voidSets this node's id.voidsetLength(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 newDocumentRangeNodefor the given range within the specified document. ThetypeCodeis 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 newDocumentRangeNodefor the given range within the specified document. ThetypeCodeis 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:IDocumentRangeReturns the underlying document.- Specified by:
getDocumentin interfaceIDocumentRange- Returns:
- the underlying document
-
getRange
Description copied from interface:IDocumentRangeReturns a position that specifies a subrange in the underlying document, ornullif this document range spans the whole underlying document.- Specified by:
getRangein 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 inequalsandhashcode.- Returns:
- the node's id
-
setId
Sets this node's id. It is used inequalsandhashcode.- 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:IStructureComparatorReturns an iterator for all children of this object ornullif there are no children.- Specified by:
getChildrenin 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
DocumentRangeNoderepresenting 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. IfsetAppendPositionhasn't been called, the position after the last character of this range is returned. This method will returnnullif the position could not be registered with the document.- Returns:
- a position where text can be legally inserted
-
equals
Implementation based ongetID.- Specified by:
equalsin interfaceIStructureComparator- Overrides:
equalsin classObject- Parameters:
other- the object to compare thisDocumentRangeNodeagainst.- Returns:
trueif theDocumentRangeNodesare equal;falseotherwise.- See Also:
-
hashCode
public int hashCode()Implementation based ongetID. -
getContents
Description copied from interface:IStreamContentAccessorReturns an openInputStreamfor this object which can be used to retrieve the object's content. The client is responsible for closing the stream when finished. Returnsnullif this object has no streamable contents.- Specified by:
getContentsin 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:
isEditablein interfaceIEditableContent- Returns:
trueif this object can be modified- See Also:
-
replace
Description copied from interface:IEditableContentThis 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:
replacein interfaceIEditableContent- Parameters:
child- the existing child of this object to be replaced; ifnulla new child can be added.other- the new child to be added or replaced; ifnullan 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:
setContentin 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:IEncodedStreamContentAccessorReturns the name of a charset encoding to be used when decoding this stream accessor's contents into characters. Returnsnullif 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
UnsupportedEncodingExceptionwhere this charset is used.- Specified by:
getCharsetin 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 anISharedDocumentAdapterthat 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:
getAdapterin 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:IEditableContentExtensionReturn 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:
isReadOnlyin interfaceIEditableContentExtension- Returns:
- whether the typed element is read-only
-
validateEdit
Description copied from interface:IEditableContentExtensionIf the element is read-only, this method should be called to attempt to make it writable.- Specified by:
validateEditin interfaceIEditableContentExtension- Parameters:
shell- a shell used to prompt the user if required.- Returns:
- a status object that is
OKif things are fine, otherwise a status describing reasons why modifying the given files is not reasonable. A status with a severity ofCANCELis returned if the validation was canceled, indicating the edit should not proceed.
-
getParentNode
Return the parent of this node ornullif the node doesn't have a parent or the parent is not known.- Returns:
- the parent of this node or
null
-