Class Fold
- java.lang.Object
-
- 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, andFolds
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
-
-
Field Summary
Fields Modifier and Type Field Description private int
cachedEndLine
private int
cachedStartLine
private int
childCollapsedLineCount
private java.util.List<Fold>
children
private boolean
collapsed
private javax.swing.text.Position
endOffs
private int
lastEndOffs
private int
lastStartOffs
private Fold
parent
private javax.swing.text.Position
startOffs
private RSyntaxTextArea
textArea
private int
type
-
Constructor Summary
Constructors Constructor Description Fold(int type, RSyntaxTextArea textArea, int startOffs)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.(package private) java.util.List<Fold>
getChildren()
Returns the array of child folds.int
getCollapsedLineCount()
Returns the number of collapsed lines under this fold.(package private) Fold
getDeepestFoldContaining(int offs)
Returns the "deepest" fold containing the specified offset.(package private) Fold
getDeepestOpenFoldContaining(int offs)
Returns the "deepest" open fold containing the specified offset.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.private void
removeMostRecentChild()
void
setCollapsed(boolean collapsed)
Sets whether thisFold
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.private void
updateChildCollapsedLineCount(int count)
-
-
-
Field Detail
-
type
private int type
-
textArea
private RSyntaxTextArea textArea
-
startOffs
private javax.swing.text.Position startOffs
-
endOffs
private javax.swing.text.Position endOffs
-
parent
private Fold parent
-
children
private java.util.List<Fold> children
-
collapsed
private boolean collapsed
-
childCollapsedLineCount
private int childCollapsedLineCount
-
lastStartOffs
private int lastStartOffs
-
cachedStartLine
private int cachedStartLine
-
lastEndOffs
private int lastEndOffs
-
cachedEndLine
private int cachedEndLine
-
-
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
- IfstartOffs
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 interfacejava.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 be between(getStartLine()+1)
andgetEndLine()
, 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 returnstrue
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 classjava.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)
-
getChildren
java.util.List<Fold> getChildren()
Returns the array of child folds. This is a shallow copy.- Returns:
- The array of child folds, or
null
if there are none.
-
getCollapsedLineCount
public int getCollapsedLineCount()
Returns the number of collapsed lines under this fold. If this fold is collapsed, this method returnsgetLineCount()
, 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.
-
getDeepestFoldContaining
Fold getDeepestFoldContaining(int offs)
Returns the "deepest" fold containing the specified offset. It is assumed that it's already been verified thatoffs
is indeed contained in this fold.- Parameters:
offs
- The offset.- Returns:
- The fold, or
null
if no child fold also contains the offset. - See Also:
FoldManager.getDeepestFoldContaining(int)
-
getDeepestOpenFoldContaining
Fold getDeepestOpenFoldContaining(int offs)
Returns the "deepest" open fold containing the specified offset. It is assumed that it's already been verified thatoffs
is indeed contained in this fold.- Parameters:
offs
- The offset.- Returns:
- The fold, or
null
if no open fold contains the offset. - See Also:
FoldManager.getDeepestOpenFoldContaining(int)
-
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 collapse 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 inFoldType
, 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 classjava.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, aFoldParser
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 byFoldParser
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()
-
removeMostRecentChild
private void removeMostRecentChild()
-
setCollapsed
public void setCollapsed(boolean collapsed)
Sets whether thisFold
is collapsed. Calling this method will update both the text area and allGutter
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 existingFold
upon finding its end. If this method isn't called, then thisFold
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
- IfendOffs
is not a valid location in the text area.
-
toggleCollapsedState
public void toggleCollapsedState()
Toggles the collapsed state of this fold.- See Also:
setCollapsed(boolean)
-
updateChildCollapsedLineCount
private void updateChildCollapsedLineCount(int count)
-
toString
public java.lang.String toString()
Overridden for debugging purposes.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A string representation of this
Fold
.
-
-