list.java

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

JAVA
613
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */
package com.funambol.mailclient.ui.view;

import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.loc.LocalizedMessages;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.util.Log;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Screen;

/**
 * generic list class.
 *
 *
 */
abstract class List extends Canvas implements CommandListener {

    /**
     * the current active element
     * i.e. the one that will have the command
     * referred to
     */
    private int activeElement = 0;
    /**
     * the first visible element
     */
    private int firstVisible = 0;
    /**
     * the last visible element
     */
    private int lastVisible = 0;
    /**
     * the current height we've painted element to,
     * the next element should start painting
     * itself from this h
     */
    protected int current_paint_h = 0;
    /**
     * the vector of objects
     */
    protected Vector elements;
    protected String theTitle = "";
    protected Command fireCommand;
    
    protected Ads ad;
    
    public int NEXT_ITEM = 1;
    public int PREVIOUS_ITEM = -1;
   
    public List() {
        this(new Vector());
    }

    /**
     * Creates a new instance of FunCanvasList
     * @param elements the array of elements
     */
    public List(Vector elements) {
        ad = UIController.getAd();
        initCommands();
        addCommands();
        setElements(elements);
    }

    /**
     * paint the element corresponding to given
     * elementId.
     * @return the height of the painted area
     */
    abstract int paintElement(int elementId, Graphics g);

    /**
     * handle the non-navigation keypress, e.g.
     * sending the keypress to the active element
     */
    abstract void handleKey(int keyCode);

    /**
     * paint the title and return the height of the
     * painted region
     */
    abstract int paintTitle(Graphics g);

    /**
     * this method is called by default constructor
     * and should create the commands
     */
    protected abstract void initCommands();

    /**
     * called once by default constructor, should add the
     * general commands (i.e. not the item-specific commands)
     */
    protected void addCommands() {
//#ifdef isBlackberry
//#     //overrides the default blackberry close command
//#     //implementors will need to call super.
//#     addCommand( UIController.blackberryExitCommand );
//#endif
    }

    /**
     * called by setElements when a empty element array is set
     */
    abstract void removeItemCommands();

    /**
     * called by setElements when a non-empty array is set
     */
    abstract void addItemCommands();

    /**
     * this method must paint the background of the canvas
     */
    abstract void paintBackground(Graphics graphics);

    /**
     * @return true if the element identified by elementid can be the active element
     */
    abstract boolean isActivable(int elementId);

    /**
     * @return true if the the element identified by elementid is visible
     */
    abstract boolean isVisible(int elementId);

    /**
     * handle the up / down navigation
     */
    protected void keyPressed(int keyCode) {
        int dir = getGameAction(keyCode);
        // todo: check if we can filter out keypad numbers
        // by checking if keycode is > 0.
        if (isSoftKey(dir, keyCode)) {
        // do nothing here, soft keys must be handled by command actions
        } else if (isDownKey(dir, keyCode)) {//&& getActiveElementId() < elements.length -1 ) {
            getNextItem(NEXT_ITEM);
            //scroll(1);
            repaint();
        } else if (isUpKey(dir, keyCode)) {// && getActiveElementId() > 0 ) {
            getNextItem(PREVIOUS_ITEM);
            //scroll(-1);
            repaint();
        } else if (isFireKey(dir, keyCode)) {
            commandAction(fireCommand, this);
        } else if (keyCode == KEY_STAR) {
            Log.debug("star key!");
            if (ad.isEnabled()) {
              UIController.handleAdClick(ad);
            } else {
                handleKey(keyCode);
            }
        } else {
            handleKey(keyCode);
        }
    }


    public boolean isSoftKey(int dir, int keyCode) {
        // handling soft keys of Motorola devices
        boolean motoSoftKey = false;
        switch (keyCode) {
            // Moto V500 uses positive codes
            case 21:
            case 22:
            case 23:
            // Moto RAZR uses negative codes
            case -21:
            case -22:
            case -23:
                motoSoftKey = true;
        }
        boolean isSoftKey =
                ((dir == 0) && motoSoftKey);

        return isSoftKey;
    }

    private boolean isDownKey(int dir, int keyCode) {
        boolean isDownKey =
                ((dir == Canvas.DOWN) &&
                ((keyCode < 0) || (keyCode == Canvas.DOWN)));

        return isDownKey;
    }

    private boolean isUpKey(int dir, int keyCode) {
        boolean isUpKey =
                ((dir == Canvas.UP) &&
                ((keyCode < 0) || (keyCode == Canvas.UP)));

        return isUpKey;
    }

    private boolean isFireKey(int dir, int keyCode) {
        boolean isFireKey =
                ((dir == Canvas.FIRE) &&
                ((keyCode < 0) || (keyCode == Canvas.FIRE) || (keyCode == 20)));

        return isFireKey;
    }

 

    /**
     * move the active element to the next activable ones
     */
    private int toNextActivable() {

        //  Log.debug("Current Active Element is " + activeElement);

        for (int i = 1; i < elements.size() - getActiveElementId(); i++) {
            // Log.debug("checking element " + i);
            if (isActivable(getActiveElementId() + i)) {
                //   Log.debug("is activable");

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

    /**
     * move the active element to the previous activable ones
     */
    private int toPreviousActivable() {

        // Log.debug("Current Active Element is " + activeElement);

        for (int i = 1; i < getActiveElementId() + 1; i++) {
            //    Log.debug("checking element " + (activeElement -i));
            if (isActivable(getActiveElementId() - i)) {
                //       Log.debug("is activable");
                setActiveElement(getActiveElementId() - i);
                return -i;
            }
        //  Log.debug("is not activable");

        }
        return 0;
    }

    /**
     * change the active element
     * @param amount the number of elements to step forward or back.
     * it can be positive or negative, and the result will be a scroll up
     * or down accordingly to the sign
     */
    protected void scroll(int amount) {

        if (elements == null || elements.size() == 0) {
            return;
        }
        // Log.debug(this, "amount = " + amount);
        int steps = 0;
        if (amount > 0) {
            for (int i = 0; i < amount; i++) {
                toNextActivable();
            }
        } else {
            for (int i = 0; i < -amount; i++) {
                toPreviousActivable();
            }
        }

⌨️ 快捷键说明

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