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

📄 treeitem.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				}
			}
		} else {
			parentNode = ((Node)node.parent);
		}
		item.parent = parentNode;
		parentNode.addChild(item);
		this.lastAddedItem = item;
		
	}

	/**
	 * Removes the specified item from this list.
	 * 
	 * @param item the item that should be removed
	 * @return true when the item was contained in this list.
	 */
	public boolean remove( Item item ) {
		return this.root.remove(item);
	}
	
	/**
	 * Clears this list.
	 */
	public void removeAll() {
		this.root.clear();
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CustomItem#getMinContentWidth()
	 */
	protected int getMinContentWidth() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CustomItem#getMinContentHeight()
	 */
	protected int getMinContentHeight() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CustomItem#getPrefContentWidth(int)
	 */
	protected int getPrefContentWidth(int maxHeight) {
		// try to use the maximum available width:
		return Integer.MAX_VALUE;
		//return this.prefContentWidth;
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CustomItem#getPrefContentHeight(int)
	 */
	protected int getPrefContentHeight(int maxWidth) {
		this.availableWidth = maxWidth;
		return this.root.getItemHeight(maxWidth, maxWidth);
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CustomItem#paint(javax.microedition.lcdui.Graphics, int, int)
	 */
	protected void paint(Graphics g, int w, int h) {
		this.root.paint( 0, 0, 0, w, g );
	}

	protected void hideNotify() {
		this.root.hideNotify();
	}
	
	protected void showNotify() {
		this.root.showNotify();
	}


	protected boolean handleKeyPressed(int keyCode, int gameAction) {
		boolean handled = this.root.handleKeyPressed(keyCode, gameAction);
		if (handled) {
			//System.out.println("invalidating through keyPressed...");
			invalidate();
		}
		return handled;
	}

	//#ifdef polish.hasPointerEvents
	//# /* (non-Javadoc)
	 //# * @see de.enough.polish.ui.Item#handlePointerPressed(int, int)
	 //# */
	//# protected void pointerPressed(int x, int y) {
		//# if (this.root.handlePointerPressed(x, y)) {
			//# invalidate();
		//# }
	//# }
	//#endif


	protected boolean traverse(int direction, int viewWidth, int viewHeight, int[] viewRect_inout) {
		boolean handled = (this.root.handleKeyPressed(0, direction));
		if (handled) {
			viewRect_inout[0] = this.root.internalX;
			viewRect_inout[1] = this.root.internalY;
			viewRect_inout[2] = this.root.internalWidth;
			viewRect_inout[3] = this.root.internalHeight;
		}
		return handled;
	}
	
	protected void traverseOut() {
		this.root.defocus(null);
	}
	
	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#focus(de.enough.polish.ui.Style, int)
	 */
	protected Style focus(Style focusstyle, int direction ) {
		invalidate();
		return this.root.focus(focusstyle, direction);
	}
	
	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#defocus(de.enough.polish.ui.Style)
	 */
	protected void defocus(Style originalStyle) {
		invalidate();
		this.root.defocus(originalStyle);
	}
	
	
	
	static class Node extends Item {
		private Item root;
		private Container children;
		private boolean isExpanded;
		int xLeftOffset = 10;
		private Style rootFocusedStyle;
		private Style rootPlainStyle;
		private Style childrenPlainStyle;
		private boolean isChildrenFocused;
		
		public Node( Item root ) {
			super( null, 0, INTERACTIVE, null );
			this.root = root;
			this.root.parent = this;
			this.children = new Container( false );
			this.children.parent = this;
		}
		
		public void addChild( Item child ) {
			this.children.add( child );
		}

		protected void initContent(int firstLineWidth, int lineWidth) {
			this.root.init(firstLineWidth, lineWidth);
			if (!this.isExpanded) {
				this.contentWidth = this.root.itemWidth;
				this.contentHeight = this.root.itemHeight;
			} else {
				//TODO re-implement drawing of lines when no ContainerView is used 
				lineWidth -= this.xLeftOffset;
				this.children.init(lineWidth, lineWidth);
				this.contentWidth = Math.max( this.root.itemWidth, this.children.itemWidth + this.xLeftOffset);
				this.contentHeight = this.root.itemHeight + this.paddingVertical + this.children.itemHeight;
			}
		}

		protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
			this.root.paint(x, y, leftBorder, rightBorder, g);
			if (this.isExpanded) {
				leftBorder += this.xLeftOffset;
				x = leftBorder;
				rightBorder -= this.xLeftOffset;
				y += this.root.itemHeight;
				this.children.paint(x, y, leftBorder, rightBorder, g);
			}
		}

		//#ifdef polish.useDynamicStyles	
		protected String createCssSelector() {
			return "node";
		}
		//#endif

		/* (non-Javadoc)
		 * @see de.enough.polish.ui.Item#handleKeyPressed(int, int)
		 */
		protected boolean handleKeyPressed(int keyCode, int gameAction) {
			boolean handled = false;
			if (this.isExpanded) {
				if (this.isChildrenFocused) {
					handled = this.children.handleKeyPressed(keyCode, gameAction);
					if (!handled && gameAction == Canvas.UP) {
						// focus this root:
						setExpanded( false );
						handled = true;
					} else if (gameAction == Canvas.FIRE) {
						// leaf selected!
//					} else {
//						this.isChildrenFocused = false;
						// this is done by the unfocus method automagically
					}
				} else if (gameAction == Canvas.DOWN && this.children.appearanceMode != PLAIN) {
					// move focus to children
					if (this.rootPlainStyle != null) {
						this.root.defocus(this.rootPlainStyle);
					}
					this.children.focus(null, gameAction);
					this.isChildrenFocused = true;
					handled = true;
				}

			}
			if (!handled && gameAction == Canvas.FIRE ) {
				setExpanded( !this.isExpanded );
				handled = true;
			}
			return handled;
		}
		
		/* (non-Javadoc)
		 * @see de.enough.polish.ui.Item#focus(de.enough.polish.ui.Style, int)
		 */
		protected Style focus(Style focusstyle, int direction ) {
			//System.out.println("focus " + this );
			//if ( !this.isExpanded || this.children.size() == 0) {
				//System.out.println("focus root " + this.root );
				this.rootFocusedStyle = focusstyle;
				this.rootPlainStyle  = this.root.focus(focusstyle, direction);
				return this.rootPlainStyle;
//			}
//			this.childrenPlainStyle = this.children.focus(focusstyle, direction); 
//			return this.childrenPlainStyle;
		}
		
		/* (non-Javadoc)
		 * @see de.enough.polish.ui.Item#defocus(de.enough.polish.ui.Style)
		 */
		protected void defocus(Style originalStyle) {
			//System.out.println("defocus " + this );
			if (this.isExpanded && this.isChildrenFocused) {
				this.children.defocus(originalStyle);
				this.isChildrenFocused = false;
			} else {
				this.root.defocus( originalStyle );
			}
		}
		
		private void setExpanded( boolean expand ) {
			if (!expand) {
				//System.out.println( "defocussing children while contracting" );
				if (this.isChildrenFocused) {
					this.children.defocus( null );
					this.children.focus( -1 );
					this.isChildrenFocused = false;
					// move focus to root:
					if (this.rootFocusedStyle != null) {
						this.root.focus(this.rootFocusedStyle, Canvas.UP);
					} else {
						this.root.focus(this.focusedStyle, Canvas.UP);
					}
				}
			}
			if (expand != this.isExpanded) {
				requestInit();
				this.isExpanded = expand;
			}
		}
		
		//#if polish.debugEnabled
		//# public String toString() {
			//# return "Node " + this.root + "/" + super.toString();
		//# }
		//#endif

		
	}

}

⌨️ 快捷键说明

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