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

📄 textfieldcomposer.java

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

import javax.microedition.lcdui.Font;

/**
 * The <code>TextFieldComposer</code> defines set of methods
 * that supports a text caret's handling and basic text editing.
 *
 * @author Greg Gridin
 */
public class TextFieldComposer implements AbstractComposer {

    /**
     * Current caret position
     */
    protected int caretPosition = 0;

    /**
     * Text position adjustment
     * Reserved for advanced input composers
     */
//    protected int adjustment;

    /**
     * The text to edit
     */
    protected char[] text;

    /**
     * Current text length
     */
    protected int curLen = 0;

    /**
     * Constructs new instance of <code>TextFieldComposer</code>.
     *
     * @param maxSize max size of editable text
     */
    public TextFieldComposer(int maxSize) {

        if (maxSize >= 0) {
            text = new char[maxSize];
        }
    }

    /**
     * Get maximum lenght of the text which can be entered in TextFieldComposer
     *
     * @return maxSize max length of the text
     */
    public int getMaxSize() {

        return text.length;
    }

    /**
     * Moves the caret one position left
     *
     * @return <code>true</code> if the moving is possible/done, <code>false</code> otherwise
     */
    public boolean caretLeft() {
        if (caretPosition > 0) {
            caretPosition--;
            return true;
        }
        return false;
    }

    /**
     * Moves the caret one position right
     *
     * @return <code>true</code> if the moving is possible/done, <code>false</code> otherwise
     */
    public boolean caretRight() {
        if (caretPosition < curLen && caretPosition < text.length) {
            caretPosition++;
            return true;
        }
        return false;
    }

    /**
     * Deletes previous symbol, moves cursor one position left
     *
     * @return <code>true</code> if the delete and moving is possible/done, <code>false</code> otherwise
     */
    public boolean backspace() {

        if (!caretLeft()) return false;
        return deleteChar();
    }

    /**
     * Deletes current symbol
     *
     * @return <code>true</code> if the delete possible/done, <code>false</code> otherwise
     */
    public boolean deleteChar() {
        if (caretPosition < 0) return false;
        if (caretPosition >= curLen) return false;
        curLen--;
        if (caretPosition != curLen) {
            System.arraycopy(text, caretPosition + 1, text, caretPosition, curLen - caretPosition);
        }
        text[curLen] = 0;
        return true;
    }

    /**
     * Adds new symbol or replaces the current one
     *
     * @param ch         new symbol
     * @param insertMode if <code>true</code> then new symbol will be added, otherwise the current symbol
     *                   will be replaced
     * @return <code>true</code> if the add/replace is possible, <code>false</code> otherwise
     */
    public boolean addChar(char ch, boolean insertMode) {

        if (curLen >= text.length && insertMode) return false;
        if (caretPosition >= curLen) caretPosition = curLen;
        while (caretPosition >= text.length) {
            caretLeft();
        }
        while (caretPosition < 0) {
            caretRight();
        }
        if (insertMode && caretPosition != curLen) {
            if (caretPosition != curLen) {
                System.arraycopy(text, caretPosition, text, caretPosition + 1, curLen - caretPosition);
            }
            text[caretPosition] = ch;
            curLen++;
        } else {
            text[caretPosition] = ch;
            if (caretPosition == curLen) curLen++;
        }
        return true;
    }

    /**
     * Returns the current text length
     */
    public int getCurrentLength() {
        return curLen;
    }

    /**
     * Returns the string that is presented by this text component.
     */
    public String getText() {
        return new String(text, 0, curLen);
    }

    /**
     * Returns the edited text
     */
    public char[] getChars() {
        return text;
    }

    /**
     * Returns the cursor offset (in pixels) for the composed string
     *
     * @param font     the current font.
     * @param echoChar the echo character, not used if equal to <code>0</code>
     */
    public int getCursorOffset(Font font, char echoChar) {

        int offset;
        if (echoChar == 0) {
            offset = font.charsWidth(text, 0, caretPosition);
        } else {
            offset = font.charWidth(echoChar) * caretPosition;
        }
        return (offset < 0) ? 0 : offset;
    }

    /**
     * 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) {
        int len = 0;
        if (newText != null) len = newText.length();
        for (int i = 0; i < text.length; i++) {
            text[i] = (i < len) ? newText.charAt(i) : 0;
        }
        curLen = (len > text.length) ? text.length : len;
        caretPosition = curLen;
    }

    /**
     * Returns the caret position
     */
    public int getCaretPosition() {
        return caretPosition;
    }

    /**
     * Sets the caret position
     * @param caretPosition new caret position
     * @return <code>true</code> if operation is successful, <code>false</code> otherwise
     */
    public boolean setCaretPosition(int caretPosition) {
        if (caretPosition < 0 || caretPosition > text.length) {
            return false;
        }
        this.caretPosition = caretPosition;
        return true;
    }

    /**
     * Default destructor. Helps VM to perform garbage collection
     */
    public void destructor() {
        text = null;
    }
} // class TextFieldComposer

⌨️ 快捷键说明

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