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

📄 commanditem.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					//#endif
					x = rightBorder - this.childIndicatorWidth;
					int indicatorY = y + this.childIndicatorYOffset;
					//#if polish.midp2
						//# g.fillTriangle(x, indicatorY, rightBorder, indicatorY + this.childIndicatorHeight/2, x, indicatorY + this.childIndicatorHeight );
					//#else
						g.drawLine( x, indicatorY, rightBorder, indicatorY + this.childIndicatorHeight/2 );
						g.drawLine( x, indicatorY + this.childIndicatorHeight, rightBorder, indicatorY + this.childIndicatorHeight/2 );
						g.drawLine( x, indicatorY, x, indicatorY + this.childIndicatorHeight );
					//#endif
			//#if polish.css.command-child-indicator
				//# }
			//#endif
			
			if (this.isOpen) {
				int originalY = y;
				// draw children:
				// when there is enough space to the right, open it on the right side, otherwise open it on the left:
				int clipX = g.getClipX();
				int clipWidth = g.getClipWidth();
				int clipY = g.getClipY() - 1;
				int clipHeight = g.getClipHeight();
//				g.setColor(0x00FF00);
//				g.fillRect(clipX, clipY, clipWidth, clipHeight);
				
				int availableWidth = (clipWidth * 2) / 3;
				int childrenWidth = this.children.getItemWidth( availableWidth, availableWidth );
				int childrenHeight = this.children.itemHeight; // is initialised because of the getItemWidth() call
				rightBorder += this.paddingHorizontal;
//				System.out.println("drawing children: children-width " + childrenWidth + ", clipWidth=" + clipWidth + ", leftBorder=" + leftBorder + ", rightBorder=" + rightBorder);
//				System.out.println("rightBorder + childrenWidth=" + (rightBorder + childrenWidth) + ", clipX + clipWidth=" + ( clipX + clipWidth ));
//				System.out.println("clipX + clipWidth - childrenWidth=" + (clipX + clipWidth - childrenWidth) + ", (leftBorder + 10)=" + (leftBorder + 10));
//				System.out.println("clipX=" + clipX + ", leftBorder - childrenWidth=" + (leftBorder - childrenWidth));
				if ( rightBorder + childrenWidth < clipX + clipWidth ) {					
					x = rightBorder;
				} else if ( clipX + clipWidth - childrenWidth > (leftBorder + 10) ) {
					x = clipX + clipWidth - (childrenWidth + 1);
				} else {
					x = Math.max( leftBorder - childrenWidth, clipX );
				}
				//System.out.println("submenu: y=" + y + ", y + childrenHeight=" + (y + childrenHeight) + ", clipY + clipHeight=" + (clipY + clipHeight));
				if ( y + childrenHeight > clipY + clipHeight ) {
					y -= (y + childrenHeight) - (clipY + clipHeight);
					//System.out.println("submenu: adjusted y=" + y + ", clipY=" + clipY);
					if ( y < clipY ) {
						y = clipY;
					}
//					g.setColor( 0x00FFFF);
//					for (int i = 0; i < 15; i++) {
//						g.drawLine( x - 20,y-i, x+childrenWidth+10, y-i);						
//					}
				}
				this.children.relativeX = x - leftBorder;
				this.children.relativeY = y - originalY;
				this.children.setScrollHeight( clipHeight );
				this.children.paint( x, y, x, x + childrenWidth, g);
				//System.out.println("set height for children to " + clipHeight + ", yOffset=" + this.children.yOffset + ", internalY=" + this.children.internalY);				
			}
		}
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#handleKeyPressed(int, int)
	 */
	protected boolean handleKeyPressed(int keyCode, int gameAction) {
		//#debug
		//# System.out.println( this + " handleKeyPressed, isOpen=" + this.isOpen);
		if ( this.isOpen ) {
			if (gameAction == Canvas.LEFT) {
				// close menu:
				open( false );
			} else {
				boolean handled = this.children.handleKeyPressed(keyCode, gameAction);
				if (!handled) {
					open( false );
				}
			}
			return true;
		} else if ( this.hasChildren && this.appearanceMode != PLAIN ) { // has children but is not open
			if ( gameAction == Canvas.FIRE || gameAction == Canvas.RIGHT ) {
				open( true );
				return true;
			}
		} else if ( gameAction == Canvas.FIRE  && this.appearanceMode != PLAIN ){ // has no children:
			// fire command action event:
			//#debug
			//# System.out.println( this + " invoking command " + this.command.getLabel() );
			Screen scr = getScreen();
			//#if polish.debug.error
			//# if (scr == null) {
				//#debug error
				//# System.out.println("Unable to retrieve screen for " + this + ", parent=" + this.parent );
			//# }
			//#endif
			scr.callCommandListener( this.command );
			return true;
		}
		return false;
	}
	
	//#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.isOpen) {
			//# handled = this.children.handlePointerPressed(x - this.children.relativeX, y - this.children.relativeY );
		//# }
		//# return handled || super.handlePointerPressed(x, y);
	//# }
	//#endif

	/**
	 * Opens or closes this command item so that the children commands are visible (or not).
	 * 
	 * @param open true when the children should be visible and the focus is moved to them.
	 */
	protected void open(boolean open) {
		//#debug
		//# System.out.println( this + ": opening children: " + open);
		this.isOpen = open;
		if ( open ) {
			// move focus to first child:
			this.children.focus( getFocusedStyle(), 0 );
		} else {
			// focus myself:
			// reset selected command element to the first one in the list:
			if ( this.children != null ) {
				this.children.focus( 0 );
				this.children.setScrollYOffset( 0 );
			}
		}
	}
	

	/**
	 * Retrieves the child item for the specified command.
	 * 
	 * @param parentCommand the corresponding command
	 * @return the corresponding item or null when the child is not found
	 */
	public CommandItem getChild(Command parentCommand) {
		if (!this.hasChildren) {
			return null;
		}
		for ( int i=0; i<this.children.size(); i++ ) {
			CommandItem child = (CommandItem) this.children.get(i);
			if ( child.command == parentCommand ) {
				return child;
			} else if (child.hasChildren) {
				child = child.getChild(parentCommand);
				if (child != null) {
					return child;
				}
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.IconItem#setStyle(de.enough.polish.ui.Style)
	 */
	public void setStyle(Style style) {
		super.setStyle(style);
		//#if polish.css.command-child-indicator-color
			//# Integer childIndicatorColorInt = style.getIntProperty(147);
			//# if ( childIndicatorColorInt != null ) {
				//# this.childIndicatorColor = childIndicatorColorInt.intValue();
			//# }
		//#endif
		//#if polish.css.command-child-indicator
			//# if (this.hasChildren) {
				//# String childIndicatorUrl = style.getProperty( 146 );
				//# if (childIndicatorUrl != null) {
					//# try {
						//# this.childIndicator = StyleSheet.getImage(childIndicatorUrl, this, true);
						//# this.childIndicatorWidth = this.childIndicator.getWidth();
						//# this.childIndicatorHeight = this.childIndicator.getHeight();
					//# } catch (IOException e) {
						//#debug error
						//# System.out.println("Unable to load command-child-indicator[ " + childIndicatorUrl + "] " + e );
					//# }
				//# }
			//# }
			//# if (this.hasChildren && this.childIndicator == null) {
		//#endif			
				this.childIndicatorWidth = this.font.getHeight();
				this.childIndicatorHeight = this.childIndicatorWidth;
		//#if polish.css.command-child-indicator
			//# }
		//#endif
		//#if polish.css.command-child-indicator-width
			//# Integer childIndicatorWidthInt = style.getIntProperty(148);
			//# if ( childIndicatorWidthInt != null ) {
				//# this.childIndicatorWidth = childIndicatorWidthInt.intValue();
			//# }
		//#endif
		//#if polish.css.command-child-indicator-height
			//# Integer childIndicatorHeightInt = style.getIntProperty(149);
			//# if ( childIndicatorHeightInt != null ) {
				//# this.childIndicatorHeight = childIndicatorHeightInt.intValue();
			//# }
		//#endif

	}
	
	

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.IconItem#animate()
	 */
	public boolean animate() {
		boolean animated = super.animate();
		if (this.isOpen && this.isFocused) {
			animated |= this.children.animate();
		}
		return animated;
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.StringItem#hideNotify()
	 */
	protected void hideNotify() {
		if (this.hasChildren) {
			this.children.hideNotify();
			if (this.isOpen) {
				open( false );
			}
		}
		super.hideNotify();
	}


	
	
	


}

⌨️ 快捷键说明

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