📄 choicegroup.java
字号:
/* * @(#)ChoiceGroup.java 1.94 01/08/21 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;/** * <p> A ChoiceGroup 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 "radio buttons" for the single choice * mode and "check boxes" for the multiple choice mode. </p> * * <p> <b>Note:</b> most of the essential methods have been specified in * the {@link Choice Choice} interface.</p> */public class ChoiceGroup extends Item implements Choice { /** * The type of this ChoiceGroup */ private int choiceType; /** * The number of elements in this ChoiceGroup */ private int numOfEls; /** * The currently selected index of this ChoiceGroup (-1 by default) */ private int selectedIndex = -1; /** * The currently highlighted index of this ChoiceGroup (-1 by default) */ private int hilightedIndex = -1; /** * The array of selected elements of this ChoiceGroup (in the case * of a multi-select type) */ private boolean selEls[]; /** * The pixel height of this ChoiceGroup */ private int height; /** * Creates a new, empty ChoiceGroup, specifying its title and its type. * The type must be one of EXCLUSIVE or MULTIPLE. The IMPLICIT * choice type is not allowed within a ChoiceGroup. <p> * * @param label the item's label (see {@link Item Item}) * @param choiceType either EXCLUSIVE or MULTIPLE * @throws IllegalArgumentException if choice type is neither * EXCLUSIVE nor MULTIPLE * @see Choice#EXCLUSIVE * @see Choice#MULTIPLE * @see Choice#IMPLICIT */ public ChoiceGroup(String label, int choiceType) { this(label, choiceType, new String[] {}, null); } /** * <p>Creates a new ChoiceGroup, specifying its title, the type of the * ChoiceGroup, and an array of Strings and Images to be used as its * initial contents. </p> * * <p>The type must be one of EXCLUSIVE or MULTIPLE. The IMPLICIT type is * not allowed for ChoiceGroup.</p> * * <p>The stringElements array must be non-null and every array element * must also be non-null. The length of the stringElements array * determines the number of elements in the ChoiceGroup. The * imageElements array * may be null to indicate that the ChoiceGroup elements have no * images. If the * imageElements array is non-null, it must be the same length as the * stringElements array. Individual elements of the imageElements array * may be null in order to indicate the absence of an image for the * corresponding ChoiceGroup element. Any elements present in the * imageElements array must refer to immutable images.</p> * * @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 * * @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 */ 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) { // If stringElements is null NullPointerException will be thrown // as expected super(label, stringElements.length + 1); if (imageElements != null && stringElements.length != imageElements.length) { throw new IllegalArgumentException(); } if (!((choiceType == Choice.MULTIPLE) || (choiceType == Choice.EXCLUSIVE) || ((choiceType == Choice.IMPLICIT) && implicitAllowed))) { throw new IllegalArgumentException(); } synchronized (Display.LCDUILock) { this.choiceType = choiceType; numOfEls = stringElements.length; switch (choiceType) { case Choice.MULTIPLE: selEls = new boolean[numOfEls]; for (int i = 0; i < numOfEls; i++) { selEls[i] = false; } break; case Choice.IMPLICIT: case Choice.EXCLUSIVE: if (numOfEls > 0) { selectedIndex = 0; } break; } // layout array and its 0 element (label) are allocated in // the super class (Item) for (int i = 0; i < numOfEls; i++) { Image img = imageElements == null ? null : imageElements[i]; checkNull(stringElements[i], img); boolean offset = choiceType == Choice.IMPLICIT && img == null; layouts[i + 1] = new IaSLayout(stringElements[i], img, Screen.CONTENT_FONT, offset); } } // synchronized } /** * @return the number of elements in the ChoiceGroup */ public int size() { // SYNC NOTE: return of atomic value, no locking necessary return numOfEls; } /** * @param elementNum the index of the element to be queried * @return the string part of the element * @throws IndexOutOfBoundsException if elementNum is invalid * @see #getImage(int) */ public String getString(int elementNum) { synchronized (Display.LCDUILock) { checkIndex(elementNum); return ((IaSLayout)layouts[elementNum + 1]).getString(); } } /** * @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 ((IaSLayout)layouts[elementNum + 1]).getImage(); } } /** * @param stringPart the string part of the element to be added * @param imagePart the image part of the element to be added, or null if * there is no image part * @return the assigned index of the element * @throws IllegalArgumentException if the image is mutable * @throws NullPointerException if stringPart is null */ public int append(String stringPart, Image imagePart) { synchronized (Display.LCDUILock) { checkNull(stringPart, imagePart); return insertImpl(numOfEls, stringPart, imagePart); } } /** * @param elementNum the index of the element where insertion is to occur * @param stringElement the string part of the element to be inserted * @param imageElement the image part of the element to be inserted, * or null if there is no image part * @throws IndexOutOfBoundsException if elementNum is invalid * @throws IllegalArgumentException if the image is mutable * @throws NullPointerException if stringPart is null */ public void insert(int elementNum, String stringElement, Image imageElement) { synchronized (Display.LCDUILock) { if (elementNum < 0 || elementNum > numOfEls) { throw new IndexOutOfBoundsException(); } checkNull(stringElement, imageElement); insertImpl(elementNum, stringElement, imageElement); } } /** * @param elementNum the index of the element to be deleted * @throws IndexOutOfBoundsException if elementNum is invalid */ public void delete(int elementNum) { synchronized (Display.LCDUILock) { checkIndex(elementNum); int deletedHeight = 0; if (initLayoutDone()) { deletedHeight = -layouts[elementNum + 1].getHeight(); height += deletedHeight; } if (elementNum != numOfEls - 1) { System.arraycopy(layouts, elementNum + 2, layouts, elementNum + 1, numOfEls - elementNum - 1); if (choiceType == ChoiceGroup.MULTIPLE) { System.arraycopy(selEls, elementNum + 1, selEls, elementNum, numOfEls - elementNum - 1); } } layouts[numOfEls] = null; if (choiceType == ChoiceGroup.MULTIPLE) { selEls[numOfEls - 1] = false; } numOfEls--; if (numOfEls == 0) { hilightedIndex = selectedIndex = -1; } else { // adjust hilighted index if (elementNum < hilightedIndex) { hilightedIndex--; } else if (elementNum == hilightedIndex && hilightedIndex == numOfEls) { hilightedIndex = numOfEls - 1; } // adjust selected index if choiceGroup is not MULTIPLE if (choiceType != ChoiceGroup.MULTIPLE) { if (elementNum < selectedIndex) { selectedIndex--; } else if (elementNum == selectedIndex && selectedIndex == numOfEls) { selectedIndex = numOfEls - 1; } } } int firstIndexY = layouts[0].getHeight(); int hilightedIndexY = hilightedIndex == -1 ? 0 : getYpos( 0, firstIndexY, hilightedIndex); Screen owner = getOwner(); if (owner != null) { owner.contentChanged(null, 0, hilightedIndexY, deletedHeight); } } // synchronized } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -