📄 rtextareabase.java
字号:
/*****************************************************************************/
/**
* Returns the default caret color.
*
* @return The default caret color.
*/
public static final Color getDefaultCaretColor() {
return DEFAULT_CARET_COLOR;
}
/*****************************************************************************/
/**
* Returns the "default" color for highlighting the current line. Note
* that this color was chosen only because it looks nice (to me) against a
* white background.
*
* @return The default color for highlighting the current line.
*/
public static final Color getDefaultCurrentLineHighlightColor() {
return DEFAULT_CURRENT_LINE_HIGHLIGHT_COLOR;
}
/*****************************************************************************/
/**
* Returns the default color for the margin line.
*
* @return The default margin line color.
* @see #getMarginLineColor
* @see #setMarginLineColor
*/
public static final Color getDefaultMarginLineColor() {
return DEFAULT_MARGIN_LINE_COLOR;
}
/*****************************************************************************/
/**
* Returns the default margin line position.
*
* @return The default margin line position.
* @see #getMarginLinePosition
* @see #setMarginLinePosition
*/
public static final int getDefaultMarginLinePosition() {
return DEFAULT_MARGIN_LINE_POSITION;
}
/*****************************************************************************/
/**
* Returns the default selection color for this text area. This
* color was chosen because it's light and <code>RTextArea</code>
* does not change text color between selected/unselected text for
* contrast like regular <code>JTextArea</code>s do.
*
* @return The default selection color.
*/
public static Color getDefaultSelectionColor() {
return DEFAULT_SELECTION_COLOR;
}
/*****************************************************************************/
/**
* Returns the default tab size, in spaces.
*
* @return The default tab size.
*/
public static final int getDefaultTabSize() {
return DEFAULT_TAB_SIZE;
}
/*****************************************************************************/
/**
* Returns whether the current line highlight is faded.
*
* @return Whether the current line highlight is faded.
* @see #setFadeCurrentLineHighlight
*/
public boolean getFadeCurrentLineHighlight() {
return fadeCurrentLineHighlight;
}
/*****************************************************************************/
/**
* Returns the offset of the last character of the line that the caret is
* on.
*
* @return The last offset of the line that the caret is currently on.
*/
public final int getLineEndOffsetOfCurrentLine() {
try {
return getLineEndOffset(getCaretLineNumber());
}
catch (BadLocationException ble) {
/* Will never happen. */
return 0;
}
}
/*****************************************************************************/
/**
* Returns the height of a line of text in this text area.
*
* @return The height of a line of text.
*/
public int getLineHeight() {
return getRowHeight();
}
/*****************************************************************************/
/**
* Returns the offset of the first character of the line that the caret is
* on.
*
* @return The first offset of the line that the caret is currently on.
*/
public final int getLineStartOffsetOfCurrentLine() {
try {
return getLineStartOffset(getCaretLineNumber());
}
catch (BadLocationException ble) {
/* Will never happen. */
return 0;
}
}
/*****************************************************************************/
/**
* Returns the color used to paint the margin line.
*
* @return The margin line color.
* @see #setMarginLineColor
*/
public Color getMarginLineColor() {
return marginLineColor;
}
/*****************************************************************************/
/**
* Returns the margin line position (in pixels) from the left-hand side of
* the text area.
*
* @return The margin line position.
* @see #getDefaultMarginLinePosition
* @see #setMarginLinePosition
*/
public int getMarginLinePixelLocation() {
return marginLineX;
}
/*****************************************************************************/
/**
* Returns the margin line position (which is the number of 'm' widths in
* the current font the margin line is over).
*
* @return The margin line position.
* @see #getDefaultMarginLinePosition
* @see #setMarginLinePosition
*/
public int getMarginLinePosition() {
return marginSizeInChars;
}
/*****************************************************************************/
/**
* Returns whether selection edges are rounded in this text area.
*
* @return Whether selection edges are rounded.
* @see #setRoundedSelectionEdges
*/
public boolean getRoundedSelectionEdges() {
return roundedSelectionEdges;
}
/*****************************************************************************/
/**
* Returns whether or not tabs are emulated with spaces (i.e., "soft"
* tabs).
*
* @return <code>true</code> if tabs are emulated with spaces;
* <code>false</code> if they aren't.
* @see #setTabsEmulated
*/
public boolean getTabsEmulated() {
return tabsEmulatedWithSpaces;
}
/*****************************************************************************/
/**
* Determines the word closest to/at the given model position.
* Uses BreakIterator.getWordInstance() to actually get the words.
*
* @param offs The offset in the document >= 0
* @return The word closest to/at that position.
* @exception BadLocationException if the offset is out of range
*/
public final String getWordAtOffset(int offs) throws BadLocationException {
Document doc = getDocument();
Element map = doc.getDefaultRootElement();
int lineNumber = map.getElementIndex(offs);
Element line = map.getElement(lineNumber);
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
String s = doc.getText(lineStart, lineEnd - lineStart);
if (s != null && s.length() > 0) {
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int wordPosition = offs - lineStart;
if (wordPosition >= words.last()) {
wordPosition = words.last() - 1;
}
//words.following(wordPosition);
int end = lineStart + words.following(wordPosition);
int start = lineStart + words.previous();
return doc.getText(start, end - start);
}
return null;
}
/*****************************************************************************/
/**
* Returns whether or not the current line is highlighted.
*
* @return Whether or the current line is highlighted.
* @see #setCurrentLineHighlightEnabled
* @see #getCurrentLineHighlightColor
* @see #setCurrentLineHighlightColor
*/
public final boolean isCurrentLineHighlightEnabled() {
return highlightCurrentLine;
}
/*****************************************************************************/
/**
* Returns whether or not the margin line is being painted.
*
* @return Whether or not the margin line is being painted.
* @see #setMarginLineEnabled
*/
public boolean isMarginLineEnabled() {
return marginLineEnabled;
}
/*****************************************************************************/
/**
* Paints the text area.
*
* @param g The graphics context with which to paint.
*/
public void paintComponent(Graphics g) {
//long startTime = System.currentTimeMillis();
backgroundPainter.paint(g, getVisibleRect());
// Paint the main part of the text area.
TextUI ui = getUI();
if (ui != null) {
// Not allowed to modify g, so make a copy.
Graphics scratchGraphics = g.create();
try {
ui.update(scratchGraphics, this);
}
finally {
scratchGraphics.dispose();
}
}
//long endTime = System.currentTimeMillis();
//System.err.println(endTime-startTime);
}
/*****************************************************************************/
/**
* Updates the current line highlight location.
*/
protected void possiblyUpdateCurrentLineHighlightLocation() {
int width = getWidth();
int lineHeight = getLineHeight();
int caretPos = getCaretPosition();
// If we're wrapping lines we need to check the actual y-coordinate
// of the caret, not just the line number, since a single logical
// line can span multiple physical lines.
if (getLineWrap()) {
try {
Rectangle temp = modelToView(caretPos);
if (temp != null) {
currentCaretY = temp.y;
}
}
catch (BadLocationException ble) {
ble.printStackTrace(); // Should never happen.
}
}
// No line wrap - we can simply check the line number (quicker).
else {
Document doc = getDocument();
if (doc != null) {
Element map = doc.getDefaultRootElement();
int caretLine = map.getElementIndex(caretPos);
Rectangle alloc = ( (RTextAreaUI) getUI()).
getVisibleEditorRect();
if (alloc != null) {
currentCaretY = alloc.y + caretLine * lineHeight;
}
}
}
// Repaint current line (to fill in entire highlight), and old line
// (to erase entire old highlight) if necessary.
repaint(0, currentCaretY, width, lineHeight);
if (currentCaretY != previousCaretY) {
repaint(0, previousCaretY, width, lineHeight);
}
previousCaretY = currentCaretY;
}
/*****************************************************************************/
/**
* Overridden so we can tell when the text area is resized and update the
* current-line highlight, if necessary (i.e., if it is enabled and if
* lineWrap is enabled.
*
* @param e The component event about to be sent to all registered
* <code>ComponentListener</code>s.
*/
protected void processComponentEvent(ComponentEvent e) {
// In line wrap mode, resizing the text area means that the caret's
// "line" could change - not to a different logical line, but a
// different physical one. So, here we force a repaint of the current
// line's highlight if necessary.
if (e.getID() == ComponentEvent.COMPONENT_RESIZED &&
getLineWrap() == true && isCurrentLineHighlightEnabled()) {
previousCaretY = -1; // So we are sure to repaint.
fireCaretUpdate(mouseListener);
}
super.processComponentEvent(e);
}
/*****************************************************************************/
/**
* Sets the background color of this text editor. Note that this is
* equivalent to calling <code>setBackgroundObject(bg)</code>.
*
* NOTE: the opaque property is set to <code>true</code> when the
* background is set to a color (by this method). When an image is used
* for the background, opaque is set to false. This is because
* we perform better when setOpaque is true, but if we use an
* image for the background when opaque is true, we get on-screen
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -