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

📄 digitalblockcomposer.java

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

/**
 * The <code>DigitalBlockComposer</code> is extension of class
 * <code>TextBlockComposer</code>, supports digits-only text field editing
 *
 * @author Greg Gridin
 */
public class DigitalBlockComposer extends TextBlockComposer {

    /**
     * Minimal value for the field
     */
    public long minValue = Long.MIN_VALUE;

    /**
     * Maximal value for the field
     */
    public long maxValue = Long.MAX_VALUE;

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

    /**
     * Constructs new instance of <code>DigitalBlockComposer</code> with specified style and length
     *
     * @param style  <code>DigitalBlockComposer</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>
     */
    public DigitalBlockComposer(byte style, int length) {
        super(style, length);
    }

    /**
     * Constructs new instance of <code>DigitalBlockComposer</code> with specified style, length
     * and filling characted
     *
     * @param style    <code>DigitalBlockComposer</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>
     */
    public DigitalBlockComposer(byte style, int length, char fillChar) {
        super(style, length, fillChar);
    }

    /**
     * Constructs new instance of <code>DigitalBlockComposer</code> with specified style, length
     * filling characted, minimal and maximal value
     *
     * @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
     * @param minValue the minimal value for the field
     * @param maxValue the maximal value for the field
     * @throws RuntimeException if style is <code>STATIC</code>
     */
    public DigitalBlockComposer(byte style, int length, char fillChar, long minValue, long maxValue) {
        super(style, length, fillChar);
        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    /**
     * Sets new value to the field
     *
     * @param newValue the new value for the field
     * @return <code>true</code> if the field was set to new value, <code>false</code> otherwise
     */
    public boolean setValue(long newValue) {

        if (minValue > newValue) return false;
        if (maxValue < newValue) return false;
        String strValue = String.valueOf(newValue);
        if (strValue.length() > this.getMaxSize()) return false;
        setText(strValue);
        return true;
    }

    /**
     * Returns value of the field
     *
     * @return the field value
     * @throws java.lang.NumberFormatException
     *          if field contants not-a-number value
     */
    public long getValue() {

        char[] value = getChars();
        StringBuffer buffer = new StringBuffer(value.length);
        char curChar;
        for (int i = 0; i < value.length; i++) {
            curChar = value[i];
            if (Character.isDigit(curChar)) {
                buffer.append(curChar);
            }
        }
        return Long.parseLong(buffer.toString());
    }

    /**
     * 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() {
        try {
            long digiValue = getValue();
            if (digiValue < minValue) return false;
            if (digiValue > maxValue) return false;
        } catch (Exception e) {
            return false;
        }
        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) {

        return super.addChar(ch, (getCurrentLength()!=getMaxSize())?insertMode:false);
    }

}

⌨️ 快捷键说明

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