list.java

来自「moblie syncml mail javame」· Java 代码 · 共 613 行 · 第 1/2 页

JAVA
613
字号


        if (getActiveElementId() > lastVisible) {
            //  Log.debug("activelement >= lastvisible!");
            for (int i = 0; i < amount; i++) {
                toNextVisible();

            }
        }

        if (getActiveElementId() < firstVisible) {
            firstVisible = getActiveElementId();
        }

    }

    /**
     * bind given command to fire event
     *
     * @param command the command to be called when fire button
     * (typically the central button) is pressed. set to null not to trigger
     * any commandaction when fire button is pressed
     *
     */
    protected void setFireCommand(Command command) {
        this.fireCommand = command;
    }

    /**
     *
     * @return the current title
     */
    /*    public String getTitle(){
    //    Log.debug(this, "getTitle called, returning the fake title '" + theTitle + "'");
    return theTitle;
    }
     */
    /**
     *
     * @return the current title
     */
    // Method getTitle() renamed in getFunTitle()
    // for double titlebar bug in Nokia S40
    public String getFunTitle() {
        //    Log.debug(this, "getTitle called, returning the fake title '" + theTitle + "'");
        return theTitle;
    }

    /**
     * paint the title and the elements
     */
    protected void paint(Graphics graphics) {
        paintBackground(graphics);

        int height = getHeight();
        current_paint_h = 0;
        
        int offset = paintAd(graphics);

        current_paint_h += offset;
        if (current_paint_h >= height) {
            return;
        }
        graphics.translate(0, offset);
        current_paint_h += paintTitle(graphics);
        graphics.translate(0, -offset);

        if (elements.size() > 0) {
            int i = firstVisible;
            //Log.debug("Starting from " + i);
            while (current_paint_h < height && i < elements.size()) {
                if (isVisible(i)) {
                    current_paint_h += paintElement(i, graphics);
                }
                i++;
            }
            if (i == elements.size() && current_paint_h <= height) {
                //this means we painted everything!
                //  Log.debug("i==elements.length && current_paint_h < height");
                lastVisible = elements.size() - 1;


            } else {
                // we have to use i-2 because if element firstvisible +2
                // is partially painted, the i output value is firstvisible +3
                // and the last completely visible element is  firstvisible +1
                // so lastvisible is at i-2
                lastVisible = Math.max(firstVisible, i - 2);


            }
        }
    }

    /**
     * handle keyrepeated events
     * @param keycode the keycode of the key
     */
    protected void keyRepeated(int keyCode) {
        //    keyPressed(keyCode);
        int dir = getGameAction(keyCode);
        if (dir == Canvas.DOWN && getActiveElementId() < elements.size() - 1) {
            scroll(1);
            repaint();
        } else if (dir == Canvas.UP && getActiveElementId() > 0) {
            scroll(-1);
            repaint();
        }
    }

    /**
     * set the title to the given title.
     *
     * passing the title does nothing, because we have to ignore it for
     * compatibility issues with motorola devices.
     */
    public void setTitle(String newTitle) {

        //we have to ignore setTitle (null) because some motorola devices
        //call this method when building the canvas
        if (newTitle == null) {
            //      Log.debug("calls to setTitle (null): ignoring it");
            return;
        }
        //   Log.debug(this, "setting fake title to " + newTitle);
        this.theTitle = newTitle;

        // we need this check to be sure we have completed the initialization
        // of the object, if removed the app will crash on some nokias (e.g. 6630)
        if (elements != null) {
            repaint();
        }
    }

    /**
     * set newElements array, calls
     * addItemCommands or removeItemCommands if needed
     * and calls repaint()
     *
     * @param newElements the new object array to be set
     */
    protected void setElements(Vector newElements) {

        if (newElements == null) {
            newElements = new Vector();
        }
        if (elements == null) {
            elements = new Vector();
        }

        if (newElements.size() > 0 && elements.size() == 0) {
            addItemCommands();
        }
        if (newElements.size() == 0 && elements.size() != 0) {
            removeItemCommands();
        }
        this.elements = newElements;
        setActiveElement(Math.min(getActiveElementId(), Math.max(0, elements.size() - 1)));
    }

    /**
     * @return the index of the current active elements
     */
    public int getActiveElementId() {
        return activeElement;
    }

    /**
     * @return the active element
     */
    public Object getActiveElement() {
        if (elements.isEmpty()) {
            return null;
        }
        return elements.elementAt(activeElement);
    }

    /**
     * deactivate the current element and activate the given one
     *
     * @param newActiveElement the index of the new active element
     */
    public void setActiveElement(int newActiveElement) {
        deactivate(activeElement);
        //   Log.debug("Activating element" + newActiveElement);
        activate(newActiveElement);

    }

    /**
     * deactivate given element.
     * default implementation does nothing
     * @param activeElement the element to be deactivated
     */
    protected void deactivate(int activeElement) {
    }

    /**
     * activate the given element, if activable
     * @param newActiveElement the element to be activated
     */
    protected void activate(int newActiveElement) {

        if (elements.size() > 0) {
            newActiveElement = Math.min(newActiveElement, elements.size());
            newActiveElement = Math.max(newActiveElement, 0);
            if (isActivable(newActiveElement)) {
                this.activeElement = newActiveElement;
            }
        }
    /* if (activeElement < firstVisible) {
    firstVisible = activeElement;
    repaint();
    }*/
    }

    /**
     * reset the active element to the first element of the list
     */
    public void rewindList() {
        this.firstVisible = 0;
        this.lastVisible = 0;

        setActiveElement(0);

        //we need this three scroll, the first two set correctly firstvisible
        //and lastvisible, the third go back to the first element of the list.
        scroll(-1);
        scroll(1);
        scroll(-1);

    }

    /**
     * move firstvisible to the next visible element
     */
    private int toNextVisible() {
        for (int i = 1; i < elements.size() - firstVisible; i++) {
            // Log.debug("checking element " + i);
            if (isVisible(firstVisible + i)) {
                //   Log.debug("is activable");

                firstVisible += i;
                return i;
            }
        // Log.debug("is not activable");
        }
        return 0;
    }

    /**
     * Sets the loop list navigation message when
     * up/down navigation is performed
     * @param shift is the way of navigation
     */
    public void getNextItem(int shift) {
        int itemToSet = getActiveElementId() + shift;
        if (itemToSet >= elements.size()) {
            itemToSet = 0;
            rewindList();
            shift = 0;
        } else if (itemToSet < 0) {
            itemToSet = elements.size() - 1;
            shift = elements.size();
        }

        if (Math.abs(shift) > 1) {
            for (int i = 0; i < shift; i++) {
                scroll(NEXT_ITEM);
                repaint();
            }
        } else {
            scroll(shift);
        }
        setActiveElement(itemToSet);

        updateItemContext();
    }
    /*
     * Can be used to add/remove context sensitive items from the command
     * menu. This should be overwritten in implementors.   
     */

    protected void updateItemContext() {

    }

    /**
     * paint ad method
     */
    protected int paintAd(Graphics graphics) {
        int r = ad.paintSmallAd(graphics);
        return r;
    }

      
    
    public void commandAction(Command command, Displayable displayable) {
    //#ifdef isBlackberry
//#        if (command == UIController.blackberryExitCommand) {
//#            UIController.midlet.destroyApp( false );
//#        }
    //#endif
    }
}

⌨️ 快捷键说明

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