Eclipse Platform
2.0

org.eclipse.compare.structuremergeviewer
Class DocumentRangeNode

java.lang.Object
  |
  +--org.eclipse.compare.structuremergeviewer.DocumentRangeNode
All Implemented Interfaces:
IDocumentRange, IEditableContent, IStreamContentAccessor, IStructureComparator

public class DocumentRangeNode
extends Object
implements IDocumentRange, IStructureComparator, IEditableContent, IStreamContentAccessor

A document range node represents a structural element when performing a structure compare of documents. 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.

Subclasses may add additional state collected while parsing the document.

See Also:
Differencer

Field Summary
 
Fields inherited from interface org.eclipse.compare.contentmergeviewer.IDocumentRange
RANGE_CATEGORY
 
Constructor Summary
DocumentRangeNode(int typeCode, String id, IDocument document, int start, int length)
          Creates a new DocumentRangeNode for the given range within the specified document.
 
Method Summary
 void addChild(DocumentRangeNode node)
          Adds the given node as a child.
 boolean equals(Object other)
          Implementation based on getID.
 Position getAppendPosition()
          Returns the position that has been set with setAppendPosition.
 Object[] getChildren()
          Returns an iterator for all children of this object or null if there are no children.
 InputStream getContents()
          Returns an open InputStream for this object which can be used to retrieve the object's content.
 IDocument getDocument()
          Returns the underlying document.
 String getId()
          Returns this node's id.
 Position getRange()
          Returns a position that specifies a subrange in the underlying document, or null if this document range spans the whole underlying document.
 int getTypeCode()
          Returns the type code of this node.
 int hashCode()
          Implementation based on getID.
 boolean isEditable()
          Returns true if this object can be modified.
 ITypedElement replace(ITypedElement child, ITypedElement other)
          This method is called on a parent to add a child, remove a child, copy the contents of a child What to do is encoded in the two arguments as follows: add: child == null other != null remove: child != null other == null copy: child != null other != null
 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)
          Replaces the current content with the given new bytes.
 void setId(String id)
          Sets this node's id.
 void setLength(int length)
          Sets the length of the range of this node.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DocumentRangeNode

public DocumentRangeNode(int typeCode,
                         String id,
                         IDocument document,
                         int start,
                         int length)
Creates a new DocumentRangeNode for the given range within the specified document. The typeCode 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 node
id - an identifier for this node
document - document on which this node is based on
start - start position of range within document
length - length of range
Method Detail

getDocument

public IDocument getDocument()
Description copied from interface: IDocumentRange
Returns the underlying document.

Specified by:
getDocument in interface IDocumentRange
Returns:
the underlying document

getRange

public Position getRange()
Description copied from interface: IDocumentRange
Returns a position that specifies a subrange in the underlying document, or null if this document range spans the whole underlying document.

Specified by:
getRange in interface IDocumentRange
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

public String getId()
Returns this node's id. It is used in equals and hashcode.

Returns:
the node's id

setId

public void setId(String id)
Sets this node's id. It is used in equals and hashcode.

Parameters:
id - the new id for this node

addChild

public void addChild(DocumentRangeNode node)
Adds the given node as a child.

Parameters:
node - the node to add as a child

getChildren

public Object[] getChildren()
Description copied from interface: IStructureComparator
Returns an iterator for all children of this object or null if there are no children.

Specified by:
getChildren in interface IStructureComparator
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 representating 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

public Position getAppendPosition()
Returns the position that has been set with setAppendPosition. If setAppendPosition hasn't been called, the position after the last character of this range is returned.

Returns:
a position where text can be legally inserted

equals

public boolean equals(Object other)
Implementation based on getID.

Specified by:
equals in interface IStructureComparator
Overrides:
equals in class Object
Parameters:
other - the reference object with which to compare
Returns:
true if this object is the same as the other argument; false otherwise
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Implementation based on getID.

Overrides:
hashCode in class Object

getContents

public InputStream getContents()
Description copied from interface: IStreamContentAccessor
Returns an open InputStream for this object which can be used to retrieve the object's content. The client is responsible for closing the stream when finished. Returns null if this object has no streamable contents.

Specified by:
getContents in interface IStreamContentAccessor
Returns:
an input stream containing the contents of this object

isEditable

public boolean isEditable()
Description copied from interface: IEditableContent
Returns true if this object can be modified. If it returns false the other methods of this API must not be called.

Specified by:
isEditable in interface IEditableContent
Returns:
true if this object can be modified

replace

public ITypedElement replace(ITypedElement child,
                             ITypedElement other)
Description copied from interface: IEditableContent
This method is called on a parent to What to do is encoded in the two arguments as follows:
add: child == null other != null
remove: child != null other == null
copy: child != null other != null

Specified by:
replace in interface IEditableContent

setContent

public void setContent(byte[] content)
Description copied from interface: IEditableContent
Replaces the current content with the given new bytes.

Specified by:
setContent in interface IEditableContent
Parameters:
content - this new contents replaces the old contents

Eclipse Platform
2.0

Copyright (c) IBM Corp. and others 2000, 2002. All Rights Reserved.