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

📄 messagelist.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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".
 *
 *
 */
/**
 * MessageList.java
 */
package com.funambol.mailclient.ui.view;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import com.funambol.mail.Address;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Store;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.ui.controller.Theme;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.push.CTPService;

public class MessageList extends List implements CommandListener {

    /**
     * string to use when cutting a long string
     */
    private static final String CONTINUE_STRING = "..";
    /**
     * the margin
     */
    private static final int MARGIN = 3;
    /**
     * constant for setting no minimal displayed message count
     */
    public static final int COUNT_NONE = -1;
    /**
     * the Message list
     */
    private Vector messageList;
    private static Font font;
    private static Font boldFont;
    private static Font titleFont;
    private static Drawer drawer;
    private int minimumDisplayedMessageCount = -1;
    public int screenWidth;

    //private static GraphicalTheme;
    //-------------------------------------------------------------- Constructor
    /**
     * create a new MessageList
     */
    public MessageList(Vector elements) {
        super(elements);
        this.drawer = UIController.getDrawer();
        this.font = drawer.getGraphicalTheme().getDefaultFont();
        this.boldFont = drawer.getGraphicalTheme().getBoldFont();
        this.titleFont = drawer.getGraphicalTheme().getTitleFont();
        this.messageList = new Vector(ConfigManager.getMaxMessageNumber());
        resetTitle();
        initializeMessageItems();
        setCommandListener(this);
        screenWidth = getWidth();
    }

    //------------------------------------------------------------------ Methods
    /**
     * set the message list, and repaint the message list
     */
    public void setMessageList(Vector msgList) {

        setElements(msgList);
        initializeMessageItems();
        repaint();
    }

    /**
     * @return the num of messages
     **/
    public int getNumMessages() {
        return elements.size();
    }

    /**
     * set the minimum displayed message count.
     * if set to COUNT_NONE the number of elements in the message
     * list is displayed
     *
     * @param i the minimum message count to be returned by
     * getDisplayedMessageCount. i must be >=0 or COUNT_NONE
     *
     */
    public void setMinimumDisplayedMessageCount(int i) {
        this.minimumDisplayedMessageCount = i < 0 ? COUNT_NONE : i;
    }

    /**
     * @return the selected message info
     */
    public Message getSelectedMessage() {
        if (getActiveElementId() < elements.size() && getActiveElementId() >= 0) {
            return (Message) elements.elementAt(getActiveElementId());
        } else {
            return null;
        }
    }

    /**
     *  paint the element
     */
    protected int paintElement(int elementId, Graphics g) {

        // this should not happen, but shomehow it happens on some
        // nokias (like 6111)
        if (elementId >= messageList.size()) {
            // skipping this
//            Log.info("Avoiding ArrayIndexOutOfBounds for element" + elementId);
            return 0;
        }


        g.translate(0, current_paint_h);

        int h = ((CanvasMessageItem) messageList.elementAt(elementId)).paint(g);

        g.translate(0, -current_paint_h);

        return h;
    }

    /**
     * create the messagelist from the array of elements
     */
    private void initializeMessageItems() {
        messageList.removeAllElements();
        //messageList.en
        for (int i = 0; i < elements.size(); i++) {
            messageList.addElement(new CanvasMessageItem(
                    (Message) elements.elementAt(i), i));
        }
    }

    /**
     * handle commandaction
     */
    public void commandAction(Command command, Displayable displayable) {

        if (command == UIController.backCommand) {
            UIController.showBackScreen();
        } else {
            super.commandAction(command, displayable);
        }
    }

    /**
     * paint the title and return the height of the painted area
     */
    public int paintTitle(Graphics g) {

        //   Log.debug("paint title!");

        String s;
        //String title = getTitle();

        // Use method getFunTitle instead of getTitle
        // for double titlebar in Nokia S40
        String title = getFunTitle();

        // keep this because otherwhise some motorola phones may throw a
        // nullpointer exception
        if (title == null) {
            //Log.debug("title null... setting to empty string");
            title = "";
        }
        s = " ";
        if (elements.size() > 0) {
            s += (getActiveElementId() + 1) + "/";
        }
        if (this.getDisplayedMessageCount() > 0) {
            s += this.getDisplayedMessageCount();
        } else {
            s = " " + this.getDisplayedMessageCount();
        }
        int sWidh = titleFont.stringWidth(s);

        String shortTitle = UiUtils.cutString(title, getWidth() - sWidh - 1, titleFont);
        drawer.drawTitleBackground(g, getWidth(), titleFont.getHeight());

        //-------- TODO remove this once basic testing is done
       /*
        
        if (CTPService.getInstance().isPushActive() ) {
        g.setColor(0x00dd00);
        } else if (CTPService.getInstance().getState() != 0) {
          g.setColor(0xFFFF00);
        } else {
        g.setColor(0xdd0000);    
        }
        g.fillRect(0, titleFont.getHeight()-3 , getWidth(), 3);
         */

        //-------- End of test code

        g.setFont(titleFont);
        g.setColor(drawer.getGraphicalTheme().getTitleColor());
        //not using drawTitle because we need to draw also the number of messages
        g.drawString(shortTitle, 1, 1, Graphics.TOP | Graphics.LEFT);
        g.drawString(s, getWidth(), 0, Graphics.TOP | Graphics.RIGHT);
        return drawer.getGraphicalTheme().getTitleFont().getHeight();
    }

    /**
     * this method return the number of messages as shown in the title bar
     * if setminimumdisplayedmessagecount has been called, the returned
     * value is the max between minimumumDisplayedMessageCount (that can be
     * set using the setMinimumDisplayedMessageCount method) and the size of 
     * the message vector
     */
    public int getDisplayedMessageCount() {
        // ternary operator
        return minimumDisplayedMessageCount < elements.size() ? elements.size()
                : minimumDisplayedMessageCount;


    }

    /**
     * return the default title
     */
    public String getDefaultTitle() {
        return "";
    }

    /**
     * reset the title to the default title
     */
    public void resetTitle() {
        //    Log.debug(this, "resetting title to " + getDefaultTitle());
        setTitle(this.getDefaultTitle());
    }

    /**
     * Add a message to the current list of messages
     * @param message the message to be added
     */
    public void addMessage(Message message) {

        if (elements.size() == 0) {
            // This is the first message, we can use setMessageList
            Vector v = new Vector();
            v.addElement(message);
            setMessageList(v);
            //repaint();
            return;
        }

        int pos = search(message.getKey());

        if (pos >= 0) {
            // we overwrite previous messageinfo and canvasmessageitem's message
            ((CanvasMessageItem) messageList.elementAt(pos)).message = message;
            elements.setElementAt(message, pos);
            repaint();
            return;
        }


        // if we're here we need to add the message to the list in the
        // right position

        int i = 0;
        while (i < elements.size() && (message.getReceivedTime() <
                ((Message) elements.elementAt(i)).getReceivedTime())) {
            i++;
        }

        // we've found the right position.
        if (i == elements.size()) {
            //it's an append
            elements.addElement(message);
            messageList.addElement(new CanvasMessageItem(message, i));
        } else {
            //it's an insert
            elements.insertElementAt(message, i);
            messageList.insertElementAt(new CanvasMessageItem(message, i), i);

            // we change the id of each following item
            for (int j = i + 1; j < messageList.size(); j++) {
                //messageList.setElementAt(elements.elementAt(j-1),j);
                ((CanvasMessageItem) messageList.elementAt(j)).setId(j);
            }
        }

        // if we are showing too many elements, we remove them

        /*   while (elements.size() > ConfigManager.getMaxMessageNumber() ) {
        messageList.removeElementAt(elements.size()-1);
        elements.removeElementAt(elements.size()-1);
        }*/
        repaint();
    }

    /**
     * copy the objects in elements and messageList from the given position to
     * the given position
     * @param to the position to copy the objects to
     * @param from the position to copy the objects from
     */
    private void move(int to, int from) {

        elements.setElementAt(elements.elementAt(from), to);

        CanvasMessageItem mItem = (CanvasMessageItem) messageList.elementAt(from);
        mItem.setId(to);
        messageList.setElementAt(mItem, to);
    //messageList.elementAt()[to].setId(to);


    }

    /**
     * put the given messageinfo in the right position in
     * elements and messagelist
     *
     *@param to the position where to put the messageinfo
     *@param msg the message to be added
     */
    private void assign(int to, Message msg) {
        elements.setElementAt(msg, to);
        messageList.setElementAt(new CanvasMessageItem(msg, to), to);
    }

    /**
     * @return the index of the messageinfo with the same messagid as
     * the given messageinfo, or -1 if not found.
     */
    public int search(String msgId) {
        int position = -1;
        for (int i = 0; i < elements.size(); i++) {
            if (getMessageAt(i).getKey().equals(msgId)) {
                position = i;
                break;
            }
        }
        return position;

⌨️ 快捷键说明

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