bluegammon.gui.menu
Class Menu

java.lang.Object
  extended bybluegammon.gui.menu.Menu
All Implemented Interfaces:
java.lang.Runnable

public class Menu
extends java.lang.Object
implements java.lang.Runnable

The Menu class takes care of coordination between MenuPages, PageItems and user interaction. It contains a navigation stack to handle breadcrumb behaviour. It handles the painting of the menu via a MenuPainter.

The navigation tree can either be implemented in the PageItems constructor, or via an implementation of ItemAction that uses the method gotoPage in this class.

A common example for using a Menu:

public class MenuCanvas extends Canvas
  implements ItemAction
{
  static final int PRINT_1 = 1;
  static final int PRINT_2 = 2;

  Menu m_menu;

  public MenuCanvas()
  {
    // Create pages
    MenuPage mainPage = 
      new MenuPage("STARTPAGE".toCharArray(), null);
    MenuPage nextPage = 
      new MenuPage("NEXTPAGE".toCharArray(), null);

    // Create menu items
    PageItem anItem = 
      new PageItem("Goto next page".toCharArray(), null, null, nextPage);
    PageItem anotherItem =
      new PageItem("Print 1".toCharArray(), null, this, null, PRINT_1);
    PageItem yetAnotherItem =
      new PageItem("Print 2".toCharArray(), null, this, null, PRINT_2);

    // Assign items to pages
    mainPage.addItem(anItem);
    mainPage.addItem(anotherItem);
    nextPage.addItem(yetAnotherItem);

    // Create and setup menu
    m_menu = 
      new Menu(mainPage, this, new DefaultMenuPainter());
    int menuPadding = 16;
    m_menu.setLocation(0, menuPadding);
    m_menu.setDimensions(getWidth(),
      getHeight() - menuPadding * 2);
    m_menu.setFrameData(10, 20);    // 10 frames per transition, 20 ms between each frame
    m_menu.start();
  }

  protected void paint(Graphics g)
  {
    g.setColor(0x000000);
    g.fillRect(0, 0, getWidth(), getHeight());
    m_menu.paint(g);
  }

  protected void keyPressed(int keyCode)
  {
    m_menu.keyPressed(keyCode);
  }

  public void itemAction(MenuPage page, PageItem item)
  {
    int id = item.getId();
    switch(id)
    {
      case PRINT_1:
        System.out.println("Item 1 is invoked");
        break;
      case PRINT_2:
        System.out.println("Item 2 is invoked");
        break;
    }
  } 
}
 

Author:
Peter Andersson

Constructor Summary
Menu(MenuPage startPage, javax.microedition.lcdui.Canvas canvas, MenuPainter painter)
          Creates a new menu.
 
Method Summary
 javax.microedition.lcdui.Canvas getCanvas()
          Returns the canvas this menu is drawn upon.
 MenuPage getCurrentPage()
          Returns current page.
 long getFrameDelay()
          Returns the frame delay in milliseconds.
 int getFrames()
          Returns number of frames in a page switch.
 int getHeight()
          Returns height of this menu.
 MenuListener getListener()
          Returns the listener.
 MenuPainter getPainter()
          Returns the painter used to paint the menu.
 PageItem getSelectedItem()
          Returns selected item or null if no item is currently selected.
 int getSelectedItemIndex()
          Returns index of selected item or -1 if no item is currently selected.
 MenuPage getStartPage()
          Returns start page of this menu.
 int getWidth()
          Returns width of this menu.
 int getX()
          Returns x offset of this menu.
 int getY()
          Returns y offset of this menu.
 void goBack()
          Steps back to previous page.
 void gotoPage(MenuPage newPage)
          Steps forward to a new page.
 void keyPressed(int keyCode)
          Call this from your displayable's keyPressed or keyRepeated method invoke user interaction on the menu.
 void paint(javax.microedition.lcdui.Graphics g)
          Call this from your displayable's paint method to paint the menu.
 void run()
          Runnable implementation, invokes the MenuPainter on transitions.
 void setCanvas(javax.microedition.lcdui.Canvas canvas)
          Sets the canvas this menu is drawn upon.
 void setDimensions(int width, int height)
          Sets the size of this menu.
 void setFrameData(int nbrOfFrames, long frameDelay)
          Sets the values used in a transition between to pages.
 void setListener(MenuListener listener)
          Sets the listener which is reported on menu events.
 void setLocation(int x, int y)
          Sets the location of this menu.
 void setPainter(MenuPainter painter)
          Sets the painter used to paint the menu.
 void setSelectedItemIndex(int itemIndex)
          Sets index of selected item.
 void start()
          Activates this menu and jumps to the starting page.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Menu

public Menu(MenuPage startPage,
            javax.microedition.lcdui.Canvas canvas,
            MenuPainter painter)
Creates a new menu.

Parameters:
startPage - The first page in this menu.
canvas - The canvas that this menu is drawn upon.
painter - The painter used to draw this menu.
Method Detail

start

public void start()
Activates this menu and jumps to the starting page.


paint

public void paint(javax.microedition.lcdui.Graphics g)
Call this from your displayable's paint method to paint the menu.

Parameters:
g - The graphics context.

keyPressed

public void keyPressed(int keyCode)
Call this from your displayable's keyPressed or keyRepeated method invoke user interaction on the menu.

Parameters:
keyCode - The keyCode.

gotoPage

public void gotoPage(MenuPage newPage)
Steps forward to a new page.

Parameters:
newPage - The new page.

goBack

public void goBack()
Steps back to previous page. Does nothing if there are no previous pages.


getCurrentPage

public MenuPage getCurrentPage()
Returns current page. If a transition between pages are ongoing, the destination page of the transition is returned.

Returns:
Current page.

getSelectedItem

public PageItem getSelectedItem()
Returns selected item or null if no item is currently selected.

Returns:
Selected item or null.

getSelectedItemIndex

public int getSelectedItemIndex()
Returns index of selected item or -1 if no item is currently selected.

Returns:
Index of selected item or -1.

setSelectedItemIndex

public void setSelectedItemIndex(int itemIndex)
Sets index of selected item.

Parameters:
itemIndex - The index of the item to select.

getCanvas

public javax.microedition.lcdui.Canvas getCanvas()
Returns the canvas this menu is drawn upon.

Returns:
The canvas drawing this menu.

setCanvas

public void setCanvas(javax.microedition.lcdui.Canvas canvas)
Sets the canvas this menu is drawn upon.

Parameters:
canvas - The canvas drawing this menu.

getListener

public MenuListener getListener()
Returns the listener.

Returns:
The menu listener.

setListener

public void setListener(MenuListener listener)
Sets the listener which is reported on menu events.

Parameters:
listener - A menu listener.

getPainter

public MenuPainter getPainter()
Returns the painter used to paint the menu.

Returns:
The painter.

setPainter

public void setPainter(MenuPainter painter)
Sets the painter used to paint the menu.

Parameters:
painter - The painter.

getFrameDelay

public long getFrameDelay()
Returns the frame delay in milliseconds.

Returns:
The delay between each frame update.

getFrames

public int getFrames()
Returns number of frames in a page switch.

Returns:
Number of frames in a page switch.

getHeight

public int getHeight()
Returns height of this menu.

Returns:
The height.

getStartPage

public MenuPage getStartPage()
Returns start page of this menu.

Returns:
The start page.

getWidth

public int getWidth()
Returns width of this menu.

Returns:
The width.

getX

public int getX()
Returns x offset of this menu.

Returns:
The x offset.

getY

public int getY()
Returns y offset of this menu.

Returns:
The y offset.

setLocation

public void setLocation(int x,
                        int y)
Sets the location of this menu.

Parameters:
x - The x offset.
y - The y offset.

setDimensions

public void setDimensions(int width,
                          int height)
Sets the size of this menu. If width and height are zero, these values will be collected from the canvas that this menu is painted upon.

Parameters:
width - The width.
height - The height.

setFrameData

public void setFrameData(int nbrOfFrames,
                         long frameDelay)
Sets the values used in a transition between to pages. A transition consists of a number of frames with a delay between each frame. A full transition will take nbrOfFrames * (frameDelay + time to paint frame) milliseconds.

Parameters:
nbrOfFrames - Number of frames in a transition.
frameDelay - Delay in milliseconds in each transition.

run

public void run()
Runnable implementation, invokes the MenuPainter on transitions.

Specified by:
run in interface java.lang.Runnable