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

📄 menubar.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
					//# if (this.singleLeftCommand != null) {
						//# this.singleLeftCommandItem.setText( this.singleLeftCommand.getLabel() );
					//# } else {
						//# this.singleLeftCommandItem.setText( null );
					//# }
					//# this.singleLeftCommandItem.setImage( (Image)null );
				//#else
					if (this.singleRightCommand != null) {
						this.singleRightCommandItem.setText( this.singleRightCommand.getLabel() );
					} else {
						this.singleRightCommandItem.setText( null );
					}
					this.singleRightCommandItem.setImage( (Image)null );
				//#endif
				if (this.commandsList.size() > 0) {
					//#if tmp.RightOptions
						//# IconItem item = this.singleRightCommandItem;
					//#else
						IconItem item = this.singleLeftCommandItem;
					//#endif
					if (this.optionsImage != null) {
						item.setImage( this.optionsImage );
						if (this.showImageAndText) {
							//#ifdef polish.i18n.useDynamicTranslations
								//# item.setText( "Options" ); 
							//#elifdef polish.command.options:defined
item.setText( "Options" );
							//#else
								//# item.setText( "Options" );				
							//#endif
						} else {
							item.setText( null );
						}
					} else {
						item.setImage( (Image)null );
						//#ifdef polish.i18n.useDynamicTranslations
							//# item.setText( "Options" ); 
						//#elifdef polish.command.options:defined
item.setText( "Options" );
						//#else
							//# item.setText( "Options" );				
						//#endif
					}
				}
			//#endif
		}
		//#if !tmp.useInvisibleMenuBar
			int availableWidth = lineWidth / 2;
			this.singleRightCommandItem.relativeX = availableWidth;
//			if ( ! this.isOpened && this.singleRightCommand == null ) {
//				availableWidth = lineWidth;
//			}
			//System.out.println("Initialising single commands with a width of " + availableWidth + " lineWidth is " + lineWidth);
			int height = Math.max( this.singleLeftCommandItem.getItemHeight( availableWidth, availableWidth),
					this.singleRightCommandItem.getItemHeight( availableWidth, availableWidth) );
			if (height > this.contentHeight) {
				this.contentHeight = height; 
			} else {
				// TODO allow LAYOUT_VEXPAND
				if (( this.singleLeftCommandItem.layout & Item.LAYOUT_VCENTER) == Item.LAYOUT_VCENTER) {
					this.singleLeftCommandItem.relativeY = (this.contentHeight - this.singleLeftCommandItem.itemHeight) / 2;					
				} else if (( this.singleLeftCommandItem.layout & Item.LAYOUT_BOTTOM) == Item.LAYOUT_BOTTOM) {
					this.singleLeftCommandItem.relativeY = this.contentHeight - this.singleLeftCommandItem.itemHeight;
				} else {
					this.singleLeftCommandItem.relativeY = 0;
				}
				if (( this.singleRightCommandItem.layout & Item.LAYOUT_VCENTER) == Item.LAYOUT_VCENTER) {
					this.singleRightCommandItem.relativeY = (this.contentHeight - this.singleRightCommandItem.itemHeight) / 2;					
				} else if (( this.singleRightCommandItem.layout & Item.LAYOUT_BOTTOM) == Item.LAYOUT_BOTTOM) {
					this.singleRightCommandItem.relativeY = this.contentHeight - this.singleRightCommandItem.itemHeight;
				} else {
					this.singleLeftCommandItem.relativeY = 0;
				}
			}
			this.contentWidth = lineWidth;
		//#endif
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#paintContent(int, int, int, int, javax.microedition.lcdui.Graphics)
	 */
	protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) 
	{
		if (this.isOpened) {
			// paint overlay background:
			//#if !polish.Bugs.noTranslucencyWithDrawRgb
				if (this.overlayBackground != null) {
					this.overlayBackground.paint( 0, this.screen.contentY, this.screen.screenWidth , this.screen.screenHeight - this.screen.contentY, g );
				}
			//#endif
			// paint opened menu:
			//System.out.println("setting clip " + this.topY + ", " + (this.screen.screenHeight - this.topY) );
			int clipX = g.getClipX();
			int clipY = g.getClipY();
			int clipWidth = g.getClipWidth();
			int clipHeight = g.getClipHeight();
			g.setClip(0, this.topY, this.screen.screenWidth , this.screen.screenHeight - this.topY);
            this.commandsContainer.paint( x + this.commandsContainer.relativeX, y + this.commandsContainer.relativeY, x + this.commandsContainer.relativeX, x + this.commandsContainer.relativeX + this.commandsContainerWidth, g);
			g.setClip(clipX, clipY, clipWidth, clipHeight );
			//System.out.println("MenuBar: commandContainer.background == null: " + ( this.commandsContainer.background == null ) );
			//System.out.println("MenuBar: commandContainer.style.background == null: " + ( this.commandsContainer.style.background == null ) );
		//#if !tmp.useInvisibleMenuBar
//			//#ifdef polish.FullCanvasSize:defined
//				//#= g.setClip(0, 0, ${polish.FullCanvasWidth}, ${polish.FullCanvasHeight} );
//			//#else
//				//TODO rob check if resetting of clip really works
//				g.setClip(0, this.commandsContainerY, this.screen.screenWidth, this.screen.screenHeight );
//			//#endif
			// paint menu-bar:
			int centerX = leftBorder + ((rightBorder - leftBorder)/2); 
			this.singleLeftCommandItem.paint(leftBorder, y + this.singleLeftCommandItem.relativeY, leftBorder, centerX, g);			
			this.singleRightCommandItem.paint(centerX, y + this.singleRightCommandItem.relativeY, centerX, rightBorder, g);
		} else {
			int centerX = leftBorder + ((rightBorder - leftBorder)/2);
			//#if tmp.RightOptions
				//# if (this.singleLeftCommand != null) {
			//#else
				if (this.commandsContainer.size() > 0 || this.singleLeftCommand != null) {
			//#endif
				//System.out.println("painting left command from " + leftBorder + " to " + centerX );
				this.singleLeftCommandItem.paint(leftBorder, y + this.singleLeftCommandItem.relativeY, leftBorder, centerX, g);
			}
			//#if tmp.RightOptions
				//# if (this.commandsContainer.size() > 0 || this.singleRightCommand != null) {
			//#else
				if (this.singleRightCommand != null) {
			//#endif
				//System.out.println("painting right command from " + centerX + " to " + rightBorder );
				this.singleRightCommandItem.paint(centerX, y + this.singleRightCommandItem.relativeY, centerX, rightBorder, g);
			}
		//#endif
		}
	}

	//#ifdef polish.useDynamicStyles	
	//# /* (non-Javadoc)
	 //# * @see de.enough.polish.ui.Item#createCssSelector()
	 //# */
	//# protected String createCssSelector() {
		//# return "menubar";
	//# }
	//#endif
	
	/**
	 * Used to toggle the opened state of the menu bar
	 * 
	 * @param open true when the menu should be opened
	 */
	protected void setOpen( boolean open ) {
		if (!open && this.isOpened) {
			this.commandsContainer.hideNotify();
		} else if (open && !this.isOpened) {
			//#if !polish.MenuBar.focusFirstAfterClose
				// focus the first item again, so when the user opens the menu again, it will be "fresh" again
				//System.out.println("MenuBar: focussing first command: " + ((Command)this.commandsList.get(0)).getLabel() );
				this.commandsContainer.focus(0);
			//#endif
			this.commandsContainer.showNotify();
		}
		this.isInitialized = (open == this.isOpened);
		this.isOpened = open;
	}
	


	protected boolean handleKeyPressed(int keyCode, int gameAction) {
		//#debug
		//# System.out.println("MenuBar: handleKeyPressed(" + keyCode + ", " + gameAction  );
		this.isSoftKeyPressed = false;
		if (this.isOpened) {
			if (keyCode == this.selectOptionsMenuKey) {
				this.isSoftKeyPressed = true;			
				CommandItem commandItem = (CommandItem) this.commandsContainer.getFocusedItem();
				//#if tmp.useInvisibleMenuBar
					//# if (commandItem.command == this.hideCommand ) {
						//# setOpen( false );
						//# return true;
					//# }
					//# //TODO find a way how to handle the hide command on BlackBerry when it is invoked directly...
				//#endif
				commandItem.handleKeyPressed( 0, Canvas.FIRE );
	//			boolean handled = commandItem.handleKeyPressed( 0, Canvas.FIRE );
	//			if (!handled) { // CommandItem returns false when it invokes the command listener
				// this is now done automatically by the Screen class
	//				setOpen( false );
	//			}				
				return true;
			//#if polish.key.ReturnKey:defined
				//#= } else  if (keyCode == this.closeOptionsMenuKey || keyCode == ${polish.key.ReturnKey}) {
			//#else
				} else  if (keyCode == this.closeOptionsMenuKey) {
			//#endif
				this.isSoftKeyPressed = true;
				int selectedIndex = this.commandsContainer.getFocusedIndex();
				if (!this.commandsContainer.handleKeyPressed(0, Canvas.LEFT)
						|| selectedIndex != this.commandsContainer.getFocusedIndex() ) 
				{
					setOpen( false );
				}
				//System.out.println("MenuBar is closing due to key " + keyCode);
				return true;
			} else {
				if (keyCode != 0) {
					gameAction = this.screen.getGameAction(keyCode);
				}
				//#if tmp.useInvisibleMenuBar
					//# // handle hide command specifically:
					//# if (  gameAction == Canvas.FIRE && ((CommandItem)this.commandsContainer.focusedItem).command == this.hideCommand ) {
						//# setOpen( false );
						//# return true;
					//# }
				//#endif
//				if (gameAction == Canvas.FIRE) {
//					int focusedIndex = this.commandsContainer.focusedIndex;
//					Command command = (Command) this.commandsList.get(focusedIndex);
//					setOpen( false );
//					this.screen.callCommandListener(command);
//					return true;
//				}
				boolean handled = this.commandsContainer.handleKeyPressed(keyCode, gameAction);
				if (handled) {
					this.isInitialized = false;
				} else { 
					if (gameAction == Canvas.DOWN || gameAction == Canvas.UP) {
						//#debug error
						//# System.out.println("Container DID NOT HANDLE DOWN OR UP, selectedIndex=" + this.commandsContainer.getFocusedIndex() + ", count="+ this.commandsContainer.size() );
					}
					setOpen( false );
				}
				return handled;				
			}
		} else {
			if (keyCode == LEFT_SOFT_KEY && this.singleLeftCommand != null && this.singleLeftCommandItem.getAppearanceMode() != PLAIN) {
				this.isSoftKeyPressed = true;			
				this.screen.callCommandListener(this.singleLeftCommand);
				return true;			
			} else if (keyCode == RIGHT_SOFT_KEY && this.singleRightCommand != null && this.singleRightCommandItem.getAppearanceMode() != PLAIN) {
				this.isSoftKeyPressed = true;			
				this.screen.callCommandListener(this.singleRightCommand);
				return true;			
			} else if (keyCode == this.openOptionsMenuKey ) {
				this.isSoftKeyPressed = true;			
				//#if tmp.useInvisibleMenuBar
					//# if ( !this.isOpened && this.positiveCommand != null 
//# //							&& ((this.singleRightCommand != null && this.commandsContainer.size() == 3) ) )
							//# && (this.singleRightCommand == null && this.commandsContainer.size() == 2) )  
					//# {
						//# // invoke positive command:
						//# this.screen.callCommandListener(this.positiveCommand);
						//# return true;
					//# } else 
				//#endif
				if (this.commandsList.size() > 0) {
					setOpen( true );
					return true;
				}
			}
		}
		return false;
	}
	
	//#ifdef polish.hasPointerEvents
	//# protected boolean handlePointerPressed(int relX, int relY) {
		//# // check if one of the command buttons has been pressed:
		//# int leftCommandEndX = this.singleLeftCommandItem.relativeX + this.singleLeftCommandItem.itemWidth;
		//# int rightCommandStartX = this.singleRightCommandItem.relativeX;
		//#debug
		//# System.out.println("MenuBar: handlePointerPressed( relX=" + relX + ", relY=" + relY + " )\nleftCommandEndX = " + leftCommandEndX + ", rightCommandStartXs = " + rightCommandStartX + " screenHeight=" + this.screen.screenHeight);
		//# if (relY > 0) {
			//# //System.out.println("menubar clicked");
			//# boolean isCloseKeySelected;
			//# boolean isOpenKeySelected;
			//# boolean isSelectKeySelected;
			//#if tmp.OkCommandOnLeft && tmp.RightOptions
				//# isCloseKeySelected = relX > rightCommandStartX;
				//# isOpenKeySelected =  isCloseKeySelected;
				//# isSelectKeySelected = relX < leftCommandEndX;
			//#elif tmp.RightOptions
				//# isCloseKeySelected = relX < leftCommandEndX;
				//# isOpenKeySelected = relX > rightCommandStartX;
				//# isSelectKeySelected = isOpenKeySelected;
			//#else
				//# isOpenKeySelected = relX < leftCommandEndX;
				//# isCloseKeySelected = relX > rightCommandStartX;
				//# isSelectKeySelected = isOpenKeySelected;
			//#endif
			//# //System.out.println("isOpened=" + this.isOpened + ", isCloseKeySelected=" + isCloseKeySelected + ", isOpenKeySelected=" + isOpenKeySelected + ", isSelectKeySelected=" + isSelectKeySelected);
			//# if (this.isOpened) {
				//# if ( isSelectKeySelected ) {
					//# //System.out.println("selecting command from opened menu");
					//# setOpen( false );
					//# Command command = (Command) this.commandsList.get( this.commandsContainer.focusedIndex );
					//# this.screen.callCommandListener(command);
				//# } else if ( isCloseKeySelected ) {
					//# //System.out.println("closing menu");
					//# setOpen( false );
				//# }
			//# } else if (this.singleLeftCommand != null 
					//# && relX < leftCommandEndX && this.singleLeftCommandItem.getAppearanceMode() != PLAIN) 
			//# {
				//# //System.out.println("calling single left command");
				//# this.screen.callCommandListener(this.singleLeftCommand);
			//# } else if (this.singleRightCommand != null
					//# && relX > rightCommandStartX && this.singleRightCommandItem.getAppearanceMode() != PLAIN) 

⌨️ 快捷键说明

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