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

📄 menubar.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//#condition polish.usePolishGui/* * Created on 24-Jan-2005 at 05:39:27. *  * Copyright (c) 2005 Robert Virkus / Enough Software * * This file is part of J2ME Polish. * * J2ME Polish is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. *  * J2ME Polish is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with J2ME Polish; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *  * Commercial licenses are also available, please * refer to the accompanying LICENSE.txt or visit * http://www.j2mepolish.org for details. */package de.enough.polish.ui;import java.io.IOException;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import de.enough.polish.ui.backgrounds.TranslucentSimpleBackground;import de.enough.polish.util.ArrayList;import de.enough.polish.util.Locale;/** * <p>Provides a more powerful alternative to the build-in menu bar of the Screen-class.</p> * * <p>Copyright (c) 2005, 2006 Enough Software</p> * <pre> * history *        24-Jan-2005 - rob creation * </pre> * @author Robert Virkus, j2mepolish@enough.de */public class MenuBar extends Item {		//#ifdef polish.key.LeftSoftKey:defined		//#= private final static int LEFT_SOFT_KEY = ${polish.key.LeftSoftKey};	//#else		private final static int LEFT_SOFT_KEY = -6;	//#endif	//#ifdef polish.key.RightSoftKey:defined		//#= private final static int RIGHT_SOFT_KEY = ${polish.key.RightSoftKey};	//#else		private final static int RIGHT_SOFT_KEY = -7;	//#endif	//#if polish.blackberry && (polish.BlackBerry.useStandardMenuBar != true)		//#define tmp.useInvisibleMenuBar		private Command hideCommand;		private Command positiveCommand;	//#endif	//#if ${lowercase(polish.MenuBar.OptionsPosition)} == right && ${lowercase(polish.MenuBar.OkPosition)} == left		//#define tmp.RightOptions		//#define tmp.OkCommandOnLeft		//# private final int openOptionsMenuKey = RIGHT_SOFT_KEY;		//# private final int selectOptionsMenuKey = LEFT_SOFT_KEY;		//# private final int closeOptionsMenuKey = RIGHT_SOFT_KEY;	//#elif ${lowercase(polish.MenuBar.OptionsPosition)} == right		//#define tmp.RightOptions		//# private final int openOptionsMenuKey = RIGHT_SOFT_KEY;		//# private final int selectOptionsMenuKey = RIGHT_SOFT_KEY;		//# private final int closeOptionsMenuKey = LEFT_SOFT_KEY;	//#elif ${lowercase(polish.MenuBar.OptionsPosition)} == middle		//#define tmp.MiddleOptions		//# private final int openOptionsMenuKey = MIDDLE_SOFT_KEY;		//# private final int selectOptionsMenuKey = LEFT_SOFT_KEY;		//# private final int closeOptionsMenuKey = RIGHT_SOFT_KEY;	//#else		//#define tmp.LeftOptions		private final int openOptionsMenuKey = LEFT_SOFT_KEY;		private final int selectOptionsMenuKey = LEFT_SOFT_KEY;		private final int closeOptionsMenuKey = RIGHT_SOFT_KEY;	//#endif		private final ArrayList commandsList;	private final Container commandsContainer;	protected boolean isOpened;	private Command singleLeftCommand;	private final IconItem singleLeftCommandItem;	private Command singleRightCommand;	private final IconItem singleRightCommandItem;	private int commandsContainerY;	private int topY;	private int commandsContainerWidth;	protected boolean isSoftKeyPressed;		protected boolean canScrollDownwards; // indicator for parent screen	protected boolean canScrollUpwards; // indicator for parent screen	protected boolean paintScrollIndicator; // indicator for parent screen	private Image optionsImage;	private boolean showImageAndText;	private Image selectImage;	private Image cancelImage;	private int singleLeftCommandY;	private int singleRightCommandY;	//#if !polish.Bugs.noTranslucencyWithDrawRgb		private Background overlayBackground;	//#endif	/**	 * Creates a new menu bar	 * 	 * @param screen the parent screen	 */	public MenuBar( Screen screen ) {		this( screen, null );	}	/**	 * Creates a new menu bar	 * 	 * @param screen the parent screen	 * @param style the style of this menu-bar	 */	public MenuBar(Screen screen, Style style) {		super(style);		this.screen = screen;		this.commandsList = new ArrayList();		//#style menu, default		this.commandsContainer = new Container( true );		if (this.commandsContainer.style != null) {			this.commandsContainer.setStyle( this.commandsContainer.style );		}		this.commandsContainer.layout |= LAYOUT_SHRINK;		this.commandsContainer.screen = screen;		this.commandsContainer.parent = this;		//#style rightcommand, default		this.singleRightCommandItem = new IconItem( null, null );		this.singleRightCommandItem.setImageAlign( Graphics.LEFT );		//#style leftcommand, default		this.singleLeftCommandItem = new IconItem( null, null );		this.singleLeftCommandItem.setImageAlign( Graphics.LEFT );	}	public void addCommand(Command cmd) {		if (cmd == this.singleLeftCommand || cmd == this.singleRightCommand || this.commandsList.contains(cmd)) {			// do not add an existing command again...			//#debug			System.out.println( this + ": Ignoring existing command " + cmd.getLabel() + ",  cmd==this.singleRightCommand: " + ( cmd == this.singleRightCommand) + ", cmd==this.singleLeftCommand: " + (cmd == this.singleLeftCommand) + ", this.commandsList.contains(cmd): " + this.commandsList.contains(cmd)  );			//#if polish.debug.debug				for (int i = 0; i < this.commandsList.size(); i++) {					System.out.println(  ((Command)this.commandsList.get(i)).getLabel() );				}			//#endif			return;		}		//#debug		System.out.println(this + ": adding command " + cmd.getLabel() + " (" + cmd + ")");		int type = cmd.getCommandType();		int priority = cmd.getPriority();		//#if tmp.useInvisibleMenuBar			if ( this.hideCommand == null )	{				// add hide command:				//#ifdef polish.i18n.useDynamicTranslations					String text =  Locale.get("polish.command.hide");				//#elifdef polish.command.hide:defined					//#= String text =  "${polish.command.hide}";				//#else					//# String text =  "Hide";				//#endif				this.hideCommand = new Command( text, Command.CANCEL, 2000 );				addCommand( this.hideCommand );			}			if ( (cmd != this.hideCommand) && 					(type == Command.BACK || type == Command.CANCEL || type == Command.EXIT) ) 			{				//#if tmp.RightOptions					if ( this.singleLeftCommand == null || this.singleLeftCommand.getPriority() > priority ) {						this.singleLeftCommand = cmd;					}				//#else					if ( this.singleRightCommand == null || this.singleRightCommand.getPriority() > priority ) {						this.singleRightCommand = cmd;					}				//#endif			}		//#else			//#if tmp.OkCommandOnLeft				if (type == Command.OK || type == Command.ITEM || type == Command.SCREEN) {					if (this.singleLeftCommand == null) {						this.singleLeftCommand = cmd;						this.singleLeftCommandItem.setImage( (Image)null );						this.singleLeftCommandItem.setText( cmd.getLabel() );						if (this.isInitialised) {							this.isInitialised = false;							repaint();						}						return;					} else if ( this.singleLeftCommand.getPriority() > priority ) {						Command oldLeftCommand = this.singleLeftCommand;						this.singleLeftCommand = cmd;						this.singleLeftCommandItem.setText( cmd.getLabel() );						cmd = oldLeftCommand;						priority = oldLeftCommand.getPriority();							}				}			//#else				if (type == Command.BACK || type == Command.CANCEL || type == Command.EXIT ) {					//#if tmp.RightOptions						if (this.singleLeftCommand == null) {							this.singleLeftCommand = cmd;							this.singleLeftCommandItem.setImage( (Image)null );							this.singleLeftCommandItem.setText( cmd.getLabel() );							if (this.isInitialised) {								this.isInitialised = false;								repaint();							}							return;						} else if ( this.singleLeftCommand.getPriority() > priority ) {							Command oldLeftCommand = this.singleLeftCommand;							this.singleLeftCommand = cmd;							this.singleLeftCommandItem.setText( cmd.getLabel() );							cmd = oldLeftCommand;							priority = oldLeftCommand.getPriority();						}					//#else						if (this.singleRightCommand == null) {							//#debug							System.out.println("Setting single right command " + cmd.getLabel() );							this.singleRightCommand = cmd;							this.singleRightCommandItem.setImage( (Image)null );							this.singleRightCommandItem.setText( cmd.getLabel() );							if (this.isInitialised) {								this.isInitialised = false;								repaint();							}							return;						} else if ( this.singleRightCommand.getPriority() > priority ) {							Command oldRightCommand = this.singleRightCommand;							this.singleRightCommand = cmd;							this.singleRightCommandItem.setText( cmd.getLabel() );							cmd = oldRightCommand;							priority = oldRightCommand.getPriority();							//#debug							System.out.println("exchanging right command " + oldRightCommand.getLabel() );						}					//#endif				}			//#endif			//#if tmp.RightOptions				if (this.singleRightCommand != null) {					// add existing right command first:					//#style menuitem, menu, default					CommandItem item = new CommandItem( this.singleRightCommand, this );					this.commandsList.add( this.singleRightCommand );					this.commandsContainer.add( item );					this.singleRightCommand = null;				}  else if (this.commandsList.size() == 0) {					// this is the new single right command!					//#debug					System.out.println("Setting single right command " + cmd.getLabel() );					this.singleRightCommand = cmd;					this.singleRightCommandItem.setText( cmd.getLabel() );					if (this.isInitialised) {						this.isInitialised = false;						repaint();					}					return;				}			//#else				if (this.singleLeftCommand != null) {					// add existing left command first:					//#debug					System.out.println("moving single left command " + this.singleLeftCommand.getLabel() + " to commandsContainer");					//#style menuitem, menu, default					CommandItem item = new CommandItem( this.singleLeftCommand, this );					this.commandsList.add( this.singleLeftCommand );					this.commandsContainer.add( item );					this.singleLeftCommand = null;				}  else if (this.commandsList.size() == 0) {					// this is the new single left command!					//#debug					System.out.println("Setting single left command " + cmd.getLabel() );					this.singleLeftCommand = cmd;					this.singleLeftCommandItem.setText( cmd.getLabel() );					if (this.isInitialised) {						this.isInitialised = false;						repaint();					}					return;				}			//#endif		//#endif					//#if tmp.useInvisibleMenuBar			if (this.positiveCommand == null 					&& type != Command.BACK					&& type != Command.CANCEL					&& type != Command.EXIT) 			{				this.positiveCommand = cmd;			}		//#endif			//#debug		System.out.println("Adding command " + cmd.getLabel() + " to the commands list...");		//#style menuitem, menu, default		CommandItem item = new CommandItem( cmd, this );		if ( this.commandsList.size() == 0 ) {			this.commandsList.add( cmd );			this.commandsContainer.add( item );		} else {			// there are already several commands,			// so add this cmd to the appropriate sorted position:			Command[] myCommands = (Command[]) this.commandsList.toArray( new Command[ this.commandsList.size() ]);			boolean inserted = false;			for (int i = 0; i < myCommands.length; i++) {				Command command = myCommands[i];				if ( cmd == command ) {					return;				}				if (command.getPriority() > priority ) {					this.commandsList.add( i, cmd );					this.commandsContainer.add(i, item);					inserted = true;					break;				}			}			if (!inserted) {				this.commandsList.add( cmd );				this.commandsContainer.add( item );			}		}				if (this.isInitialised) {			this.isInitialised = 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 + ")");		//#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.isInitialised) {				this.isInitialised = false;				repaint();

⌨️ 快捷键说明

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