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

📄 containerview.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//		int direction = Canvas.UP;
//		if (forwardFocus) {
//			direction = Canvas.DOWN;
//		}
		focusItem(i, nextItem );
		return nextItem;
	}

	/**
	 * Focuses the item with the given index.
	 * The container will then set the style of the 
	 * retrieved item. The default implementation just
	 * sets the internal focusedIndex field along with focusedItem. 
	 * When this method is overwritten, please do call super.focusItem first
	 * or set the fields "focusedIndex" and "focusedItem" yourself.
	 * 
	 * @param index the index of the item
	 * @param item the item which should be focused
	 */
	protected void focusItem( int index, Item item  ) {
		int direction = 0;
		if (this.focusedIndex < index ) {
			direction = Canvas.DOWN;
		} else if (this.focusedIndex == index ) {
			direction = 0;
		} else {
			direction = Canvas.UP;
		}
		// this is done within container.focus() anyhow...
		// this.focusedIndex = index;
		// this.focusedItem = item;
		this.parentContainer.focus(index, item, direction );
	}

	/**
	 * Sets the focus to this container view.
	 * The default implementation sets the style and the field "isFocused" to true.
	 * 
	 * @param focusstyle the appropriate style.
	 * @param direction the direction from the which the focus is gained, 
	 *        either Canvas.UP, Canvas.DOWN, Canvas.LEFT, Canvas.RIGHT or 0.
	 *        When 0 is given, the direction is unknown.1
	 * 
	 */
	public void focus(Style focusstyle, int direction) {
		this.isFocused = true;
		setStyle( focusstyle );
	}

	
	/**
	 * Notifies this view that the parent container is not focused anymore.
	 * Please call super.defocus() when overriding this method.
	 * The default implementation calls setStyle( originalStyle )
	 * and sets the field "isFocused" to false.
	 * 
	 * @param originalStyle the previous used style.
	 */
	protected void defocus( Style originalStyle ) {
		this.isFocused = false;
		setStyle( originalStyle );
	}
	
	/**
	 * Sets the style for this view.
	 * The style can include additional parameters for the view.
	 * Subclasses should call super.setStyle(style) first.
	 * 
	 * @param style the style
	 */
	protected void setStyle( Style style ) {
		//#debug
		//# System.out.println("Setting style for " + this + " with vertical padding=" + style.paddingVertical  );
		this.paddingHorizontal = style.paddingHorizontal;
		this.paddingVertical = style.paddingVertical;
		this.layout = style.layout;
		// horizontal styles: center -> right -> left
		if ( ( this.layout & Item.LAYOUT_CENTER ) == Item.LAYOUT_CENTER ) {
			this.isLayoutCenter = true;
			this.isLayoutRight = false;
		} else {
			this.isLayoutCenter = false;
			if ( ( this.layout & Item.LAYOUT_RIGHT ) == Item.LAYOUT_RIGHT ) {
				this.isLayoutRight = true;
			} else {
				this.isLayoutRight = false;
				// meaning: layout == Item.LAYOUT_LEFT
			}
		}
		//this.columnsSetting = NO_COLUMNS;
		//#ifdef polish.css.columns
			//# Integer columns = style.getIntProperty(4);
			//# if (columns != null) {
				//# this.numberOfColumns = columns.intValue();
				//# //System.out.println("ContainerView: supporting " + this.numberOfColumns + " cols");
				//# this.columnsSetting = NORMAL_WIDTH_COLUMNS;
				//#ifdef polish.css.columns-width
					//# String width = style.getProperty(5);
					//# if (width != null) {
						//# if ("equal".equals(width)) {
							//# this.columnsSetting = EQUAL_WIDTH_COLUMNS;
						//# } else if ("normal".equals(width)) {
							//# //this.columnsSetting = NORMAL_WIDTH_COLUMNS;
							//# // this is the default value set above...
						//# } else {
							//# // these are pixel settings.
							//# String[] widths = TextUtil.split( width, ',');
							//# if (widths.length != this.numberOfColumns) {
								//# // this is an invalid setting! 
								//# this.columnsSetting = NORMAL_WIDTH_COLUMNS;
								//#debug warn
								//# System.out.println("Container: Invalid [columns-width] setting: [" + width + "], the number of widths needs to be the same as with [columns] specified.");
							//# } else {
								//# this.columnsSetting = STATIC_WIDTH_COLUMNS;
								//# this.columnsWidths = new int[ this.numberOfColumns ];
								//#ifdef polish.css.columns-width.star
									//# //int combinedWidth = 0;
									//# this.starIndex = -1;
								//#endif
								//# for (int i = 0; i < widths.length; i++) {
									//#ifdef polish.css.columns-width.star
										//# String widthStr = widths[i];
										//# if ("*".equals( widthStr )) {
											//# this.starIndex = i;
											//# this.columnsWidths[i] = 0;
										//# } else {
											//# int w = Integer.parseInt( widthStr );
											//# //combinedWidth += w;
											//# this.columnsWidths[i] = w;
										//# }
									//#else
										//# this.columnsWidths[i] = Integer.parseInt( widths[i] );
									//#endif
								//# }
								//# this.columnsSetting = STATIC_WIDTH_COLUMNS;
							//# }					
						//# }
					//# }
				//#endif
				//# //TODO rob allow definition of the "fill-policy"
			//# }
		//#endif
		//#if polish.css.view-type-left-x-offset
			//# Integer leftXOffsetInt = style.getIntProperty(108);
			//# if (leftXOffsetInt != null) {
				//# this.leftXOffset = leftXOffsetInt.intValue();
			//# }
		//#endif
		//#if polish.css.view-type-right-x-offset
			//# Integer rightXOffsetInt = style.getIntProperty(109);
			//# if (rightXOffsetInt != null) {
				//# this.rightXOffset = rightXOffsetInt.intValue();
			//# }
		//#endif
		//#if polish.css.view-type-top-y-offset
			//# Integer topYOffsetInt = style.getIntProperty(110);
			//# if (topYOffsetInt != null) {
				//# this.topYOffset = topYOffsetInt.intValue();
			//# }
		//#endif
	
	}
	
	/**
	 * Removes the background from the parent container so that the containerview implementation can paint it itself.
	 * 
	 * @return the background of the parent, can be null
	 */
	public Background removeParentBackground() {
		if (this.parentContainer == null) {
			//#debug warn
			//# System.out.println("Unable to remove parent background when parentContainer field is not set.");
			return null;
		}
		Background bg = this.parentContainer.background;
		this.parentContainer.background = null;
		return bg;
	}
	
	/**
	 * Removes the border from the parent container so that the containerview implementation can paint it itself.
	 * 
	 * @return the border of the parent, can be null
	 */
	public Border removeParentBorder() {
		if (this.parentContainer == null) {
			//#debug warn
			//# System.out.println("Unable to remove parent border when parentContainer field is not set.");
			return null;
		}
		Border border = this.parentContainer.border;
		this.parentContainer.border = null;
		return border;
	}
	
	/**
	 * Requests the re-initialization of this container view.
	 * This should be called when this view type radically changes
	 * its size.
	 */
	public void requestInit(){
		if (this.parentContainer != null) {
			this.parentContainer.requestInit();
		}
	}

	
	/**
	 * Retrieves the next focusable item.
	 * This helper method can be called by view-implementations.
	 * The index of the currently focused item can be retrieved with the focusedIndex-field.
	 * 
	 * @param items the available items
	 * @param forward true when a following item should be looked for,
	 *        false if a previous item should be looked for.
	 * @param steps the number of steps which should be used (e.g. 2 in a table with two columns)
	 * @param allowCircle true when either the first focusable or the last focusable element
	 *        should be returned when there is no focusable item in the given direction.
	 * @return either the next focusable item or null when there is no such element
	 * @see #focusItem(int, Item)
	 */
	protected Item getNextFocusableItem( final Item[] items, final boolean forward, int steps, boolean allowCircle ) {
		int i = this.focusedIndex;
		boolean isInLoop;
		while  ( true ) {
			if (forward) {
				i += steps;
				isInLoop = i < items.length;
				if (!isInLoop) {
					if (steps > 1) {
						i = items.length - 1;
						isInLoop = true;
					} else if (allowCircle) {
						steps = 1;
						allowCircle = false;
						i = 0;
						isInLoop = true;
					}
				}
			} else {
				i -= steps;
				isInLoop = i >= 0;
				if (!isInLoop) {
					if (steps > 1) {
						i = 0;
						isInLoop = true;
					} else if (allowCircle) {
						steps = 1;
						allowCircle = false;
						i = items.length - 1;
						isInLoop = true;
					}
				}
			}
			if (isInLoop) {
				Item item = items[i];
				if (item.appearanceMode != Item.PLAIN) {
					this.focusedIndex = i;
					return item;
				}
			} else {
				break;
			}
		}
		return null;
	}
	
	/**
	 * Animates this view.
	 * 
	 * @return true when the view was actually animated.
	 */
	public boolean animate() {
		return false;
	}

	/**
	 * Notifies this view that it is about to be shown (again).
	 * The default implementation just sets the restartAnimation-field to true.
	 */
	public void showNotify() {
		this.restartAnimation = true;
	}
	
	/**
	 * Called by the system to notify the item that it is now completely
	 * invisible, when it previously had been at least partially visible.  No
	 * further <code>paint()</code> calls will be made on this item
	 * until after a <code>showNotify()</code> has been called again.
	 * 
	 * <p>The container implementation calls hideNotify() on the embedded items.</p>
	 */
	public void hideNotify() {
		// subclasses can override this
	}
	
	/**
	 * Retrieves the screen to which this view belongs to.
	 * This is necessary since the getScreen()-method of item has only protected
	 * access. The screen can be useful for setting the title for example. 
	 * 
	 * @return the screen in which this view is embedded.
	 */
	protected Screen getScreen() {
		return this.parentContainer.getScreen();
	}
	
	/**
	 * Handles the given keyPressed event when the currently focused item was not able to handle it.
	 * The default implementation just calls getNextItem() and focuses the returned item.
	 * 
	 * @param keyCode the key code
	 * @param gameAction the game action like Canvas.UP etc
	 * @return true when the key was handled.
	 */
	public boolean handleKeyPressed( int keyCode, int gameAction) {
		//#debug
		//# System.out.println("ContainerView.handleKeypressedU() of container " + this);
		Item item = getNextItem( keyCode, gameAction );
		if (item != null) {
			// this is done within getNextItem/shiftFocus anyhow
			//focusItem(this.focusedIndex, item);
			return true;
		}
		return false;
	}

	//#ifdef polish.hasPointerEvents
	//# /**
	 //# * Handles pointer pressed events.
	 //# * This is an optional feature that doesn't need to be implemented by subclasses, since the parent container already forwards the event to the appropriate item (when this method returns false).
	 //# * The default implementation just returns false.
	 //# * You only need to implement this method when there are pointer events:
	 //# * <pre>
	 //# * //#if polish.hasPointerEvents
	 //# * </pre>
	 //# * 
	 //# * @param x the x position of the event
	 //# * @param y the y position of the event
	 //# * @return true when the event has been handled. When false is returned the parent container
	 //# *         will forward the event to the affected item.
	 //# */
	//# public boolean handlePointerPressed(int x, int y) {
		//# return false;
	//# }
	//#endif
	
	
	/**
	 * Scrolls the parent container (or one of its parent containers) so that the given positions relative to the parent's leftXPos/topYPos coordinates are shown as much as possible.
	 * 
	 * @param isDownwards true when the bottom is more important than the top
	 * @param x the x coordinate relative to the parent container's leftXPos
	 * @param y the y coordinate relative to the parent container's topYPos
	 * @param width the width of the visible area
	 * @param height the height of the area that should be as much as possible visible
	 */
	protected void scrollRelative( boolean isDownwards, int x, int y, int width, int height ){
		Container container = this.parentContainer;
		while (!container.enableScrolling) {
			Item item = container.parent;
			if (item instanceof Container) {
				container = (Container) item;
			} else {
				break;
			}
		} 
		if (container.enableScrolling) {
			container.scroll( isDownwards, this.parentContainer.xLeftPos + x, this.parentContainer.yTopPos + y, width, height );
		}
	}
	
	protected void scrollTo( int absoluteY ) {
		//System.out.println("scrollTo: original=" + absoluteY + ", current=" + this.parentContainer.yTopPos + ", difference=" + (absoluteY - this.parentContainer.yTopPos));
		int difference = absoluteY - this.parentContainer.yTopPos;
		Container container = this.parentContainer;
		while (!container.enableScrolling) {
			Item item = container.parent;
			if (item instanceof Container) {
				container = (Container) item;
			} else {
				break;
			}
		} 
		if (container.enableScrolling) {
			container.targetYOffset = container.yOffset + difference;
		}
	}
	
	protected int getParentYTopPos(){
		return this.parentContainer.yTopPos;
	}
	
	protected int getItemYTopPos( Item item ){
		return item.yTopPos;
	}

	protected int getItemYBottomPos( Item item ){
		return item.yBottomPos;
	}


}

⌨️ 快捷键说明

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