📄 jedittextarea.java
字号:
}
/**
* Converts an x co-ordinate to an offset within a line.
*
* @param line The line
* @param x The x co-ordinate
*/
public final int xToOffset (final int line, final int x)
{
final TokenMarker tokenMarker = getTokenMarker();
/* Use painter's cached info for speed */
FontMetrics fm = painter.getFontMetrics();
getLineText(line, lineSegment);
final char[] segmentArray = lineSegment.array;
final int segmentOffset = lineSegment.offset;
final int segmentCount = lineSegment.count;
int width = horizontalOffset;
if (tokenMarker == null)
{
for (int i = 0; i < segmentCount; i++)
{
final char c = segmentArray[i + segmentOffset];
final int charWidth;
if (c == '\t')
{
charWidth = (int) painter.nextTabStop(width, i)
- width;
}
else
{
charWidth = fm.charWidth(c);
}
if (painter.isBlockCaretEnabled())
{
if (x - charWidth <= width)
{
return i;
}
}
else
{
if (x - charWidth / 2 <= width)
{
return i;
}
}
width += charWidth;
}
return segmentCount;
}
else
{
Token tokens;
if (painter.currentLineIndex == line &&
painter.currentLineTokens != null)
{
tokens = painter.currentLineTokens;
}
else
{
painter.currentLineIndex = line;
tokens = painter.currentLineTokens
= tokenMarker.markTokens(lineSegment, line);
}
int offset = 0;
//final Toolkit toolkit = painter.getToolkit();
final Font defaultFont = painter.getFont();
final SyntaxStyle[] styles = painter.getStyles();
for (; ;)
{
final byte id = tokens.id;
if (id == Token.END)
{
return offset;
}
if (id == Token.NULL)
{
fm = painter.getFontMetrics();
}
else
{
fm = styles[id].getFontMetrics(this, defaultFont);
}
final int length = tokens.length;
for (int i = 0; i < length; i++)
{
final char c = segmentArray[segmentOffset + offset + i];
final int charWidth;
if (c == '\t')
{
charWidth = (int) painter.nextTabStop(width, offset + i)
- width;
}
else
{
charWidth = fm.charWidth(c);
}
if (painter.isBlockCaretEnabled())
{
if (x - charWidth <= width)
{
return offset + i;
}
}
else
{
if (x - charWidth / 2 <= width)
{
return offset + i;
}
}
width += charWidth;
}
offset += length;
tokens = tokens.next;
}
}
}
/**
* Converts a point to an offset, from the start of the text.
*
* @param x The x co-ordinate of the point
* @param y The y co-ordinate of the point
*/
private int xyToOffset (final int x, final int y)
{
final int line = yToLine(y);
final int start = getLineStartOffset(line);
return start + xToOffset(line, x);
}
/**
* Returns the document this text area is editing.
*/
public final SyntaxDocument getDocument ()
{
return document;
}
/**
* Sets the document this text area is editing.
*
* @param document The document
*/
private void setDocument (final SyntaxDocument document)
{
if (this.document == document)
{
return;
}
if (this.document != null)
{
this.document.removeDocumentListener(documentHandler);
}
this.document = document;
document.addDocumentListener(documentHandler);
select(0, 0);
updateScrollBars();
painter.repaint();
}
/**
* Returns the document's token marker. Equivalent to calling
* <code>getDocument().getTokenMarker()</code>.
*/
private TokenMarker getTokenMarker ()
{
return document.getTokenMarker();
}
/**
* Sets the document's token marker. Equivalent to caling <code>getDocument().setTokenMarker()</code>.
*
* @param tokenMarker The token marker
*/
public final void setTokenMarker (final TokenMarker tokenMarker)
{
document.setTokenMarker(tokenMarker);
}
/**
* Returns the length of the document. Equivalent to calling
* <code>getDocument().getLength()</code>.
*/
public final int getDocumentLength ()
{
return document.getLength();
}
/**
* Returns the number of lines in the document.
*/
public final int getLineCount ()
{
return document.getDefaultRootElement().getElementCount();
}
/**
* Returns the line containing the specified offset.
*
* @param offset The offset
*/
private int getLineOfOffset (final int offset)
{
return document.getDefaultRootElement().getElementIndex(offset);
}
/**
* Returns the start offset of the specified line.
*
* @param line The line
* @return The start offset of the specified line, or -1 if the line is invalid
*/
public final int getLineStartOffset (final int line)
{
final Element lineElement = document.getDefaultRootElement()
.getElement(line);
if (lineElement == null)
{
return -1;
}
else
{
return lineElement.getStartOffset();
}
}
/**
* Returns the end offset of the specified line.
*
* @param line The line
* @return The end offset of the specified line, or -1 if the line is invalid.
*/
public final int getLineEndOffset (final int line)
{
final Element lineElement = document.getDefaultRootElement()
.getElement(line);
if (lineElement == null)
{
return -1;
}
else
{
return lineElement.getEndOffset();
}
}
/**
* Returns the length of the specified line.
*
* @param line The line
*/
public final int getLineLength (final int line)
{
final Element lineElement = document.getDefaultRootElement()
.getElement(line);
if (lineElement == null)
{
return -1;
}
else
{
return lineElement.getEndOffset()
- lineElement.getStartOffset() - 1;
}
}
/**
* Returns the entire text of this text area.
*/
public final String getText ()
{
try
{
return document.getText(0, document.getLength());
}
catch (BadLocationException bl)
{
bl.printStackTrace();
return null;
}
}
/**
* Sets the entire text of this text area.
*/
public final void setText (final String text)
{
try
{
document.beginCompoundEdit();
document.remove(0, document.getLength());
document.insertString(0, text, null);
}
catch (BadLocationException bl)
{
bl.printStackTrace();
}
finally
{
document.endCompoundEdit();
}
}
/**
* Returns the specified substring of the document.
*
* @param start The start offset
* @param len The length of the substring
* @return The substring, or null if the offsets are invalid
*/
private String getText (final int start, final int len)
{
try
{
return document.getText(start, len);
}
catch (BadLocationException bl)
{
bl.printStackTrace();
return null;
}
}
/**
* Copies the specified substring of the document into a segment. If the offsets are
* invalid, the segment will contain a null string.
*
* @param start The start offset
* @param len The length of the substring
* @param segment The segment
*/
private void getText (final int start, final int len, final Segment segment)
{
try
{
document.getText(start, len, segment);
}
catch (BadLocationException bl)
{
bl.printStackTrace();
segment.offset = segment.count = 0;
}
}
/**
* Returns the text on the specified line.
*
* @param lineIndex The line
* @return The text, or null if the line is invalid
*/
public final String getLineText (final int lineIndex)
{
final int start = getLineStartOffset(lineIndex);
return getText(start, getLineEndOffset(lineIndex) - start - 1);
}
/**
* Copies the text on the specified line into a segment. If the line is invalid, the
* segment will contain a null string.
*
* @param lineIndex The line
*/
public final void getLineText (final int lineIndex, final Segment segment)
{
final int start = getLineStartOffset(lineIndex);
getText(start, getLineEndOffset(lineIndex) - start - 1, segment);
}
/**
* Returns the selection start offset.
*/
public final int getSelectionStart ()
{
return selectionStart;
}
/**
* Returns the offset where the selection starts on the specified line.
*/
public final int getSelectionStart (final int line)
{
if (line == selectionStartLine)
{
return selectionStart;
}
else if (rectSelect)
{
final Element map = document.getDefaultRootElement();
final int start = selectionStart - map.getElement(selectionStartLine)
.getStartOffset();
final Element lineElement = map.getElement(line);
final int lineStart = lineElement.getStartOffset();
final int lineEnd = lineElement.getEndOffset() - 1;
return Math.min(lineEnd, lineStart + start);
}
else
{
return getLineStartOffset(line);
}
}
/**
* Returns the selection start line.
*/
public final int getSelectionStartLine ()
{
return selectionStartLine;
}
/**
* Sets the selection start. The new selection will be the new selection start and the
* old selection end.
*
* @param selectionStart The selection start
* @see #select(int,int)
*/
public final void setSelectionStart (final int selectionStart)
{
select(selectionStart, selectionEnd);
}
/**
* Returns the selection end offset.
*/
public final int getSelectionEnd ()
{
return selectionEnd;
}
/**
* Returns the offset where the selection ends on the specified line.
*/
public final int getSelectionEnd (final int line)
{
if (line == selectionEndLine)
{
return selectionEnd;
}
else if (rectSelect)
{
final Element map = document.getDefaultRootElement();
final int end = selectionEnd - map.getElement(selectionEndLine)
.getStartOffset();
final Element lineElement = map.getElement(line);
final int lineStart = lineElement.getStartOffset();
final int lineEnd = lineElement.getEndOffset() - 1;
return Math.min(lineEnd, lineStart + end);
}
else
{
return getLineEndOffset(line) - 1;
}
}
/**
* Returns the selection end line.
*/
public final int getSelectionEndLine ()
{
return selectionEndLine;
}
/**
* Sets the selection end. The new selection will be the old selection start and the bew
* selection end.
*
* @param selectionEnd The selection end
* @see #select(int,int)
*/
public final void setSelectionEnd (final int selectionEnd)
{
select(selectionStart, selectionEnd);
}
/**
* Returns the caret position. This will either be the selection start or the selection
* end, depending on which direction the selection was made in.
*/
public final int getCaretPosition ()
{
return (biasLeft ? selectionStart : selectionEnd);
}
/**
* Returns the caret line.
*/
public final int getCaretLine ()
{
return (biasLeft ? selectionStartLine : selectionEndLine);
}
/**
* Returns the mark position. This will be the opposite selection bound to the caret
* position.
*
* @see #getCaretPosition()
*/
public final int getMarkPosition ()
{
return (biasLeft ? selectionEnd : selectionStart);
}
/**
* Returns the mark line.
*/
public final int getMarkLine ()
{
return (biasLeft ? selectionEndLine : selectionStartLine);
}
/**
* Sets the caret position. The new selection will consist of the caret position only
* (hence no text will be selected)
*
* @param caret The caret position
* @see #select(int,int)
*/
public final void setCaretPosition (final int caret)
{
select(caret, caret);
}
/**
* Selects all text in the document.
*/
public final void selectAll ()
{
select(0, getDocumentLength());
}
/**
* Moves the mark to the caret position.
*/
public final void selectNone ()
{
select(getCaretPosition(), getCaretPosition());
}
/**
* Selects from the start offset to the end offset. This is the general selection method
* used by all other selecting methods. The caret position will be start if start <
* end, and end if end > start.
*
* @param start The start offset
* @param end The end offset
*/
public final void select (final int start, final int end)
{
final int newStart;
final int newEnd;
final boolean newBias;
if (start <= end)
{
newStart = start;
newEnd = end;
newBias = false;
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -