📄 jtextarea.java
字号:
}
/**
* Implements the abstract method in charva.awt.Component.
*/
public void draw() {
Point tempCaret = null;
Point caret = _caret;
/* Get the absolute origin of this component.
*/
Point origin = getLocationOnScreen();
Toolkit term = Toolkit.getDefaultToolkit();
int colorpair = getCursesColor();
/* Start by blanking out the text area
*/
term.blankBox(origin, getSize(), colorpair);
term.setCursor(origin);
/* Scan through the entire document, drawing each character in it.
*/
ScrollEvent scrollevent = null;
int row = 0, col = 0;
outerloop:
for (int i=0; i<super._document.length(); i++) {
/* At some point during the scan of the document, the
* caret position should match the scan index, unless the caret
* position is after the last character of the document.
*/
if (_caretPosition == i) {
tempCaret = new Point(col, row);
/* If the caret has changed, generate a ScrollEvent. Note
* that this method may be called multiple times during the
* scan; however, we must post only the last event generated.
*/
if (tempCaret.equals(caret) == false) {
scrollevent = generateScrollEvent(tempCaret,
new Point(col, row));
caret = tempCaret;
}
}
char chr = super._document.charAt(i);
if (col < _columns) {
if (chr == '\n') {
col = 0;
row++;
if (row >= _rows)
_rows++;
term.setCursor(origin.addOffset(col, row));
}
else {
term.addChar(chr, 0, colorpair);
col++;
}
}
else { // We have reached the right-hand column.
if (_lineWrap == false) {
if (chr == '\n') {
col = 0;
row++;
if (row >= _rows)
_rows++;
term.setCursor(origin.addOffset(col, row));
}
else {
term.addChar(chr, 0, colorpair);
col++;
_columns++;
}
}
else { // line-wrap is true
if (_wrapStyleWord == false) {
col = 0;
row++;
if (row >= _rows)
_rows++;
term.setCursor(origin.addOffset(col, row));
if (chr != '\n') // thanks to Chris Rogers for this
term.addChar(chr, 0, colorpair);
}
else {
/* We must back-track until we get to whitespace, so
* that we can move the word to the next line.
*/
int j;
for (j=0; j<_columns; j++) {
char tmpchr = super._document.charAt(i-j);
if (tmpchr == ' ' || tmpchr == '\t') {
deleteEOL(term, col-j, row, colorpair);
col = 0;
row++;
if (row >= _rows)
_rows++;
i -= j;
term.setCursor(origin.addOffset(col, row));
break;
}
}
if (j == _columns) { // the word was too long
if (chr == ' ' || chr == '\n' || chr == '\t') {
col = 0;
row++;
if (row >= _rows)
_rows++;
term.setCursor(origin.addOffset(col, row));
}
}
}
} // end if line-wrap is true
} // end if we have reached the right-hand column
} // end FOR loop.
/* Check for the case where the caret position is after the last
* character of the document.
*/
if (_caretPosition == super._document.length()) {
tempCaret = new Point(col, row);
/* If the caret has changed, generate a ScrollEvent
*/
if (tempCaret.equals(caret) == false) {
scrollevent = generateScrollEvent(tempCaret,
new Point(col, row));
}
caret = tempCaret;
}
/* Post a ScrollEvent, if one was generated; but only if the
* caret has really changed. We have to be careful to avoid an
* endless loop, where a ScrollEvent triggers a draw(), which
* triggers an unnecessary ScrollEvent and so on.
*/
if ((_caret.equals(caret) == false) && scrollevent != null) {
term.getSystemEventQueue().postEvent(scrollevent);
_caret = caret;
}
}
public void requestFocus() {
/* Generate the FOCUS_GAINED event.
*/
super.requestFocus();
/* Get the absolute origin of this component.
*/
Point origin = getLocationOnScreen();
Toolkit.getDefaultToolkit().setCursor(origin.addOffset(_caret));
}
/**
* Register a ScrollListener object for this JTextArea.
*/
public void addScrollListener(ScrollListener sl_) {
if (_scrollListeners == null)
_scrollListeners = new Vector();
_scrollListeners.add(sl_);
}
/**
* Remove a ScrollListener object that is registered for this JTextArea.
*/
public void removeScrollListener(ScrollListener sl_) {
if (_scrollListeners == null)
return;
_scrollListeners.remove(sl_);
}
/** Process scroll events generated by this JTextArea.
*/
public void processScrollEvent(ScrollEvent e_) {
if (_scrollListeners != null) {
for (Enumeration e = _scrollListeners.elements();
e.hasMoreElements(); ) {
ScrollListener sl = (ScrollListener) e.nextElement();
sl.scroll(e_);
}
}
}
/** Returns the preferred size of the viewport for this JTextArea
* when it is in a JScrollPane (this method implements the
* Scrollable interface). The size is determined by the number of
* rows and columns set for this JTextArea (either in the constructor
* or in the setColumns() and setRows() methods).
*/
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(_preferredColumns, _preferredRows);
}
public void debug(int level_)
{
for (int i=0; i<level_; i++)
System.err.print(" ");
System.err.println("JTextArea origin=" + _origin +
" size=" + getSize());
}
/** A private helper method to delete from a specified position
* until the end of the line.
*/
private void deleteEOL(Toolkit term_, int col_, int row_, int colorpair_)
{
Point origin = getLocationOnScreen();
term_.setCursor(origin.addOffset(col_, row_));
for (int i=col_; i<_columns ; i++)
term_.addChar(' ', 0, colorpair_);
}
/** This helper method converts offset to line number and
* vice versa.
*/
private int offsetCalc(int mode_, int value_)
{
int lineOfOffset=0;
int row = 0;
if (mode_ == LINE_START_OFFSET && value_ == 0)
return 0;
for (int col=0, i=0;
i<super._document.length();
i++) {
if (mode_ == LINE_OF_OFFSET && value_ == i) {
lineOfOffset = row;
}
char chr = super._document.charAt(i);
if (col < _columns) {
if (chr == '\n') {
col = 0;
row++;
}
else {
col++;
}
}
else { // We have reached the right-hand column.
if (_lineWrap == false) {
if (chr == '\n') {
col = 0;
row++;
}
}
else { // line-wrap is true
if (_wrapStyleWord == false) {
col = 0;
row++;
}
else {
/* We must back-track until we get to whitespace, so
* that we can move the word to the next line.
*/
int j;
for (j=0; j<_columns; j++) {
char tmpchr = super._document.charAt(i-j);
if (tmpchr == ' ' || tmpchr == '\t') {
col = 0;
row++;
i -= j;
break;
}
}
if (j == _columns) { // the word was too long
if (chr == ' ' || chr == '\n' || chr == '\t') {
col = 0;
row++;
}
}
}
} // end if line-wrap is true
} // end if we have reached the right-hand column
if (mode_ == LINE_START_OFFSET && col == 0 && row == value_) {
return i+1;
}
else if (mode_ == LINE_END_OFFSET && col == 0 && row == value_+1) {
return i;
}
} // end FOR loop.
if (mode_ == LINE_OF_OFFSET) {
if (value_ == super._document.length()) {
return row;
}
else
return lineOfOffset;
}
else if (mode_ == LINE_COUNT) {
return row+1;
}
else if (mode_ == LINE_END_OFFSET && row == value_) {
return super._document.length();
}
else {
throw new IndexOutOfBoundsException(
"Invalid offset or line number: mode=" + mode_ +
" value=" + value_ + " row=" + row + " doc=\"" + _document + "\"");
}
}
/* Private helper method used to redraw the component if its state
* has changed.
*/
private void refresh()
{
/* If this JTextArea is contained in a JViewport, the PaintEvent
* that we post must request a redraw of the JViewport, so that
* the JViewport can set the clipping rectangle before calling the
* draw() method of this JTextArea.
*/
Component todraw;
if (getParent() instanceof JViewport)
todraw = getParent();
else
todraw = this;
/* If this component is already displayed, generate a PaintEvent
* and post it onto the queue.
*/
todraw.repaint();
}
/** Private method, called whenever the caret changes.
*/
private ScrollEvent generateScrollEvent(Point tempCaret_, Point col_row_)
{
int direction;
/* Determine the direction of scrolling
*/
if (tempCaret_.y > _caret.y) {
if (tempCaret_.x > _caret.x)
direction = ScrollEvent.UP_LEFT;
else if (tempCaret_.x < _caret.x)
direction = ScrollEvent.UP_RIGHT;
else
direction = ScrollEvent.UP;
}
else if (tempCaret_.y < _caret.y) {
if (tempCaret_.x > _caret.x)
direction = ScrollEvent.DOWN_LEFT;
else if (tempCaret_.x < _caret.x)
direction = ScrollEvent.DOWN_RIGHT;
else
direction = ScrollEvent.DOWN;
}
else {
if (tempCaret_.x > _caret.x)
direction = ScrollEvent.LEFT;
else
direction = ScrollEvent.RIGHT;
}
return new ScrollEvent(this, direction, col_row_);
}
//====================================================================
// INSTANCE VARIABLES
private int _rows;
private int _columns;
private int _preferredRows;
private int _preferredColumns;
/** The caret is updated only when the component is drawn.
*/
private Point _caret = new Point(0,0);
private boolean _lineWrap;
private boolean _wrapStyleWord = false;
/** A list of ScrollListeners registered for this JTextArea.
*/
private Vector _scrollListeners = null;
private static final int LINE_COUNT = 1;
private static final int LINE_START_OFFSET = 2;
private static final int LINE_END_OFFSET = 3;
private static final int LINE_OF_OFFSET = 4;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -