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

📄 funcanvasmessagelist.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 */


/**
 * FunMessageCanvasList.java
 */

package com.funambol.mailclient.ui.view;

import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Store;
import com.funambol.mail.StoreFactory;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.mm.FolderInfo;
import com.funambol.mailclient.mm.MessageInfo;
import com.funambol.mailclient.mm.MessageManager;
import com.funambol.mailclient.ui.controller.Theme;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.util.Log;
import com.funambol.util.MailDateFormatter;
import java.util.Calendar;
import java.util.Vector;
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;

public class FunCanvasMessageList extends  FunCanvasList implements CommandListener {
    /**
     * string to use when cutting a long string
     */
    private static final String CONTINUE_STRING="..";
    
    /**
     * the margin
     */
    private static final int MARGIN=3;
    
    /**
     * the MessageInfo list
     */
    // private CanvasMessageItem[] messageList;
    
    private Vector messageList;
    
    private static Font font;
    private static Font boldFont;
    private static Font titleFont;
    private static Drawer drawer;
    
    
    
    //private static GraphicalTheme;
    
    
    //-------------------------------------------------------------- Constructor
    
    /**
     * create a new FunCanvasMessageList
     */
    public FunCanvasMessageList(MessageInfo [] 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);
        
    }
    
    
    //------------------------------------------------------------------ Methods
    
    /**
     * set the messageinfo list, and repaint the message list
     */
    public void setMessageInfoList(MessageInfo[] msgInfoList ) {
        setElements(msgInfoList);
        
        initializeMessageItems();
        
        repaint();
    }
    
    /**
     * @return the num of messages
     **/
    public int getNumMessages() {
        return elements.length;
    }
    
    /**
     * @return the selected message info
     */
    public MessageInfo getSelectedMessageInfo() {
        if (getActiveElement()<elements.length && getActiveElement() >=0) {
            return (MessageInfo) elements[getActiveElement()];
        } else {
            return null;
        }
    }
    
    
    
    /**
     *  paint the element
     */
    protected int paintElement(int elementId, Graphics g) {
        //Log.info ("painting element " + elementId);
        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.length;i++){
            messageList.addElement(new CanvasMessageItem(
                    (MessageInfo) elements[i], i));
        }
    }
    /**
     * handle commandaction
     */
    public void commandAction(Command command, Displayable displayable) {
        
        if (command == UIController.backCommand) {
            UIController.showBackScreen();
        }
    }
    /**
     * 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();
        
        // 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.length > 0) {
            s += (getActiveElement() + 1) + "/";
        }
        if ( getDisplayedMessageCount() > 0 ) {
            s += getDisplayedMessageCount();
        } else {
            s =" "+ getDisplayedMessageCount();
        }
        int sWidh = titleFont.stringWidth(s);
        
        String shortTitle = UiUtils.cutString(title,getWidth()-sWidh-1,titleFont);
        drawer.drawTitleBackground(g,getWidth(), titleFont.getHeight());
        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
     * it can be different from the number of elements in the message array
     * because messages can be dinamically loaded or hidden for some reason
     */
    protected int getDisplayedMessageCount() {
        //TODO: use the count of visible element instead of elements.length
        
        return elements.length;
    }
    
    /**
     * 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 the given messageInfo to the messagelist or overwrite the message
     * if exists
     *
     * @param messageInfo the messageinfo to be added
     */
    public void addMessage(MessageInfo messageInfo) {
        //   Log.debug(this, "addMessage (messageinfo)");
        if(elements.length == 0) {
            // This is the first message.
            setMessageInfoList(new MessageInfo[] { messageInfo});
            return;
        }
        // Otherwise, insert the message in the right position.
        
        int pos = search(messageInfo.messageId);
        if (pos < 0 ) {
            // this means we have to add the message
            if (elements.length >= ConfigManager.getMaxMessageNumber()) {
                //we need to remove one element from the list to make room
                //for this new message
                for (int i = 0; i< elements.length; i++) {
                    if (messageInfo.received.getTime() >
                            ((MessageInfo)elements[i]).received.getTime() ) {
                        for(int j=elements.length-1; j>i; j--) {
                            move(j, j-1);
                        }
                        //we insert the new message here
                        assign(i, messageInfo);
                        break;
                    }
                }
            } else {
                // we do not need to remove messages, we have to simply add the
                // new one at the right position
                //#ifdef low_mem
//#                 UIController.freeMemory();      //XXX
                //#endif
                
                MessageInfo[] newMessageInfoList=new MessageInfo[elements.length+1];
                
                //security check
                if (elements.length != messageList.size()) {
                    Log.error("elements length different from message list, " +
                            "reloading from rms");
                    setMessageInfoList(
                            UIController.getSortedMessageInfo(
                            UIController.getFolderInfo(MessageManager.INBOX)
                            ));
                    repaint();
                    return;
                }
                
                // find the right position to add the message to
                int i;
                for (i=0; i<elements.length; i++) {
                    if (messageInfo.received.getTime() >
                            ((MessageInfo)elements[i]).received.getTime() ) {
                        break;
                    }
                    newMessageInfoList[i] = (MessageInfo) elements[i];
                }
                //add the new message
                newMessageInfoList[i] = messageInfo;
                
                if (i < messageList.size() ) {
                    // inseert the element in the middle of the messageList
                    messageList.insertElementAt(
                            new CanvasMessageItem(messageInfo, i), i);
                    
                    for (i++; i < messageList.size() ; i++) {
                        newMessageInfoList[i] = (MessageInfo) elements[i-1];
                        ((CanvasMessageItem)messageList.elementAt(i)).setId(i);
                        
                    }
                } else {
                    // append the element at the end of the messagelist
                    messageList.addElement(
                            new CanvasMessageItem(messageInfo, i));
                }
                
                setElements(newMessageInfoList);
            }
            
        } else {
            //we've found the message, we set it over the old copy
            elements[pos] = messageInfo;
            //we do not need to recalculate short string, the message should
            // be the same except for the flags
            ((CanvasMessageItem)messageList.elementAt(pos)).message = messageInfo;
        }
        //Log.info(">>addMessage: " + Runtime.getRuntime().freeMemory());
        
        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[to] = elements[from];
        
        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 mInfo the messageInfo to be added
     */
    private void assign( int to, MessageInfo mInfo ) {
        elements[to] = mInfo;
        messageList.setElementAt(new CanvasMessageItem(mInfo, to),to);
    }
    
    
    

⌨️ 快捷键说明

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