public class JEditBuffer
extends java.lang.Object
JEditBuffer
represents the contents of an open text
file as it is maintained in the computer's memory (as opposed to
how it may be stored on a disk).This class is partially thread-safe, however you must pay attention to two very important guidelines:
Modifier and Type | Class and Description |
---|---|
protected static class |
JEditBuffer.PropValue |
Modifier and Type | Field and Description |
---|---|
java.lang.Object |
columnBlockLock |
protected boolean |
contextInsensitive
If true the syntax highlight is context insensitive.
|
boolean |
elasticTabstopsOn
This field should be read instead of "elasticTabstops" property
when efficiency matters.
|
static java.lang.String |
ENCODING
Character encoding used when loading and saving.
|
static int |
HIGH_PRIORITY |
static java.lang.String |
LINESEP
Line separator property.
|
protected Mode |
mode
The edit mode of the buffer.
|
static int |
NORMAL_PRIORITY |
protected TokenMarker |
tokenMarker |
protected UndoManager |
undoMgr |
Constructor and Description |
---|
JEditBuffer()
Create a new JEditBuffer.
|
JEditBuffer(java.util.Map props) |
Modifier and Type | Method and Description |
---|---|
void |
addBufferListener(BufferListener listener)
Adds a buffer change listener.
|
void |
addBufferListener(BufferListener listener,
int priority)
Adds a buffer change listener.
|
void |
beginCompoundEdit()
Starts a compound edit.
|
boolean |
canRedo() |
boolean |
canUndo() |
javax.swing.text.Position |
createPosition(int offset)
Creates a floating position (
javax.swing.text.Position ). |
void |
endCompoundEdit()
Ends a compound edit.
|
protected void |
fireBeginRedo() |
protected void |
fireBeginUndo() |
protected void |
fireBufferLoaded() |
protected void |
fireContentInserted(int startLine,
int offset,
int numLines,
int length) |
protected void |
fireContentRemoved(int startLine,
int offset,
int numLines,
int length) |
protected void |
fireEndRedo() |
protected void |
fireEndUndo() |
protected void |
fireFoldHandlerChanged() |
protected void |
fireFoldLevelChanged(int start,
int end) |
protected void |
firePreContentInserted(int startLine,
int offset,
int numLines,
int length) |
protected void |
firePreContentRemoved(int startLine,
int offset,
int numLines,
int length) |
protected void |
fireTransactionComplete() |
boolean |
getBooleanProperty(java.lang.String name) |
boolean |
getBooleanProperty(java.lang.String name,
boolean def) |
BufferListener[] |
getBufferListeners() |
ColumnBlock |
getColumnBlock() |
java.lang.String |
getContextSensitiveProperty(int offset,
java.lang.String name)
Some settings, like comment start and end strings, can
vary between different parts of a buffer (HTML text and inline
JavaScript, for example).
|
int |
getCurrentIndentForLine(int lineIndex,
int[] whitespaceChars) |
java.lang.Object |
getDefaultProperty(java.lang.String key) |
int[] |
getFoldAtLine(int line) |
FoldHandler |
getFoldHandler() |
int |
getFoldLevel(int line) |
int |
getIdealIndentForLine(int lineIndex) |
int |
getIndentSize() |
int |
getIntegerProperty(java.lang.String name,
int defaultValue) |
KeywordMap |
getKeywordMapAtOffset(int offset) |
int |
getLength() |
TokenMarker.LineContext |
getLineContext(int line)
Returns the line context of the token marker for the specified line.
|
int |
getLineCount() |
int |
getLineEndOffset(int line)
Returns the end offset of the specified line.
|
int |
getLineLength(int line) |
int |
getLineOfOffset(int offset)
Returns the line containing the specified offset.
|
java.lang.CharSequence |
getLineSegment(int line)
Returns the text on the specified line.
|
int |
getLineStartOffset(int line)
Returns the start offset of the specified line.
|
java.lang.String |
getLineText(int line)
Returns the text on the specified line.
|
void |
getLineText(int line,
int relativeStartOffset,
javax.swing.text.Segment segment)
Returns the specified line from the starting point passed in relativeStartOffset in a
Segment . |
void |
getLineText(int line,
javax.swing.text.Segment segment)
Returns the specified line in a
Segment . |
Mode |
getMode() |
int |
getOffsetOfVirtualColumn(int line,
int column,
int[] totalVirtualWidth)
Returns the offset of a virtual column number (taking tabs
into account) relative to the start of the line in question.
|
java.util.regex.Pattern |
getPatternProperty(java.lang.String name,
int flags) |
int |
getPriorNonEmptyLine(int lineIndex)
Auto indent needs this.
|
java.lang.Object |
getProperty(java.lang.Object name) |
ParserRuleSet |
getRuleSetAtOffset(int offset) |
java.lang.CharSequence |
getSegment(int start,
int length)
Returns the specified text range.
|
java.lang.String |
getStringProperty(java.lang.String name) |
int |
getTabSize() |
int |
getTabStopPosition(javax.swing.text.Segment seg) |
java.lang.String |
getText() |
java.lang.String |
getText(int start,
int length) |
void |
getText(int start,
int length,
javax.swing.text.Segment seg)
Returns the specified text range in a
Segment . |
TokenMarker |
getTokenMarker() |
java.lang.Object |
getUndoId() |
int |
getVirtualWidth(int line,
int column) |
boolean |
indentLine(int lineIndex,
boolean canDecreaseIndent)
Indents the specified line.
|
void |
indentLines(int[] lines)
Indents all specified lines.
|
void |
indentLines(int start,
int end)
Indents all specified lines.
|
void |
indentUsingElasticTabstops() |
void |
insert(int offset,
java.lang.CharSequence seq)
Inserts a string into the buffer.
|
void |
insert(int offset,
javax.swing.text.Segment seg)
Inserts a string into the buffer.
|
void |
insert(int offset,
java.lang.String str)
Inserts a string into the buffer.
|
void |
insertAtColumn(int line,
int col,
java.lang.String str)
Like the
insert(int,String) method, but inserts the string at
the specified virtual column. |
int |
insertIndented(int offset,
java.lang.String text)
Inserts a string into the buffer, indenting each line of the string
to match the indent of the first line.
|
boolean |
insideCompoundEdit() |
void |
invalidateCachedFoldLevels()
Invalidates all cached fold level information.
|
protected void |
invalidateFoldLevels() |
boolean |
isContextInsensitive()
Returns true if the buffer highlight is
not sensitive to the context.
|
boolean |
isDirty() |
boolean |
isEditable() |
boolean |
isElectricKey(char ch,
int line)
Should inserting this character trigger a re-indent of
the current line?
|
protected boolean |
isFileReadOnly() |
boolean |
isFoldEnd(int line) |
boolean |
isFoldStart(int line) |
boolean |
isLoading() |
boolean |
isPerformingIO() |
boolean |
isReadOnly() |
boolean |
isTransactionInProgress() |
boolean |
isUndoInProgress() |
protected void |
loadText(javax.swing.text.Segment seg,
IntegerArray endOffsets) |
void |
markTokens(int lineIndex,
TokenHandler tokenHandler)
Returns the syntax tokens for the specified line.
|
protected TokenMarker.LineContext |
markTokens(javax.swing.text.Segment seg,
TokenMarker.LineContext prevContext,
TokenHandler _tokenHandler) |
protected void |
parseBufferLocalProperties() |
void |
propertiesChanged()
Reloads settings from the properties.
|
void |
readLock()
The buffer is guaranteed not to change between calls to
readLock() and readUnlock() . |
void |
readUnlock()
The buffer is guaranteed not to change between calls to
readLock() and readUnlock() . |
void |
redo(TextArea textArea)
Redoes the most recently undone edit.
|
void |
remove(int offset,
int length)
Removes the specified rang efrom the buffer.
|
void |
removeBufferListener(BufferListener listener)
Removes a buffer change listener.
|
void |
removeTrailingWhiteSpace(int[] lines)
Removes trailing whitespace from all lines in the specified list.
|
void |
resetCachedProperties() |
void |
setBooleanProperty(java.lang.String name,
boolean value)
Sets a boolean property.
|
void |
setContextInsensitive(boolean contextInsensitive)
Set the buffer to be insensitive to the context during
highlight.
|
void |
setDefaultProperty(java.lang.String name,
java.lang.Object value) |
void |
setDirty(boolean d) |
protected void |
setFileReadOnly(boolean readOnly) |
void |
setFoldHandler(FoldHandler foldHandler)
Sets the buffer's fold handler.
|
void |
setIntegerProperty(java.lang.String name,
int value)
Sets an integer property.
|
void |
setLoading(boolean loading) |
void |
setMode(Mode mode)
Sets this buffer's edit mode.
|
void |
setMode(Mode mode,
boolean forceContextInsensitive)
Sets this buffer's edit mode.
|
void |
setMode(java.lang.String mode)
Sets this buffer's edit mode.
|
void |
setPerformingIO(boolean io) |
void |
setProperty(java.lang.String name,
java.lang.Object value)
Sets the value of a buffer-local property.
|
void |
setReadOnly(boolean readOnly)
Sets the read only flag.
|
void |
setStringProperty(java.lang.String name,
java.lang.String value)
Sets a string property.
|
void |
setTokenMarker(TokenMarker tokenMarker) |
void |
setUndoLimit(int limit)
Set the undo limit of the Undo Manager.
|
void |
shiftIndentLeft(int[] lines)
Shifts the indent of each line in the specified list to the left.
|
void |
shiftIndentRight(int[] lines)
Shifts the indent of each line in the specified list to the right.
|
void |
simpleIndentLine(int lineIndex)
Simply indents the given line to the same level as the previous nonempty line
|
void |
undo(TextArea textArea)
Undoes the most recent edit.
|
void |
unsetProperty(java.lang.String name)
Clears the value of a buffer-local property.
|
void |
updateColumnBlocks(int startLine,
int endLine,
int startColumn,
Node parent) |
void |
writeLock()
Attempting to obtain read lock will block between calls to
writeLock() and writeUnlock() . |
void |
writeUnlock()
Attempting to obtain read lock will block between calls to
writeLock() and writeUnlock() . |
public static final java.lang.String LINESEP
public static final java.lang.String ENCODING
public static final int NORMAL_PRIORITY
public static final int HIGH_PRIORITY
protected Mode mode
protected boolean contextInsensitive
protected UndoManager undoMgr
protected TokenMarker tokenMarker
public boolean elasticTabstopsOn
public final java.lang.Object columnBlockLock
public JEditBuffer(java.util.Map props)
public JEditBuffer()
public boolean isDirty()
public boolean isLoading()
public void setLoading(boolean loading)
public boolean isPerformingIO()
public void setPerformingIO(boolean io)
io
- true if the buffer is currently performing I/O.
This method is thread-safe.public boolean isEditable()
public boolean isReadOnly()
public void setReadOnly(boolean readOnly)
readOnly
- The read only flagpublic void setDirty(boolean d)
d
- Sets the 'dirty' (changed since last save) flag of this buffer.public void readLock()
readLock()
and readUnlock()
.
Calls to this method may be nested.public void readUnlock()
readLock()
and readUnlock()
.public void writeLock()
writeLock()
and writeUnlock()
.
Calls to this method may be nested.public void writeUnlock()
writeLock()
and writeUnlock()
.public int getLength()
public int getLineCount()
public int getLineOfOffset(int offset)
offset
- The offsetpublic int getLineStartOffset(int line)
line
- The linepublic int getLineEndOffset(int line)
line
- The linebuffer.getLineOfOffset(buffer.getLineEndOffset(x))
does not return x
but x+1
.public int getLineLength(int line)
line
- The linepublic int getPriorNonEmptyLine(int lineIndex)
lineIndex
- the linepublic java.lang.String getLineText(int line)
line
- The linepublic void getLineText(int line, javax.swing.text.Segment segment)
Segment
.
Using a Segment
is generally more
efficient than using a String
because it
results in less memory allocation and array copying.
This method is thread-safe.
line
- The linesegment
- the segmentpublic void getLineText(int line, int relativeStartOffset, javax.swing.text.Segment segment)
Segment
.
Using a Segment
is generally more
efficient than using a String
because it
results in less memory allocation and array copying.
This method is thread-safe.
line
- The linesegment
- the segmentrelativeStartOffset
- the relative start offsetpublic java.lang.CharSequence getLineSegment(int line)
line
- The line index.public java.lang.String getText(int start, int length)
start
- The start offsetlength
- The number of characters to getpublic java.lang.String getText()
public void getText(int start, int length, javax.swing.text.Segment seg)
Segment
.
Using a Segment
is generally more
efficient than using a String
because it
results in less memory allocation and array copying.
This method is thread-safe.
start
- The start offsetlength
- The number of characters to getseg
- The segment to copy the text topublic java.lang.CharSequence getSegment(int start, int length)
start
- The start offsetlength
- The number of characters to getpublic void insert(int offset, java.lang.String str)
offset
- The offsetstr
- The stringpublic void insert(int offset, javax.swing.text.Segment seg)
offset
- The offsetseg
- The segmentpublic void insert(int offset, java.lang.CharSequence seq)
offset
- The offsetseq
- The charsequencepublic void remove(int offset, int length)
offset
- The start offsetlength
- The number of characters to removepublic void removeTrailingWhiteSpace(int[] lines)
lines
- The line numberspublic void shiftIndentLeft(int[] lines)
lines
- The line numberspublic void shiftIndentRight(int[] lines)
lines
- The line numberspublic void indentLines(int start, int end)
start
- The first line to indentend
- The last line to indentpublic void indentLines(int[] lines)
lines
- The line numberspublic void simpleIndentLine(int lineIndex)
lineIndex
- The line number to indentpublic boolean indentLine(int lineIndex, boolean canDecreaseIndent)
lineIndex
- The line number to indentcanDecreaseIndent
- If true, the indent can be decreased as a
result of this. Set this to false for Tab key.public int getCurrentIndentForLine(int lineIndex, int[] whitespaceChars)
lineIndex
- The line numberwhitespaceChars
- If this is non-null, the number of whitespace
characters is stored at the 0 indexpublic int getIdealIndentForLine(int lineIndex)
lineIndex
- The line numberpublic int getVirtualWidth(int line, int column)
line
- The line numbercolumn
- The column numberpublic int getOffsetOfVirtualColumn(int line, int column, int[] totalVirtualWidth)
line
- The line numbercolumn
- The virtual column numbertotalVirtualWidth
- If this array is non-null, the total
virtual width will be stored in its first location if this method
returns -1.public void insertAtColumn(int line, int col, java.lang.String str)
insert(int,String)
method, but inserts the string at
the specified virtual column. Inserts spaces as appropriate if
the line is shorter than the column.line
- The line numbercol
- The virtual column numberstr
- The stringpublic int insertIndented(int offset, java.lang.String text)
offset
- The offsettext
- The textpublic boolean isElectricKey(char ch, int line)
ch
- a characterline
- the linepublic TokenMarker.LineContext getLineContext(int line)
public void markTokens(int lineIndex, TokenHandler tokenHandler)
lineIndex
- The line numbertokenHandler
- The token handler that will receive the syntax
tokenspublic TokenMarker getTokenMarker()
public void setTokenMarker(TokenMarker tokenMarker)
public javax.swing.text.Position createPosition(int offset)
javax.swing.text.Position
).
The position is retained despite text editions.
No explicit removal of position is necessary, only dereferencing it.
offset
- The offsetpublic void propertiesChanged()
syntax
or folding
buffer-local properties are changed.public int getTabSize()
getProperty("tabSize")
.
This method is thread-safe.public int getIndentSize()
getProperty("indentSize")
.
This method is thread-safe.public java.lang.Object getProperty(java.lang.Object name)
name
- The property name. For backwards compatibility, this
is an Object
, not a String
.Object
which must be cast to another type
in order to be useful, and this can cause problems if the object
is of a different type than what the caller expects.The following methods should be used instead:
This method is thread-safe.public java.lang.Object getDefaultProperty(java.lang.String key)
public void setProperty(java.lang.String name, java.lang.Object value)
name
- The property namevalue
- The property valuepublic void setDefaultProperty(java.lang.String name, java.lang.Object value)
public void unsetProperty(java.lang.String name)
name
- The property namepublic void resetCachedProperties()
public java.lang.String getStringProperty(java.lang.String name)
name
- The property namepublic void setStringProperty(java.lang.String name, java.lang.String value)
name
- The property namevalue
- The valuepublic boolean getBooleanProperty(java.lang.String name)
name
- The property namepublic boolean getBooleanProperty(java.lang.String name, boolean def)
name
- The property namedef
- The default valuepublic void setBooleanProperty(java.lang.String name, boolean value)
name
- The property namevalue
- The valuepublic int getIntegerProperty(java.lang.String name, int defaultValue)
name
- The property namedefaultValue
- the default value to usepublic void setIntegerProperty(java.lang.String name, int value)
name
- The property namevalue
- The valuepublic java.util.regex.Pattern getPatternProperty(java.lang.String name, int flags)
name
- The property nameflags
- Regular expression compilation flagspublic ParserRuleSet getRuleSetAtOffset(int offset)
offset
- the offsetpublic KeywordMap getKeywordMapAtOffset(int offset)
offset
- The offsetpublic java.lang.String getContextSensitiveProperty(int offset, java.lang.String name)
offset
- The offsetname
- The property namepublic Mode getMode()
public void setMode(java.lang.String mode)
mode
- The mode namepublic void setMode(Mode mode)
mode
- The modepublic void setMode(Mode mode, boolean forceContextInsensitive)
mode
- The modeforceContextInsensitive
- true if you want to force the buffer to be
insensitive to the context. Careful it can break syntax highlight. Default
value is falsepublic boolean isFoldStart(int line)
line
- the linepublic boolean isFoldEnd(int line)
line
- the linepublic void invalidateCachedFoldLevels()
public int getFoldLevel(int line)
line
- A physical line indexpublic int[] getFoldAtLine(int line)
line
- The line number@Nonnull public FoldHandler getFoldHandler()
public void setFoldHandler(@Nonnull FoldHandler foldHandler)
foldHandler
- the fold handlerpublic void undo(TextArea textArea)
textArea
- the text areapublic void redo(TextArea textArea)
textArea
- the textAreapublic boolean isTransactionInProgress()
BufferListener.transactionComplete(JEditBuffer)
buffer event will get fired.public void beginCompoundEdit()
endCompoundEdit()
are called will be merged
into one. This can be used to make a complex operation
undoable in one step. Nested calls to
beginCompoundEdit()
behave as expected,
requiring the same number of endCompoundEdit()
calls to end the edit.endCompoundEdit()
public void endCompoundEdit()
beginCompoundEdit()
was called can now
be undone in one step by calling undo(TextArea)
.beginCompoundEdit()
public boolean insideCompoundEdit()
public boolean isUndoInProgress()
public java.lang.Object getUndoId()
public void addBufferListener(BufferListener listener, int priority)
listener
- The listenerpriority
- Listeners with HIGH_PRIORITY get the event before
listeners with NORMAL_PRIORITYpublic void addBufferListener(BufferListener listener)
listener
- The listenerpublic void removeBufferListener(BufferListener listener)
listener
- The listenerpublic BufferListener[] getBufferListeners()
public void setUndoLimit(int limit)
limit
- the new limitpublic boolean canUndo()
public boolean canRedo()
public boolean isContextInsensitive()
public void setContextInsensitive(boolean contextInsensitive)
contextInsensitive
- the new contextInsensitive value
the contextprotected void fireFoldLevelChanged(int start, int end)
protected void fireContentInserted(int startLine, int offset, int numLines, int length)
protected void fireContentRemoved(int startLine, int offset, int numLines, int length)
protected void firePreContentInserted(int startLine, int offset, int numLines, int length)
protected void firePreContentRemoved(int startLine, int offset, int numLines, int length)
protected void fireBeginUndo()
protected void fireEndUndo()
protected void fireBeginRedo()
protected void fireEndRedo()
protected void fireTransactionComplete()
protected void fireFoldHandlerChanged()
protected void fireBufferLoaded()
protected boolean isFileReadOnly()
protected void setFileReadOnly(boolean readOnly)
protected void loadText(javax.swing.text.Segment seg, IntegerArray endOffsets)
protected void invalidateFoldLevels()
protected void parseBufferLocalProperties()
protected TokenMarker.LineContext markTokens(javax.swing.text.Segment seg, TokenMarker.LineContext prevContext, TokenHandler _tokenHandler)
public void updateColumnBlocks(int startLine, int endLine, int startColumn, Node parent)
public int getTabStopPosition(javax.swing.text.Segment seg)
public void indentUsingElasticTabstops()
public ColumnBlock getColumnBlock()