choicegroup.java

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

JAVA
940
字号
/* *    * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package javax.microedition.lcdui;/** * A <code>ChoiceGroup</code> is a group of selectable elements intended to be * placed within a * {@link Form}. The group may be created with a mode that requires a * single choice to be made or that allows multiple choices. The * implementation is responsible for providing the graphical representation of * these modes and must provide visually different graphics for different * modes. For example, it might use &quot;radio buttons&quot; for the * single choice * mode and &quot;check boxes&quot; for the multiple choice mode. * * <p> <strong>Note:</strong> most of the essential methods have been * specified in the {@link Choice Choice} interface.</p> * @since MIDP 1.0 */public class ChoiceGroup extends Item implements Choice {    /**     * Creates a new, empty <code>ChoiceGroup</code>, specifying its     * title and its type.     * The type must be one of <code>EXCLUSIVE</code>,     * <code>MULTIPLE</code>, or <code>POPUP</code>. The     * <code>IMPLICIT</code>     * choice type is not allowed within a <code>ChoiceGroup</code>.     *     * @param label the item's label (see {@link Item Item})     * @param choiceType <code>EXCLUSIVE</code>, <code>MULTIPLE</code>,     * or <code>POPUP</code>     * @throws IllegalArgumentException if <code>choiceType</code>      *      is not one of     * <code>EXCLUSIVE</code>, <code>MULTIPLE</code>, or <code>POPUP</code>     * @see Choice#EXCLUSIVE     * @see Choice#MULTIPLE     * @see Choice#IMPLICIT     * @see Choice#POPUP     */    public ChoiceGroup(String label, int choiceType) {        this(label, choiceType, new String[] {}, null);    }    /**     * Creates a new <code>ChoiceGroup</code>, specifying its title,     * the type of the     * <code>ChoiceGroup</code>, and an array of <code>Strings</code>     * and <code>Images</code> to be used as its     * initial contents.     *     * <p>The type must be one of <code>EXCLUSIVE</code>,     * <code>MULTIPLE</code>, or <code>POPUP</code>.  The     * <code>IMPLICIT</code>     * type is not allowed for <code>ChoiceGroup</code>.</p>     *     * <p>The <code>stringElements</code> array must be non-null and     * every array element     * must also be non-null.  The length of the     * <code>stringElements</code> array     * determines the number of elements in the <code>ChoiceGroup</code>.  The     * <code>imageElements</code> array     * may be <code>null</code> to indicate that the     * <code>ChoiceGroup</code> elements have no images.     * If the     * <code>imageElements</code> array is non-null, it must be the     * same length as the     * <code>stringElements</code> array.  Individual elements of the     * <code>imageElements</code> array     * may be <code>null</code> in order to indicate the absence of an     * image for the     * corresponding <code>ChoiceGroup</code> element.  Non-null elements     * of the     * <code>imageElements</code> array may refer to mutable or     * immutable images.</p>     *     * @param label the item's label (see {@link Item Item})     * @param choiceType <code>EXCLUSIVE</code>, <code>MULTIPLE</code>,     * or <code>POPUP</code>     * @param stringElements set of strings specifying the string parts of the     * <code>ChoiceGroup</code> elements     * @param imageElements set of images specifying the image parts of     * the <code>ChoiceGroup</code> elements     *     * @throws NullPointerException if <code>stringElements</code>     * is <code>null</code>     * @throws NullPointerException if the <code>stringElements</code>     * array contains     * any <code>null</code> elements     * @throws IllegalArgumentException if the <code>imageElements</code>     * array is non-null     * and has a different length from the <code>stringElements</code> array     * @throws IllegalArgumentException if <code>choiceType</code>      *      is not one of     * <code>EXCLUSIVE</code>, <code>MULTIPLE</code>, or <code>POPUP</code>     *     * @see Choice#EXCLUSIVE     * @see Choice#MULTIPLE     * @see Choice#IMPLICIT     * @see Choice#POPUP     */    public ChoiceGroup(String label, int choiceType,                       String[] stringElements, Image[] imageElements) {        this(label, choiceType, stringElements, imageElements, false);    }    /**     * Special constructor used by List     *     * @param label the item's label (see {@link Item Item})     * @param choiceType EXCLUSIVE or MULTIPLE     * @param stringElements set of strings specifying the string parts of the     * ChoiceGroup elements     * @param imageElements set of images specifying the image parts of     * the ChoiceGroup elements     * @param implicitAllowed Flag to allow implicit selection     *     * @throws NullPointerException if stringElements is null     * @throws NullPointerException if the stringElements array contains     * any null elements     * @throws IllegalArgumentException if the imageElements array is non-null     * and has a different length from the stringElements array     * @throws IllegalArgumentException if choiceType is neither     * EXCLUSIVE nor MULTIPLE     * @throws IllegalArgumentException if any image in the imageElements     * array is mutable     *     * @see Choice#EXCLUSIVE     * @see Choice#MULTIPLE     * @see Choice#IMPLICIT     */    ChoiceGroup(String label, int choiceType, String[] stringElements,            Image[] imageElements, boolean implicitAllowed) {        super(label);        if (!((choiceType == Choice.MULTIPLE) ||                (choiceType == Choice.EXCLUSIVE) ||                ((choiceType == Choice.IMPLICIT) && implicitAllowed) ||                (choiceType == Choice.POPUP))) {            throw new IllegalArgumentException();        }        // If stringElements is null NullPointerException will be thrown        // as expected        for (int x = 0; x < stringElements.length; x++) {            if (stringElements[x] == null) {                throw new NullPointerException();            }        }        if (imageElements != null) {            if (stringElements.length != imageElements.length) {                throw new IllegalArgumentException();            }        }        synchronized (Display.LCDUILock) {            this.choiceType = choiceType;            numOfEls = stringElements.length;            cgElements = new CGElement[numOfEls + GROW_FACTOR];            if (imageElements != null) {                for (int i = 0; i < numOfEls; i++) {                    cgElements[i] = new CGElement(stringElements[i],                                                  imageElements[i]);                }            } else {                for (int i = 0; i < numOfEls; i++) {                    cgElements[i] = new CGElement(stringElements[i],                                                  null /* image */);                }            }            itemLF = choiceGroupLF = LFFactory.getFactory().getChoiceGroupLF(this);	    // initialize fonts to default one in all elements;	    // this has to be done after ChoiceGroupLF is created	    for (int i = 0; i < numOfEls; i++) {		cgElements[i].setFont(null);	    }        } // synchronized    }    /**     * Returns the number of elements in the <code>ChoiceGroup</code>.     * @return the number of elements in the <code>ChoiceGroup</code>     */    public int size() {        // SYNC NOTE: return of atomic value, no locking necessary        return numOfEls;    }    /**     * Gets the <code>String</code> part of the element referenced by     * <code>elementNum</code>.     *     * @param elementNum the index of the element to be queried     * @return the string part of the element     * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid     * @see #getImage(int)     */    public String getString(int elementNum) {        synchronized (Display.LCDUILock) {            checkIndex(elementNum);            // return stringEls[elementNum];            return cgElements[elementNum].stringEl;        }    }    /**     * Gets the <code>Image</code> part of the element referenced by     * <code>elementNum</code>.     *     * @param elementNum the number of the element to be queried     * @return the image part of the element, or null if there is no image     * @throws IndexOutOfBoundsException if elementNum is invalid     * @see #getString(int)     */    public Image getImage(int elementNum) {        synchronized (Display.LCDUILock) {            checkIndex(elementNum);            // return as mutable, if possible            return (cgElements[elementNum].mutableImageEl == null ?                    cgElements[elementNum].imageEl :                    cgElements[elementNum].mutableImageEl);        }    }    /**     * Appends an element to the <code>ChoiceGroup</code>.     *     * @param stringPart the string part of the element to be added     * @param imagePart the image part of the element to be added, or     * <code>null</code> if there is no image part     * @return the assigned index of the element     * @throws NullPointerException if <code>stringPart</code> is     * <code>null</code>     */    public int append(String stringPart, Image imagePart) {        int elementNum = -1;        synchronized (Display.LCDUILock) {            checkNull(stringPart);            if ((elementNum = insertImpl(numOfEls, stringPart, imagePart))                 >= 0) {                choiceGroupLF.lInsert(elementNum, stringPart, imagePart);            }        }        return elementNum;    }    /**     * Inserts an element into the <code>ChoiceGroup</code> just prior to     * the element specified.     *     * @param elementNum the index of the element where insertion is to occur     * @param stringPart the string part of the element to be inserted     * @param imagePart the image part of the element to be inserted,     * or <code>null</code> if there is no image part     * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid     * @throws NullPointerException if <code>stringPart</code>     * is <code>null</code>     */    public void insert(int elementNum, String stringPart,                       Image imagePart) {        synchronized (Display.LCDUILock) {            if (elementNum < 0 || elementNum > numOfEls) {                throw new IndexOutOfBoundsException();            }            checkNull(stringPart);            if (insertImpl(elementNum, stringPart, imagePart) >= 0) {                choiceGroupLF.lInsert(elementNum, stringPart, imagePart);            }        }    }    /**     * Deletes the element referenced by <code>elementNum</code>.     *     * @param elementNum the index of the element to be deleted     * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid

⌨️ 快捷键说明

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