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

📄 treeitem.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	
	
	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;
		private int availableWidth;
		//#if polish.css.treeitem-closed-indicator && polish.css.treeitem-opened-indicator
			//#define tmp.useIndicators
			//# private Image closedIndicator;
			//# private Image openedIndicator;
			//# private int indicatorWidth;
		//#endif
		
		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) {
			//#debug
			//# System.out.println("Node (" + this.root + ").initContent()");
			this.availableWidth = lineWidth - this.xLeftOffset;
			this.root.init(firstLineWidth, lineWidth);
			this.children.relativeX = this.xLeftOffset;
			this.children.relativeY = this.root.itemHeight;
			
			int rootWidth = this.root.itemWidth;
			//#if tmp.useIndicators
				//# int w = 0;
				//# if (this.openedIndicator != null) {
					//# w = this.openedIndicator.getWidth();
				//# }
				//# if (this.closedIndicator != null && this.closedIndicator.getWidth() > w) {
					//# w = this.closedIndicator.getWidth();
				//# }
				//# if (w != 0) {
					//# rootWidth += w + this.paddingHorizontal;
				//# }
				//# this.indicatorWidth = w;
			//#endif
			if (!this.isExpanded) {
				this.contentWidth = rootWidth;
				this.contentHeight = this.root.itemHeight;
			} else {
				lineWidth -= this.xLeftOffset;
				this.children.init(lineWidth, lineWidth);
				this.contentWidth = Math.max(rootWidth, 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) {
			//#if tmp.useIndicators
				//# Image image;
				//# if (this.isExpanded) {
					//# image = this.openedIndicator;
				//# } else {
					//# image = this.closedIndicator;
				//# }
				//# if (image != null) {
					//# int height = image.getHeight();
					//# int rootHeight = this.root.itemHeight;
					//# g.drawImage(image, x, y + (rootHeight-height)/2, Graphics.TOP | Graphics.RIGHT );
				//# }
				//# x += this.indicatorWidth;
			//#endif
			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) {
			//#debug
			//# System.out.println("Note " + this + " handleKeyPressed: isExpanded=" + this.isExpanded);
			boolean handled = false;
			if (this.isExpanded) {
				//if (this.isChildrenFocused) {
				if (this.children.isFocused) {
					handled = this.children.handleKeyPressed(keyCode, gameAction);
					if (!handled && gameAction == Canvas.UP) {
						// focus this root:
						focusRoot();
						handled = true;
					} else {
						if (this.children.internalX != -999) {
							this.internalX = this.children.relativeX + this.children.contentX + this.children.internalX;
							this.internalY = this.children.relativeY + this.children.contentY + this.children.internalY;
							this.internalWidth = this.children.internalWidth;
							this.internalHeight = this.children.internalHeight;
						} else {
							this.internalX = -9999;
						}
					}
				} 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;
		}
		
		//#ifdef polish.hasPointerEvents
		//# /* (non-Javadoc)
		 //# * @see de.enough.polish.ui.Item#handlePointerPressed(int, int)
		 //# */
		//# protected boolean handlePointerPressed(int x, int y) {
			//# boolean handled = false;
			//# if (this.isExpanded) {
				//# handled = this.children.handlePointerPressed(x - this.children.relativeX, y - this.children.relativeY );
				//# if (handled) {
					//# if (!this.children.isFocused) {
						//# this.children.focus(this.style, 0);
						//# this.children.isFocused = true;
					//# }
					//# //System.out.println("PP: CHILD HANDLED PP for " + this );
					//# //this.isChildrenFocused = true;
					//# if (this.rootPlainStyle != null) {
						//# this.root.setStyle( this.rootPlainStyle );
					//# }
				//# } else if ( this.root.isInItemArea(x, y)) {
					//# //if (this.isChildrenFocused) {
					//# if (this.children.isFocused) {
						//# focusRoot();
					//# }
					//# setExpanded( false );
					//# handled = true;
				//# }
			//# }
			//# return handled || super.handlePointerPressed(x, y);
		//# }
		//#endif
		
		/* (non-Javadoc)
		 * @see de.enough.polish.ui.Item#focus(de.enough.polish.ui.Style, int)
		 */
		protected Style focus(Style focusstyle, int direction ) {
			//
			this.isFocused = true;
			this.rootFocusedStyle = focusstyle;
			if ( !this.isExpanded || direction != Canvas.UP || this.children.size() == 0 || this.children.appearanceMode == PLAIN)
			{
				this.rootPlainStyle  = this.root.focus(focusstyle, direction);
				return this.rootPlainStyle;
			}
			//this.isChildrenFocused = true;
			this.childrenPlainStyle = this.children.focus(focusstyle, direction); 
			return this.root.style;
		}
		
		/* (non-Javadoc)
		 * @see de.enough.polish.ui.Item#defocus(de.enough.polish.ui.Style)
		 */
		protected void defocus(Style originalStyle) {
			this.isFocused = false;
			//System.out.println("defocus " + this );
			//if (this.isExpanded && this.isChildrenFocused) {
			if (this.isExpanded && this.children.isFocused) {
				this.children.defocus(originalStyle);
				//this.isChildrenFocused = false;
			} else {
				this.root.defocus( originalStyle );
			}
		}
		
		private void focusRoot() {
			this.internalX = 0;
			this.internalY = 0;
			this.internalWidth = this.root.itemWidth;
			this.internalHeight = this.root.itemHeight;
			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);
			}
		}
				
		private void setExpanded( boolean expand ) {
			if (!expand) {
				this.internalX = -9999;
				// close down all chidren nodes as well when closing:
				Item[] items = this.children.getItems();
				for (int i = 0; i < items.length; i++) {
					Item item = items[i];
					if (item instanceof Node) {
						((Node)item).setExpanded(false);
					}
				}
				//if (this.isChildrenFocused) {
				if (this.children.isFocused) {
					focusRoot();
				}
			} else if (!this.isExpanded) {
				// trick so that the parent container can scroll correctly when this node is expanded:
				this.internalX = 0;
				this.internalY = 0;
				this.internalHeight = this.root.itemHeight + this.children.getItemHeight(this.availableWidth, this.availableWidth);
			}
			if (expand != this.isExpanded) {
				requestInit();
				this.isExpanded = expand;
			}
		}
		
		/* (non-Javadoc)
		 * @see de.enough.polish.ui.Item#setStyle(de.enough.polish.ui.Style)
		 */
		public void setStyle(Style style) {
			super.setStyle(style);
			//#if tmp.useIndicators
				//# String closedUrl = style.getProperty(-1);
				//# if (closedUrl != null) {
					//# try {
						//# this.closedIndicator = StyleSheet.getImage(closedUrl, this, true );
					//# } catch (IOException e) {
						//#debug error
						//# System.out.println("Unable to load treeitem-closed-indicator " + closedUrl + e );
					//# }
				//# }
				//# String openedUrl = style.getProperty(-1);
				//# if (openedUrl != null) {
					//# try {
						//# this.openedIndicator = StyleSheet.getImage(openedUrl, this, true );
					//# } catch (IOException e) {
						//#debug error
						//# System.out.println("Unable to load treeitem-opened-indicator " + openedUrl + e );
					//# }
				//# }
			//#endif
		}

		//#if polish.debugEnabled
		//# public String toString() {
			//# return "Node " + this.root + "/" + super.toString();
		//# }
		//#endif

		
	}

}

⌨️ 快捷键说明

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