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

📄 container.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		}
		if (this.isInitialized) {
			this.isInitialized = false;
			//this.yBottom = this.yTop = 0;
			repaint();
		}
	}
	
	/**
	 * 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, can be empty but not null.
	 */
	public Item[] getItems() {
		if (!this.isInitialized || this.items == null) {
			this.items = (Item[]) this.itemsList.toArray( new Item[ this.itemsList.size() ]);
		}
		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 
	 * @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;
			Item item = this.focusedItem; 
			if (item != null && this.itemStyle != null && item.isFocused) {
				item.defocus( this.itemStyle );
			}
			this.focusedItem = null;
			//#ifdef tmp.supportViewType
				if (this.containerView != null) {
					this.containerView.focusedIndex = -1;
					this.containerView.focusedItem = null;
				}
			//#endif
			return true;
		}
		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
		//# System.out.println("Container (" + getClass().getName() + "): Focusing item " + index );
		
		//#if polish.blackberry
        	//# getScreen().setFocus( item );
		//#endif
		
		
		if (this.autoFocusEnabled  && !this.isInitialized) {
			// setting the index for automatically focusing the appropriate item
			// during the initialisation:
			//#debug
			//# System.out.println("Container: Setting autofocus-index to " + index );
			this.autoFocusIndex = index;
			//this.isFirstPaint = true;
			return;
		}
		
		if (index == this.focusedIndex && item.isFocused) {
			//#debug
			//# System.out.println("Container: ignoring focusing of item " + index );
			// ignore the focusing of the same element:
			return;
		}
		// indicating if either the former focusedItem or the new focusedItem has changed it's size or it's layout by losing/gaining the focus, 
		// of course this can only work if this container is already initialized:
		boolean isReinitializationRequired = false;
		// first defocus the last focused item:
		if (this.focusedItem != null) {
			Item fItem = this.focusedItem;
			int wBefore = fItem.itemWidth;
			int hBefore = fItem.itemHeight;
			int layoutBefore = fItem.layout;
			if (this.itemStyle != null) {
				fItem.defocus(this.itemStyle);
			} else {
				//#debug error
				//# System.out.println("Container: Unable to defocus item - no previous style found.");
				fItem.defocus( StyleSheet.defaultStyle );
			}
			if (this.isInitialized) {
				int wAfter = fItem.getItemWidth( this.contentWidth, this.contentWidth );
				int hAfter = fItem.itemHeight;
				int layoutAfter = fItem.layout;
				if (wAfter != wBefore || hAfter != hBefore || layoutAfter != layoutBefore ) {
					isReinitializationRequired = true;
					fItem.isInitialized = false; // could be that a container view poses restrictions on the possible size, i.e. within a table
				}
			}
		}
		int wBefore = item.itemWidth;
		int hBefore = item.itemHeight;
		int layoutBefore = item.layout;
		this.itemStyle = item.focus( this.focusedStyle, direction );
		//#ifdef polish.debug.error
			//# if (this.itemStyle == null) {
				//#debug error 
				//# System.out.println("Container: Unable to retrieve style of item " + item.getClass().getName() );
			//# }
		//#endif
		boolean isDownwards = (direction == Canvas.DOWN) || (direction == Canvas.RIGHT) || (direction == 0 &&  index > this.focusedIndex);
		int previousIndex = this.focusedIndex; // need to determine whether the user has scrolled from the bottom to the top
		this.focusedIndex = index;
		this.focusedItem = item;
		//#if tmp.supportViewType
			if ( this.containerView != null ) {
				this.containerView.focusedIndex = index;
				this.containerView.focusedItem = item;
			}
		//#endif
		if  (this.isInitialized) {
			// this container has been initialised already,
			// so the dimensions are known.
			int wAfter = item.getItemWidth( this.contentWidth, this.contentWidth );
			int hAfter = item.itemHeight;
			int layoutAfter = item.layout;
			if (wAfter != wBefore || hAfter != hBefore || layoutAfter != layoutBefore ) {
				isReinitializationRequired = true;
				item.isInitialized = false; // could be that a container view poses restrictions on the possible size, i.e. within a table
			}

			if (item.internalX != -9999) {
				this.internalX =  item.relativeX + item.contentX + item.internalX;
				this.internalY = item.relativeY + item.contentY + item.internalY;
				this.internalWidth = item.internalWidth;
				this.internalHeight = item.internalHeight;
				
				//#debug
				//# System.out.println("Container (" + getClass().getName() + "): internal area found in item " + item + ": 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.relativeX;
				this.internalY = item.relativeY;
				this.internalWidth = item.itemWidth;
				this.internalHeight = item.itemHeight;
				//#debug
				//# System.out.println("Container (" + getClass().getName() + "): NO internal area found in item " + item + ": setting internalY=" + this.internalY + ", internalHeight=" + this.internalHeight + ", this.yOffset=" + this.yOffset + ", item.itemHeight=" + item.itemHeight + ", this.availableHeight=" + this.availableHeight);
			}
			if (this.enableScrolling) {	
				// Now adjust the scrolling:			
				Item nextItem;
				if ( isDownwards && index < this.itemsList.size() - 1 ) {
					nextItem = (Item) this.itemsList.get( index + 1 );
					//#debug
					//# System.out.println("Focusing downwards, nextItem.relativY = [" + nextItem.relativeY + "], focusedItem.relativeY=[" + item.relativeY + "], this.yOffset=" + this.yOffset + ", this.targetYOffset=" + this.targetYOffset);
				} else if ( !isDownwards && index > 0 ) {
					nextItem = (Item) this.itemsList.get( index - 1 );
					//#debug
					//# System.out.println("Focusing upwards, nextItem.yTopPos = " + nextItem.relativeY + ", focusedItem.relativeY=" + item.relativeY );
				} else {
					//#debug
					//# System.out.println("Focusing last or first item.");
					nextItem = item;
				}

				
				if ( (index == 0) || (isDownwards && (index < previousIndex) || (previousIndex == -1)) ) {
					// either the first item or the first selectable item has been focused, so scroll to the very top:
					//#ifdef polish.css.scroll-mode
						//# if (!this.scrollSmooth) {
							//# this.yOffset = 0;
						//# } else {
					//#endif
							this.targetYOffset = 0;
					//#ifdef polish.css.scroll-mode
						//# }
					//#endif
				} else {
					int itemYTop = isDownwards ? item.relativeY : nextItem.relativeY;
					int itemYBottom = isDownwards ? nextItem.relativeY + nextItem.itemHeight : item.relativeY + item.itemHeight;
					scroll( direction, this.relativeX, itemYTop, item.internalWidth, itemYBottom - itemYTop );
				}
				

			}
		} else if (this.enableScrolling) {
			//#debug
			//# System.out.println("focus: postpone scrolling to initContent() for " + this );
			this.isScrollRequired = true;
			
		}
		if (this.isInitialized) {
			this.isInitialized = !isReinitializationRequired;
		}
	}
	
	/**
	 * Scrolls this container so that the (internal) area of the given item is best seen.
	 * This is used when a GUI even has been consumed by the currently focused item.
	 * The call is fowarded to scroll( direction, x, y, w, h ).
	 * 
	 * @param direction the direction, is used for adjusting the scrolling when the internal area is to large. Either 0 or Canvas.UP, Canvas.DOWN, Canvas.LEFT or Canvas.RIGHT
	 * @param item the item for which the scrolling should be adjusted
	 */
	protected void scroll(int direction, Item item) {
		//#debug
		//# System.out.println("scroll: scrolling for item " + item  + ", item.internalX=" + item.internalX);
		if (item.internalX != -9999) {
			int relativeInternalX = item.relativeX + item.contentX + item.internalX;
			int relativeInternalY = item.relativeY + item.contentY + item.internalY;
			scroll(  direction, relativeInternalX, relativeInternalY, item.internalWidth, item.internalHeight );
		} else {
			scroll(  direction, item.relativeX, item.relativeY, item.itemWidth, item.itemHeight );			
		}
	}
	
	/**
	 * Adjusts the yOffset or the targetYOffset so that the given relative values are inside of the visible area.
	 * The call is forwarded to a parent container when scrolling is not enabled for this item.
	 * 
	 * @param direction the direction, is used for adjusting the scrolling when the internal area is to large. Either 0 or Canvas.UP, Canvas.DOWN, Canvas.LEFT or Canvas.RIGHT
	 * @param x the horizontal position of the area relative to this content's left edge, is ignored in the current version
	 * @param y the vertical position of the area relative to this content's top edge
	 * @param width the width of the area
	 * @param height the height of the area
	 */
	protected void scroll( int direction, int x, int y, int width, int height ) {
		if (!this.enableScrolling) {
			if (this.parent instanceof Container) {
				x += this.contentX + this.relativeX;
				y += this.contentY + this.relativeY;
				((Container)this.parent).scroll(direction, x, y, width, height );
			}
			return;
		}
		//#debug
		//# System.out.println("scroll: direction=" + direction + ", y=" + y + ", availableHeight=" + this.availableHeight +  ", height=" +  height + ", focusedIndex=" + this.focusedIndex + ", yOffset=" + this.yOffset + ", targetYOffset=" + this.targetYOffset );
		
		// assume scrolling down when the direction is not known:
		boolean isDownwards = (direction == Canvas.DOWN || direction == Canvas.RIGHT ||  direction == 0);
		boolean isUpwards = (direction == Canvas.UP );
		
		int currentYOffset = this.targetYOffset; // yOffset starts at 0 and grows to -contentHeight + lastItem.itemHeight
		//#if polish.css.scroll-mode
			//# if (!this.scrollSmooth) {
				//# currentYOffset = this.yOffset;
			//# }
		//#endif

		int verticalSpace = this.availableHeight - (this.contentY + this.marginBottom + this.paddingBottom + this.borderWidth); // the available height for this container
		if ( height == 0 || !this.enableScrolling) {
			return;
		} else if ( y + height + currentYOffset > verticalSpace ) {
			// the area is too low, so scroll down (= increase the negative yOffset):
			currentYOffset += verticalSpace - (y + height + currentYOffset);
			//#debug
			//# System.out.println("scroll: item too low: verticalSpace=" + verticalSpace + "  y=" + y + ", height=" + height + ", yOffset=" + currentYOffset);
			// check if the top of the area is still visible when scrolling downwards:
			if ( isDownwards && y + currentYOffset < 0 ) {
				currentYOffset -= (y + currentYOffset);
			}
		} else if ( y + currentYOffset < 0 ) {
			// area is too high, so scroll up (= decrease the negative yOffset):
			currentYOffset -=  y + currentYOffset; 
			//#debug
			//# System.out.println("scroll: item too high: , y=" + y + ", target=" + currentYOffset ); //+ ", focusedTopMargin=" + this.focusedTopMargin );
			// check if the bottom of the area is still visible when scrolling upwards:
			if (isUpwards && y + height + currentYOffset > verticalSpace ) {
				currentYOffset += verticalSpace - (y + height + currentYOffset);
			}

		} else {
			//#debug
			//# System.out.println("scroll: do nothing");
			return;
		}
				
		//#if polish.css.scroll-mode
			//# if (!this.scrollSmooth) {
				//# this.yOffset = currentYOffset;
			//# } else {
		//#endif
				this.targetYOffset = currentYOffset;
				//#debug
				//# System.out.println("scroll: adjusting targetYOffset to " + this.targetYOffset + ", y=" + y);
		//#if polish.css.scroll-mode
			//# }
		//#endif	
	}
	

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#initItem( int, int )
	 */
	protected void initContent(int firstLineWidth, int lineWidth) {
		//#debug
		//# System.out.println("Container: intialising content for " + this + ": autofocus=" + this.autoFocusEnabled);
		int myContentWidth = 0;
		int myContentHeight = 0;
		try {
		Item[] myItems = (Item[]) this.itemsList.toArray( new Item[ this.itemsList.size() ]);
		this.items = myItems;
		if (this.autoFocusEnabled && this.autoFocusIndex >= myItems.length ) {
			this.autoFocusIndex = 0;
		}
		//#if tmp.supportViewType
			if (this.containerView != null) {
				// additional initialization is necessary when a view is used for this container:
				boolean requireScrolling = this.isScrollRequired;
				synchronized (this) {
					if (this.autoFocusEnabled) {
						//#debug
						//# System.out.println("Container/View: autofocusing element " + this.autoFocusIndex);
						if (this.autoFocusIndex >= 0 ) {
							for (int i = this.autoFocusIndex; i < myItems.length; i++) {
								Item item = myItems[i];
								if (item.appearanceMode != Item.PLAIN) {
									// make sure that the item has applied it's own style first:
									item.getItemHeight( firstLineWidth, lineWidth );
									// now focus the item:
									this.autoFocusEnabled = false;
									requireScrolling = (this.autoFocusIndex != 0);
									focus( i, item, 0 );
									this.isScrollRequired = this.isScrollRequired && requireScrolling; // override setting in focus()
									this.containerView.focusedIndex = i;
									this.containerView.focusedItem = item;
									//System.out.println("autofocus: found item " + i );
									break;
								}							
							}
						} else {

⌨️ 快捷键说明

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