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

📄 textarea.java

📁 j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件
💻 JAVA
字号:
package com.jmobilecore.ui.core;

import javax.microedition.lcdui.*;

/**
 * A <code>textArea</code> object is a text component
 * that allows for the editing of a single line of text.
 *
 * @author Evgeniy Krapiva
 * @author Greg Gridin
 */
public class TextArea extends TextComponent {

    /**
     * By words wrapping style
     */
    static public final byte WORD_WRAPPING = 1;

    /**
     * By symbol wrapping style
     */
    static public final byte SYMBOL_WRAPPING = 2;

    /**
     * Default height of component
     */
    final static public int ROWS = 3;

    /**
     * The number of rows in the <code>TextArea</code>.
     * This parameter will determine the text area's height.
     */
    public int rows;

    /**
     * Handles input of the number keys
     */
    public TextAreaComposer composer;

    /**
     * Enables or disables text input.
     */
    boolean active = false;

    /**
     * Constructs a new text area with the empty string as text.
     */
    public TextArea() {
        this(null, ROWS, C_ANY);
    }

    /**
     * Constructs a new text area with the specified text.
     *
     * @param text the text to be displayed; if text is null, the empty string "" will be displayed
     */
    public TextArea(String text) {
        this(text, ROWS, C_ANY);
    }

    /**
     * Constructs a new text area with the specified number of rows and the empty string as text.
     *
     * @param nRows the number of rows
     */
    public TextArea(int nRows) {
        this(null, nRows, C_ANY);
    }

    /**
     * Constructs a new text area with the specified text, and with the specified number of rows
     *
     * @param text  the text to be displayed; if text is null, the empty string "" will be displayed
     * @param nRows the number of rows
     */
    public TextArea(String text, int nRows) {
        this(text, nRows, C_ANY);
    }

    /**
     * Constructs a new text area with the specified number of rows, the binary mask of constraints
     * and the empty string as text.
     *
     * @param nRows  the number of rows
     * @param constr constraints binary mask
     */
    public TextArea(int nRows, int constr) {
        this(null, nRows, constr);
    }

    /**
     * Constructs a new text area with the specified text, the specified number of rows
     * and binary mask constraints
     *
     * @param text   the text to be displayed; if text is null, the empty string "" will be displayed
     * @param nRows  the number of rows
     * @param constr constraints binary mask
     */
    public TextArea(String text, int nRows, int constr) {
        super(constr);
        rows = nRows;
        if (rows < 1) rows = ROWS;
        focusedForeground = Style.FOCUSED_TEXT_COLOR;
        foreground = Style.TEXT_COLOR;
        setFont(Style.TEXT_FIELD_FONT);

        composer = new TextAreaComposer(rows, getWidth() - (BORDER_WIDTH + BORDER_GAP + Style.H_GAP), font);
        setText(text);
    }

    /**
     * Returns the text that is presented by this text component.
     */
    public String getText() {
        return composer.getText();
    }

    /**
     * Sets the text that is presented by this
     * text component to be the specified text.
     *
     * @param newText the new text.
     */
    public void setText(String newText) {
        composer.setText(newText);
    }

    /**
     * Gets current input composer
     */
    protected AbstractComposer getComposer() {
        return composer;
    }

    /**
     * Appends the given text to the text area's current text.
     */
    public void append(String str) {
        composer.appendText(str);
    }

    /**
     * Inserts the specified text at the specified position
     * in this text area.
     */
    public void insert(String str, int pos) {
        composer.insertText(str, pos);
    }

    /**
     * Sets the font of this component.
     *
     * @param font the font to become this component's font;
     *             if this parameter is <code>null</code> then this
     *             component font won't be changed
     */
    public void setFont(Font font) {
        super.setFont(font);
        if (composer != null) composer.reset(font);
    }

    /**
     * Sets the component wrapping style
     *
     * @param style indicates new wrapping style
     * @see #WORD_WRAPPING
     * @see #SYMBOL_WRAPPING
     */
    public void setWrappingStyle(byte style) {
        composer.wrappingStyle = style;
        composer.reset(font);
    }

    /**
     * Paints the component
     *
     * @param g Graphics object
     */
    public void paint(Graphics g) {
        final int voffset = BORDER_WIDTH + BORDER_GAP + Style.V_GAP;
        final int hoffset = BORDER_WIDTH + BORDER_GAP + Style.H_GAP;

        paintBackground(g);
        g.setColor(Style.BORDER_COLOR);
        g.drawRect(0, screenY, width - 1, height - 1);
        prepareForeground(g);
        int y = screenY + voffset;

        //paint the text !!!
        TextAreaComposer.LineRange range;
        String text = composer.getText();

        //paint from current string
        for (int i = composer.startRow; i < composer.startRow + rows; i++) {
            //get ranges for current string
            try {
                range = (TextAreaComposer.LineRange) composer.linebreaks.elementAt(i);
            } catch (Exception e) {
                break;
            }

            if (alignment == LEFT || active) {
                g.drawSubstring(text, range.from, range.to-range.from, hoffset, y, Graphics.TOP | Graphics.LEFT);
            } else if (alignment == RIGHT) {
                g.drawSubstring(text, range.from, range.to-range.from, getWidth() - hoffset, y, Graphics.TOP | Graphics.RIGHT);
            } else if (alignment == CENTER) {
                g.drawSubstring(text, range.from, range.to-range.from, getWidth() / 2, y, Graphics.TOP | Graphics.HCENTER);
            }
            y += font.getHeight();
        }
        super.paint(g);

        if (active) if (isFocusOwner()) paintCursor(g);
    }

    /**
     * Calculates the height of the component.
     */
    protected void setHeight() {
        this.height = font.getHeight() * rows + 2 * Style.V_GAP +
                BORDER_WIDTH * 2 + BORDER_GAP * 2;
    }

    /**
     * Paints the cursor for this component
     *
     * @param g Graphics object
     */
    protected void paintCursor(Graphics g) {
        //displays selected symbol and caret position
        final int cp = composer.caretPositionX + composer.currentRange.from;
        final int voffset = BORDER_WIDTH + BORDER_GAP + Style.V_GAP;
        final int hoffset = BORDER_WIDTH + BORDER_GAP + Style.H_GAP;
        final int cY = screenY + voffset + composer.caretPositionY * font.getHeight();
        final int rh = font.getHeight();

        int lineOffset = hoffset;
        for (int i = composer.currentRange.from; i < cp; i++) {
            lineOffset += font.charWidth(composer.text.charAt(i));
        }

        g.setColor(Style.CURSOR_BACKGROUND_COLOR);

        if (!insertMode) {
            final char ch = composer.text.charAt(cp);
            final int cw = font.charWidth(ch);
            g.fillRect(lineOffset, cY, cw + 1, rh);
            g.setColor(Style.CURSOR_TEXT_COLOR);
            g.setClip(lineOffset, cY, cw, rh);
            g.drawChar(ch, lineOffset, cY, Graphics.TOP | Graphics.LEFT);
            g.setClip(0, 0, ScreenCanvas.WIDTH, ScreenCanvas.HEIGHT);

        } else
            g.fillRect(lineOffset, cY, 1, rh);
    }

    /**
     * Responds to a key press.
     * <p/>
     * If the key is a number key and the constraint is {@link TextComponent#C_NUMERIC} only,
     * no timing interval is used, the number is immediately inserted into the
     * field (if there is room).
     * If the key is a number key and the constraint IS NOT ONLY {@link TextComponent#C_NUMERIC},
     * the timing interval begins so that
     * characters may be cycled through via repeated presses of the same key
     * within the timing interval.
     * <p/>
     * Presses of the left and right sides of the 4-way navigation key move the
     * cursor one character left or right within the field. Pressing the <b>*</b>
     * key deletes the character to the left of the cursor. Pressing the <b>#</b>
     * key inserts a space at the cursor position.
     * <p/>
     * You can override this method if you want a custom component to perform
     * additional filtering of key presses.  For example, if you want a field that
     * accepts only odd digits you could extend this class, ensure it is
     * {@link TextComponent#C_NUMERIC}, then in this method check that the pressed key is an odd
     * digit before passing it to the parent's <code>keyPressed</code> method.
     *
     * @param keyCode The code for the key that was pressed.
     * @return <code>true</code> if the key was successfully processed, <code>false</code> otherwise
     */
    synchronized public boolean keyPressed(int keyCode) {

        boolean bRes = false;

        resetCursor();
        final boolean curInsertMode = insertMode;
        if (!curInsertMode && preProcessKeyCode(keyCode)) {
            if (active) composer.caretRight();
        }

        //Activation of TextArea component
        if (keyCode == PlatformCanvas.KEY_ENTER) {
            active = !active;
            bRes = true;
        }

        if (keyCode == PlatformCanvas.KEY_LEFT) {
            if (active) bRes = composer.caretLeft();
        }
        if (keyCode == PlatformCanvas.KEY_RIGHT) {
            if (active) bRes = composer.caretRight();
        }
        if (keyCode == PlatformCanvas.KEY_UP) {
            if (active) bRes = composer.caretUp();
        }
        if (keyCode == PlatformCanvas.KEY_DOWN) {
            if (active) bRes = composer.caretDown();
        }

        if (editable) {

            if (keyCode == PlatformCanvas.KEY_STAR || keyCode == PlatformCanvas.KEY_CLEAR) {
                bRes = composer.backspace();
            }

            if (active) {
                if (keyCode == PlatformCanvas.KEY_POUND || (keyCode >= PlatformCanvas.KEY_NUM0 && keyCode <= PlatformCanvas.KEY_NUM9)) {
                    char ch = processKeyCode(keyCode);
                    if (ch == 0) return false;
                    bRes = composer.addChar(ch, curInsertMode);
                    if (insertMode) composer.caretRight();
                }
            }
        }
        if (bRes) parentScreen.repaint();

        initCursor();
        lastKeyCode = keyCode;
        return bRes;
    }
}


⌨️ 快捷键说明

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