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

📄 ipv4field.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 one or two fields for the entry a TCP/IP address
 * in specifield format
 *
 * @author Igor Shevlyakov - initial implementation
 * @author Greg Gridin - redesign
 */
public class IPv4Field extends TextField {

// internal constants and variables

    /**
     * IP address and no port information to display
     */
    public static final byte IP_ONLY = 1;

    /**
     * IP address and port information to display
     */
    public static final byte IP_PORT = 2;

    /**
     * The current IPField formatting style.
     */
    public final byte STYLE;

    /**
     * Constructs a new <code>IPv4Field</code> object.
     */
    public IPv4Field() {
        this(IP_ONLY);
    }

    /**
     * Constructs a new <code>IPv4Field</code> object of specified format.
     *
     * @param style the IP address format
     */
    public IPv4Field(byte style) {
        super(-1, TextComponent.C_NUMERIC);
        STYLE = style;
        composer = initComposer();
        composer.setCaretPosition(0);
    }

    /**
     * Constructs a new <code>IPv4Field</code> object of specified format
     * and sets it to the specified value.
     *
     * @param style the IP address format
     * @param host  the IP address host value
     * @param port  the IP address port value
     */
    public IPv4Field(byte style, int host, int port) {
        this(style);
        setIPv4(host, port);
    }

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

        TextBlockComposer[] textBlocks = new TextBlockComposer[(STYLE == IP_ONLY) ? 4 : 5];
        textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.VARIABLE_SIZE, 3, ' ', 0, 255);
        textBlocks[1] = new DigitalBlockComposer(DigitalBlockComposer.VARIABLE_SIZE, 3, ' ', 0, 255);
        textBlocks[2] = new DigitalBlockComposer(DigitalBlockComposer.VARIABLE_SIZE, 3, ' ', 0, 255);
        textBlocks[3] = new DigitalBlockComposer(DigitalBlockComposer.VARIABLE_SIZE, 3, ' ', 0, 255);
        if (STYLE != IP_ONLY) {
            textBlocks[4] = new DigitalBlockComposer(DigitalBlockComposer.VARIABLE_SIZE, 5, ' ', 0, 65535);
        }

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

    protected char[] getFormattedText(boolean calcCursorOffset) {

        TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
        StringBuffer buffer = new StringBuffer(21);

        for (int i = 0; i < 4; i++) {
            buffer.append(blocks[i].getText());
            if (i!=3) buffer.append('.');
        }
        if (STYLE==IP_PORT) {
            buffer.append(':');
            buffer.append(blocks[4].getText());
        }
        char[] formattedText = buffer.toString().toCharArray();
        if (calcCursorOffset) {
            calculatedCaretPosition = composer.getCaretPosition() + ((CustomFieldComposer)composer).getCurrentBlock();
            calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
        }
        currentLength = formattedText.length;
        return formattedText;
    }

    /**
     * Get formatted representation of <code>Ipv4Field</code> value
     * or <code>null</code> if <code>IPv4Field</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;
    }

    /**
     * Returns the string representation of TCP/IP address host value that is presented by this component.
     *
     * @return the <code>String</code> value representing the host part of TCP/IP address or
     *         <code>null</code> if IPv4 field is invalid or incomplete
     */
    public String getHost() {
        String temp = getFormattedText();
        if (STYLE == IP_PORT && temp!=null) {
            return temp.substring(0, temp.indexOf(':'));
        }
        return temp;
    }

    /**
     * Returns the string representation of TCP/IP address port value that is presented by this component.
     *
     * @return the <code>String</code> value representing the port part of TCP/IP address or
     *         <code>null</code> if IPv4 field is invalid or incomplete
     */
    public String getPort() {
        long port = getIPv4Port();
        if (port != -1) {
            return String.valueOf(getIPv4Port());
        }
        return null;
    }

    /**
     * Returns the TCP/IP address host value that is presented by this component.
     *
     * @return the <code>int</code> value representing the host part of TCP/IP address or
     *         <code>-1</code> if IPv4 field is invalid or incomplete
     */
    public int getIPv4Host() {
        if (!isValid()) return -1;

        int temp = (int) ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[0]).getValue();
        int rslt = temp << 24;
        temp = (int) ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[1]).getValue();
        rslt |= temp << 16;
        temp = (int) ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[2]).getValue();
        rslt |= temp << 8;
        temp = (int) ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[3]).getValue();
        rslt |= temp;
        return rslt;
    }

    /**
     * Returns the TCP/IP address port value that is presented by this component.
     *
     * @return the <code>int</code> value representing the port part of TCP/IP address or
     *         <code>-1</code> if IPv4 field is invalid or incomplete
     */
    public long getIPv4Port() {
        if (STYLE == IP_PORT && isValid()) {
            return ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[4]).getValue();
        }
        return -1;
    }

    /**
     * Sets the TCP/IP address that is presented by this IPv4 component to be the specified value.
     *
     * @param host the IP address host value
     * @param port the IP address port value
     */
    public boolean setIPv4(int host, long port) {
        ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[0]).setValue(0xff & (host >> 24));
        ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[1]).setValue(0xff & (host >> 16));
        ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[2]).setValue(0xff & (host >> 8));
        ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[3]).setValue(0xff & (host));
        if (STYLE == IP_PORT) {
            return ((DigitalBlockComposer) ((CustomFieldComposer) composer).textBlocks[4]).setValue(port);
        }
        return true;
    }

    /**
     * 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 #setIPv4(int ip, int post) method instead.
     *
     * @deprecated
     */
    public void setText(String text) {
    };

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


⌨️ 快捷键说明

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