de.enough.polish.ui
Class MenuBar

java.lang.Object
  extended by de.enough.polish.ui.Item
      extended by de.enough.polish.ui.MenuBar

public class MenuBar
extends Item

Provides a more powerful alternative to the build-in menu bar of the Screen-class.

Copyright (c) Enough Software 2005 - 2008

 history
        24-Jan-2005 - rob creation
 

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
protected  java.util.Hashtable allCommands
           
protected  javax.microedition.lcdui.Image cancelImage
           
protected  boolean canScrollDownwards
           
protected  boolean canScrollUpwards
           
protected  ArrayList commandsList
           
protected  boolean isOpened
           
protected  boolean isOrientationVertical
           
protected  boolean isSoftKeyPressed
           
protected  Style menuItemStyle
           
protected  javax.microedition.lcdui.Image optionsImage
           
protected  Background overlayBackground
           
protected  boolean paintScrollIndicator
           
protected  javax.microedition.lcdui.Image selectImage
           
protected  boolean showImageAndText
           
protected static javax.microedition.lcdui.Image windowsSipImage
           
protected  int windowsSipX
           
protected  int windowsSipY
           
 
Fields inherited from class de.enough.polish.ui.Item
_bbField, _bbFieldAdded, appearanceMode, background, backgroundHeight, backgroundWidth, backgroundYOffset, border, borderWidth, BUTTON, colSpan, commands, completeBackground, completeBackgroundPadding, completeBorder, contentHeight, contentWidth, contentX, contentY, cssSelector, defaultCommand, focusedStyle, HORIZONTAL, HYPERLINK, includeLabel, INTERACTIVE, internalHeight, internalWidth, internalX, internalY, isFocused, isInitialized, isInvisible, isLayoutCenter, isLayoutExpand, isLayoutRight, isPressed, isShown, isStyleInitialised, itemCommandListener, itemHeight, itemWidth, label, labelStyle, layout, LAYOUT_2, LAYOUT_BOTTOM, LAYOUT_CENTER, LAYOUT_DEFAULT, LAYOUT_EXPAND, LAYOUT_LEFT, LAYOUT_NEWLINE_AFTER, LAYOUT_NEWLINE_BEFORE, LAYOUT_RIGHT, LAYOUT_SHRINK, LAYOUT_TOP, LAYOUT_VCENTER, LAYOUT_VEXPAND, LAYOUT_VSHRINK, marginBottom, marginLeft, marginRight, marginTop, maximumHeight, maximumWidth, minimumHeight, minimumWidth, NO_POSITION_SET, opacity, opacityPaintNormally, opacityRgbData, paddingBottom, paddingHorizontal, paddingLeft, paddingRight, paddingTop, paddingVertical, parent, PLAIN, preferredHeight, preferredWidth, preserveViewType, relativeX, relativeY, rowSpan, screen, style, TRANSPARENT, useSingleRow, VERTICAL, view
 
Constructor Summary
MenuBar(Screen screen)
          Creates a new menu bar
MenuBar(Screen screen, Style style)
          Creates a new menu bar
 
Method Summary
 void addCommand(javax.microedition.lcdui.Command cmd)
          Adds a context sensitive Command to the item.
 void addCommand(javax.microedition.lcdui.Command cmd, Style commandStyle)
          Adds a context sensitive Command to the item.
 void addSubCommand(javax.microedition.lcdui.Command childCommand, javax.microedition.lcdui.Command parentCommand)
          Adds the given command as a subcommand to the specified parent command.
 void addSubCommand(javax.microedition.lcdui.Command childCommand, javax.microedition.lcdui.Command parentCommand, Style commandStyle)
          Adds the given command as a subcommand to the specified parent command.
 void animate(long currentTime, ClippingRegion repaintRegion)
          Animates this item.
protected  java.lang.String createCssSelector()
          Retrieves the CSS selector for this item.
 CommandItem getCommandItem(javax.microedition.lcdui.Command command)
          Retrieves the CommandItem used for rendering the specified command.
 Container getCommandsContainer()
           
 Item getItemAt(int relX, int relY)
          Determines if this item or one of it's children is within the specified point.
 Style getMenuItemStyle()
           
 int getSpaceBottom(int width, int height)
           
 int getSpaceLeft(int width, int height)
           
 int getSpaceRight(int width, int height)
           
 int getSpaceTop(int width, int height)
           
protected  boolean handleKeyPressed(int keyCode, int gameAction)
          Handles the key-pressed event.
protected  boolean handleKeyReleased(int keyCode, int gameAction)
          Handles the key-released event.
protected  boolean handlePointerPressed(int relX, int relY)
          Handles the event when a pointer has been pressed at the specified position.
protected  boolean handlePointerReleased(int relX, int relY)
          Handles the event when a pointer has been released at the specified position.
protected  void initContent(int firstLineWidth, int lineWidth)
          Initialises this item.
 boolean isOrientationVertical()
           
static void notifyKeyPressed()
          Commits different actions depending on the device using a menubar
protected  void paintBackgroundAndBorder(int x, int y, int width, int height, javax.microedition.lcdui.Graphics g)
          Paints the background and border of this item.
protected  void paintContent(int x, int y, int leftBorder, int rightBorder, javax.microedition.lcdui.Graphics g)
          Paints the content of this item.
 void removeAllCommands()
          Removes all commands from this MenuBar.
 void removeCommand(javax.microedition.lcdui.Command cmd)
          Removes the context sensitive command from item.
 void setMenuItemStyle(Style menuItemStyle)
           
protected  void setOpen(boolean open)
          Used to toggle the opened state of the menu bar
 void setOrientationVertical(boolean isVertical)
           
 void setStyle(Style style)
          Sets the style of this item.
 int size()
          Retrieves the number of commands in this menubar.
 
Methods inherited from class de.enough.polish.ui.Item
addCommands, addRelativeToBackgroundRegion, addRelativeToBackgroundRegion, addRelativeToContentRegion, animate, containsCommand, defocus, focus, getAbsoluteX, getAbsoluteY, getAppearanceMode, getAttribute, getAttributes, getBackgroundHeight, getBackgroundWidth, getBackgroundX, getBackgroundY, getContentHeight, getContentWidth, getContentX, getContentY, getDefaultCommand, getFocusedStyle, getItemCommandListener, getItemCommands, getItemHeight, getItemStateListener, getItemWidth, getLabel, getLabelItem, getLayout, getMinimumHeight, getMinimumWidth, getParent, getPreferredHeight, getPreferredWidth, getScreen, getStyle, handleCommand, handleKeyRepeated, hideNotify, init, initStyle, isInContentArea, isInItemArea, isInItemArea, isInitialized, isVisible, notifyItemPressedEnd, notifyItemPressedStart, notifyStateChanged, paint, paintBackground, paintBorder, releaseResources, repaint, repaint, repaintFully, requestInit, setAppearanceMode, setAttribute, setDefaultCommand, setItemCommandListener, setItemCommandListener, setItemStateListener, setLabel, setLayout, setParent, setParent, setPreferredSize, setVisible, show, showCommands, showNotify, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

commandsList

protected final ArrayList commandsList

isOpened

protected boolean isOpened

isSoftKeyPressed

protected boolean isSoftKeyPressed

canScrollDownwards

protected boolean canScrollDownwards

canScrollUpwards

protected boolean canScrollUpwards

paintScrollIndicator

protected boolean paintScrollIndicator

optionsImage

protected javax.microedition.lcdui.Image optionsImage

showImageAndText

protected boolean showImageAndText

selectImage

protected javax.microedition.lcdui.Image selectImage

cancelImage

protected javax.microedition.lcdui.Image cancelImage

overlayBackground

protected Background overlayBackground

allCommands

protected final java.util.Hashtable allCommands

isOrientationVertical

protected boolean isOrientationVertical

windowsSipImage

protected static javax.microedition.lcdui.Image windowsSipImage

windowsSipX

protected int windowsSipX

windowsSipY

protected int windowsSipY

menuItemStyle

protected Style menuItemStyle
Constructor Detail

MenuBar

public MenuBar(Screen screen)
Creates a new menu bar

Parameters:
screen - the parent screen

MenuBar

public MenuBar(Screen screen,
               Style style)
Creates a new menu bar

Parameters:
screen - the parent screen
style - the style of this menu-bar
Method Detail

addCommand

public void addCommand(javax.microedition.lcdui.Command cmd)
Description copied from class: Item
Adds a context sensitive Command to the item. The semantic type of Command should be ITEM. The implementation will present the command only when the item is active, for example, highlighted.

If the added command is already in the item (tested by comparing the object references), the method has no effect. If the item is actually visible on the display, and this call affects the set of visible commands, the implementation should update the display as soon as it is feasible to do so.

It is illegal to call this method if this Item is contained within an Alert.

Overrides:
addCommand in class Item
Parameters:
cmd - the command to be added

addCommand

public void addCommand(javax.microedition.lcdui.Command cmd,
                       Style commandStyle)
Description copied from class: Item
Adds a context sensitive Command to the item. The semantic type of Command should be ITEM. The implementation will present the command only when the item is active, for example, highlighted.

If the added command is already in the item (tested by comparing the object references), the method has no effect. If the item is actually visible on the display, and this call affects the set of visible commands, the implementation should update the display as soon as it is feasible to do so.

It is illegal to call this method if this Item is contained within an Alert.

Overrides:
addCommand in class Item
Parameters:
cmd - the command to be added
commandStyle - the style of the command, for the moment this is ignored

removeCommand

public void removeCommand(javax.microedition.lcdui.Command cmd)
Description copied from class: Item
Removes the context sensitive command from item. If the command is not in the Item (tested by comparing the object references), the method has no effect. If the Item is actually visible on the display, and this call affects the set of visible commands, the implementation should update the display as soon as it is feasible to do so. If the command to be removed happens to be the default command, the command is removed and the default command on this Item is set to null. The following code:
 // Command c is the default command on Item item
 item.removeCommand(c);
 
is equivalent to the following code:
 // Command c is the default command on Item item
 item.setDefaultCommand(null);
 item.removeCommand(c);
 

Overrides:
removeCommand in class Item
Parameters:
cmd - - the command to be removed

initContent

protected void initContent(int firstLineWidth,
                           int lineWidth)
Description copied from class: Item
Initialises this item. The implementation needs to calculate and set the contentWidth and contentHeight fields. The implementation should take the fields preferredWidth and preferredHeight into account.

Specified by:
initContent in class Item
Parameters:
firstLineWidth - the maximum width of the first line
lineWidth - the maximum width of any following lines
See Also:
Item.contentWidth, Item.contentHeight, Item.preferredWidth, Item.preferredHeight

paintBackgroundAndBorder

protected void paintBackgroundAndBorder(int x,
                                        int y,
                                        int width,
                                        int height,
                                        javax.microedition.lcdui.Graphics g)
Description copied from class: Item
Paints the background and border of this item. The call is forwarded to paintBackground() and paintBorder().

Overrides:
paintBackgroundAndBorder in class Item
Parameters:
x - the horizontal start position
y - the vertical start position
width - the width
height - the height
g - graphics context
See Also:
Item.paintBackground(int, int, int, int, Graphics), Item.paintBorder(int, int, int, int, Graphics)

paintContent

protected void paintContent(int x,
                            int y,
                            int leftBorder,
                            int rightBorder,
                            javax.microedition.lcdui.Graphics g)
Description copied from class: Item
Paints the content of this item. The background has already been painted and the border will be added after this method returns.

Specified by:
paintContent in class Item
Parameters:
x - the left start position
y - the upper start position
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position
g - the Graphics on which this item should be painted.

createCssSelector

protected java.lang.String createCssSelector()
Description copied from class: Item
Retrieves the CSS selector for this item. The CSS selector is used for the dynamic assignment of styles - that is the styles are assigned by the usage of the item and not by a predefined style-name. With the #style preprocessing command styles are set fix, this method yields in a faster GUI and is recommended. When in a style-sheet dynamic styles are used, e.g. "Form>p", than the selector of the item is needed.
This abstract method needs only be implemented, when dynamic styles are used: #ifdef polish.useDynamicStyles
The returned selector needs to be in lower case.

Specified by:
createCssSelector in class Item
Returns:
the appropriate CSS selector for this item. The selector needs to be in lower case.

setOpen

protected void setOpen(boolean open)
Used to toggle the opened state of the menu bar

Parameters:
open - true when the menu should be opened

handleKeyPressed

protected boolean handleKeyPressed(int keyCode,
                                   int gameAction)
Description copied from class: Item
Handles the key-pressed event. Please note, that implementation should first try to handle the given key-code, before the game-action is processed. The default implementation just handles the FIRE game-action when a default-command and an item-command-listener have been registered.

Overrides:
handleKeyPressed in class Item
Parameters:
keyCode - the code of the pressed key, e.g. Canvas.KEY_NUM2
gameAction - the corresponding game-action, e.g. Canvas.UP
Returns:
true when the key has been handled / recognized

notifyKeyPressed

public static void notifyKeyPressed()
Commits different actions depending on the device using a menubar


handleKeyReleased

protected boolean handleKeyReleased(int keyCode,
                                    int gameAction)
Description copied from class: Item
Handles the key-released event. Please note, that implementation should first try to handle the given key-code, before the game-action is processed. The default implementation invokes the default command if one is present

Overrides:
handleKeyReleased in class Item
Parameters:
keyCode - the code of the pressed key, e.g. Canvas.KEY_NUM2
gameAction - the corresponding game-action, e.g. Canvas.UP
Returns:
true when the key has been handled / recognized
See Also:
Item.handleKeyPressed(int, int)

handlePointerPressed

protected boolean handlePointerPressed(int relX,
                                       int relY)
Description copied from class: Item
Handles the event when a pointer has been pressed at the specified position. The default method discards this event when relX/relY is outside of the item's area. When the event took place inside of the content area, the pointer-event is translated into an artificial FIRE game-action keyPressed event, which is subsequently handled bu the handleKeyPressed(-1, Canvas.FIRE) method. This method needs should be overwritten only when the "polish.hasPointerEvents" preprocessing symbol is defined: "//#ifdef polish.hasPointerEvents".

Overrides:
handlePointerPressed in class Item
Parameters:
relX - the x position of the pointer pressing relative to this item's left position
relY - the y position of the pointer pressing relative to this item's top position
Returns:
true when the pressing of the pointer was actually handled by this item.
See Also:
this method is used for determining whether the event belongs to this item, for a helper method for determining whether the event took place into the actual content area, Item.handleKeyPressed(int, int), for calculating the horizontal position relative to the content (relX - contentX), for calculating the vertical position relative to the content (relY - contentY)

handlePointerReleased

protected boolean handlePointerReleased(int relX,
                                        int relY)
Description copied from class: Item
Handles the event when a pointer has been released at the specified position. The default method discards this event when relX/relY is outside of the item's area. When the event took place inside of the content area, the pointer-event is translated into an artificial FIRE game-action keyReleased event, which is subsequently handled bu the handleKeyPressed(-1, Canvas.FIRE) method. This method needs should be overwritten only when the "polish.hasPointerEvents" preprocessing symbol is defined: "//#ifdef polish.hasPointerEvents".

Overrides:
handlePointerReleased in class Item
Parameters:
relX - the x position of the pointer pressing relative to this item's left position
relY - the y position of the pointer pressing relative to this item's top position
Returns:
true when the pressing of the pointer was actually handled by this item.
See Also:
this method is used for determining whether the event belongs to this item, for a helper method for determining whether the event took place into the actual content area, Item.handleKeyPressed(int, int), for calculating the horizontal position relative to the content (relX - contentX), for calculating the vertical position relative to the content (relY - contentY)

setStyle

public void setStyle(Style style)
Description copied from class: Item
Sets the style of this item.

Overrides:
setStyle in class Item
Parameters:
style - the new style for this item.

animate

public void animate(long currentTime,
                    ClippingRegion repaintRegion)
Description copied from class: Item
Animates this item. Subclasses can override this method to create animations. The default implementation animates the background and the item view if present.

Overrides:
animate in class Item
Parameters:
currentTime - the current time in milliseconds
repaintRegion - the repaint area that needs to be updated when this item is animated
See Also:
Item.addRelativeToContentRegion(ClippingRegion, int, int, int, int)

addSubCommand

public void addSubCommand(javax.microedition.lcdui.Command childCommand,
                          javax.microedition.lcdui.Command parentCommand)
Adds the given command as a subcommand to the specified parent command.

Parameters:
parentCommand - the parent command
childCommand - the child command
Throws:
java.lang.IllegalStateException - when the parent command has not be added before
See Also:
addSubCommand(Command, Command, Style)

addSubCommand

public void addSubCommand(javax.microedition.lcdui.Command childCommand,
                          javax.microedition.lcdui.Command parentCommand,
                          Style commandStyle)
Adds the given command as a subcommand to the specified parent command.

Parameters:
parentCommand - the parent command
childCommand - the child command
commandStyle - the style for the command
Throws:
java.lang.IllegalStateException - when the parent command has not be added before
See Also:
addSubCommand(Command, Command)

removeAllCommands

public void removeAllCommands()
Removes all commands from this MenuBar. This option is only available when the "menu" fullscreen mode is activated.


getCommandItem

public CommandItem getCommandItem(javax.microedition.lcdui.Command command)
Retrieves the CommandItem used for rendering the specified command.

Parameters:
command - the command
Returns:
the corresponding CommandItem or null when this command is not present in this MenuBar.

getItemAt

public Item getItemAt(int relX,
                      int relY)
Description copied from class: Item
Determines if this item or one of it's children is within the specified point. The default implementation returns this item or this item's label when the position fits.

Overrides:
getItemAt in class Item
Parameters:
relX - the x position of the point relative to this item's left position
relY - the y position of the point relative to this item's top position
Returns:
this item or one of it's children, when the position fits, otherwise null is returned

isOrientationVertical

public boolean isOrientationVertical()
Returns:
true when this menubar should be positioned vertically, e.g. on the right side of the screen

setOrientationVertical

public void setOrientationVertical(boolean isVertical)

getSpaceTop

public int getSpaceTop(int width,
                       int height)

getSpaceLeft

public int getSpaceLeft(int width,
                        int height)

getSpaceRight

public int getSpaceRight(int width,
                         int height)

getSpaceBottom

public int getSpaceBottom(int width,
                          int height)

size

public int size()
Retrieves the number of commands in this menubar.

Returns:
the number of commands in this menubar.

getMenuItemStyle

public Style getMenuItemStyle()

setMenuItemStyle

public void setMenuItemStyle(Style menuItemStyle)

getCommandsContainer

public Container getCommandsContainer()