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

📄 textblockcomposer.java

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

import com.jmobilecore.ui.core.TextFieldComposer;

/**
 * The <code>TextBlockComposer</code> defines set of methods
 * that supports a custom text field handling and editing
 *
 * @author Greg Gridin
 */
public class TextBlockComposer extends TextFieldComposer {

    /**
     * Static but focusable style - modification is possible for whole block only
     */
    public static final byte STATIC_FOCUSABLE = 1;

    /**
     * Fixed size style - Block size id predefined,
     * user is scrolling though each symbol of the block
     */
    public static final byte FIXED_SIZE = 2;

    /**
     * Variable size style - Max length of the block is defined only,
     * user is scrolling though entered symbols only
     */
    public static final byte VARIABLE_SIZE = 3;

    /**
     * The current TextBlockComposer formatting style.
     */
    protected final byte STYLE;

    /**
     * Characted for filling out "empty" symbols
     */
    public char fillChar = ' ';

    /**
     * Constructs new instance of <code>TextBlockComposer</code> with static style.
     *
     * @param staticContent the static (non-editable) content
     */
    public TextBlockComposer(String staticContent) {
        this(STATIC_FOCUSABLE, staticContent);
    }

    /**
     * Constructs new instance of <code>TextBlockComposer</code> with static style.
     *
     * @param style <code>TextBlockComposer</code> style, can be <code>STATIC</code> or <code>STATIC_FOCUSABLE</code> only
     * @param staticContent the static (non-editable) content
     */
    public TextBlockComposer(byte style, String staticContent) {
        super(-1);
        STYLE = style;
        setText(staticContent);
    }

    /**
     * Constructs new instance of <code>TextBlockComposer</code> with specified style and length
     *
     * @param style <code>TextBlockComposer</code> style, cannot be <code>STATIC</code>
     * @param length the length for fixed size style, the max length for variable size style
     * @throws RuntimeException if style is <code>STATIC</code> or <code>STATIC_FOCUSABLE</code>
     */
    public TextBlockComposer(byte style, int length) {
        this(style, length, style == FIXED_SIZE ? ' ' : 0);
    }

    /**
     * Constructs new instance of <code>TextBlockComposer</code> with specified style, length
     * and filling characted
     *
     * @param style <code>TextBlockComposer</code> style, cannot be <code>STATIC</code>
     * @param length the length for fixed size style, the max length for variable size style
     * @param fillChar the characted for filling out "empty" symbols
     * @throws RuntimeException if style is <code>STATIC</code> or <code>STATIC_FOCUSABLE</code>
     */
    public TextBlockComposer(byte style, int length, char fillChar) {
        super(length);
        if (style == STATIC_FOCUSABLE) throw new RuntimeException();
        STYLE = style;
        this.fillChar = fillChar;
        clear();
    }

    /**
     * Clear out the field
     */
    public void clear() {
        if (STYLE == STATIC_FOCUSABLE) return;
        for (int i = 0; i < text.length; i++) {
            text[i] = fillChar;
        }
        curLen = (STYLE == FIXED_SIZE) ? text.length : 0;
        caretPosition = 0;
    }

    /**
     * Moves the caret one position left
     *
     * @return <code>true</code> if the moving is possible/done, <code>false</code> otherwise
     */
    public boolean caretLeft() {
        if (STYLE == STATIC_FOCUSABLE) return false;
        return super.caretLeft();
    }

    /**
     * Moves the caret one position right
     *
     * @return <code>true</code> if the moving is possible/done, <code>false</code> otherwise
     */
    public boolean caretRight() {
        if (STYLE == STATIC_FOCUSABLE) return false;
        return super.caretRight();
    }

    /**
     * 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 (STYLE == STATIC_FOCUSABLE) return false;
        return super.backspace();
    }

    /**
     * Deletes the current symbol for variable size field
     * Replace the burrent symbol for fixed size field
     * Do nothing for static field
     *
     * @return <code>true</code> if the delete possible/done, <code>false</code> otherwise
     */
    public boolean deleteChar() {
        if (STYLE == STATIC_FOCUSABLE) return false;
        if (caretPosition < 0 || caretPosition >= curLen) return false;
        if (STYLE == FIXED_SIZE) {
            text[caretPosition] = fillChar;
            return true;
        }
        return super.deleteChar();
    }

    /**
     * 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 (STYLE == STATIC_FOCUSABLE) return false;
        if (STYLE == FIXED_SIZE) return super.addChar(ch, false);
        return super.addChar(ch, insertMode);
    }

    public int getCaretPosition() {
        if (STYLE == STATIC_FOCUSABLE) return 0;
        return super.getCaretPosition();
    }

    public boolean setCaretPosition(int caretPosition) {
        if (STYLE == STATIC_FOCUSABLE) return caretPosition==0;
        return super.setCaretPosition(caretPosition);
    }

    public void setText(String newText) {
        if (STYLE == STATIC_FOCUSABLE) {
            text = newText.toCharArray();
            curLen = text.length;
            return;
        }
        super.setText(newText);
        caretPosition = 0;
        if (STYLE == FIXED_SIZE) {
            for (int i = newText.length(); i < text.length; i++) {
                text[i] = fillChar;
            }
            curLen = text.length;
            return;
        }
    }

    /**
     * Tests if editing of the field is complete
     * @return <code>true</code> if field is valid (complete and has correct value), <code>false</code> otherwise
     */
    public boolean isComplete() {
        return true;
    }
}

⌨️ 快捷键说明

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