📄 choicegroup.java
字号:
* @param elementNum the index of the element to be set * @param stringPart the string part of the new element * @param imagePart the image part of the element, 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 set(int elementNum, String stringPart, Image imagePart) { synchronized (Display.LCDUILock) { checkIndex(elementNum); checkNull(stringPart, imagePart); setImpl(elementNum, stringPart, imagePart); } } /** * @param elementNum the index of the element to be queried * * @return selection state of the element * * @throws IndexOutOfBoundsException if elementNum is invalid */ public boolean isSelected(int elementNum) { synchronized (Display.LCDUILock) { checkIndex(elementNum); return (choiceType == Choice.MULTIPLE ? selEls[elementNum] : (selectedIndex == elementNum)); } } /** * <p>Returns the index number of an element in the ChoiceGroup that is * selected. For ChoiceGroup objects of type EXCLUSIVE * there is at most one element selected, so * this method is useful for determining the user's choice. Returns -1 if * there are no elements in the ChoiceGroup. </p> * * <p>For ChoiceGroup objects of type MULTIPLE, this always * returns -1 because no * single value can in general represent the state of such a ChoiceGroup. * To get the complete state of a MULTIPLE Choice, see {@link * #getSelectedFlags getSelectedFlags}.</p> * * @return index of selected element, or -1 if none * * @see #setSelectedIndex */ public int getSelectedIndex() { // SYNC NOTE: return of atomic value, no locking necessary return selectedIndex; } /** * <p>Queries the state of a ChoiceGroup and returns the state * of all elements in the * boolean array selectedArray_return. NOTE: this is a result parameter. * It must be at least as long as the size * of the ChoiceGroup as returned by size(). * If the array is longer, the extra * elements are set to false.</p> * * <p>For ChoiceGroup objects of type MULTIPLE, any * number of elements may be selected and set to true in the result * array. For ChoiceGroup objects of type EXCLUSIVE, * exactly one element will be selected, unless there are * zero elements in the ChoiceGroup. </p> * * @return the number of selected elements in the ChoiceGroup * * @param selectedArray_return array to contain the results. * @throws IllegalArgumentException if selectedArray_return is shorter * than the size of the ChoiceGroup. * @throws NullPointerException if selectedArray_return is null. * * @see #setSelectedFlags */ public int getSelectedFlags(boolean[] selectedArray_return) { synchronized (Display.LCDUILock) { checkFlag(selectedArray_return); int selectedNum = 0; if (choiceType == Choice.MULTIPLE) { System.arraycopy(selEls, 0, selectedArray_return, 0, numOfEls); for (int i = 0; i < numOfEls; i++) { if (selEls[i]) selectedNum++; } for (int i = numOfEls; i < selectedArray_return.length; i++) { selectedArray_return[i] = false; } } else { for (int i = 0; i < selectedArray_return.length; i++) { selectedArray_return[i] = false; } if (selectedIndex != -1) { selectedArray_return[selectedIndex] = true; selectedNum = 1; } } return selectedNum; } } /** * <P>For ChoiceGroup objects of type MULTIPLE, this simply sets an * individual element's selected state. </P> * * <P>For ChoiceGroup objects of type EXCLUSIVE, this can be used only to * select an element. That is, the <code> selected </code> parameter must * be <code> true </code>. When an element is selected, the previously * selected element is deselected. If <code> selected </code> is <code> * false </code>, this call is ignored.</P> * * <p>For both list types, the elementNum parameter must be within the range * [0..size()-1], inclusive. </p> * * @param elementNum the number of the element. Indexing of the elements * is zero-based. * @param selected the new state of the element true=selected, * false=not selected. * @throws IndexOutOfBoundsException if elementNum is invalid * * @see #getSelectedIndex */ public void setSelectedIndex(int elementNum, boolean selected) { synchronized (Display.LCDUILock) { checkIndex(elementNum); Screen owner = getOwner(); switch (choiceType) { case Choice.IMPLICIT: // hilight changes as a result of selection only // if it is an implicit choice hilightedIndex = elementNum; /* falls through */ case Choice.EXCLUSIVE: // selected item cannot be deselected if (selectedIndex == elementNum && !selected) { return; } int oldSelected = selectedIndex; selectedIndex = elementNum; if (owner != null && owner.isShown()) { repaintElements(oldSelected, selectedIndex); } break; case Choice.MULTIPLE: selEls[elementNum] = selected; if (owner != null && owner.isShown()) { repaintElements(elementNum, -1); } break; } } // synchronized } /** * Attempts to set the selected state of every element in the * ChoiceGroup. The array * must be at least as long as the size of the ChoiceGroup. If the array is * longer, the additional values are ignored. <p> * * For ChoiceGroup objects of type MULTIPLE, this sets the * selected state of every * element in the Choice. An arbitrary number of elements may be selected. * <p> * * For ChoiceGroup objects of type EXCLUSIVE, exactly one array * element must have the value <code>true</code>. If no element * is true, the first element * in the Choice will be selected. If two or more elements are true, the * implementation will choose the first true element and select it. <p> * * @param selectedArray an array in which the method collect * the selection status * @throws IllegalArgumentException if selectedArray is shorter than the * size of the ChoiceGroup. * @throws NullPointerException if the selectedArray is null. * * @see #getSelectedFlags */ public void setSelectedFlags(boolean[] selectedArray) { synchronized (Display.LCDUILock) { checkFlag(selectedArray); if (numOfEls == 0) { return; } if (choiceType == Choice.MULTIPLE) { System.arraycopy(selectedArray, 0, selEls, 0, numOfEls); // repaint if ChoiceGroup was added to Form or List repaint(0, 0, Display.WIDTH, height); } else { int i = 0; for (; i < numOfEls; i++) { if (selectedArray[i]) { break; } } if (i == numOfEls) { i = 0; } setSelectedIndex(i, true); } } // synchronized } /** * Sets the label of the Item. If label is null, specifies that this * item has no label. * @param label the label string */ public void setLabel(String label) { synchronized (Display.LCDUILock) { super.setLabel(label); int deltaHeight = ((StringLayout)layouts[0]).setString(label); height += deltaHeight; contentChanged(0, 0, deltaHeight); } } // package private /** * Signal this ChoiceGroup that its focus state has changed */ void focusChanged() { repaintElements(hilightedIndex, -1); } /** * Traverse this ChoiceGroup * * @param dir The direction of traversal, UP or DOWN * @param top * @param bottom * @param traversingIn * @param allowTraverseOut * @return int The number of pixels traversed */ int traverse(int dir, int top, int bottom, boolean traversingIn, boolean allowTraverseOut) { boolean down = dir == Canvas.DOWN; // If there are no items scolling is done only inside the label if (numOfEls == 0) { return Form.traverse(down, top, bottom, getHeight(), Screen.CONTENT_HEIGHT, traversingIn, false); } // When we are traversingIn // we should scroll by as much as we could if (traversingIn) { int s = Form.traverse(down, top, bottom, getHeight(), Screen.CONTENT_HEIGHT, traversingIn, true); if (down) { // first item will not be made visible after scroll if (bottom + s < layouts[1].getHeight()) { return s; } hilightedIndex = 0; } else { hilightedIndex = numOfEls-1; } if (s == 0) { repaintElements(hilightedIndex, -1); } return s; } // top and bottom in the coordinate system of hilightedIndex int hilightedY = getYpos(0, layouts[0].getHeight(), hilightedIndex); int hilTop = top - hilightedY; int hilBottom = bottom - hilightedY; // see if traversal within current item is needed int s = Form.traverse(down, hilTop, hilBottom, layouts[hilightedIndex+1].getHeight(), layouts[hilightedIndex+1].getLineHeight(), false, true); // move to the next item if (s < 0) { // traversal within bottom most item was done if (down && hilightedIndex == numOfEls-1) { return s; } if (!down && hilightedIndex == 0) { // traversal within top most item was done return Form.traverse(down, top, bottom, getHeight(), Screen.CONTENT_HEIGHT, traversingIn, true); } // move hilight to the next item int oldHilight = hilightedIndex; if (down) { hilightedY += layouts[hilightedIndex+1].getHeight(); hilightedIndex++; } else { hilightedIndex--; hilightedY -= layouts[hilightedIndex+1].getHeight(); } hilTop = top - hilightedY; hilBottom = bottom - hilightedY; // see if traversal within current item is needed s = Form.traverse(down, hilTop, hilBottom, layouts[hilightedIndex+1].getHeight(), layouts[hilightedIndex+1].getLineHeight(), true, true); if (choiceType == Choice.IMPLICIT) { // in IMPLICIT list hilight and selection go together selectedIndex = hilightedIndex; } // no scrolling is needed // (entire new hilighted element is visible) if (hilightedIndex != -1 && top <= hilightedY && bottom >= hilightedY+layouts[hilightedIndex+1].getHeight()) { repaintElements(oldHilight, hilightedIndex); return 0; } } return s; } /** * Determine if this ChoiceGroup can take the focus or not * * @return boolean Wether this ChoiceGroup has enough elements to * take the focus
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -