⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choicegroup.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
				throw new IllegalArgumentException("invalid choiceType [" + choiceType + "] - IMPLICIT=" + Choice.IMPLICIT + ".");			//#else				//# throw new IllegalArgumentException();			//#endif		}		this.choiceType = choiceType;		for (int i = 0; i < items.length; i++) {			ChoiceItem item = items[i];			append( item );		}	}		/**	 * Builds an array of <code>ChoiceItems</code> out of	 * an array of <code>String</code>s and <code>Image</code>s,	 * specifying the <code>choiceType</code> and <code>style</code>	 * common to any <code>ChoiceItem</code> in the resulting array.	 * 	 * @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 choiceType EXCLUSIVE, MULTIPLE, or POPUP	 * @param style The CSS style for this item	 * @return an aray of choice items	 * @throws NullPointerException if stringElements is null or 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	 * @see Choice#EXCLUSIVE	 * @see Choice#MULTIPLE	 * @see Choice#IMPLICIT	 * @see Choice#POPUP	 */	protected static ChoiceItem[] buildChoiceItems(String[] stringElements, Image[] imageElements, int choiceType, Style style)	{		//#ifndef polish.skipArgumentCheck			if (imageElements != null && imageElements.length != stringElements.length) {				//#ifdef polish.verboseDebug					throw new IllegalArgumentException("imageElements need to have the same length as the stringElements.");				//#else					//# throw new IllegalArgumentException();				//#endif			}		//#endif		ChoiceItem[] items = new ChoiceItem[stringElements.length];		for (int i = 0; i < stringElements.length; ++i) {			Image img = null;			if (imageElements != null) {				img = imageElements[i];			}			items[i] = new ChoiceItem( stringElements[i], img, choiceType, style );		}		return items;	}		//#ifdef polish.usePopupItem	/**	 * Creates or returns the default image for popup groups.	 * 	 * @return the default popup image	 */	protected Image createPopupImage() {		if (popupImage == null) {			popupImage = Image.createImage( 9, 12 );			Graphics g = popupImage.getGraphics();			g.setColor( this.popupBackgroundColor );			g.fillRect(0, 0, 10, 13 );			g.setColor( this.popupColor );			g.drawLine(0, 0, 9, 0 );			g.drawLine( 3, 3, 3, 9 );			g.drawLine( 4, 3, 4, 10 );			g.drawLine( 5, 3, 5, 9 );			g.drawLine( 2, 8, 6, 8 );				g.drawLine( 1, 7, 7, 7 );			}		return popupImage;	}	//#endif	/**	 * 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 elementNum is invalid	 * @see Choice#getString(int) in interface Choice	 * @see #getImage(int)	 */	public String getString(int elementNum)	{		ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );		return item.getText();	}	/**	 * 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 Choice#getImage(int) in interface Choice	 * @see #getString(int)	 */	public Image getImage(int elementNum)	{		ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );		return item.getImage();	}	/**	 * Gets the <code>ChoiceItem</code> of the element referenced by	 * <code>elementNum</code>.	 *	 * @param elementNum the number of the element to be queried	 * @return the ChoiceItem of the element	 * @throws IndexOutOfBoundsException if elementNum is invalid	 */	public ChoiceItem getItem( int elementNum )	{		return (ChoiceItem)this.itemsList.get( elementNum );	}	/**	 * 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 null if there is no image part	 * @return the assigned index of the element	 * @throws NullPointerException if stringPart is null	 * @see Choice#append( String, Image) in interface Choice	 */	public int append( String stringPart, Image imagePart)	{		return append( stringPart, imagePart, null );	}		/**	 * 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 null if there is no image part	 * @param elementStyle the style for the appended ChoiceItem	 * @return the assigned index of the element	 * @throws NullPointerException if stringPart is null	 * @see Choice#append( String, Image) in interface Choice	 */	public int append( String stringPart, Image imagePart, Style elementStyle )	{		ChoiceItem item = new ChoiceItem( stringPart, imagePart, this.choiceType, elementStyle );		return append( item, elementStyle );	}		/**	 * Appends a ChoiceItem to this choice group.	 * 	 * @param item the item	 * @return the assigned index of the element	 */	public int append( ChoiceItem item ) {		return append( item, null );	}	/**	 * Appends a ChoiceItem to this choice group.	 * 	 * @param item the item	 * @param elementStyle the style of the item, ignored when null	 * @return the assigned index of the element	 */	public int append( ChoiceItem item, Style elementStyle ) {		add( item );		if ( elementStyle != null ) {			item.setStyle( elementStyle );		}		//#ifdef polish.usePopupItem			if (this.isPopup && this.isPopupClosed && this.selectedIndex == -1) {				this.popupItem.setText( item.text );				this.selectedIndex = 0;			}		//#endif		return this.itemsList.size() - 1;	}	/**	 * 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 null if there is no image part	 * @throws IndexOutOfBoundsException if elementNum is invalid	 * @throws NullPointerException if stringPart is null	 * @see Choice#insert(int, String, Image)  in interface Choice	 */	public void insert(int elementNum, String stringPart, Image imagePart)	{		insert( elementNum, stringPart, imagePart, null );	}	/**	 * 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 null if there is no image part	 * @param elementStyle the style for the inserted ChoiceItem	 * @throws IndexOutOfBoundsException if elementNum is invalid	 * @throws NullPointerException if stringPart is null	 * @see Choice#insert(int, String, Image)  in interface Choice	 */	public void insert(int elementNum, String stringPart, Image imagePart, Style elementStyle)	{		ChoiceItem item = new ChoiceItem( stringPart, imagePart, this.choiceType, elementStyle );		add(elementNum, item);	}	/**	 * 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 item ChoiceItem of the element to be inserted	 * @throws IndexOutOfBoundsException if elementNum is invalid	 */	public void insert(int elementNum, ChoiceItem item)	{		add(elementNum, item);	}	/**	 * 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 null if there is no image part	 * @throws IndexOutOfBoundsException - if elementNum is invalid	 * @throws NullPointerException - if stringPart is null	 * @see Choice#set(int, String, Image) in interface Choice	 */	public void set(int elementNum, String stringPart, Image imagePart)	{		set( elementNum, stringPart, imagePart, null );	}	/**	 * 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 null if there is no image part	 * @param elementStyle the style for the new list element.	 * @throws IndexOutOfBoundsException if elementNum is invalid	 * @throws NullPointerException if stringPart is null	 * @see Choice#set(int, String, Image) in interface Choice	 */	public void set(int elementNum, String stringPart, Image imagePart, Style elementStyle )	{		ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );		item.setText( stringPart );		if (imagePart != null) {			item.setImage(imagePart);		}		if (elementStyle != null) {			item.setStyle(elementStyle);		}				//#if polish.i18n.useDynamicTranslations//		if (elementNum == this.focusedIndex) {//				if (this.selectCommand == List.SELECT_COMMAND ) {//					String selectLabel = Locale.get("polish.command.select");//					if ( selectLabel != List.SELECT_COMMAND.getLabel()) {//						List.SELECT_COMMAND = new Command( selectLabel, Command.ITEM, 3 );//						removeCommand(this.selectCommand);//						this.selectCommand = List.SELECT_COMMAND;//						addCommand( this.selectCommand );//					}//				}//		}		//#endif		if (this.isInitialised) {			this.isInitialised = false;			repaint();		}	}	/**	 * Sets the <code>ChoiceItem</code> of the	 * element referenced by <code>elementNum</code>,	 * replacing the previous one.	 * 	 * @param elementNum the index of the element to be set	 * @param item the ChoiceItem of the new element	 * @throws IndexOutOfBoundsException if elementNum is invalid	 */	public void set(int elementNum, ChoiceItem item )	{		delete( elementNum );		add( elementNum, item );		if (this.isInitialised) {			this.isInitialised = false;			repaint();		}	}	/**	 * Deletes the element referenced by <code>elementNum</code>.	 * 	 * @param elementNum the index of the element to be deleted	 * @throws IndexOutOfBoundsException if elementNum is invalid	 * @see Choice#delete(int) in interface Choice	 */	public void delete(int elementNum)	{		remove(elementNum);		//#ifdef polish.usePopupItem			if (this.isPopup) {				if (this.selectedIndex == elementNum ) {					if (this.itemsList.size() > 0) {						this.selectedIndex = -1;						if (this.isPopupClosed) {							setSelectedIndex( 0, true );						}					} else {						this.selectedIndex = -1;					}				} else if ( elementNum < this.selectedIndex ) {					this.selectedIndex--;				}			} else {		//#endif			if (this.selectedIndex == elementNum ) {				this.selectedIndex = -1;			} else if (elementNum < this.selectedIndex) {				this.selectedIndex--;			}		//#ifdef polish.usePopupItem			}		//#endif	}	/**	 * Deletes all elements from this <code>ChoiceGroup</code>.	 * 	 * @see Choice#deleteAll() in interface Choice	 */	public void deleteAll()	{		clear();		this.selectedIndex = -1;	}		/**	 * 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 elementNum is invalid	 * @see Choice#isSelected(int) in interface Choice	 */	public boolean isSelected(int elementNum)	{		ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );		return item.isSelected;	}	/**	 * 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 <A HREF="../../../javax/microedition/lcdui/ChoiceGroup.html#getSelectedFlags(boolean[])"><CODE>getSelectedFlags</CODE></A>.</p>	 * 	 * @return index of selected element, or -1 if none	 * @see Choice#getSelectedIndex() in interface Choice	 * @see #setSelectedIndex(int, boolean)	 */	public int getSelectedIndex()	{		if (this.isMultiple || this.itemsList.size() == 0) {			return -1;		} else if (this.isImplicit) {			return this.focusedIndex;		} else {			return this.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>	 * 	 * @param selectedArray_return array to contain the results	 * @return the number of selected elements in the ChoiceGroup	 * @throws IllegalArgumentException if selectedArray_return is shorter than the size of the ChoiceGroup	 * @throws NullPointerException if selectedArray_return is null	 * @see Choice#getSelectedFlags(boolean[]) in interface Choice	 * @see #setSelectedFlags(boolean[])	 */

⌨️ 快捷键说明

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