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

📄 menubar.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			}
			//#if tmp.RightOptions
				//# if (this.singleRightCommand != null) {
					//# if ( this.singleRightCommand.getCommandType() == Command.BACK 
						//# || this.singleRightCommand.getCommandType() == Command.CANCEL ) 
					//# {
						//# this.singleLeftCommand = this.singleRightCommand;
						//# this.singleLeftCommandItem.setText( this.singleLeftCommand.getLabel() );
						//# this.singleRightCommand = null;
					//# }
					//# if (this.isInitialised) {
						//# this.isInitialised = false;
						//# repaint();
					//# }
					//# return;
				//# }
				//# int newSingleRightCommandIndex = getNextNegativeCommandIndex();
				//# if ( newSingleRightCommandIndex != -1 ) {
					//# this.singleLeftCommand = (Command) this.commandsList.remove(newSingleRightCommandIndex);
					//# this.singleLeftCommandItem.setText( this.singleLeftCommand.getLabel() );
					//# this.commandsContainer.remove( newSingleRightCommandIndex );
				//# }	
				//# // don't return here yet, since it could well be that there is only
				//# // one remaining item in the commandsList. In such a case the 
				//# // single right command is used instead.
//# 	
			//#endif
		}
		
		// 2.case: cmd == this.singleRightCommand
		if ( cmd == this.singleRightCommand ) {
			// remove single right command:
			this.singleRightCommand = null;
			//System.out.println("removing single right command");
			// check if there is another BACK or CANCEL command and 
			// select the one with the highest priority:
			//#if !tmp.RightOptions
				if (this.singleLeftCommand != null) {
					if ( this.singleLeftCommand.getCommandType() == Command.BACK 
						|| this.singleLeftCommand.getCommandType() == Command.CANCEL ) 
					{
						this.singleRightCommand = this.singleLeftCommand;
						this.singleRightCommandItem.setText( this.singleLeftCommand.getLabel() );
						this.singleLeftCommand = null;
					}
					if (this.isInitialised) {
						this.isInitialised = false;
						repaint();
					}
					return;
				}
				int newSingleRightCommandIndex = getNextNegativeCommandIndex();
				if ( newSingleRightCommandIndex != -1 ) {
					this.singleRightCommand = (Command) this.commandsList.remove(newSingleRightCommandIndex);
					this.singleRightCommandItem.setText( this.singleRightCommand.getLabel() );
					this.commandsContainer.remove( newSingleRightCommandIndex );
				}	
				// don't return here yet, since it could well be that there is only
				// one remaining item in the commandsList. In such a case the 
				// single left command is used instead.

			//#endif

		}
		
		// 3.case: cmd belongs to command collection
		int index = this.commandsList.indexOf( cmd );
		if (index != -1) {
			//System.out.println("removing normal command");
			this.commandsList.remove( index );
			this.commandsContainer.remove( index );
		}
		// if there is only one remaining item in the commandsList, the 
		// single left command is used instead:
		//#if !tmp.useInvisibleMenuBar
		if (this.commandsList.size() == 1) {
			//System.out.println("moving only left command to single-left-one");
			CommandItem item = (CommandItem) this.commandsContainer.get( 0 );
			if (!item.hasChildren) {
				Command command = (Command) this.commandsList.remove( 0 );
				this.commandsContainer.remove( 0 );
				//#if tmp.RightOptions
					//# this.singleRightCommand = command;
					//# this.singleRightCommandItem.setText( command.getLabel() );
				//#else
					this.singleLeftCommand = command;
					this.singleLeftCommandItem.setText( command.getLabel() );
				//#endif
			}
		}
		//#endif

		if (this.isInitialised) {
			this.isInitialised = false;
			repaint();
		}
	}

	private int getNextNegativeCommandIndex() {
	
		// there are several commands available, from the which the BACK/CANCEL command
		// with the highest priority needs to be chosen:
		Command[] myCommands = (Command[]) this.commandsList.toArray( new Command[ this.commandsList.size() ]);
		int maxPriority = 1000;
		int maxPriorityId = -1;
		for (int i = 0; i < myCommands.length; i++) {
			Command command = myCommands[i];
			int type = command.getCommandType();
			//#if polish.blackberry
				//# if ((type == Command.BACK || type == Command.CANCEL || type == Command.EXIT)
			//#else
				if ((type == Command.BACK || type == Command.CANCEL)
			//#endif
					&& command.getPriority() < maxPriority ) 
			{
				maxPriority = command.getPriority();
				maxPriorityId = i;
			}
		}
		return maxPriorityId;
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#initContent(int, int)
	 */
	protected void initContent(int firstLineWidth, int lineWidth) {
		//#debug
		//# System.out.println("Init content of MenuBar - isOpened=" + this.isOpened );
		if (this.isOpened) {
			int titleHeight = this.screen.titleHeight; // + this.screen.subTitleHeight + this.screen.infoHeight;
			int screenHeight = this.screen.screenHeight;
			this.topY = titleHeight;
			this.commandsContainer.setVerticalDimensions( titleHeight, screenHeight );
			//System.out.println("setting vertical dimension: " + topMargin + ", " + (this.screen.screenHeight - topMargin) );
			//#if polish.Screen.maxMenuWidthInPercent:defined
				//#= this.commandsContainerWidth = (this.screen.screenWidth * ${polish.Screen.maxMenuWidthInPercent}) / 100;
			//#else
				this.commandsContainerWidth = this.screen.screenWidth;
			//#endif
			int containerHeight = this.commandsContainer.getItemHeight(this.commandsContainerWidth, this.commandsContainerWidth);
			this.commandsContainerWidth = this.commandsContainer.itemWidth;
			//#debug
			//# System.out.println("commandsContainerWidth=" + this.commandsContainerWidth);
			this.commandsContainerY = screenHeight - containerHeight - 1;
			if (this.commandsContainerY < titleHeight) {
				this.commandsContainerY = titleHeight;
			}
			/*
			int focusedIndex = this.commandsContainer.focusedIndex;
			this.canScrollUpwards = (focusedIndex != 0); 
			this.canScrollDownwards = (focusedIndex != this.commandsList.size() - 1 );
			*/
			this.canScrollUpwards = (this.commandsContainer.yOffset != 0)
				&& (this.commandsContainer.focusedIndex != 0);
			this.canScrollDownwards = (this.commandsContainer.yOffset + containerHeight > screenHeight - titleHeight) 
				&& (this.commandsContainer.focusedIndex != this.commandsList.size() - 1 );
			this.paintScrollIndicator = this.canScrollUpwards || this.canScrollDownwards;
			//#if !tmp.useInvisibleMenuBar
				//#if tmp.OkCommandOnLeft
					//# IconItem item = this.singleLeftCommandItem;
				//#elif tmp.RightOptions
					//# IconItem item = this.singleRightCommandItem;
				//#else
					IconItem item = this.singleLeftCommandItem;
				//#endif
				if (this.selectImage != null) {
					item.setImage( this.selectImage );
					if (this.showImageAndText) {
						//#ifdef polish.i18n.useDynamicTranslations
							//# item.setText( "Select" ); 
						//#elifdef polish.command.select:defined
item.setText( "Select" );
						//#else
							//# item.setText( "Select" );
						//#endif
					} else {
						item.setText( null );
					}
				} else {
					item.setImage( (Image)null );
					//#ifdef polish.i18n.useDynamicTranslations
						//# item.setText( "Select" ); 
					//#elifdef polish.command.select:defined
item.setText( "Select" );
					//#else
						//# item.setText( "Select" );
					//#endif
				}
				//#if tmp.OkCommandOnLeft
					//# this.singleRightCommandItem.setText(null);
					//# this.singleRightCommandItem.setImage( (Image)null );
				//#else
					//#if tmp.RightOptions
						//# item = this.singleLeftCommandItem;
					//#else
						item = this.singleRightCommandItem;
					//#endif
					if (this.cancelImage != null) {
						item.setImage( this.cancelImage );
						if (this.showImageAndText) {
							//#ifdef polish.i18n.useDynamicTranslations
								//# item.setText( "Cancel" ); 
							//#elifdef polish.command.cancel:defined
item.setText(  "Cancel" );
							//#else
								//# item.setText( "Cancel" );
							//#endif
						} else {
							item.setText( null );
						}
					} else {
						//#ifdef polish.i18n.useDynamicTranslations
							//# item.setText( "Cancel" ); 
						//#elifdef polish.command.cancel:defined
item.setText(  "Cancel"  );
						//#else
							//# item.setText( "Cancel" );
						//#endif
					}
				//#endif
			//#endif
		} else {
			//#if tmp.useInvisibleMenuBar
				//# this.background = null;
				//# this.border = null;
				//# this.contentWidth = 0;
				//# this.contentHeight = 0;
			//#else
				if (this.singleLeftCommand == null && this.singleRightCommand == null 
						&& this.commandsList.size() == 0 ) 
				{
					this.contentWidth = 0;
					this.contentHeight = 0;
					return;
				}
				// the menu is closed
				this.paintScrollIndicator = false;
				//#if tmp.RightOptions
					//# 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;
//			if ( ! this.isOpened && this.singleRightCommand == null ) {
//				availableWidth = lineWidth;
//			}
			//System.out.println("Initialising single commands with a width of " + availableWidth + " lineWidth is " + lineWidth);
			int height = this.singleLeftCommandItem.getItemHeight( availableWidth, availableWidth);
			if (this.singleRightCommandItem.getItemHeight( availableWidth, availableWidth ) > height) {
				height = this.singleRightCommandItem.itemHeight;
			}
			if (height > this.contentHeight) {
				this.contentHeight = height; 
			}
			this.singleLeftCommandY = this.contentHeight - this.singleLeftCommandItem.itemHeight;
			this.singleRightCommandY = this.contentHeight - this.singleRightCommandItem.itemHeight;
			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) );
			//#if tmp.useInvisibleMenuBar
				//# // paint menu at the top right corner:
				//# g.setClip(0, this.screen.contentY, this.screen.screenWidth , this.screen.screenHeight - this.screen.contentY);
				//# this.commandsContainer.paint( this.screen.screenWidth - this.commandsContainerWidth, this.topY, this.screen.screenWidth - this.commandsContainerWidth, this.screen.screenWidth, g);
				//# g.setClip(0, 0, this.screen.screenWidth , this.screen.screenHeight );
			//#elif tmp.RightOptions
				//# // paint menu at the lower right corner:
				//# g.setClip(0, this.topY, this.screen.screenWidth , this.screen.screenHeight - this.topY);
				//# this.commandsContainer.paint( this.screen.screenWidth - this.commandsContainerWidth, this.commandsContainerY, this.screen.screenWidth - this.commandsContainerWidth, this.screen.screenWidth, g);
				//# g.setClip(0, 0, this.screen.screenWidth , this.screen.screenHeight );
			//#else
				// paint menu at the lower left corner:
				g.setClip(0, this.topY, this.screen.screenWidth , this.screen.screenHeight - this.topY);
				this.commandsContainer.paint(0, this.commandsContainerY, 0, this.commandsContainerWidth , g);
			//#endif
			//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.singleLeftCommandY, leftBorder, centerX, g);			
			this.singleRightCommandItem.paint(centerX, y + this.singleRightCommandY, 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.singleLeftCommandY, 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.singleRightCommandY, centerX, rightBorder, g);
			}

⌨️ 快捷键说明

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