textfield.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,236 行 · 第 1/4 页

JAVA
1,236
字号
        }    }    /**     * Sets the input constraints of the <code>TextField</code>. If     * the the current contents     * of the <code>TextField</code> do not match the new     * <code>constraints</code>, the contents are     * set to empty.     *     * @param constraints see <a href="#constraints">input constraints</a>     *     * @throws IllegalArgumentException if constraints is not any of the ones     * specified in <a href="TextField.html#constraints">input constraints</a>     * @see #getConstraints     */    public void setConstraints(int constraints)  {        synchronized (Display.LCDUILock) {            setConstraintsImpl(constraints);        }    }      /**     * Gets the current input constraints of the <code>TextField</code>.     *     * @return the current constraints value (see     * <a href="#constraints">input constraints</a>)     * @see #setConstraints     */    public int getConstraints() {	return constraints;    }    /**     * Sets a hint to the implementation as to the input mode that should be      * used when the user initiates editing of this <code>TextField</code>.  The     * <code>characterSubset</code> parameter names a subset of Unicode      * characters that is used by the implementation to choose an initial      * input mode.  If <code>null</code> is passed, the implementation should      * choose a default input mode.     *     * <p>See <a href="#modes">Input Modes</a> for a full explanation of input      * modes. </p>     *     * @param characterSubset a string naming a Unicode character subset,     * or <code>null</code>     *     */    public void setInitialInputMode(String characterSubset) {        synchronized (Display.LCDUILock) {            initialInputMode = characterSubset;            textFieldLF.lSetInitialInputMode(initialInputMode);                    }    }    // ========================================================================    // package private methods    // ========================================================================    /**     * Creates a new <code>TextField</code> object with the given label, initial     * contents, maximum size in characters, and constraints.  Behaves     * the same as the public <code>TextField</code> constructor above except     * for an additional argument <code>forTextBox</code> which signals      * this <code>TextField</code> will be used alone as a      * <code>TextBox</code> widget.     * @param label item label     * @param text the initial contents, or <code>null</code> if the     * <code>TextField</code> is to be empty     * @param maxSize the maximum capacity in characters     * @param constraints see <a href="#constraints">input constraints</a>     * @param forTextBox true if this textField will be used to implement     *        a TextBox object.  when false, this method's results are     *        identical to the public <code>TextField</code> constructor.     * @throws IllegalArgumentException if <code>maxSize</code> is zero or less     * @throws IllegalArgumentException if the value of the constraints     * parameter     * is invalid     * @throws IllegalArgumentException if <code>text</code> is illegal     * for the specified constraints     * @throws IllegalArgumentException if the length of the string exceeds     * the requested maximum capacity      */        TextField(String label, String text, int maxSize, 	      int constraints, boolean forTextBox) {        super(label);        synchronized (Display.LCDUILock) {            // IllegalArgumentException thrown here            buffer = new DynamicCharacterArray(maxSize);	    // Constraint value is checked here. Since textFieldLF is not	    // yet created, no LF notification will happen.            setConstraintsImpl(constraints);	    if (forTextBox) {		itemLF = textFieldLF = LFFactory.getFactory().getTextBoxLF(this);	    } else {		// Create a LF with empty content		itemLF = textFieldLF = LFFactory.getFactory().getTextFieldLF(this);	    }            //            // this will use inputClient	    // Right now setCharsImpl notifies LF a content change.	    // If LF is created as an absolutely last thing then            // setCharsImple here does not need the notification.            if (text == null) {                setCharsImpl(null, 0, 0);            } else {                setCharsImpl(text.toCharArray(), 0, text.length());            }        }    }    /**     * Deletes characters from the <code>TextField</code>.     *     * <p>The <code>offset</code> and <code>length</code> parameters must     * specify a valid range of characters within     * the contents of the <code>TextField</code>.     * The <code>offset</code> parameter must be within the     * range <code>[0..(size())]</code>, inclusive.     * The <code>length</code> parameter     * must be a non-negative integer such that     * <code>(offset + length) &lt;= size()</code>.</p>     *      * @param offset the beginning of the region to be deleted     * @param length the number of characters to be deleted     *     * @throws IllegalArgumentException if the resulting contents     * would be illegal for the current     * <a href="TextField.html#constraints">input constraints</a>     * @throws StringIndexOutOfBoundsException if <code>offset</code>     * and <code>length</code> do not     * specify a valid range within the contents of the <code>TextField</code>     */    void deleteImpl(int offset, int length) {        if (length == 0) {	    return;	}	// Update buffer with latest user input	textFieldLF.lUpdateContents();	// Keep old contents in case we need to restore below	String oldContents = buffer.toString();	// StringIndexOutOfBoundsException can be thrown here        buffer.delete(offset, length);        if (!textFieldLF.lValidate(buffer, constraints)) {	    // Restore to old contents	    buffer.delete(0, buffer.length()); 	    buffer.insert(0, oldContents);	    throw new IllegalArgumentException();        }	// Notify LF that contents has changed due to delete	textFieldLF.lDelete(offset, length);    }    /**     * Sets the contents of the <code>TextField</code> from a     * character array, replacing the     * previous contents.     *     * @param data the source of the character data     * @param offset the beginning of the region of characters to copy     * @param length the number of characters to copy     * @throws ArrayIndexOutOfBoundsException if <code>offset</code>     * and <code>length</code> do not specify     * a valid range within the data array     * @throws IllegalArgumentException if <code>data</code>     * is illegal for the current     * <a href="TextField.html#constraints">input constraints</a>     * @throws IllegalArgumentException if the text would exceed the current     * maximum capacity     */    void setCharsImpl(char[] data, int offset, int length) {        if (data == null) {            buffer.delete(0, buffer.length());        } else {                        if (offset < 0 || offset > data.length                || length < 0 || length > data.length                || offset + length < 0                || offset + length > data.length) {                throw new ArrayIndexOutOfBoundsException();            }                        if (length > buffer.capacity()) {                throw new IllegalArgumentException();            }                        if (length > 0) {                DynamicCharacterArray dca =                     new DynamicCharacterArray(length);                                dca.set(data, offset, length);                                if (!textFieldLF.lValidate(dca, constraints)) {                    throw new IllegalArgumentException();                }            }                        buffer.set(data, offset, length);        }	// Notify LF contents has changed due to setChars	textFieldLF.lSetChars();    }    /**     * Sets the input constraints of the <code>TextField</code>.     * @param constraints see <a href="#constraints">input constraints</a>     *     * @throws IllegalArgumentException if constraints is not any of the ones     * specified in <a href="TextField.html#constraints">input constraints</a>     */    void setConstraintsImpl(int constraints)  {        if ((constraints & CONSTRAINT_MASK) < ANY ||             (constraints & CONSTRAINT_MASK) > DECIMAL) {                 throw new IllegalArgumentException();        }        	this.constraints = constraints;	// Since this function is called from Constructor before	// LF is created, checking is necessary.        if (textFieldLF == null) {	    return;	}	textFieldLF.lSetConstraints();	// If current contents doesn't satisfy new constraints,	// set it to empty.	textFieldLF.lUpdateContents();	int curLen = buffer.length();        if (curLen > 0 && !textFieldLF.lValidate(buffer, constraints)) {            buffer.delete(0, curLen);	    textFieldLF.lDelete(0, curLen);        }    }    /**     * Inserts data into the buffer.     *      * @param data - data to be inserted     * @param offset - <placeholder>     * @param length - <placeholder>     * @param position - <placeholder>     */    void insertImpl(char data[], int offset, int length, int position) {	textFieldLF.lUpdateContents();        int pos = buffer.insert(data, offset, length, position);        if (!textFieldLF.lValidate(buffer, constraints)) {            buffer.delete(pos, length); // reverse insertion            throw new IllegalArgumentException();        }	// Notify LF contents has changed due a insertion        textFieldLF.lInsert(data, offset, length, pos);    }        /**     * Return whether the Item takes user input focus.     *     * @return Always return <code>true</code> so user can scroll     * or highlight selection.     */    boolean acceptFocus() {	return true;    }        /**     * Notify the item to the effect that it has been recently deleted.     * In addition to default action call TraverseOut for the TextField      */     void itemDeleted() {         textFieldLF.itemDeleted();         super.itemDeleted();     }    /**     * The look&feel associated with this TextField.      * Set in the constructor.     */    TextFieldLF textFieldLF; // = null    /** buffer to store the text */    DynamicCharacterArray buffer;     /** Input constraints */    int constraints;    /** the initial input mode for when the text field gets focus */    String initialInputMode = null; } 

⌨️ 快捷键说明

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