📄 choicegroup.java
字号:
public int getSelectedFlags(boolean[] selectedArray_return) { //#ifndef polish.skipArgumentCheck if (selectedArray_return.length < this.itemsList.size()) { //#ifdef polish.verboseDebug throw new IllegalArgumentException("length of selectedArray is too small"); //#else //# throw new IllegalArgumentException(); //#endif } //#endif ChoiceItem[] myItems = (ChoiceItem[]) this.itemsList.toArray( new ChoiceItem[ this.itemsList.size() ] ); int selectedItems = 0; for (int i = 0; i < myItems.length; i++) { ChoiceItem item = myItems[i]; if (item.isSelected) { selectedArray_return[i] = true; selectedItems++; } else { selectedArray_return[i] = false; } } return selectedItems; } /** * 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 true=selected, false=not selected * @throws IndexOutOfBoundsException if elementNum is invalid * @see Choice#setSelectedIndex(int, boolean) in interface Choice * @see #getSelectedIndex() */ public void setSelectedIndex(int elementNum, boolean selected) { if (this.isMultiple) { ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum ); item.select( selected ); } else { if (selected == false) { return; // ignore this call } if (this.selectedIndex != -1) { ChoiceItem oldSelected = (ChoiceItem) this.itemsList.get( this.selectedIndex ); oldSelected.select( false ); } ChoiceItem newSelected = (ChoiceItem) this.itemsList.get( elementNum ); newSelected.select( true ); this.selectedIndex = elementNum; if (this.isFocused) { if ( this.isInitialised) { focus( elementNum, newSelected, 0 ); } else { this.autoFocusEnabled = true; this.autoFocusIndex = elementNum; } } //#ifdef polish.usePopupItem if (this.isPopup) { this.popupItem.setText( newSelected.getText() ); } //#endif } if (this.isInitialised) { this.isInitialised = false; repaint(); } } /** * 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 selectedArray is shorter than the size of the ChoiceGroup * @throws NullPointerException if the selectedArray is null * @see Choice#setSelectedFlags(boolean[]) in interface Choice * @see #getSelectedFlags(boolean[]) */ public void setSelectedFlags(boolean[] selectedArray) { //#ifndef polish.skipArgumentCheck if (selectedArray.length < this.itemsList.size()) { //#ifdef polish.verboseDebug throw new IllegalArgumentException("length of selectedArray is too small"); //#else //# throw new IllegalArgumentException(); //#endif } //#endif if (this.isMultiple) { ChoiceItem[] myItems = (ChoiceItem[]) this.itemsList.toArray( new ChoiceItem[ this.itemsList.size() ] ); for (int i = 0; i < myItems.length; i++) { ChoiceItem item = myItems[i]; item.select( selectedArray[i]); } } else { int index = 0; for (int i = 0; i < selectedArray.length; i++) { if (selectedArray[i]) { index = i; break; } } if (index > this.itemsList.size()) { index = 0; } setSelectedIndex( index, true ); } if (this.isInitialised) { this.isInitialised = false; repaint(); } } /** * 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 * <CODE>Choice.TEXT_WRAP_DEFAULT</CODE>, * <CODE>Choice.TEXT_WRAP_ON</CODE>, * and <CODE>Choice.TEXT_WRAP_OFF</CODE>. * Fit policy is a hint, and the * implementation may disregard the application's preferred policy. * The J2ME Polish implementation always uses the TEXT_WRAP_ON policy. * * @param fitPolicy preferred content fit policy for choice elements * @see Choice#setFitPolicy(int) in interface Choice * @see #getFitPolicy() * @since MIDP 2.0 */ public void setFitPolicy(int fitPolicy) { //this.fitPolicy = fitPolicy; // ignore hint } /** * 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 always Choice.TEXT_WRAP_ON * @see Choice#getFitPolicy() in interface Choice * @see #setFitPolicy(int) * @since MIDP 2.0 */ public int getFitPolicy() { return Choice.TEXT_WRAP_ON; } /** * 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. * The J2ME Polish implementation uses the font defined by the appropriate * CSS style and ignores the font which is set here. * * @param elementNum the index of the element, starting from zero * @param font the preferred font to use to render the element * @throws IndexOutOfBoundsException if elementNum is invalid * @see Choice#setFont(int, Font) in interface Choice * @see #getFont(int) * @since MIDP 2.0 */ public void setFont(int elementNum, Font font) { ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum ); item.setPreferredFont( 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 elementNum is invalid * @see Choice#getFont(int) in interface Choice * @see #setFont(int elementNum, Font font) * @since MIDP 2.0 */ public Font getFont(int elementNum) { ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum ); Font font = item.preferredFont; if (font == null) { font = item.font; } return font; } //#ifdef polish.usePopupItem protected void hideNotify() { if (this.isPopup && !this.isPopupClosed) { closePopup(); } } //#endif //#ifdef polish.usePopupItem /* (non-Javadoc) * @see de.enough.polish.ui.Item#paint(int, int, javax.microedition.lcdui.Graphics) */ public void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) { //#if tmp.supportViewType if (this.view != null) { super.paintContent(x, y, leftBorder, rightBorder, g); } //#endif if (this.isPopup && this.isPopupClosed) { this.popupItem.paintContent(x, y, leftBorder, rightBorder, g); } else { super.paintContent(x, y, leftBorder, rightBorder, g ); } } //#endif //#ifdef polish.usePopupItem protected void init( int firstLineWidth, int lineWidth ) { super.init(firstLineWidth, lineWidth); //#if tmp.supportViewType if (this.view != null) { return; } //#endif if (this.isPopup && !this.isPopupClosed) { this.backgroundWidth += (this.originalContentWidth - this.contentWidth); this.backgroundHeight += (this.originalContentHeight - this.contentHeight); } } //#endif //#ifdef polish.usePopupItem /* (non-Javadoc) * @see de.enough.polish.ui.Item#initItem() */ protected void initContent(int firstLineWidth, int lineWidth) { super.initContent(firstLineWidth, lineWidth); //#if tmp.supportViewType if (this.view != null) { return; } //#endif if (this.isPopup) { if (this.popupItem.image == null) { this.popupItem.setImage( createPopupImage() ); } if (this.isPopupClosed) { if (this.popupItem.getText() == null && this.itemsList.size() > 0) { ChoiceItem selectedItem = (ChoiceItem) this.itemsList.get( 0 ); this.popupItem.setText( selectedItem.getText() ); } if (!this.popupItem.isInitialised) { int noneContentWidth = this.marginLeft + this.borderWidth + this.paddingLeft + this.marginRight + this.borderWidth + this.paddingRight; this.popupItem.init(firstLineWidth + noneContentWidth, lineWidth + noneContentWidth); } } else { this.originalContentWidth = this.contentWidth; this.originalContentHeight = this.contentHeight; } //this.contentWidth = this.popupItem.contentWidth; this.contentHeight = this.popupItem.contentHeight; } } //#endif //#ifdef polish.useDynamicStyles /* (non-Javadoc) * @see de.enough.polish.ui.Item#getCssSelector() */ protected String createCssSelector() { return "choicegroup"; } //#endif //#ifdef polish.usePopupItem private void closePopup() { this.isPopupClosed = true; /* int difference = this.popupOpenY - this.yTopPos; if (difference > 0 && (this.parent instanceof Container) && (((Container)this.parent).yOffset != 0) ) { //System.out.println("Closing popup: adjusting parent.offset from [" + (((Container)this.parent).yOffset) + "] to [" + (((Container)this.parent).yOffset + difference) + "]."); ((Container)this.parent).yOffset += difference; } */ if (this.parent instanceof Container) { ((Container)this.parent).yOffset = this.popupParentOpenY; } /* this.internalX = -9999; if (this.yOffset < 0) { System.out.println("Closing popup: adjusting ChoiceGroup.offset from [" + this.yOffset + "] to [" + (this.yOffset + (this.contentHeight - this.popupItem.contentHeight)) + "]."); this.yOffset += (this.contentHeight - this.popupItem.contentHeight); if (this.yOffset > 0 ) { this.yOffset = 0; } } */ requestInit(); } //#endif //#ifdef polish.usePopupItem private void openPopup() { //this.popupOpenY = this.yTopPos; if (this.parent instanceof Container) { this.popupParentOpenY = ((Container)this.parent).yOffset; } this.isPopupClosed = false; focus( this.selectedIndex ); // recalculate the internal positions of the selected choice: Item item = this.items[ this.selectedIndex ]; if (item.yTopPos != item.yBottomPos) { // okay, this item has been painted alrady: this.internalY = (item.itemHeight + this.paddingVertical) * this.selectedIndex; this.internalHeight = item.itemHeight; this.internalX = 0; this.internalWidth = item.itemWidth; } else { this.internalX = 0; this.internalY = 0; this.internalHeight = this.itemHeight + 20; this.internalWidth = this.itemWidth; } } //#endif /* (non-Javadoc) * @see de.enough.polish.ui.Item#handleKeyPressed(int, int) */ protected boolean handleKeyPressed(int keyCode, int gameAction) { if (this.itemsList.size() == 0) { return false; } //#if polish.ChoiceGroup.handleDefaultCommandFirst == true if (gameAction == Canvas.FIRE) { //#ifdef polish.usePopupItem if (!this.isPopup || this.isPopupClosed) { //#endif //#ifndef tmp.suppressAllCommands if (this.defaultCommand != null && this.additionalItemCommandListener != null) { this.additionalItemCommandListener.commandAction( this.defaultCommand, this ); return true; } //#else if (this.defaultCommand != null && this.itemCommandListener != null) { this.itemCommandListener.commandAction( this.defaultCommand, this ); return true; } //#endif //#ifdef polish.usePopupItem } //#endif } //#endif boolean processed = false; //#ifdef polish.usePopupItem if (!(this.isPopup && this.isPopupClosed)) { processed = super.handleKeyPressed(keyCode, gameAction); //#debug System.out.println("ChoiceGroup: container handled keyPressEvent: " + processed); } if (!processed) { if ( gameAction == Canvas.FIRE && keyCode != Canvas.KEY_NUM5 ) { if (this.isMultiple) { ChoiceItem item = (ChoiceItem) this.focusedItem; item.toggleSelect(); } else if (this.isPopup){ if (this.isPopupClosed) { openPopup(); } else { setSelectedIndex(this.focusedIndex, true); closePopup(); } requestInit(); } else { setSelectedIndex(this.focusedIndex, true); if (this.isImplicit) { // call command listener: Screen scr = getScreen(); if (scr != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -