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

📄 container.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	}
	
	/**
	 * Retrieves the number of items stored in this container.
	 * 
	 * @return The number of items stored in this container.
	 */
	public int size() {
		return this.itemsList.size();
	}
	
	/**
	 * Retrieves all items which this container holds.
	 * The items might not have been intialised.
	 * 
	 * @return an array of all items.
	 */
	public Item[] getItems() {
		if (!this.isInitialised) {
			return (Item[]) this.itemsList.toArray( new Item[ this.itemsList.size() ]);
		} else {		
			return this.items;
		}
	}
	
	/**
	 * Focuses the specified item.
	 * 
	 * @param index the index of the item. The first item has the index 0, 
	 * 		when -1 is given, the focus will be removed altogether (remember to call defocus( Style ) first in that case). 
	 * @return true when the specified item could be focused.
	 * 		   It needs to have an appearanceMode which is not Item.PLAIN to
	 *         be focusable.
	 */
	public boolean focus(int index) {
		if (index == -1) {
			this.focusedIndex = -1;
			this.focusedItem = null;
			//#ifdef tmp.supportViewType
				//# if (this.view != null) {
					//# this.view.focusedIndex = -1;
					//# this.view.focusedItem = null;
				//# }
			//#endif
			return false;
		}
		Item item = (Item) this.itemsList.get(index );
		if (item.appearanceMode != Item.PLAIN) {
			int direction = 0;
			if (this.isFocused) {
				if (this.focusedIndex == -1) {
					// nothing
				} else if (this.focusedIndex < index ) {
					direction = Canvas.DOWN;
				} else if (this.focusedIndex > index) {
					direction = Canvas.UP;
				}
			
			}
			focus( index, item, direction );			
			return true;
		}
		return false;
	}
	
	/**
	 * Sets the focus to the given item.
	 * 
	 * @param index the position
	 * @param item the item which should be focused
	 * @param direction the direction, either Canvas.DOWN, Canvas.RIGHT, Canvas.UP, Canvas.LEFT or 0.
	 */
	public void focus( int index, Item item, int direction ) {
		//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 427, "Container (" + getClass().getName() + "): Focusing item ", index );
		
		//#if polish.blackberry
			//# getScreen().setFocus( item );
		//#endif
		
		
		if (this.autoFocusEnabled  && !this.isInitialised) {
			// setting the index for automatically focusing the appropriate item
			// during the initialisation:
			//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 438, "Container: Setting autofocus-index to ", index );
			this.autoFocusIndex = index;
			//this.isFirstPaint = true;
			return;
		}
		
		if (index == this.focusedIndex && item.isFocused) {
			//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 446, "Container: ignoring focusing of item ", index );
			// ignore the focusing of the same element:
			return;
		}
		// first defocus the last focused item:
		if (this.focusedItem != null) {
			if (this.itemStyle != null) {
				this.focusedItem.defocus(this.itemStyle);
			} else {
				//#debug error
de.enough.polish.util.Debug.debug("error", "de.enough.polish.ui.Container", 456, "Container: Unable to defocus item - no previous style found.");
				this.focusedItem.defocus( StyleSheet.defaultStyle );
			}
		}
		
		this.itemStyle = item.focus( this.focusedStyle, direction );
		//#ifdef polish.debug.error
			if (this.itemStyle == null) {
				//#debug error 
de.enough.polish.util.Debug.debug("error", "de.enough.polish.ui.Container", 465, "Container: Unable to retrieve style of item ", item.getClass().getName() );
			}
		//#endif
		boolean isDownwards = (direction == Canvas.DOWN) || (direction == Canvas.RIGHT) || (direction == 0 &&  index > this.focusedIndex);
		this.focusedIndex = index;
		this.focusedItem = item;
		//#if tmp.supportViewType
			//# if ( this.view != null ) {
				//# this.view.focusedIndex = index;
				//# this.view.focusedItem = item;
			//# }
		//#endif
		if  (this.isInitialised) { // (this.yTopPos != this.yBottomPos) {
			// this container has been initialised already,
			// so the dimensions are known.
			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;
				//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 486, "Container (" + getClass().getName() + "): setting internalY=" + this.internalY + ", item.contentY=" + item.contentY + ", this.contentY=" + this.contentY + ", item.internalY=" + item.internalY+ ", this.yOffset=" + this.yOffset + ", item.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;
				//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 493, "Container (" + getClass().getName() + "): setting internalY=" + this.internalY + ", item.yTopPos=" + item.yTopPos + ", this.contentY=" + this.contentY + ", this.yOffset=" + this.yOffset + ", item.itemHeight=", item.itemHeight);
			}
			if (this.enableScrolling) {	
				// Now adjust the scrolling:
				
				Item nextItem;
				if ( isDownwards && index < this.itemsList.size() - 1 ) {
					nextItem = (Item) this.itemsList.get( index + 1 );
					//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 502, "Focusing downwards, nextItem.y = [" + nextItem.yTopPos + "-" + nextItem.yBottomPos + "], focusedItem.y=[" + item.yTopPos + "-" + item.yBottomPos + "], this.yOffset=" + this.yOffset + ", this.targetYOffset=", this.targetYOffset);
				} else if ( !isDownwards && index > 0 ) {
					nextItem = (Item) this.itemsList.get( index - 1 );
					//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 506, "Focusing upwards, nextItem.yTopPos = " + nextItem.yTopPos + ", focusedItem.yTopPos=", item.yTopPos );
				} else {
					//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 509, "Focusing last or first item.");
					nextItem = item;
				}

				
				int itemYTop = isDownwards ? item.yTopPos : nextItem.yTopPos;
				int itemYBottom = isDownwards ? nextItem.yBottomPos : item.yBottomPos;
				
//				if (itemYBottom - itemYTop > this.yTop - this.yBottom) {
//					if ( isDownwards ) {
//						itemYBottom = this.internalY + this.internalHeight;
//					} else {
//						itemYTop = this.internalY;
//					}
//				}
//				int difference = 0;
				scroll( isDownwards, this.xLeftPos, itemYTop, item.internalWidth, itemYBottom - itemYTop );
//				if (itemYTop == itemYBottom) {
//					//#debug
//					System.out.println("Container: unable to auto-scroll, item.yBottomPos == item.yTopPos");
//				} else if (itemYBottom > this.yBottom) {
//					// this item is too low:
//					difference = this.yBottom - itemYBottom;
//					// #debug
//					//System.out.println("item too low: difference: " + difference + "  itemYBottom=" + itemYBottom + "  container.yBottom=" + this.yBottom );
//					//if ( itemYTop + difference < this.yTop) {
//					if ( isDownwards && itemYTop + difference < this.yTop) {
//						// #debug
//						//System.out.println("correcting: difference: " + difference + "  itemYTop=" + itemYTop + "  <  this.yTop=" +  this.yTop + "  to new difference=" + (this.yTop - itemYTop + 10) );
//						difference = this.yTop - itemYTop + 10; // additional pixels for adjusting the focused style above:
//					}
//					/*
//					if ( itemYTop + difference < this.internalY) {
//						//#debug
//						System.out.println("correcting: difference: " + difference + "  itemYTop=" + itemYTop + "  <  this.internalY=" +  this.internalY + "  to new difference=" + (this.internalY - itemYTop + 10) );
//						difference = this.internalY - itemYTop + 10; // additional pixels for adjusting the focused style above:
//					}
//					*/
//				} else if (itemYTop < this.yTop) {
//					// this item is too high:
//					//#if tmp.supportViewType
//						//TODO when colpan is used, the index might need to be higher than anticipated
//						if ((index == 0) || ( this.view != null && (index < this.view.numberOfColumns) ) ) {
//					//#else
//						//# if (index == 0) {
//					//#endif
//						// scroll to the very top:
//						difference = -1 * this.yOffset;
//					} else {
//						difference = this.yTop - itemYTop + this.focusedTopMargin;
//					}
//					// re-adjust the scrolling in case we scroll up and the previous
//					// item is very large:
//					if ( !isDownwards && itemYBottom + difference > this.yBottom  ) {
//						difference = this.yBottom - itemYBottom;
//					}
//						
//					// #debug
//					//System.out.println("item too high: difference: " + difference + "  itemYTop=" + itemYTop + "  container.yTop=" + this.yTop  );
//				}
//				//#debug
//				System.out.println("Container (" + getClass().getName() + "): difference: " + difference + "  container.yOffset=" + this.yOffset + ", itemY=[" + itemYTop + "-" + itemYBottom + "],  item.internalY: " + (item.internalY) + " bis " + (item.internalY + item.internalHeight ) + "  contentY:" + this.contentY + "  top:" + this.yTop + " bottom:" + this.yBottom  + "   ---- , this.internalY=" +  this.internalY);
//						
//				//#if polish.css.scroll-mode
//					if (!this.scrollSmooth) {
//						this.yOffset += difference;
//					} else {
//				//#endif
//						this.targetYOffset = this.yOffset + difference;
//				//#if polish.css.scroll-mode
//					}
//				//#endif
			}
		} else if (this.enableScrolling) {
			//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 584, "focus: postpone scrolling to initContent()");
			this.isScrollRequired = true;
			
//		} else if (this.enableScrolling) {
//			this.isFirstPaint = true;
		}
		this.isInitialised = false;
	}
	
	/**
	 * Adjusts the yOffset or the targetYOffset so that the given relative values are inside of the visible area.
	 * 
	 * @param isDownwards true when the scrolling is downwards
	 * @param x the relative horizontal position of the area
	 * @param y the relative vertical position of the area
	 * @param width the width of the area
	 * @param height the height of the area
	 */
	protected void scroll( boolean isDownwards, int x, int y, int width, int height ) {
		//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 604, "scroll: isDownwards=" + isDownwards + ", y=" + y + ", Container.yTop=" + this.yTop +  ", height=" +  height + ", Container.yBottom=" + this.yBottom + ", focusedIndex=" + this.focusedIndex + ", yOffset=" + this.yOffset + ", targetYOffset=", this.targetYOffset );
		y += this.paddingTop; // this.marginTop + this.paddingTop; marginTop is already used in setVerticalDimension!
		int difference = 0;
//		int index = this.focusedIndex;
		int target = this.targetYOffset;
		int current = this.yOffset;
		// calculate the absolute position from the relative one:
//		int absoluteY = y + current + this.yTop;
//		int absoluteYBottom = absoluteY + height;
//		int yOffsetDiff = 0;
//		//#ifdef polish.css.scroll-mode
//			if (this.scrollSmooth) {
//		//#endif
//				yOffsetDiff = target - current;
//		//#ifdef polish.css.scroll-mode
//			}
//		//#endif
		int verticalSpace = this.yBottom - this.yTop; // the available height for this container
		if ( height == 0 || !this.enableScrolling) {
			return;
		} else if ( y + height + target > verticalSpace ) {
			// the area is too low:
			difference = verticalSpace - (y + height + target);
			//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 628, "scroll: item too low: difference: " + difference + "  verticalSpace=" + verticalSpace + "  y=" + y + ", height=" + height + ", target=", target);
			// check if the top of the area is still visible when scrolling downwards:
			if ( isDownwards && y + target + difference < 0 ) {
				difference -= y + target + difference;
			}
		} else if ( y + target < 0 ) {
			// area is too high:
			difference = - (y + target); 
			//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 637, "scroll: item too high: setting difference to " + difference + ", y=" + y + ", target=" + target ); //+ ", focusedTopMargin=" + this.focusedTopMargin );
			if (!isDownwards && y + height + target + difference > verticalSpace ) {
				difference += verticalSpace - (y + height + target + difference);
			}
//			
//		} else if (absoluteYBottom + yOffsetDiff > this.yBottom + 1) { //TODO +1 is a quick hack here
//			// this item is too low:
//			difference = this.yBottom - absoluteYBottom;
//			//#debug
//			System.out.println("scroll: item too low: difference: " + difference + "  absoluteYBottom=" + absoluteYBottom + "  container.yBottom=" + this.yBottom + ", yOffsetDiff=" + yOffsetDiff);
//			//if ( itemYTop + difference < this.yTop) {
//			if ( isDownwards && absoluteY + difference < this.yTop) {
//				//#debug
//				System.out.println("scroll: correcting: difference: " + difference + "  absoluteY=" + absoluteY + "  <  this.yTop=" +  this.yTop + "  to new difference=" + (this.yTop - y + 10) );
//				difference = this.yTop - absoluteY + 10; // additional pixels for adjusting the focused style above:
//			}
//			/*
//			if ( itemYTop + difference < this.internalY) {
//				//#debug
//				System.out.println("correcting: difference: " + difference + "  itemYTop=" + itemYTop + "  <  this.internalY=" +  this.internalY + "  to new difference=" + (this.internalY - itemYTop + 10) );
//				difference = this.internalY - itemYTop + 10; // additional pixels for adjusting the focused style above:
//			}
//			*/
//		} else if (y + yOffsetDiff < this.yTop) {
//			// this item is too high:
//			//#if tmp.supportViewType
//				//TODO when colspan is used, the index might be lower than anticipated
//				if ((index == 0) || ( this.view != null && (index < this.view.numberOfColumns) ) ) {
//			//#else
//				//# if (index == 0) {
//			//#endif
//				// scroll to the very top:
//				difference = -current;
//				//#debug
//				System.out.println("scroll: Scrolling to first item: setting difference to " + difference );
//			} else {
//				difference = this.yTop + yOffsetDiff - absoluteY; // + this.focusedTopMargin;
//				//#debug
//				System.out.println("scroll: item too high: setting difference to " + difference + ", absoluteY=" + absoluteY + ", yOffsetDiff=" + yOffsetDiff ); //+ ", focusedTopMargin=" + this.focusedTopMargin );
//			}
//			// re-adjust the scrolling in case we scroll up and the previous
//			// item is very large:
//			if ( !isDownwards && (absoluteYBottom + yOffsetDiff + difference > this.yBottom  )) {
//				difference = this.yBottom - (absoluteYBottom + yOffsetDiff);
//				//#debug
//				System.out.println("scroll: Scrolling upwards: adjusting difference to " + difference );
//			}
		} else {
			//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 686, "scroll: do nothing");
			return;
		}
				
		//#if polish.css.scroll-mode
			//# if (!this.scrollSmooth) {
				//# this.yOffset = current + difference;
			//# } else {
		//#endif
				this.targetYOffset = target + difference;
				//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 697, "scroll: adjusting targetYOffset to " + this.targetYOffset + ", y=", y);
//				if (this.focusedItem != null) {
//					this.focusedItem.backgroundYOffset = difference;
//				}
		//#if polish.css.scroll-mode
			//# }
		//#endif
		

⌨️ 快捷键说明

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