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

📄 menu.java

📁 j2me写的google地图
💻 JAVA
字号:
// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2008-6-16 11:23:23
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Menu.java

package com.eightmotions.menu;

import java.util.Vector;
import javax.microedition.lcdui.*;

// Referenced classes of package com.eightmotions.menu:
//            MenuItem

public class Menu
{

    public Menu(Canvas inC)
    {
        label = null;
        isOpen = false;
        selecteItemIndex = 0;
        items = new Vector();
        menuFont = null;
        menuFontHeight = 0;
        itemHeight = 0;
        currentCanvas = null;
        subMenu = null;
        alternateCommand = null;
        menuLabel = null;
        currentCanvas = inC;
    }

    public void addCommand(MenuItem inCmd)
    {
        if(alternateCommand == null && inCmd.getPriority() == 1)
            setAlternateCommand(inCmd);
        else
            items.addElement(inCmd);
    }

    public void removeCommand(MenuItem inCmd)
    {
        if(inCmd == alternateCommand)
            alternateCommand = null;
        else
            items.removeElement(inCmd);
    }

    public void setAlternateCommand(MenuItem inCmd)
    {
        if(alternateCommand != null)
            items.addElement(alternateCommand);
        alternateCommand = inCmd;
        label = inCmd.getLabel();
    }

    public void setCommandListener(CommandListener l)
    {
        if(isEnabled)
            listener = l;
        else
            currentCanvas.setCommandListener(l);
    }

    public void paint(Graphics g)
    {
        if(isEnabled)
            paint(g, 0, 0, -1);
    }

    public void drawString(Graphics g, String s, int px, int py, int col1, int col2, int al, 
            boolean back)
    {
        if(s != null)
        {
            if(back)
            {
                int x = px - 2;
                int h = g.getFont().getHeight();
                int size = g.getFont().stringWidth(s);
                g.setColor(0x808080);
                if((al & 8) != 0)
                    x -= size;
                g.fillRoundRect(x - 2, py, size + 6, h, 5, 5);
                g.setColor(0);
                g.drawRoundRect(x - 2, py, size + 6, h, 5, 5);
            }
            g.setColor(col1);
            g.drawString(s, px + 1, py + 1, al);
            g.setColor(col2);
            g.drawString(s, px, py, al);
        }
    }

    public void paint(Graphics g, int level, int startx, int starty)
    {
        if(menuFont == null)
        {
            menuFont = Font.getDefaultFont();
            menuFontHeight = menuFont.getHeight();
            itemHeight = menuFontHeight + 2;
            if(menuLabel != null)
            {
                w = g.getFont().stringWidth(menuLabel) + 10;
                xs = currentCanvas.getWidth() - 10 - w;
            }
            ys = currentCanvas.getHeight() - itemHeight - 4;
        }
        g.setFont(menuFont);
        boolean drawBack = false;
        if(menuBar != null)
        {
            menuBarHeight = menuFontHeight + 2;
            int pos = 0;
            do
            {
                g.drawImage(menuBar, pos, currentCanvas.getHeight() - menuBarHeight - 1, 20);
                pos += menuBar.getWidth();
            } while(pos < currentCanvas.getWidth());
        } else
        {
            menuBarHeight = menuFontHeight + 2;
            drawBack = true;
        }
        String leftCommand = "Menu";
        if(items.size() == 1)
            leftCommand = ((MenuItem)items.elementAt(0)).getLabel();
        else
        if(items.size() == 0)
            leftCommand = "";
        String rightCommand = null;
        if(isOpen)
            rightCommand = "Cancel";
        else
        if(label != null)
            rightCommand = label;
        if(!isLeftMenu)
        {
            String swap = leftCommand;
            leftCommand = rightCommand;
            rightCommand = swap;
        }
        drawString(g, rightCommand, currentCanvas.getWidth() - 8, (currentCanvas.getHeight() - menuBarHeight) + 3, 0x666666, 0xffffff, 24, drawBack);
        drawString(g, leftCommand, 6, (currentCanvas.getHeight() - menuBarHeight) + 3, 0x666666, 0xffffff, 20, drawBack);
        if(isOpen)
        {
            menuHeight = items.size() * itemHeight + 4;
            int y = currentCanvas.getHeight() - 1 - menuHeight - level * 4 - menuBarHeight;
            int x = 0;
            if(isLeftMenu)
            {
                x = startx;
                if(x + menuWidth > currentCanvas.getWidth())
                    x = currentCanvas.getWidth() - menuWidth;
            } else
            {
                x = currentCanvas.getWidth() - 1 - menuWidth - level * 3 - startx;
                if(x < 0)
                    x = 0;
            }
            if(starty != -1 && starty < y)
                y = starty;
            starty -= 20;
            g.setColor(0xffffff);
            g.fillRoundRect(x, y, menuWidth, menuHeight, 2, 2);
            g.setColor(0x666666);
            g.drawRoundRect(x, y, menuWidth, menuHeight, 2, 2);
            g.setColor(0xcccccc);
            g.drawRoundRect(x + 1, y + 1, menuWidth - 2, menuHeight - 2, 2, 2);
            g.drawLine(x - 1, y, x - 1, (y + menuHeight) - 1);
            g.drawLine(x + menuWidth, y, x + menuWidth, (y + menuHeight) - 1);
            g.drawLine(x, y - 1, (x + menuWidth) - 1, y - 1);
            g.drawLine(x, y + menuHeight, (x + menuWidth) - 1, y + menuHeight);
            int foundy = -1;
            y += 2;
            for(int i = 0; i < items.size(); i++)
            {
                MenuItem mi = (MenuItem)items.elementAt(i);
                mi.paint(g, selecteItemIndex == i, x, y, menuWidth);
                if(subMenu == mi.subMenu)
                    foundy = y;
                y += itemHeight;
            }

            if(subMenu != null)
                subMenu.paint(g, level + 1, menuWidth, foundy);
        } else
        if(menuLabel != null)
        {
            g.setColor(0xf0f0f0);
            g.fillRoundRect(xs, ys, w, itemHeight, 4, 4);
            g.setColor(0);
            g.drawString(menuLabel, xs + 4, ys + 1, 20);
            g.setColor(0x808080);
            g.drawRoundRect(xs, ys, w, itemHeight, 4, 4);
        }
    }

    private void doOpen()
    {
        if(items.size() == 1)
        {
            if(listener != null)
                listener.commandAction((Command)items.elementAt(0), currentCanvas);
            return;
        }
        isOpen = true;
        for(int i = 0; i < items.size(); i++)
        {
            MenuItem mi = (MenuItem)items.elementAt(i);
            int width = mi.getWidth() + 8;
            if(menuWidth < width)
                menuWidth = width;
        }

        currentCanvas.repaint();
    }

    public boolean pointerPressed(int x, int y)
    {
        if(!isEnabled)
            return false;
        if(isOpen)
        {
            if(subMenu != null)
            {
                subMenu.pointerPressed(x, y);
            } else
            {
                if(!isLeftMenu)
                    x = currentCanvas.getWidth() - x;
                int py = currentCanvas.getHeight() - menuHeight - menuBarHeight;
                if(x < menuWidth && y > py)
                {
                    for(int i = 0; i < items.size(); i++)
                    {
                        MenuItem mi = (MenuItem)items.elementAt(i);
                        if(py + itemHeight >= y)
                        {
                            selecteItemIndex = i;
                            doAction();
                            return true;
                        }
                        py += itemHeight;
                    }

                } else
                {
                    isOpen = false;
                    return false;
                }
            }
        } else
        if(y > ys)
        {
            if(isLeftMenu && x < currentCanvas.getWidth() / 2 || !isLeftMenu && x > currentCanvas.getWidth() / 2)
            {
                doOpen();
                currentCanvas.repaint();
                return true;
            }
            if(listener != null && alternateCommand != null)
                listener.commandAction(alternateCommand, currentCanvas);
        }
        return false;
    }

    private void doAction()
    {
        MenuItem i = (MenuItem)items.elementAt(selecteItemIndex);
        if(i.subMenu != null)
        {
            subMenu = i.subMenu;
            subMenu.listener = listener;
            subMenu.doOpen();
        } else
        {
            isOpen = false;
        }
        if(listener != null)
            listener.commandAction(i, currentCanvas);
    }

    public boolean keyPressed(int inKey, int inAction)
    {
        if(inKey == KEY_LEFTSOFTKEY || inKey == KEY_RIGHTSOFTKEY)
            inAction = 0;
        if(inAction == 1 || inAction == 6 || inAction == 2 || inAction == 5 || inAction == 8)
            inKey = 0;
        inKey = checkCode(inKey);
        if(!isEnabled)
            return false;
        if(isOpen)
        {
            if(subMenu != null)
            {
                subMenu.keyPressed(inKey, inAction);
                if(subMenu != null && !subMenu.isOpen)
                {
                    subMenu = null;
                    if(inAction == 8)
                        isOpen = false;
                }
                return true;
            }
            switch(inAction)
            {
            case 1: // '\001'
                selecteItemIndex = (items.size() + (selecteItemIndex - 1)) % items.size();
                currentCanvas.repaint();
                break;

            case 6: // '\006'
                selecteItemIndex = (items.size() + (selecteItemIndex + 1)) % items.size();
                currentCanvas.repaint();
                break;

            case 5: // '\005'
            case 8: // '\b'
                doAction();
                currentCanvas.repaint();
                break;

            case 2: // '\002'
                isOpen = false;
                currentCanvas.repaint();
                break;
            }
            if(inKey == KEY_RIGHTSOFTKEY || inKey == KEY_LEFTSOFTKEY || inKey == KEY_CLEAR)
            {
                isOpen = false;
                currentCanvas.repaint();
            }
            return true;
        }
        if(inKey == KEY_LEFTSOFTKEY)
        {
            if(isLeftMenu)
                doOpen();
            else
            if(alternateCommand != null && listener != null)
                listener.commandAction(alternateCommand, currentCanvas);
        } else
        {
            if(inKey == KEY_RIGHTSOFTKEY)
            {
                if(!isLeftMenu)
                    doOpen();
                else
                if(alternateCommand != null && listener != null)
                    listener.commandAction(alternateCommand, currentCanvas);
                return true;
            }
            if(inKey == KEY_CLEAR || inKey == KEY_BACK)
            {
                isOpen = false;
                currentCanvas.repaint();
                return true;
            }
        }
        return false;
    }

    public static int checkCode(int keycode)
    {
        int res = keycode;
        if(res == -21 || res == -22 && KEY_LEFTSOFTKEY != -21)
        {
            KEY_LEFTSOFTKEY = -21;
            KEY_RIGHTSOFTKEY = -22;
        }
        return res;
    }

    public static int KEY_LEFTSOFTKEY = -6;
    public static int KEY_RIGHTSOFTKEY = -7;
    public static int KEY_CLEAR = -8;
    public static int KEY_BACK = -11;
    String label;
    public boolean isOpen;
    int selecteItemIndex;
    public Vector items;
    Font menuFont;
    int menuFontHeight;
    int itemHeight;
    public Canvas currentCanvas;
    Menu subMenu;
    MenuItem alternateCommand;
    public CommandListener listener;
    int menuWidth;
    int menuHeight;
    int xs;
    int ys;
    int w;
    String menuLabel;
    public static Image menuBar = null;
    public static boolean isLeftMenu = true;
    public static int menuBarHeight = 0;
    public static boolean isEnabled = true;

}

⌨️ 快捷键说明

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