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

📄 menuelementcontainer.java

📁 国外的j2me播放器软件
💻 JAVA
字号:
package no.auc.one.portableplayer.userinterface;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import org.apache.log4j.Logger;

public class MenuElementContainer extends MenuElementBase {
    private static Logger LOG = Logger.getLogger("MENU");

    public static final int NAVIGATE_DIRECTION_LEFT  = 0;
    public static final int NAVIGATE_DIRECTION_RIGHT = 1;
    
	protected Vector children = new Vector();
    protected MenuElementContainer parent = null;
    protected int topIndex = 0;
    protected int markerPosition = 0;
    protected int currentChildIndex = 0;

    public MenuElementContainer(String name) {
        super(name);
    }

    public MenuElementContainer(
        String name, 
        MenuEventListener listener) 
    {
        super(name, listener);
    }
    
    public MenuElementContainer(
        String name,
        MenuEventListener listener,
        MenuElementBase[] children) 
    {
        super(name, listener);

        addChild(children);
    }
    
    public String toString() {
        if (children.size() > 0) {
            return name + " ->";
        } else {
            return name + " (empty)";
        }
    }

    public MenuElementContainer getParent() {
        return parent;
    }

    public void setParent(MenuElementContainer newParent) {
        parent = newParent;
    }
    
    public int getMarkerPosition() {
        return markerPosition;
    }

    public int indexOfChild(MenuElementBase child) {
        return children.indexOf(child);
    }
    
	public MenuElementBase childAt(int index) {
        if (index < 0 || index > children.size()) {
            throw new IllegalArgumentException(
                "Index out of range. Allowed range is: [0, " + 
                children.size() + "]");
        }

		MenuElementBase me = (MenuElementBase)children.elementAt(
            currentChildIndex);

        return me;
	}
    
	public MenuElementBase[] children() {
        MenuElementBase[] me = new MenuElementBase[children.size()];

        for(int i = 0; i < children.size(); i++) {
            me[i] = (MenuElementBase)children.elementAt(i);
        }

        return me;
    }
	
    public int childrenLength() {
        return children.size();
    }
    
    public void addChild(MenuElementBase child) {
        if (child instanceof MenuElementContainer) {
            ((MenuElementContainer)child).setParent(this);
        }
        
        children.addElement(child);
    }

    public void addChild(MenuElementBase[] kids) {
        for(int i = 0; i < kids.length; i++) {
            addChild(kids[i]);
        }
    }

    public void addChildAt(int index, MenuElementBase child) 
        throws ArrayIndexOutOfBoundsException 
    {
        if (child instanceof MenuElementContainer) {
            ((MenuElementContainer)child).setParent(this);
        }
        
        children.insertElementAt(child, index);
    }

    public void removeChild(int index) {
        children.removeElementAt(index);
    }

    public void removeChild(MenuElementBase me) {
        children.removeElement(me);
    }

    public void removeAllChildren() {
        children.removeAllElements();
        topIndex = 0;
        markerPosition = 0;
        currentChildIndex = 0;        
    }

    public void paint(Graphics g) {
        g.drawString(
            toString(),
            g.getClipX(),
            g.getClipY(),
            Graphics.TOP | Graphics.LEFT);
    }

    /**
     * Paint all children which fits into the current view.
     *
     * Draws all child items which are supposed to be displayed now.
     *
     * @param g Graphics to draw on. The clipping is set by the @see Menu 
     *          class to where it is allowed to draw upon.
     */
    public void paintChildren(Graphics g) {        
        int maxCurrentMenuElements = 
            UI.getInstance().getMainMenu().getMaxCurrentMenuElements();

        //
        // First paint the 'upper' menu items, which is the range
        // [topIndex, sizeof(children)]
        // 
        int upperElementsAdded = 0;
        //int lowerElementsAdded = 0;
        
        for (int i = topIndex; 
             i < children.size() && 
             upperElementsAdded < maxCurrentMenuElements;
             i++, upperElementsAdded++) 
        {
            g.setClip(
                10,
                10 + upperElementsAdded * Menu.menuElementHeight,
                g.getClipWidth(),
                Menu.menuElementHeight);
            ((MenuElementBase)children.elementAt(i)).paint(g);
        }

/*

   Do not draw lower menu elements currently.
   
        // System.out.println("Upper menu items drawn: " + upperElementsAdded);

        //
        // If space permits, draw the 'lower' menu items too, which is the set:
        // [0, topIndex]
        // 
        if (upperElementsAdded < children.size() && 
            upperElementsAdded < maxCurrentMenuElements) 
        {
            for (int i = 0; 
                 i < maxCurrentMenuElements - upperElementsAdded;
                 i++, lowerElementsAdded++)
            {
                g.setClip(
                    10,
                    10 + (upperElementsAdded + lowerElementsAdded) * 
                    Menu.menuElementHeight,
                    g.getClipWidth(),
                    Menu.menuElementHeight);
                ((MenuElementBase)children.elementAt(i)).paint(g);
            }
            
            /*
            System.out.println(
                "Lower menu items drawn: " + lowerElementsAdded);
            *x/
        }
*/
    }

    public void invokeAction() {
        if (children.size() > 0) {
            MenuElementBase me = (MenuElementBase)children.elementAt(
                currentChildIndex);
            me.invokeAction();
        }
    }

    public void navigateLeft() {
        
        boolean childOk = true;
        
        if (children.size() > 0) {
            MenuElementBase me = (MenuElementBase)children.elementAt(
                currentChildIndex);

            childOk = me.prepareNavigateLeft();
        }
                
        if (childOk && 
            parent != null &&
            parent.prepareNavigateLeft()) 
        {
            //
            // XXX Not good to couple UI + Menu + this class :/
            // 
            UI.getInstance().getMainMenu().setCurrentContainer(parent);
        } 
    }

    public void navigateRight() {
        if (children.size() > 0) {
            if (children.elementAt(currentChildIndex)
                instanceof MenuElementContainer)
            {
                System.out.println("Current child is a container");
                MenuElementContainer mec = 
                    (MenuElementContainer)children.elementAt(
                        currentChildIndex);
            
                if (mec.prepareNavigateRight()) {
                    //
                    // XXX Not good to couple UI + Menu + this class :/
                    // 
                    UI.getInstance().getMainMenu().setCurrentContainer(mec);
                }
            } else {
                System.out.println("Current child is a menu element");
                MenuElement me = (MenuElement)children.elementAt(
                    currentChildIndex);
                me.navigateRight();
            }
        }
    }

    public void navigateUp() {
        markerPosition--;
        
        if (markerPosition < 0) {
            if (topIndex > 0) {
                topIndex--;
            } else {
                topIndex = 0; 
            }
            
            markerPosition = 0;
        }
        
        recalculateCurrentChildIndex();
    }

    public void navigateDown() {
        int maxCurrentMenuElements = 
            UI.getInstance().getMainMenu().getMaxCurrentMenuElements();
        
        markerPosition++;
        
        if (markerPosition >= children.size() - 1) {
            markerPosition = children.size() - 1;
        }

        if (markerPosition >= maxCurrentMenuElements) {
            topIndex++;
            markerPosition = maxCurrentMenuElements - 1;

            if (topIndex > children.size()) {
                topIndex = 1;
            }
        }

        recalculateCurrentChildIndex();
    }

    public boolean prepareNavigateLeft() {
        return true;
    }

    public boolean prepareNavigateRight() {
        return true;
    }

    protected void recalculateCurrentChildIndex() {
        currentChildIndex = (topIndex + markerPosition) % children.size();
    }
}

⌨️ 快捷键说明

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