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

📄 menubar.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
						repaint();
					}
					return;
				}
			//#endif
		//#endif
			
		//#if tmp.useInvisibleMenuBar
			//# if (this.positiveCommand == null 
					//# && type != Command.BACK
					//# && type != Command.CANCEL
					//# && type != Command.EXIT) 
			//# {
				//# this.positiveCommand = cmd;
			//# }
		//#endif
	
		addCommand( item );
		
		if (this.isInitialized) {
			this.isInitialized = false;
			repaint();
		}
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.Displayable#removeCommand(javax.microedition.lcdui.Command)
	 */
	public void removeCommand(Command cmd) {
		//#debug
		//# System.out.println(this + ": removing command " + cmd.getLabel() + " (" + cmd + ")");
		this.allCommands.remove( cmd );
		//#if tmp.useInvisibleMenuBar
			//# if (cmd == this.positiveCommand) {
				//# this.positiveCommand = null;
			//# }
		//#endif
		
		// 1.case: cmd == this.singleLeftCommand
		if ( cmd == this.singleLeftCommand ) {
			this.singleLeftCommand = null;
			if (this.isInitialized) {
				this.isInitialized = false;
				repaint();
			}
			//#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.isInitialized) {
						//# this.isInitialized = false;
						//# repaint();
					//# }
					//# return;
				//# }
				//# int newSingleLeftCommandIndex = getNextNegativeCommandIndex();
				//# if ( newSingleLeftCommandIndex != -1 ) {
					//# //System.out.println("moving commmand with index " + newSingleLeftCommandIndex + " from commands container (focused=" + this.commandsContainer.getFocusedIndex() + ") - new Single Left=" + ((Command) this.commandsList.get(newSingleLeftCommandIndex)).getLabel() );
					//# if (newSingleLeftCommandIndex == this.commandsContainer.getFocusedIndex()) {
						//# this.commandsContainer.focus(-1);
					//# }
					//# this.singleLeftCommand = (Command) this.commandsList.remove(newSingleLeftCommandIndex);
					//# this.singleLeftCommandItem.setText( this.singleLeftCommand.getLabel() );
					//# this.commandsContainer.remove( newSingleLeftCommandIndex );
				//# }	
				//# // 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.isInitialized) {
						this.isInitialized = false;
						repaint();
					}
					return;
				}
				int newSingleRightCommandIndex = getNextNegativeCommandIndex();
				if ( newSingleRightCommandIndex != -1 ) {
					//#if tmp.useInvisibleMenuBar
						//# this.singleRightCommand = (Command) this.commandsList.get(newSingleRightCommandIndex);
					//#else
						if (newSingleRightCommandIndex == this.commandsContainer.getFocusedIndex()) {
							this.commandsContainer.focus(-1);
						}
						this.singleRightCommand = (Command) this.commandsList.remove(newSingleRightCommandIndex);
						this.commandsContainer.remove( newSingleRightCommandIndex );
					//#endif
					this.singleRightCommandItem.setText( this.singleRightCommand.getLabel() );
				}	
				// 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");
			if (index == this.commandsContainer.getFocusedIndex()) {
				this.commandsContainer.focus(-1);
			}
			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/right-one, currently focused index=" + this.commandsContainer.getFocusedIndex() );
			CommandItem item = (CommandItem) this.commandsContainer.get( 0 );
			if (!item.hasChildren) {
				Command command = (Command) this.commandsList.remove( 0 );
				this.commandsContainer.focus(-1);
				this.commandsContainer.remove( 0 );
				//System.out.println("MenuBar: moving command " + command.getLabel() + ", new currently focused index=" + this.commandsContainer.getFocusedIndex() );
				//#if tmp.RightOptions
					//# this.singleRightCommand = command;
					//# this.singleRightCommandItem.setText( command.getLabel() );
				//#else
					this.singleLeftCommand = command;
					this.singleLeftCommandItem.setText( command.getLabel() );
				//#endif
			}
		}
		//#endif

		if (this.isInitialized) {
			this.isInitialized = 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 + ", firstLineWidth=" + firstLineWidth + ", lineWidth=" + lineWidth + ", screen=" + this.screen );
		if (this.isOpened) {
			int titleHeight = this.screen.titleHeight; // + this.screen.subTitleHeight + this.screen.infoHeight;
			int screenHeight = this.screen.screenHeight;
			this.topY = titleHeight;
			this.commandsContainer.setScrollHeight( screenHeight - titleHeight );
			//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);
			int commandsContainerY = screenHeight - containerHeight - 1;
			if ( commandsContainerY < titleHeight) {
				containerHeight -= titleHeight - commandsContainerY;
				commandsContainerY = titleHeight;
			}
			this.commandsContainer.relativeY = - containerHeight;
			//#if tmp.useInvisibleMenuBar || tmp.RightOptions
				//# // move menu to the right of the screen:
				//# this.commandsContainer.relativeX = lineWidth - this.commandsContainerWidth;
			//#endif
			/*
			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

⌨️ 快捷键说明

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