⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtextarea.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* class JTextArea
 *
 * Copyright (C) 2001  R M Pitman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package charvax.swing;

import java.util.Enumeration;
import java.util.Vector;

import charva.awt.Component;
import charva.awt.Dimension;
import charva.awt.Insets;
import charva.awt.Point;
import charva.awt.Toolkit;
import charva.awt.event.KeyEvent;
import charva.awt.event.MouseEvent;
import charva.awt.event.ScrollEvent;
import charva.awt.event.ScrollListener;

/**
 * JTextArea is an (optionally editable) multi-line area that displays 
 * plain text.
 * The JTextArea class implements the Scrollable interface, which enables
 * it to be placed inside a charvax.swing.JScrollPane. In fact, in the 
 * CHARVA framework it should ALWAYS be used inside a JScrollPane, otherwise 
 * it will be unusable (its size depends on the text it contains).<p>
 *
 * Note that, unlike the javax.swing.JTextArea, pressing the TAB key while
 * the keyboard focus is in the JTextArea will not cause a tab to be 
 * inserted; instead, it will move the keyboard input focus to the next
 * focus-traversable component (if there is one). This is because (in
 * javax.swing) the user can user can use the mouse to move the keyboard
 * input focus away from the JTextArea, whereas CHARVA has no mouse support.
 */
public class JTextArea
    extends charvax.swing.text.JTextComponent
    implements charva.awt.Scrollable
{
    /** The default constructor creates an empty text area with 10 rows
     * and 10 columns.
     */
    public JTextArea()
    {
	this("");
    }

    /** Construct a text area with 10 rows and 10 columns, and containing
     * the specified text.
     */
    public JTextArea(String text_)
    {
	this(text_, 10, 10);
    }

    /** Construct a text area wth the specified number of rows and columns,
     * and containing the specified text.
     */
    public JTextArea(String text_, int rows_, int columns_)
    {
	setDocument(text_);
	_rows = rows_;
	_preferredRows = rows_;
	_columns = columns_;
	_preferredColumns = columns_;
	setCaretPosition(0);
    }

    /** Sets the number of columns in this JTextArea.
     */
    public void setColumns(int columns_) { 
	_columns = columns_; 
	_preferredColumns = columns_;
    }

    /** Returns the number of columns in this JTextArea.
     */
    public int getColumns() { return _preferredColumns; }

    /** Sets the number of rows in this JTextArea.
     */
    public void setRows(int rows_) { 
	_rows = rows_; 
	_preferredRows = rows_;
    }

    /** Returns the number of rows in this JTextArea.
     */
    public int getRows() { return _preferredRows; }

    /** Returns the size of this component.
     */
    public Dimension getSize()
    {
	return new Dimension(_columns, _rows);
    }

    public int getWidth() {
	return _columns;
    }

    public int getHeight() {
	return _rows;
    }

    /**
     * Appends the specified text to the end of the document.
     */
    public synchronized void append(String text_)
    {
	super._document.append(text_);
	_caretPosition = super._document.length();

	refresh();
    }

    /**
     * Inserts the specified text at the specified position (ie at the
     * specified offset from the start of the document)..
     */
    public synchronized void insert(String text_, int pos_)
    {
	super._document.insert(pos_, text_);
	_caretPosition = pos_ + text_.length();

	refresh();
    }

    /**
     * Replaces the text from the specified start position to end position
     * with the specified text.
     */
    public synchronized void replaceRange(String text_, int start_, int end_)
    {
	super._document.replace(start_, end_, text_);
	_caretPosition = start_ + text_.length();

	refresh();
    }

    /** Sets the position of the text insertion caret for this JTextArea.
     */
    public void setCaretPosition(int caret_)
    {
	super.setCaretPosition(caret_);

	refresh();
    }

    /**
     * Returns the number of lines of text displayed in the JTextArea.
     */
    public int getLineCount()
    {
	return offsetCalc(LINE_COUNT, 0);
    }

    /**
     * Returns the offset of the first character in the specified line 
     * of text.
     */
    public int getLineStartOffset(int line_)
    {
	return offsetCalc(LINE_START_OFFSET, line_);
    }

    /**
     * Returns the offset of the last character in the specified line.
     */
    public int getLineEndOffset(int line_)
    {
	return offsetCalc(LINE_END_OFFSET, line_);
    }

    /**
     * Translates an offset (relative to the start of the document)
     * to a line number.
     */
    public int getLineOfOffset(int offset_)
    {
	return offsetCalc(LINE_OF_OFFSET, offset_);
    }

    /**
     * Sets the line-wrapping policy of the JTextArea. If set to true,
     * lines will be wrapped if they are too long to fit within the
     * allocated width. If set to false, the lines will always be 
     * unwrapped. The default value of this property is false.
     */
    public void setLineWrap(boolean wrap_) { 
	_lineWrap = wrap_; 
	_rows = _preferredRows;
	_columns = _preferredColumns;
    }

    /** 
     * Returns the line-wrapping policy of the JTextArea. If set to true,
     * lines will be wrapped if they are too long to fit within the
     * allocated width. If set to false, the lines will always be 
     * unwrapped. 
     */
    public boolean getLineWrap() { return _lineWrap; }

    /**
     * Sets the line-wrapping style to be used if getLineWrap() is true.
     * If true, lines will be wrapped at word boundaries (whitespace) if
     * they are too long to fit in the allocated number of columns. If 
     * false, lines will be wrapped at character boundaries. The default 
     * value of this property is false.
     */
    public void setWrapStyleWord(boolean wrapWord_)
    {
	_wrapStyleWord = wrapWord_;
    }

    /**
     * Returns the line-wrapping style to be used if getLineWrap() is true.
     * If true, lines will be wrapped at word boundaries (whitespace) if
     * they are too long to fit in the allocated number of columns. If 
     * false, lines will be wrapped at character boundaries. 
     */
    public boolean getWrapStyleWord() { return _wrapStyleWord; }

    /** Called by the LayoutManager.
     */
    public Dimension minimumSize() {
	return getSize();
    }

    /**
     * Process KeyEvents that have been generated by this JTextArea.
     */
    public void processKeyEvent(KeyEvent ke_) {

	/* First call all KeyListener objects that may have been registered
	 * for this component. 
	 */
	super.processKeyEvent(ke_);

	/* Check if any of the KeyListeners consumed the KeyEvent.
	 */
	if (ke_.isConsumed())
	    return;

	int caret = getCaretPosition();
	int line = getLineOfOffset(caret);
	int key = ke_.getKeyCode();
	if (key == '\t') {
	    getParent().nextFocus();
	    return;
	}
	else if (key == KeyEvent.VK_BACK_TAB) {
	    getParent().previousFocus();
	    return;
	}
	else if (key == KeyEvent.VK_LEFT && caret > 0) {
	    setCaretPosition(caret-1);
	}
	else if (key == KeyEvent.VK_RIGHT &&
		caret < getDocument().length()) {
	    setCaretPosition(caret+1);
	}
	else if (key == KeyEvent.VK_HOME) {
	    int lineStart = getLineStartOffset(line);
	    setCaretPosition(lineStart);
	}
	else if (key == KeyEvent.VK_END) {
	    int lineEnd = getLineEndOffset(line);
	    setCaretPosition(lineEnd);
	}
	else if ((key == KeyEvent.VK_PAGE_UP || 
		key == KeyEvent.VK_PAGE_DOWN) &&
		(getParent() instanceof JViewport)) {

	    JViewport viewport = (JViewport) getParent();
	    int vertical_offset = -1 * viewport.getViewPosition().y;
	    int viewport_height = viewport.getSize().height;
	    if (key == KeyEvent.VK_PAGE_UP) {
		if (line > vertical_offset)
		    line = vertical_offset;
		else
		    line = vertical_offset - viewport_height;

		line = (line < 0) ? 0 : line;
	    }
	    else {
		if (line < vertical_offset + viewport_height - 1)
		    line = vertical_offset + viewport_height - 1;
		else
		    line = vertical_offset + (2 * viewport_height) - 1;

		line = (line > getLineCount() - 1) ?
			(getLineCount() - 1) :
			line;
	    }
	    setCaretPosition(getLineStartOffset(line));
	}
	else if (key == KeyEvent.VK_UP && line > 0) {
	    int column = caret - getLineStartOffset(line);
	    int prevlineStart = getLineStartOffset(line-1);
	    int prevlineEnd = getLineEndOffset(line-1);
	    if (column > prevlineEnd - prevlineStart) {
		column = prevlineEnd - prevlineStart;
	    }
	    setCaretPosition(prevlineStart + column);
	}
	else if (key == KeyEvent.VK_DOWN && 
		line < getLineCount()-1) {
	    int column = caret - getLineStartOffset(line);
	    int nextlineStart = getLineStartOffset(line+1);
	    int nextlineEnd = getLineEndOffset(line+1);
	    if (column > nextlineEnd - nextlineStart) {
		column = nextlineEnd - nextlineStart;
	    }
	    setCaretPosition(nextlineStart + column);
	}
	else if (super.isEditable() == false) {
	    Toolkit.getDefaultToolkit().beep();
	}
	else if (key >= ' ' && key <= 0177) {
	    char[] arry = { (char) key };
	    insert(new String(arry), caret);
	}
	else if (key == KeyEvent.VK_ENTER) {
	    char[] arry = { '\n' };
	    insert(new String(arry), caret);
	}
	else if (key == KeyEvent.VK_BACK_SPACE && caret > 0) {
	    replaceRange("", caret-1, caret);
	}
	else if (key == KeyEvent.VK_DELETE && 
		caret < getDocument().length()-1) {
	    replaceRange("", caret, caret+1);
	}

	/* If this JTextArea is contained in a JViewport, let the JViewport
	 * do the drawing, after setting the clip rectangle.
	 */
	if ((getParent() instanceof JViewport) == false) {
	    draw();
	    requestFocus();
	    super.requestSync();
	}
    }

    /** Process a MouseEvent that was generated by clicking the mouse
     * somewhere inside this JTextArea.
     * Clicking the mouse inside the JTextArea moves the caret position
     * to where the mouse was clicked.
     */
    public void processMouseEvent(MouseEvent e_) {
	super.processMouseEvent(e_);

	if (e_.getButton() == MouseEvent.BUTTON1 &&
		e_.getModifiers() == MouseEvent.MOUSE_CLICKED &&
		this.isFocusTraversable()) {

	    /* Get the absolute origin of this component.
	     */
	    Point origin = getLocationOnScreen();
	    Insets insets = super.getInsets();
	    origin.translate(insets.left, insets.top);

	    int line = e_.getY() - origin.y;
	    if (line > getLineCount() - 1)
		return;

	    int column = e_.getX() - origin.x;
	    int lineStart = getLineStartOffset(line);
	    int lineEnd = getLineEndOffset(line);
	    if (column > lineEnd - lineStart) {
		column = lineEnd - lineStart;
	    }
	    setCaretPosition(lineStart + column);
	    repaint();
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -