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

📄 item.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	 * If the width is between zero and the minimum width, inclusive,
	 * the minimum width is used instead.
	 * If the height is between zero and the minimum height, inclusive,
	 * the minimum height is used instead.
	 * 
	 * <p>Supplying a width or height value greater than the minimum width or
	 * height <em>locks</em> that dimension to the supplied
	 * value.  The implementation may silently enforce a maximum dimension for
	 * an <code>Item</code> based on factors such as the screen size.
	 * Supplying a value of
	 * <code>-1</code> for the width or height unlocks that dimension.
	 * See <a href="#sizes">Item Sizes</a> for a complete discussion.</p>
	 * 
	 * <p>It is illegal to call this method if this <code>Item</code>
	 * is contained within  an <code>Alert</code>.</p>
	 * 
	 * @param width - the value to which the width should be locked, or -1 to unlock
	 * @param height - the value to which the height should be locked, or -1 to unlock
	 * @throws IllegalArgumentException - if width or height is less than -1
	 * @throws IllegalStateException - if this Item is contained within an Alert
	 * @see #getPreferredHeight()
	 * @see #getPreferredWidth()
	 * @since  MIDP 2.0
	 */
	public void setPreferredSize(int width, int height)
	{
		this.preferredHeight = height;
		this.preferredWidth = width;
	}

	/**
	 * Gets the minimum width for this <code>Item</code>.  This is a width
	 * at which the item can function and display its contents,
	 * though perhaps not optimally.
	 * See <a href="#sizes">Item Sizes</a> for a complete discussion.
	 * 
	 * @return the minimum width of the item
	 * @since  MIDP 2.0
	 */
	public int getMinimumWidth()
	{
		return this.minimumWidth;
	}

	/**
	 * Gets the minimum height for this <code>Item</code>.  This is a height
	 * at which the item can function and display its contents,
	 * though perhaps not optimally.
	 * See <a href="#sizes">Item Sizes</a> for a complete discussion.
	 * 
	 * @return the minimum height of the item
	 * @since  MIDP 2.0
	 */
	public int getMinimumHeight()
	{
		return this.minimumHeight;
	}

	/**
	 * Sets default <code>Command</code> for this <code>Item</code>.
	 * If the <code>Item</code> previously had a
	 * default <code>Command</code>, that <code>Command</code>
	 * is no longer the default, but it
	 * remains present on the <code>Item</code>.
	 * 
	 * <p>If not <code>null</code>, the <code>Command</code> object
	 * passed becomes the default <code>Command</code>
	 * for this <code>Item</code>.  If the <code>Command</code> object
	 * passed is not currently present
	 * on this <code>Item</code>, it is added as if <A HREF="../../../javax/microedition/lcdui/Item.html#addCommand(javax.microedition.lcdui.Command)"><CODE>addCommand(javax.microedition.lcdui.Command)</CODE></A>
	 * had been called
	 * before it is made the default <code>Command</code>, unless the &quot;polish.Item.suppressDefaultCommand&quot; preprocessing variable is set to &quot;true&quot;.</p>
	 * 
	 * <p>If <code>null</code> is passed, the <code>Item</code> is set to
	 * have no default <code>Command</code>.
	 * The previous default <code>Command</code>, if any, remains present
	 * on the <code>Item</code>.
	 * </p>
	 * 
	 * <p>It is illegal to call this method if this <code>Item</code>
	 * is contained within  an <code>Alert</code>.</p>
	 * 
	 * @param cmd the command to be used as this Item's default Command, or null if there is to be no default command
	 * @throws IllegalStateException - if this Item is contained within an Alert
	 * @since  MIDP 2.0
	 */
	public void setDefaultCommand( Command cmd)
	{
		//#if !polish.Item.suppressDefaultCommand
		if (this.defaultCommand != null && cmd != this.defaultCommand) {
			addCommand(this.defaultCommand);
		}
		//#endif		
		this.defaultCommand = cmd;
		//#if !polish.Item.suppressDefaultCommand
		if (cmd != null) {
			addCommand(cmd);
		}
		//#endif
	}

	/**
	 * Causes this <code>Item's</code> containing <code>Form</code> to notify
	 * the <code>Item's</code> <CODE>ItemStateListener</CODE>.
	 * The application calls this method to inform the
	 * listener on the <code>Item</code> that the <code>Item's</code>
	 * state has been changed in
	 * response to an action.  Even though this method simply causes a call
	 * to another part of the application, this mechanism is useful for
	 * decoupling the implementation of an <code>Item</code> (in particular, the
	 * implementation of a <code>CustomItem</code>, though this also applies to
	 * subclasses of other items) from the consumer of the item.
	 * 
	 * <p>If an edit was performed by invoking a separate screen, and the
	 * editor now wishes to &quot;return&quot; to the form which contained the
	 * selected <code>Item</code>, the preferred method is
	 * <code>Display.setCurrent(Item)</code>
	 * instead of <code>Display.setCurrent(Displayable)</code>,
	 * because it allows the
	 * <code>Form</code> to restore focus to the <code>Item</code>
	 * that initially invoked the editor.</p>
	 * 
	 * <p>In order to make sure that the documented behavior of
	 * <code>ItemStateListener</code> is maintained, it is up to the caller
	 * (application) to guarantee that this function is
	 * not called unless:</p>
	 * 
	 * <ul>
	 * <li>the <code>Item's</code> value has actually been changed, and</li>
	 * <li>the change was the result of a user action (an &quot;edit&quot;)
	 * and NOT as a result of state change via calls to
	 * <code>Item's</code> APIs </li>
	 * </ul>
	 * 
	 * <p>The call to <code>ItemStateListener.itemStateChanged</code>
	 * may be delayed in order to be serialized with the event stream.
	 * The <code>notifyStateChanged</code> method does not block awaiting
	 * the completion of the <code>itemStateChanged</code> method.</p>
	 * 
	 * @throws IllegalStateException if the Item is not owned by a Form
	 * @since  MIDP 2.0
	 */
	public void notifyStateChanged()
	{
		
		Screen scr = StyleSheet.currentScreen;
		if (scr == null || !(scr instanceof Form)) {
			scr = getScreen();
		}
		//#ifndef polish.skipArgumentCheck
			if ( (!(scr instanceof Form)) || (scr == null)) {
				//#ifdef polish.verboseDebug
					//# throw new IllegalStateException("notifyStateChanged() is valid only for items in Forms.");
				//#else
					throw new IllegalStateException();
				//#endif
			}
		//#endif
		((Form) scr).addToStateNotifyQueue(this);
	}
	
	/**
	 * Paints this item on the screen.
	 * This method should normally not be overriden. Override it
	 * only when you know what you are doing!
	 * 
	 * @param x the left start position of this item.
	 * @param y the top start position of this item.
	 * @param leftBorder the left border, nothing must be painted left of this position
	 * @param rightBorder the right border, nothing must be painted right of this position,
	 * 		  rightBorder > x >= leftBorder
	 * @param g the Graphics on which this item should be painted.
	 */
	public void paint( int x, int y, int leftBorder, int rightBorder, Graphics g ) {
		// initialise this item if necessary:
		int availableWidth = rightBorder - leftBorder;
		int originalX = x;
		int originalY = y;
		if (!this.isInitialized || (availableWidth < this.itemWidth )) {
			//#if polish.debug.info
			//# if (availableWidth < this.itemWidth ) {
				//#debug info
				//# System.out.println("re-initializing item " + this + " for availableWidth=" + availableWidth + ", itemWidth=" + this.itemWidth);
			//# }
			//#endif
			init( rightBorder - x, availableWidth );
		}
		boolean isLayoutShrink = (this.layout & LAYOUT_SHRINK) == LAYOUT_SHRINK;

		// paint background and border when the label should be included in this:
		//#if polish.css.include-label
			//# if (this.includeLabel) {
				//# int width = this.itemWidth - this.marginLeft - this.marginRight;
				//# int height = this.itemHeight - this.marginTop - this.marginBottom;
				//# int bX = x + this.marginLeft;
				//# int bY = y + this.marginTop + this.backgroundYOffset;
				//# if ( this.background != null ) {
					//# this.background.paint(bX, bY, width, height, g);
				//# }
				//# if ( this.border != null ) {
					//# this.border.paint(bX, bY, width, height, g);
				//# }
			//# }
		//#endif
		
		// paint label:
		if (this.label != null) {
			if (this.useSingleRow) {
				this.label.paint( x, y, leftBorder, rightBorder - (this.contentWidth + this.paddingHorizontal), g );
				x += this.label.itemWidth;
				leftBorder += this.label.itemWidth;
			} else {
				this.label.paint( x, y, leftBorder, rightBorder, g );
				y += this.label.itemHeight;
			}
		}
		
		leftBorder += (this.marginLeft + this.borderWidth + this.paddingLeft);
		//#ifdef polish.css.before
			//# leftBorder += this.beforeWidth;
		//#endif
		//System.out.println( this.style.name + ":  increasing leftBorder by " + (this.marginLeft + this.borderWidth + this.paddingLeft));
		rightBorder -= (this.marginRight + this.borderWidth + this.paddingRight);
		//#ifdef polish.css.after
			//# rightBorder -= this.afterWidth;
		//#endif

		//System.out.println( this.style.name + ":  decreasing rightBorder by " + (this.marginRight + this.borderWidth + this.paddingRight));
		if ( this.isLayoutCenter  && availableWidth > this.itemWidth) {
			int difference = (availableWidth - this.itemWidth) / 2; 
			x += difference;
			if (isLayoutShrink) {
				leftBorder += difference;
				rightBorder -= difference;
				//System.out.println("item " + this + ": (center) shrinking left border to " + leftBorder + ", right border to " + rightBorder);
			}
		} else if ( this.isLayoutRight && availableWidth > this.itemWidth) {
			// adjust the x-position so that the item is painted up to
			// the right border (when it starts at x):
			x += availableWidth - this.itemWidth;
			if (isLayoutShrink) {
				leftBorder += availableWidth - this.itemWidth;
				//System.out.println("item " + this + ": (right) shrinking left border to " + leftBorder);
			}
		} else if (isLayoutShrink && availableWidth > this.itemWidth) {
			rightBorder -= availableWidth - this.itemWidth;
			//System.out.println("item " + this + ": (left) shrinking right border to " + rightBorder);
		}
		
		// paint background:
		x += this.marginLeft;
		y += this.marginTop;
		//#if polish.css.include-label
			//# if (!this.includeLabel) {
		//#endif
				if (this.background != null) {
					this.background.paint(x, y + this.backgroundYOffset, this.backgroundWidth, this.backgroundHeight, g);
				}
				// paint border:
				if (this.border != null) {
					this.border.paint(x, y + this.backgroundYOffset, this.backgroundWidth, this.backgroundHeight, g);
				}
		//#if polish.css.include-label
			//# }
		//#endif
		
		x += this.borderWidth + this.paddingLeft;
		y += this.borderWidth + this.paddingTop;
		int originalContentY = y;
		
		// paint before element:
		//#if polish.css.before || polish.css.after || polish.css.min-height  || polish.css.max-height
			//# boolean isVerticalCenter = (this.layout & LAYOUT_VCENTER) == LAYOUT_VCENTER; 
			//# boolean isTop = !isVerticalCenter && (this.layout & LAYOUT_TOP) == LAYOUT_TOP; 
			//# boolean isBottom = !isVerticalCenter && (this.layout & LAYOUT_BOTTOM) == LAYOUT_BOTTOM; 
		//#endif
		//#if polish.css.min-height
			//# if (this.minimumHeight != 0) {
				//# int minHeight = this.minimumHeight - ( 2 * this.borderWidth + this.marginTop + this.marginBottom + this.paddingTop + this.paddingBottom);
				//# if ( isVerticalCenter ) {
//# //					System.out.println("vertical: adjusting contY by " + ((this.minimumHeight - this.contentHeight) / 2)
//# //							+ ", contentHeight=" + this.contentHeight + ", minHeight=" + this.minimumHeight );
					//# y += (minHeight - this.contentHeight) / 2; 
				//# } else if ( isBottom ) {
					//# //System.out.println("bottom: adjusting contY by " + (this.minimumHeight - this.contentHeight) );
					//# y += (minHeight - this.contentHeight);
				//# }
			//# }
		//#endif
		//#ifdef polish.css.before
			//# if (this.beforeImage != null) {
				//# int beforeY = y;
				//# int yAdjustment = this.beforeHeight - this.contentHeight;
				//# if ( this.beforeHeight < this.contentHeight) {
					//# if (isTop) {
						//# beforeY -= yAdjustment;
					//# } else if (isBottom) {
						//# beforeY += yAdjustment;
					//# } else {
						//# beforeY -= yAdjustment / 2;
					//# }
				//# } else {
					//# if (isTop) {
						//# // keep contY
					//# } else if (isBottom) {
						//# y += yAdjustment;
					//# } else {
						//# y += yAdjustment / 2;
					//# }
					//# //contY += (this.beforeHeight - this.contentHeight) / 2;
				//# }
				//# g.drawImage(this.beforeImage, x, beforeY, Graphics.TOP | Graphics.LEFT );
				//# x += this.beforeWidth;
			//# }
		//#endif
		
		// paint after element:
		//#ifdef polish.css.after
			//# if (this.afterImage != null) {
				//# int afterY = originalContentY;
				//# int yAdjustment = this.afterHeight - this.contentHeight;
				//# if ( this.afterHeight < this.contentHeight) {
					//# if (isTop) {
						//# afterY -= yAdjustment;
					//# } else if (isBottom) {
						//# afterY += yAdjustment;
					//# } else {
						//# afterY -= yAdjustment / 2;
					//# }
					//# //afterY += (this.contentHeight - this.afterHeight) / 2;
				//# } else {
					//#ifdef polish.css.before
					//# if (this.afterHeight > this.beforeHeight) {
					//#endif
						//# if (isTop) {
							//# // keep contY
						//# } else if (isBottom) {
							//# y = originalContentY + yAdjustment;
						//# } else {
							//# y = originalContentY + yAdjustment / 2;
						//# }
						//# //contY = originalContentY + (this.afterHeight - this.contentHeight) / 2;
					//#

⌨️ 快捷键说明

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