📄 menubar.java
字号:
//#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 java.util.Hashtable;
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)} != right
//#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
protected final ArrayList commandsList;
private final Container commandsContainer;
protected boolean isOpened;
private Command singleLeftCommand;
private final CommandItem singleLeftCommandItem;
private Command singleRightCommand;
private final CommandItem singleRightCommandItem;
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;
//#if !polish.Bugs.noTranslucencyWithDrawRgb
private Background overlayBackground;
//#endif
private final Hashtable allCommands;
/**
* 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();
this.allCommands = new Hashtable();
//#style menu, default
this.commandsContainer = new Container( true , de.enough.polish.ui.StyleSheet.menuStyle );
if (this.commandsContainer.style != null) {
this.commandsContainer.setStyle( this.commandsContainer.style );
}
this.commandsContainer.layout |= LAYOUT_SHRINK;
this.commandsContainer.screen = screen;
this.commandsContainer.parent = this;
Command dummy = new Command("", Command.ITEM, 10000);
//#style rightcommand, default
this.singleRightCommandItem = new CommandItem( dummy, this , de.enough.polish.ui.StyleSheet.defaultStyle );
this.singleRightCommandItem.setImageAlign( Graphics.LEFT );
//#style leftcommand, default
this.singleLeftCommandItem = new CommandItem( dummy, this , de.enough.polish.ui.StyleSheet.defaultStyle );
this.singleLeftCommandItem.setImageAlign( Graphics.LEFT );
}
public void addCommand(Command cmd) {
//#style menuitem, menu, default
addCommand( cmd , de.enough.polish.ui.StyleSheet.menuitemStyle );
}
public void addCommand(Command cmd, Style commandStyle) {
//System.out.println("adding cmd " + cmd.getLabel() + " with style " + commandStyle );
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;
}
CommandItem item = new CommandItem( cmd, this, commandStyle );
this.allCommands.put( cmd, item );
//#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 = "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, commandStyle );
//# }
//# 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.isInitialized) {
//# this.isInitialized = false;
//# repaint();
//# }
//# return;
//# } else if ( this.singleLeftCommand.getPriority() > priority ) {
//# Command oldLeftCommand = this.singleLeftCommand;
//# this.singleLeftCommand = cmd;
//# this.singleLeftCommandItem.setText( cmd.getLabel() );
//# cmd = oldLeftCommand;
//# item = (CommandItem) this.allCommands.get( cmd );
//# priority = oldLeftCommand.getPriority();
//# //System.out.println("MenuBar: now adding previous singleleft command " + cmd.getLabel() );
//# }
//# }
//#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.isInitialized) {
//# this.isInitialized = false;
//# repaint();
//# }
//# return;
//# } else if ( this.singleLeftCommand.getPriority() > priority ) {
//# Command oldLeftCommand = this.singleLeftCommand;
//# this.singleLeftCommand = cmd;
//# this.singleLeftCommandItem.setText( cmd.getLabel() );
//# cmd = oldLeftCommand;
//# item = (CommandItem) this.allCommands.get( cmd );
//# 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.isInitialized) {
this.isInitialized = false;
repaint();
}
return;
} else if ( this.singleRightCommand.getPriority() > priority ) {
Command oldRightCommand = this.singleRightCommand;
this.singleRightCommand = cmd;
this.singleRightCommandItem.setText( cmd.getLabel() );
cmd = oldRightCommand;
item = (CommandItem) this.allCommands.get( cmd );
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:
//# CommandItem singleItem = (CommandItem) this.allCommands.get( this.singleRightCommand );
//# this.commandsList.add( this.singleRightCommand );
//# this.commandsContainer.add( singleItem );
//# 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.isInitialized) {
//# this.isInitialized = 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");
CommandItem singleItem = (CommandItem) this.allCommands.get( this.singleLeftCommand );
this.commandsList.add( this.singleLeftCommand );
this.commandsContainer.add( singleItem );
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.isInitialized) {
this.isInitialized = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -