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

📄 currencyfield.java

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

import com.jmobilecore.ui.core.TextField;
import com.jmobilecore.ui.core.TextComponent;

/**
 * This class displays two fields, one for currency major and one for currency minor
 * (like dollars and cents) separated by a decimal point.
 * If alignment is <code>Component.LEFT</code> or <code>Component.CENTER</code>
 * the currency major field is preceded by a currency symbol.
 * If alignment is <code>Component.RIGHT</code> then the currency symbol follows
 * the currency minor field
 *
 * @author Igor Shevlyakov - initial implementation
 * @author Greg Gridin - redesign
 */
public class CurrencyField extends TextField {

    /**
     * The number of decimal digits in <code>CurrencyField</code>
     */
    protected int decimals = 2;

    /**
     * The currency symbol
     * Examples: "$", "USD", "RUR" etc.
     */
    public String symbol = "$";

    /**
     * Constructs a new <code>CurrencyField</code> field.
     */
    public CurrencyField() {
        this(0, 0);
    };

    /**
     * Constructs a new <code>CurrencyField</code> object with the specified value.
     *
     * @param major the major part of <code>CurrencyField</code> value
     * @param minor the minor part of <code>CurrencyField</code> value
     * @see #setCurrency
     */
    public CurrencyField(int major, int minor) {
        super(-1, TextComponent.C_NUMERIC);
        composer = initComposer();
        setCurrency(major, minor);
        composer.setCaretPosition(0);
    }

    /**
     * Initialize currency field composer
     *
     * @return <code>CustomFieldComposer</code> for IPv4 field
     */
    protected CustomFieldComposer initComposer() {

        TextBlockComposer[] textBlocks = new TextBlockComposer[2];
        textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.VARIABLE_SIZE, 19);
        textBlocks[1] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, decimals, '0');

        return new CustomFieldComposer(textBlocks) {
            public void setText(String text) {
            }
        };
    }

    /**
     * Get the major part of <code>CurrencyField</code> value
     * or <code>-1</code> if <code>CurrencyField</code> object value is set to null or incomplete
     *
     * @return the <code>int</code> value representing the specified currency major
     */
    public long getMajor() {
        return ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[0]).getValue();
    }


    /**
     * Get the minor part of <code>CurrencyField</code> value
     * or <code>-1</code> if <code>CurrencyField</code> object value is set to null or incomplete
     *
     * @return the <code>int</code> value representing the specified currency minor
     */
    public int getMinor() {
        return (int) ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[1]).getValue();
    }

    /**
     * Sets <code>CurrencyField</code> object to the specified value.
     * The <code>CurrencyField</code> major/minor values should be calcuated according to formulae:
     * <code>CurrencyField.minor = minor % (10**digits); </code>
     * <code>CurrencyField.major = major + minor / (10**digits); </code>
     *
     * @param major the major part of <code>CurrencyField</code> value
     * @param minor the minor part of <code>CurrencyField</code> value
     */
    public void setCurrency(long major, long minor) {
        long multiplier = 1;
        for (int i = 0; i < decimals; i++) {
            multiplier *= 10;
        }
        long newMajor = major + minor / multiplier;
        long newMinor = minor % multiplier;

        ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[0]).setValue(newMajor);
        ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[1]).setValue(newMinor);
    }

    /**
     * Get formatted representation of <code>CurrencyField</code> value
     * or <code>null</code> if <code>CurrencyField</code> object value is set to null or incomplete
     *
     * @return the <code>String</code> representing bank card number
     */
    public String getFormattedText() {
        if (isValid()) {
            return String.valueOf(getFormattedText(false));
        }
        return null;
    }

    protected char[] getFormattedText(boolean calcCursorOffset) {

        TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
        StringBuffer buffer = new StringBuffer(32);
        int curBlock = ((CustomFieldComposer) composer).getCurrentBlock();
        char symbolArray[] = symbol.toCharArray();
        int fmtCaretPos = 0;
        if (alignment != RIGHT) {
            buffer.append(symbol);
            fmtCaretPos += symbolArray.length;
        }
        buffer.append(blocks[0].getText());
        if (curBlock == 0) {
            fmtCaretPos += blocks[0].getCaretPosition();
        } else {
            fmtCaretPos += blocks[0].getCurrentLength();
        }

        buffer.append('.');
        if (curBlock == 1) fmtCaretPos++;

        buffer.append(blocks[1].getChars());
        if (curBlock == 1) {
            fmtCaretPos += blocks[1].getCaretPosition();
        }

        if (alignment == RIGHT) {
            buffer.append(symbol);
        }
        char[] formattedText = buffer.toString().toCharArray();
        if (calcCursorOffset) {
            calculatedCaretPosition = fmtCaretPos;
            calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
        }
        currentLength = formattedText.length;
        return formattedText;
    }

    /**
     * This method should not be used for the class and intentionally set to deprecated
     * Use #getFormattedText() method instead.
     *
     * @deprecated
     */
    public String getText() {
        return null;
    }

    /**
     * This method should not be used for the class and intentionally set to deprecated
     * Use #setCurrency(int major, int minor) method instead.
     *
     * @deprecated
     */
    public void setText(String text) {
    }

    /**
     * Tests currency field value for correctness
     *
     * @return <code>true</code> if the currency field is valid
     *         <code>false</code> otherwise
     */
    public boolean isValid() {
        return ((CustomFieldComposer) composer).isComplete();
    }

    public void destructor() {
        symbol = null;
        super.destructor();
    }
}

⌨️ 快捷键说明

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