📄 choicegroup.java
字号:
numOfEls - elementNum - 1); } if (fontEls != null) { System.arraycopy(fontEls, elementNum + 1, fontEls, elementNum, numOfEls - elementNum - 1); } if (choiceType == ChoiceGroup.MULTIPLE) { System.arraycopy(selEls, elementNum + 1, selEls, elementNum, numOfEls - elementNum - 1); } } if (choiceType == ChoiceGroup.MULTIPLE) { selEls[numOfEls - 1] = false; } --numOfEls; stringEls[numOfEls] = null; if (imageEls != null) { imageEls[numOfEls] = null; mutableImageEls[numOfEls] = null; } if (fontEls != null) { fontEls[numOfEls] = null; } // layouts[--numOfEls] = null; 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; } } } invalidate(); } // synchronized } /** * Deletes all elements from this <code>ChoiceGroup</code>. */ public void deleteAll() { synchronized (Display.LCDUILock) { for (int x = 0; x < numOfEls; x++) { stringEls[x] = null; if (imageEls != null) { imageEls[x] = null; mutableImageEls[x] = null; } if (fontEls != null) { fontEls[x] = null; } } numOfEls = 0; hilightedIndex = selectedIndex = -1; invalidate(); } } /** * Sets the <code>String</code> and <code>Image</code> parts of the * element referenced by <code>elementNum</code>, * replacing the previous contents of the element. * * @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 <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 set(int elementNum, String stringPart, Image imagePart) { synchronized (Display.LCDUILock) { checkIndex(elementNum); checkNull(stringPart, imagePart); setImpl(elementNum, stringPart, imagePart); } } /** * Gets a boolean value indicating whether this element is selected. * * @param elementNum the index of the element to be queried * * @return selection state of the element * * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid */ public boolean isSelected(int elementNum) { synchronized (Display.LCDUILock) { checkIndex(elementNum); return (choiceType == Choice.MULTIPLE ? selEls[elementNum] : (selectedIndex == elementNum)); } } /** * Returns the index number of an element in the * <code>ChoiceGroup</code> that is * selected. For <code>ChoiceGroup</code> objects of type * <code>EXCLUSIVE</code> and <code>POPUP</code> * there is at most one element selected, so * this method is useful for determining the user's choice. * Returns <code>-1</code> if * there are no elements in the <code>ChoiceGroup</code>. * * <p>For <code>ChoiceGroup</code> objects of type * <code>MULTIPLE</code>, this always * returns <code>-1</code> because no * single value can in general represent the state of such a * <code>ChoiceGroup</code>. * To get the complete state of a <code>MULTIPLE</code> * <code>Choice</code>, see {@link * #getSelectedFlags getSelectedFlags}.</p> * * @return index of selected element, or <code>-1</code> if none * @see #setSelectedIndex */ public int getSelectedIndex() { // SYNC NOTE: return of atomic value, no locking necessary return selectedIndex; } /** * Queries the state of a <code>ChoiceGroup</code> and returns the state of * all elements in the * boolean array * <code>selectedArray_return</code>. <strong>Note:</strong> this * is a result parameter. * It must be at least as long as the size * of the <code>ChoiceGroup</code> as returned by <code>size()</code>. * If the array is longer, the extra * elements are set to <code>false</code>. * * <p>For <code>ChoiceGroup</code> objects of type * <code>MULTIPLE</code>, any * number of elements may be selected and set to true in the result * array. For <code>ChoiceGroup</code> objects of type * <code>EXCLUSIVE</code> and <code>POPUP</code> * exactly one element will be selected, unless there are * zero elements in the <code>ChoiceGroup</code>. </p> * * @return the number of selected elements in the <code>ChoiceGroup</code> * * @param selectedArray_return array to contain the results * @throws IllegalArgumentException if <code>selectedArray_return</code> * is shorter than the size of the <code>ChoiceGroup</code> * @throws NullPointerException if <code>selectedArray_return</code> * 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; } } /** * For <code>ChoiceGroup</code> objects of type * <code>MULTIPLE</code>, this simply sets an * individual element's selected state. * * <P>For <code>ChoiceGroup</code> objects of type * <code>EXCLUSIVE</code> and <code>POPUP</code>, 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 <code>elementNum</code> parameter * must be within * the range * <code>[0..size()-1]</code>, inclusive. </p> * * @param elementNum the number of the element. Indexing of the * elements is zero-based * @param selected the new state of the element <code>true=selected</code>, * <code>false=not</code> selected * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid * @see #getSelectedIndex */ public void setSelectedIndex(int elementNum, boolean selected) { synchronized (Display.LCDUILock) { setSelectedIndexImpl(elementNum, selected); } // synchronized } /** * Attempts to set the selected state of every element in the * <code>ChoiceGroup</code>. The array * must be at least as long as the size of the * <code>ChoiceGroup</code>. If the array is * longer, the additional values are ignored. <p> * * For <code>ChoiceGroup</code> objects of type * <code>MULTIPLE</code>, this sets the selected * state of every * element in the <code>Choice</code>. An arbitrary number of * elements may be selected. * <p> * * For <code>ChoiceGroup</code> objects of type * <code>EXCLUSIVE</code> and <code>POPUP</code>, exactly one array * element must have the value <code>true</code>. If no element is * <code>true</code>, * the first element * in the <code>Choice</code> will be selected. If two or more * elements are <code>true</code>, the * implementation will choose the first <code>true</code> element * and select it. <p> * * @param selectedArray an array in which the method collect the * selection status * @throws IllegalArgumentException if <code>selectedArray</code> * is shorter than the size of the <code>ChoiceGroup</code> * @throws NullPointerException if the <code>selectedArray</code> * is <code>null</code> * @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); } else { int i = 0; for (; i < numOfEls; i++) { if (selectedArray[i]) { break; } } if (i == numOfEls) { i = 0; } setSelectedIndexImpl(i, true); } repaint(); } // synchronized } /** * Sets the application's preferred policy for fitting * <code>Choice</code> element * contents to the available screen space. The set policy applies for all * elements of the <code>Choice</code> object. Valid values are * {@link #TEXT_WRAP_DEFAULT}, {@link #TEXT_WRAP_ON}, * and {@link #TEXT_WRAP_OFF}. Fit policy is a hint, and the * implementation may disregard the application's preferred policy. * * @param fitPolicy preferred content fit policy for choice elements * @throws IllegalArgumentException if <code>fitPolicy</code> is invalid * @see #getFitPolicy * @since MIDP 2.0 */ public void setFitPolicy(int fitPolicy) { if (fitPolicy < TEXT_WRAP_DEFAULT || fitPolicy > TEXT_WRAP_OFF) { throw new IllegalArgumentException(); } synchronized (Display.LCDUILock) { if (this.fitPolicy != fitPolicy) { this.fitPolicy = fitPolicy; invalidate(); } } } /** * Gets the application's preferred policy for fitting * <code>Choice</code> element * contents to the available screen space. The value returned is the * policy that had been set by the application, even if that value had * been disregarded by the implementation. * * @return one of {@link #TEXT_WRAP_DEFAULT}, {@link #TEXT_WRAP_ON}, or * {@link #TEXT_WRAP_OFF} * @see #setFitPolicy * @since MIDP 2.0 */ public int getFitPolicy() { // SYNC NOTE: return of atomic value, no locking necessary return fitPolicy; } /** * Sets the application's preferred font for * rendering the specified element of this <code>Choice</code>. * An element's font is a hint, and the implementation may disregard * the application's preferred font. * * <p> The <code>elementNum</code> parameter must be within the range * <code>[0..size()-1]</code>, inclusive.</p> * * <p> The <code>font</code> parameter must be a valid <code>Font</code> * object or <code>null</code>. If the <code>font</code> parameter is * <code>null</code>, the implementation must use its default font * to render the element.</p> * * @param elementNum the index of the element, starting from zero * @param font the preferred font to use to render the element * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid * @see #getFont * @since MIDP 2.0 */ public void setFont(int elementNum, Font font) { synchronized (Display.LCDUILock) { checkIndex(elementNum); if (fontEls == null) { fontEls = new Font[numOfEls]; } fontEls[elementNum] = font; } } /** * Gets the application's preferred font for * rendering the specified element of this <code>Choice</code>. The * value returned is the font that had been set by the application, * even if that value had been disregarded by the implementation. * If no font had been set by the application, or if the application * explicitly set the font to <code>null</code>, the value is the default * font chosen by the implementation. * * <p> The <code>elementNum</code> parameter must be within the range * <code>[0..size()-1]</code>, inclusive.</p> * * @param elementNum the index of the element, starting from zero * @return the preferred font to use to render the element * @throws IndexOutOfBoundsException if <code>elementNum</code> is invalid * @see #setFont(int elementNum, Font font) * @since MIDP 2.0 */ public Font getFont(int elementNum) { synchronized (Display.LCDUILock) { checkIndex(elementNum); if (fontEls != null && fontEls[elementNum] != null) { return fontEls[elementNum]; } else { return Screen.CONTENT_FONT; } } }// ***********************************************************// package private// *********************************************************** /** * Determine if this Item should have a newline after it * * @return true if it should have a newline after */ boolean equateNLA() { if (super.equateNLA()) { return true; } return ((layout & Item.LAYOUT_2) != Item.LAYOUT_2); } /** * Determine if this Item should have a newline before it * * @return true if it should have a newline before */ boolean equateNLB() { if (super.equateNLB()) { return true; } return ((layout & Item.LAYOUT_2) != Item.LAYOUT_2); } /** * Get the minimum width of this Item * * @return the minimum width */ int callMinimumWidth() { int lW = getLabelWidth(); if (lW == 0 && numOfEls == 0) { return 0; } if ((layout == Item.LAYOUT_DEFAULT) || (equateNLB() && equateNLA())) { if (owner != null) { return ((Form)owner).getWidth(); } return Item.DEFAULT_WIDTH; } // Find the widest element and then base our width on that int textOffset = 0; int w = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -