org.fife.ui.rsyntaxtextarea.folding
Class Fold

java.lang.Object
  extended by org.fife.ui.rsyntaxtextarea.folding.Fold
All Implemented Interfaces:
java.lang.Comparable<Fold>

public class Fold
extends java.lang.Object
implements java.lang.Comparable<Fold>

Information about a foldable region.

A Fold has zero or more children, and Folds thus form a hierarchical structure, with "parent" folds containing the info about any "child" folds they contain.

Fold regions are denoted by a starting and ending offset, but the actual folding is done on a per-line basis, so Fold instances provide methods for retrieving both starting and ending offsets and lines. The starting and ending offsets/lines are "sticky" and correctly track their positions even as the document is modified.

Version:
1.0
Author:
Robert Futrell

Constructor Summary
Fold(int type, RSyntaxTextArea textArea, int startOffs)
           
 
Method Summary
 int compareTo(Fold otherFold)
          Two folds are considered equal if they start at the same offset.
 boolean containsLine(int line)
          Returns whether the specified line would be hidden in this fold.
 boolean containsOffset(int offs)
          Returns whether the specified offset is "inside" the fold.
 boolean containsOrStartsOnLine(int line)
          Returns whether the given line is in the range [getStartLine(), getEndLine()], inclusive.
 Fold createChild(int type, int startOffs)
          Creates a fold that is a child of this one.
 boolean equals(java.lang.Object otherFold)
          Two folds are considered equal if they have the same starting offset.
 Fold getChild(int index)
          Returns a specific child fold.
 int getChildCount()
          Returns the number of child folds.
 int getCollapsedLineCount()
          Returns the number of collapsed lines under this fold.
 int getEndLine()
          Returns the end line of this fold.
 int getEndOffset()
          Returns the end offset of this fold.
 int getFoldType()
          Returns the type of fold this is.
 boolean getHasChildFolds()
          Returns whether this fold has any child folds.
 Fold getLastChild()
          Returns the last child fold.
 int getLineCount()
          Returns the number of lines that are hidden when this fold is collapsed.
 Fold getParent()
          Returns the parent fold of this one.
 int getStartLine()
          Returns the starting line of this fold region.
 int getStartOffset()
          Returns the starting offset of this fold region.
 int hashCode()
           
 boolean isCollapsed()
          Returns whether this fold is collapsed.
 boolean isOnSingleLine()
          Returns whether this fold is entirely on a single line.
 boolean removeFromParent()
          Removes this fold from its parent.
 void setCollapsed(boolean collapsed)
          Sets whether this Fold is collapsed.
 void setEndOffset(int endOffs)
          Sets the ending offset of this fold, such as the closing curly brace of a code block in C or Java.
 void toggleCollapsedState()
          Toggles the collapsed state of this fold.
 java.lang.String toString()
          Overridden for debugging purposes.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Fold

public Fold(int type,
            RSyntaxTextArea textArea,
            int startOffs)
     throws javax.swing.text.BadLocationException
Throws:
javax.swing.text.BadLocationException
Method Detail

createChild

public Fold createChild(int type,
                        int startOffs)
                 throws javax.swing.text.BadLocationException
Creates a fold that is a child of this one.

Parameters:
type - The type of fold.
startOffs - The starting offset of the fold.
Returns:
The child fold.
Throws:
javax.swing.text.BadLocationException - If startOffs is invalid.
See Also:
FoldType

compareTo

public int compareTo(Fold otherFold)
Two folds are considered equal if they start at the same offset.

Specified by:
compareTo in interface java.lang.Comparable<Fold>
Parameters:
otherFold - Another fold to compare this one to.
Returns:
How this fold compares to the other.

containsLine

public boolean containsLine(int line)
Returns whether the specified line would be hidden in this fold. Since RSTA displays the "first" line in a fold, this means that the line must must be between (getStartLine()+1) and getEndLine(), inclusive.

Parameters:
line - The line to check.
Returns:
Whether the line would be hidden if this fold is collapsed.
See Also:
containsOffset(int), containsOrStartsOnLine(int)

containsOrStartsOnLine

public boolean containsOrStartsOnLine(int line)
Returns whether the given line is in the range [getStartLine(), getEndLine()], inclusive.

Parameters:
line - The line to check.
Returns:
Whether this fold contains, or starts on, the line.
See Also:
containsLine(int)

containsOffset

public boolean containsOffset(int offs)
Returns whether the specified offset is "inside" the fold. This method returns true if the offset is greater than the fold start offset, and no further than the last offset of the last folded line.

Parameters:
offs - The offset to check.
Returns:
Whether the offset is "inside" the fold.
See Also:
containsLine(int)

equals

public boolean equals(java.lang.Object otherFold)
Two folds are considered equal if they have the same starting offset.

Overrides:
equals in class java.lang.Object
Parameters:
otherFold - Another fold to compare this one to.
Returns:
Whether the two folds are equal.
See Also:
compareTo(Fold)

getChild

public Fold getChild(int index)
Returns a specific child fold.

Parameters:
index - The index of the child fold.
Returns:
The child fold.
See Also:
getChildCount()

getChildCount

public int getChildCount()
Returns the number of child folds.

Returns:
The number of child folds.
See Also:
getChild(int)

getCollapsedLineCount

public int getCollapsedLineCount()
Returns the number of collapsed lines under this fold. If this fold is collapsed, this method returns getLineCount(), otherwise it returns the sum of all collapsed lines of all child folds of this one.

The value returned is cached, so this method returns quickly and shouldn't affect performance.

Returns:
The number of collapsed lines under this fold.

getEndLine

public int getEndLine()
Returns the end line of this fold. For example, in languages such as C and Java, this might be the line containing the closing curly brace of a code block.

The value returned by this method will automatically update as the text area's contents are modified, to track the ending line of the code block.

Returns:
The end line of this code block.
See Also:
getEndOffset(), getStartLine()

getEndOffset

public int getEndOffset()
Returns the end offset of this fold. For example, in languages such as C and Java, this might be the offset of the closing curly brace of a code block.

The value returned by this method will automatically update as the text area's contents are modified, to track the ending offset of the code block.

Returns:
The end offset of this code block, or Integer.MAX_VALUE if this fold region isn't closed properly. The latter causes this fold to collapsed all lines through the end of the file.
See Also:
getEndLine(), getStartOffset()

getFoldType

public int getFoldType()
Returns the type of fold this is. This will be one of the values in FoldType, or a user-defined value.

Returns:
The type of fold this is.

getHasChildFolds

public boolean getHasChildFolds()
Returns whether this fold has any child folds.

Returns:
Whether this fold has any children.
See Also:
getChildCount()

getLastChild

public Fold getLastChild()
Returns the last child fold.

Returns:
The last child fold, or null if this fold does not have any children.
See Also:
getChild(int), getHasChildFolds()

getLineCount

public int getLineCount()
Returns the number of lines that are hidden when this fold is collapsed.

Returns:
The number of lines hidden.
See Also:
getStartLine(), getEndLine()

getParent

public Fold getParent()
Returns the parent fold of this one.

Returns:
The parent fold, or null if this is a top-level fold.

getStartLine

public int getStartLine()
Returns the starting line of this fold region. This is the only line in the fold region that is not hidden when a fold is collapsed.

The value returned by this method will automatically update as the text area's contents are modified, to track the starting line of the code block.

Returns:
The starting line of the code block.
See Also:
getEndLine(), getStartOffset()

getStartOffset

public int getStartOffset()
Returns the starting offset of this fold region. For example, for languages such as C and Java, this would be the offset of the opening curly brace of a code block.

The value returned by this method will automatically update as the text area's contents are modified, to track the starting offset of the code block.

Returns:
The start offset of this fold.
See Also:
getStartLine(), getEndOffset()

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

isCollapsed

public boolean isCollapsed()
Returns whether this fold is collapsed.

Returns:
Whether this fold is collapsed.
See Also:
setCollapsed(boolean), toggleCollapsedState()

isOnSingleLine

public boolean isOnSingleLine()
Returns whether this fold is entirely on a single line. In general, a FoldParser should not remember fold regions all on a single line, since there's really nothing to fold.

Returns:
Whether this fold is on a single line.
See Also:
removeFromParent()

removeFromParent

public boolean removeFromParent()
Removes this fold from its parent. This should only be called by FoldParser implementations if they determine that a fold is all on a single line (and thus shouldn't be remembered) after creating it.

Returns:
Whether this fold had a parent to be removed from.
See Also:
isOnSingleLine()

setCollapsed

public void setCollapsed(boolean collapsed)
Sets whether this Fold is collapsed. Calling this method will update both the text area and all Gutter components.

Parameters:
collapsed - Whether this fold should be collapsed.
See Also:
isCollapsed(), toggleCollapsedState()

setEndOffset

public void setEndOffset(int endOffs)
                  throws javax.swing.text.BadLocationException
Sets the ending offset of this fold, such as the closing curly brace of a code block in C or Java. FoldParser implementations should call this on an existing Fold upon finding its end. If this method isn't called, then this Fold is considered to have no end, i.e., it will collapse everything to the end of the file.

Parameters:
endOffs - The end offset of this fold.
Throws:
javax.swing.text.BadLocationException - If endOffs is not a valid location in the text area.

toggleCollapsedState

public void toggleCollapsedState()
Toggles the collapsed state of this fold.

See Also:
setCollapsed(boolean)

toString

public java.lang.String toString()
Overridden for debugging purposes.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this Fold.