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

📄 container.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			}		//#endif					boolean isLayoutShrink = (this.layout & LAYOUT_SHRINK) == LAYOUT_SHRINK;		boolean hasFocusableItem = false;		for (int i = 0; i < myItems.length; i++) {			Item item = myItems[i];			//System.out.println("initalising " + item.getClass().getName() + ":" + i);			int width = item.getItemWidth( lineWidth, lineWidth );			int height = item.itemHeight; // no need to call getItemHeight() since the item is now initialised...			// now the item should have a style, so it can be safely focused			// without loosing the style information:			if (item.appearanceMode != PLAIN) {				hasFocusableItem = true;			}			if (this.autoFocusEnabled  && (i >= this.autoFocusIndex ) && (item.appearanceMode != Item.PLAIN)) {				//#debug				System.out.println("Container: autofocusing element " + i);				this.autoFocusEnabled = false;				focus( i, item, 0 );				height = item.getItemHeight(lineWidth, lineWidth);				if (!isLayoutShrink) {					width = item.itemWidth;  // no need to call getItemWidth() since the item is now initialised...				} else {					width = 0;				}				if (this.enableScrolling) {					//#debug					System.out.println("initContent(): scrolling autofocused item");					scroll( true, 0, myContentHeight, width, height );				}			} else if (i == this.focusedIndex) {				if (isLayoutShrink) {					width = 0;				}				if (this.isScrollRequired) {					//#debug					System.out.println("initContent(): scroll is required.");					scroll( true, 0, myContentHeight, width, height );					this.isScrollRequired = false;				}			} 			if (width > myContentWidth) {				myContentWidth = width; 			}			item.yTopPos = myContentHeight;			item.yBottomPos = myContentHeight + height;			myContentHeight += height + this.paddingVertical;			//System.out.println("item.yTopPos=" + item.yTopPos);		}		if (!hasFocusableItem) {			this.appearanceMode = PLAIN;		} else {			this.appearanceMode = INTERACTIVE;			if (isLayoutShrink && this.focusedItem != null) {				Item item = this.focusedItem;				//System.out.println("container has shrinking layout and contains focuse item " + item);				item.isInitialised = false;				boolean doExpand = item.isLayoutExpand;				int width;				if (doExpand) {					item.isLayoutExpand = false;					width = item.getItemWidth( lineWidth, lineWidth );					item.isInitialised = false;					item.isLayoutExpand = true;				} else {					width = item.itemWidth;				}				if (width > myContentWidth) {					myContentWidth = width;				}				if ( this.minimumWidth != 0 && myContentWidth < this.minimumWidth ) {					myContentWidth = this.minimumWidth;				}				//myContentHeight += item.getItemHeight( lineWidth, lineWidth );			}		}		} catch (ArrayIndexOutOfBoundsException e) {			//#debug error			System.out.println("Unable to init container " + e );		}		this.contentHeight = myContentHeight;		this.contentWidth = myContentWidth;		}		/* (non-Javadoc)	 * @see de.enough.polish.ui.Item#paintItem(int, int, javax.microedition.lcdui.Graphics)	 */	protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {		// paints all items,		// the layout will be done according to this containers'		// layout or according to the items layout, when specified.		// adjust vertical start for scrolling:		System.out.println("2------------------------");		//#if polish.debug.debug			if (this.enableScrolling) {				//#debug 				System.out.println("Container: drawing " + getClass().getName() + " with yOffset=" + this.yOffset );			}		//#endif		boolean setClipping = (this.yOffset != 0 && this.marginTop != 0);		int clipX = 0;		int clipY = 0;		int clipWidth = 0;		int clipHeight = 0;		if (setClipping) {			clipX = g.getClipX();			clipY = g.getClipY();			clipWidth = g.getClipWidth();			clipHeight = g.getClipHeight();			g.clipRect(clipX, y - this.paddingTop, clipWidth, clipHeight - ((y - this.paddingTop) - clipY) );		}		x = leftBorder;		y += this.yOffset;		//#ifdef tmp.supportViewType			if (this.view != null) {				this.view.paintContent(x, y, leftBorder, rightBorder, g);			} else {		//#endif			Item[] myItems;			//synchronized (this.itemsList) {				myItems = this.items;			//}			int focusedX = x;			int focusedY = 0;			int focusedRightBorder = rightBorder;			if (!(this.isLayoutCenter || this.isLayoutRight)) {				// adjust the right border:				rightBorder = leftBorder + this.contentWidth;			}			Item focItem = this.focusedItem;			int focIndex = this.focusedIndex;			for (int i = 0; i < myItems.length; i++) {				Item item = myItems[i];				// currently the NEWLINE_AFTER and NEWLINE_BEFORE layouts will be ignored,				// since after every item a line break will be done.				if (i == focIndex) {					focusedY = y;					item.getItemHeight( rightBorder - x, rightBorder - leftBorder );				} else {					// the currently focused item is painted last					item.paint(x, y, leftBorder, rightBorder, g);				}				y += item.itemHeight + this.paddingVertical;			}							// paint the currently focused item:			if (focItem != null) {				//System.out.println("Painting focusedItem " + this.focusedItem + " with width=" + this.focusedItem.itemWidth + " and with increased colwidth of " + (focusedRightBorder - focusedX)  );				focItem.paint(focusedX, focusedY, focusedX, focusedRightBorder, g);			}		//#ifdef tmp.supportViewType			}		//#endif				Item item = this.focusedItem;		if (item != null) {			if (item.internalX != -9999) {				this.internalX =  item.contentX - this.contentX + item.internalX;				this.internalWidth = item.internalWidth;				this.internalY = item.contentY - this.contentY + item.internalY;				this.internalHeight = item.internalHeight;			} else {				this.internalX = item.xLeftPos - this.contentX;				this.internalWidth = item.itemWidth;				this.internalY = item.yTopPos + this.yOffset; //(item.yTopPos - this.yOffset) - this.contentY;				this.internalHeight = item.itemHeight;			}			// outcommented by rob - 2006-07-13 - now positions are determined in the initContent() method already//			if (this.isFirstPaint) {//				this.isFirstPaint = false;//				if (this.enableScrolling) {//					if ( this.contentY + this.internalY + this.internalHeight > this.yBottom ) {//						//#debug//						System.out.println("first paint, now scrolling...");//						scroll( true, this.contentX, this.contentY + this.internalY, this.contentWidth, this.internalHeight + 3 ); // + 3 gives room for a focused border etc//					}//				}//			}		}		if (setClipping) {			g.setClip(clipX, clipY, clipWidth, clipHeight);		}	}	//#ifdef polish.useDynamicStyles	/* (non-Javadoc)	 * @see de.enough.polish.ui.Item#getCssSelector()	 */	protected String createCssSelector() {		return "container";	}	//#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 (this.focusedItem != null) {			Item item = this.focusedItem;			if ( item.handleKeyPressed(keyCode, gameAction) ) {				if (this.enableScrolling && item.internalX != -9999) {					adjustScrolling(item);				}				//#debug				System.out.println("Container(" + this + "): handleKeyPressed consumed by item " + item.getClass().getName() + "/" + item );								return true;			}			}		// now allow a navigation within the container:		//#ifdef tmp.supportViewType			if (this.view != null) {				boolean handled = this.view.handleKeyPressed(keyCode, gameAction);				if (handled) {					return true;				}//				Item next = this.view.getNextItem(keyCode, gameAction);//				if (next != null) {//					focus( this.view.focusedIndex, next, gameAction );//					return true;//				} else 				if (this.enableScrolling) {										if (gameAction == Canvas.UP && this.targetYOffset < 0 ) {						// scroll the container view upwards without changing the focused item:						//#if polish.Container.ScrollDelta:defined							//#= this.targetYOffset += ${polish.Container.ScrollDelta};						//#else							this.targetYOffset += 30;						//#endif						if (this.targetYOffset > 0 ) {							this.targetYOffset = 0;						}						//#if polish.scroll-mode							if (!this.scrollSmooth) {								this.yOffset = this.targetYOffset;							}						//#endif						return true;					}					if (gameAction == Canvas.DOWN							&& (this.itemHeight + this.targetYOffset > (this.yBottom - this.yTop)) ) 					{						// scroll the container view downwards without changing the focused item:						//#if polish.Container.ScrollDelta:defined							//#= this.targetYOffset -= ${polish.Container.ScrollDelta};						//#else							this.targetYOffset -= 30;						//#endif						//#if polish.scroll-mode							if (!this.scrollSmooth) {								this.yOffset = this.targetYOffset;							}						//#endif						return true;					}				}				return false;			}		//#endif		boolean processed = false;		int availableHeight = this.yBottom - this.yTop;		int offset;		//#if polish.scroll-mode			if (this.scrollSmooth) {		//#endif				offset = this.targetYOffset;		//#if polish.scroll-mode			} else {				offset = this.yOffset;			}		//#endif		if ( (gameAction == Canvas.RIGHT  && keyCode != Canvas.KEY_NUM6) 				|| (gameAction == Canvas.DOWN  && keyCode != Canvas.KEY_NUM8)) {			if (this.focusedItem != null 					&& this.enableScrolling					&& offset + this.focusedItem.yBottomPos > availableHeight) 			{				if (gameAction == Canvas.RIGHT) {					return false;				}				// keep the focus do scroll downwards:				//#debug				System.out.println("Container(" + this + "): scrolling down: keeping focus, focusedIndex=" + this.focusedIndex );			} else {				processed = shiftFocus( true, 0 );			}			//#debug			System.out.println("Container(" + this + "): forward shift by one item succeded: " + processed + ", focusedIndex=" + this.focusedIndex );			if ((!processed) && this.enableScrolling 					&&  ( (this.focusedItem != null && offset + this.focusedItem.yBottomPos > availableHeight)						|| offset + this.itemHeight > availableHeight)) {				// scroll downwards:				//#if polish.Container.ScrollDelta:defined					//#= offset -= ${polish.Container.ScrollDelta};				//#else					offset -= 30;				//#endif				//#debug				System.out.println("Down/Right: Reducing (target)YOffset to " + offset);					processed = true;				//#if polish.scroll-mode					if (this.scrollSmooth) {				//#endif						this.targetYOffset = offset;				//#if polish.scroll-mode					} else {						this.yOffset = offset;					}				//#endif			}		} else if ( (gameAction == Canvas.LEFT  && keyCode != Canvas.KEY_NUM4) 				|| (gameAction == Canvas.UP && keyCode != Canvas.KEY_NUM2) ) {			if (this.focusedItem != null 					&& this.enableScrolling					&& offset + this.focusedItem.yTopPos < 0 ) // this.focusedItem.yTopPos < this.yTop ) 			{				if (gameAction == Canvas.LEFT) {					return false;				}				// keep the focus do scroll upwards:				//#debug				System.out.println("Container(" + this + "): scrolling up: keeping focus, focusedIndex=" + this.focusedIndex + ", focusedItem.yTopPos=" + this.focusedItem.yTopPos + ", this.yTop=" + this.yTop + ", targetYOffset=" + this.targetYOffset);			} else {				processed = shiftFocus( false, 0 );			}			//#debug			System.out.println("Container(" + this + "): upward shift by one item succeded: " + processed + ", focusedIndex=" + this.focusedIndex );			if ((!processed) && this.enableScrolling && (offset < 0)) {				// scroll upwards:				//#if polish.Container.ScrollDelta:defined					//#= offset += ${polish.Container.ScrollDelta};				//#else					offset += 30;				//#endif				if (offset > 0) {					offset = 0;				}				//#if polish.scroll-mode					if (this.scrollSmooth) {				//#endif						this.targetYOffset = offset;				//#if polish.scroll-mode					} else {						this.yOffset = offset;					}				//#endif				//#debug				System.out.println("Up/Left: Increasing (target)YOffset to " + offset);					processed = true;			}		}		return processed;	}			/* (non-Javadoc)	 * @see de.enough.polish.ui.Item#handleKeyReleased(int, int)	 */	protected boolean handleKeyReleased(int keyCode, int gameAction) {		if (this.itemsList.size() == 0) {			return false;		}		if (this.focusedItem != null) {			Item item = this.focusedItem;			if ( item.handleKeyReleased( keyCode, gameAction ) ) {				if (this.enableScrolling && item.internalX != -9999) {					adjustScrolling(item);

⌨️ 快捷键说明

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