/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.pabloid.ui; import java.util.Enumeration; import java.util.Vector; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Choice; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import org.pabloid.util.ArrayList; import org.pabloid.util.List; /** * @author pabloid */ public class PList extends Canvas implements Choice { public static final int STATE_MENU = 1; public static final int STATE_NORMAL = 0; public static final int MODEL_SIEMENS = 0; public static final int MODEL_NOKIA = 1; public static final int MODEL_SONYERICSSON = 2; public static final int MODEL_MOTOROLA = 3; public static final int MODEL_SAMSUNG = 4; public static final int MODEL_ALCATEL = 5; public static final Command SELECT_COMMAND = javax.microedition.lcdui.List.SELECT_COMMAND; private int currentMode = 0; private int w, h; private Vector elements, images; private int index = 0, scroll = 0; private Font fnt = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL); private int ch = fnt.getHeight() + 2; private Command backCommand, selectCommand = SELECT_COMMAND; private Vector vCommands; private CommandListener commandListener; private int menuFocus; private int menuX, menuY, menuW, menuH; private int leftSoft, rightSoft; private String title = ""; /** * constructor */ public PList(String title, int mode) { this(); this.title = title; } public PList() { setFullScreenMode(true); w = getWidth(); h = getHeight() - ch; elements = new Vector(); images = new Vector(); vCommands = new Vector(); getSofts(); } private static boolean classExists(String name) { try { Class.forName(name); return true; } catch (ClassNotFoundException e) { return false; } catch (Throwable t) { return false; } } public final void getSofts() { int model = -1; if (classExists("com.siemens.mp.io.File")) model = MODEL_SIEMENS; if (classExists("com.nokia.mid.ui.FullCanvas")) model = MODEL_NOKIA; if (classExists("com.mascotcapsule.micro3d.v3.ActionTable")) model = MODEL_SONYERICSSON; if (classExists("com.alcatelonetouchx.CanvasPlus")) model = MODEL_ALCATEL; if (classExists("com.motorola.io.ConnectorEvent")) model = MODEL_MOTOROLA; if (classExists("com.samsung.util.LCDLight")) model = MODEL_SAMSUNG; if (classExists("com.siemens.mp.io.file.FileConnection") && !classExists("javax.microedition.io.file.FileConnection")) model = MODEL_SIEMENS; if (classExists("com.siemens.mp.game.Light") && classExists("javax.microedition.io.file.FileConnection")) model = MODEL_SIEMENS; switch (model) { case MODEL_SIEMENS: leftSoft = -1; rightSoft = -4; break; case MODEL_NOKIA: case MODEL_SONYERICSSON: case MODEL_SAMSUNG: case -1: leftSoft = -6; rightSoft = -7; break; default: leftSoft = '*'; rightSoft = '#'; break; } } public void setSelectCommand(Command cmd) { selectCommand = cmd; repaint(); } public void setCommandListener(CommandListener listener) { commandListener = listener; } public void addCommand(Command c) { if (c == null) { vCommands.addElement(null); repaint(); return; } if (!vCommands.contains(c)) { if (c.getCommandType() == Command.BACK) backCommand = c; vCommands.addElement(c); if (menuW - 4 < fnt.stringWidth(c.getLabel())) { menuW = 4 + fnt.stringWidth(c.getLabel()); menuX = getWidth() - menuW; } menuH = 4 + vCommands.size() * (ch); menuY = h - ch - menuH; repaint(); } menuW += fnt.stringWidth(String.valueOf(vCommands.size())); menuX -= fnt.stringWidth(String.valueOf(vCommands.size())); } public void showMenu(boolean bShow) { if (bShow && !vCommands.isEmpty()) currentMode = STATE_MENU; else currentMode = STATE_NORMAL; menuFocus = 0; repaint(); } /** * paint */ public void paint(Graphics g) { g.setFont(fnt); g.setColor(230, 255, 230); g.fillRect(0, 0, w, h); g.translate(0, ch); for (int i = scroll; i < scroll + h / ch - 1 && i < elements.size(); i++) { if (i == index) { g.setColor(40, 160, 40); g.fillRect(0, (i - scroll) * ch, w, ch); g.setColor(220, 255, 220); } else g.setColor(20, 40, 20); int wd = 2; if (getImage(i) != null) { wd = getImage(i).getWidth() + 4; g.drawImage(getImage(i), wd / 2 + 1, (i - scroll) * ch + ch / 2, Graphics.HCENTER | Graphics.VCENTER); } if (getString(i) != null || getImage(i) != null) g.drawString(getString(i), wd, (i - scroll) * ch, 20); else g.drawLine(0, (i - scroll) * ch+ch/2, w, (i - scroll) * ch+ch/2); } g.setColor(20, 80, 20); g.fillRect(0, h - ch, w, ch); g.setColor(220, 255, 220); g.drawString("Menu", w - 1, h - 1, Graphics.RIGHT | Graphics.BOTTOM); g.drawString(selectCommand.getLabel(), 1, getHeight() - 1, Graphics.LEFT | Graphics.BOTTOM); if (currentMode == STATE_MENU) { g.setColor(200, 255, 200); g.fillRect(menuX, menuY, menuW, menuH); g.setColor(50, 100, 50); g.drawRect(menuX, menuY, menuW, menuH); g.setColor(80, 120, 80); g.fillRect(menuX + 1, menuY + 2 + (ch) * menuFocus, menuW - 2, ch); int y = menuY + 2; g.setColor(0); int i = 0; for (Enumeration en = vCommands.elements(); en.hasMoreElements();) { Command cmd = (Command) en.nextElement(); if (cmd == null) { if (en.hasMoreElements()) g.drawLine(menuX, y + ch / 2, h, y + ch / 2); } else { String s = cmd.getLabel(); g.drawString(String.valueOf(++i), menuX + 1, y, Graphics.LEFT | Graphics.TOP); g.drawString(s, menuX + 2 + fnt.stringWidth(String.valueOf(vCommands.size())), y, Graphics.LEFT | Graphics.TOP); } y += ch; } } g.setColor(20, 80, 20); g.drawLine(0, 0, w, 0); g.setColor(230, 255, 230); g.fillRect(0, -ch, w, ch); g.setColor(20, 80, 20); g.drawString(title, w / 2, -2, Graphics.BOTTOM | Graphics.HCENTER); } protected void sizeChanged(int w, int h) { super.sizeChanged(w, h); this.w = w; this.h = h - ch; } public int getSelectedIndex() { return index; } public String getString(int i) { Object o = elements.elementAt(i); if (o != null) return o.toString().intern(); else return null; } public void removeCommand(Command cmd) { vCommands.removeElement(cmd); repaint(); } public int append(String str, Image img) { elements.addElement(str); if (img != null && img.getHeight() > ch) { double cof = (double) img.getHeight() / (double) ch; int nw = (int) (img.getWidth() / cof); images.addElement(ImageUtils.resize(img, nw, ch)); } else images.addElement(img); repaint(); return elements.size() - 1; } public void delete(int i) { elements.removeElementAt(i); images.removeElementAt(i); repaint(); } /** * Called when a key is pressed. */ protected void keyPressed(int key) { int gameAction = getGameAction(key); if (currentMode == STATE_MENU) { if (key == rightSoft) showMenu(false); else if (gameAction == Canvas.LEFT) showMenu(false); else if (gameAction == Canvas.RIGHT) { if (commandListener != null) commandListener.commandAction((Command) vCommands.elementAt(menuFocus), this); showMenu(false); } else if (gameAction == Canvas.UP) { menuFocus--; if (menuFocus < 0) menuFocus = vCommands.size() - 1; while (vCommands.elementAt(menuFocus) == null) { menuFocus--; if (menuFocus < 0) menuFocus = vCommands.size() - 1; } } else if (gameAction == Canvas.DOWN) { menuFocus++; if (menuFocus >= vCommands.size()) menuFocus = 0; while (vCommands.elementAt(menuFocus) == null) { menuFocus++; if (menuFocus >= vCommands.size()) menuFocus = 0; } } else if (gameAction == Canvas.FIRE) { if (commandListener != null) commandListener.commandAction((Command) vCommands.elementAt(menuFocus), this); showMenu(false); } } else if (key == rightSoft) showMenu(true); else if (gameAction == DOWN) moveDown(); else if (gameAction == UP) moveUp(); else if (gameAction == FIRE || key == leftSoft) { if (commandListener != null) commandListener.commandAction(selectCommand, this); } else if (key == -11) if (commandListener != null) commandListener.commandAction(backCommand, this); repaint(); } public void append(String str) { append(str, null); } public void deleteAll() { scroll = 0; index = 0; elements.removeAllElements(); images.removeAllElements(); repaint(); } public void setTitle(String title) { this.title = title; repaint(); } private void moveDown() { if (++index >= scroll + h / ch - 1 || index >= elements.size()) { if (index >= elements.size()) index = 0; if (scroll + h / ch - 1 >= elements.size()) { scroll = 0; index = 0; } else scroll++; } if (getString(index) == null && getImage(index) == null) moveDown(); repaint(); } private void moveUp() { if (--index < scroll) if (scroll == 0) { if (h / ch - 1 < elements.size()) scroll = elements.size() - h / ch + 1; index = elements.size() - 1; } else scroll--; if (getString(index) == null && getImage(index) == null) moveUp(); repaint(); } /** * Called when a key is released. */ protected void keyReleased(int keyCode) { } /** * Called when a key is repeated (held down). */ protected void keyRepeated(int keyCode) { keyPressed(keyCode); } /** * Called when the pointer is dragged. */ protected void pointerDragged(int x, int y) { } /** * Called when the pointer is pressed. */ protected void pointerPressed(int x, int y) { } /** * Called when the pointer is released. */ protected void pointerReleased(int x, int y) { } public int getFitPolicy() { return 0; } public Font getFont(int elementNum) { return fnt; } public Image getImage(int elementNum) { return (Image) images.elementAt(elementNum); } public int getSelectedFlags(boolean[] selectedArray_return) { selectedArray_return = new boolean[size()]; selectedArray_return[index] = true; return 1; } public void insert(int elementNum, String stringPart, Image imagePart) { elements.insertElementAt(stringPart, elementNum); images.insertElementAt(imagePart, elementNum); } public void insert(int elementNum, String stringPart) { insert(elementNum, stringPart); } public boolean isSelected(int elementNum) { return index == elementNum; } public void set(int elementNum, String stringPart, Image imagePart) { elements.setElementAt(stringPart, elementNum); images.setElementAt(imagePart, elementNum); } public void set(int elementNum, String stringPart) { set(elementNum, stringPart, null); } public void setFitPolicy(int fitPolicy) { } public void setFont(int elementNum, Font font) { } public void setSelectedFlags(boolean[] selectedArray) { } public void setSelectedIndex(int elementNum, boolean selected) { if (selected) { index = elementNum; normalizeScroll(); } } public int size() { return elements.size(); } private void normalizeScroll() { scroll = index - h / ch / 2; if (scroll >= size() - h / ch) scroll = size() - h / ch - 1; if (scroll < 0) scroll = 0; } }