viewmessage.java

来自「moblie syncml mail javame」· Java 代码 · 共 1,271 行 · 第 1/4 页

JAVA
1,271
字号
/*
 * 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".
 *
 */

/*
 * ViewMessage.java
 */
package com.funambol.mailclient.ui.view;

import com.funambol.mail.Address;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.RMSStore;
import com.funambol.mail.Store;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.rms.RMSContactManager;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.sm.SyncClient;
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.ChunkedString;
import com.funambol.util.Log;
import com.funambol.util.MailDateFormatter;
import com.funambol.util.StringUtil;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
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;

public class ViewMessage extends Canvas implements CommandListener {

    /**
     * the message
     */
    private Message message;
    private Drawer drawer;
    private GraphicalTheme theme;
    private Font bodyFont;
    private static final int SCROLL_AMOUNT = 2;
    private static final int MARGIN = 2;
    private static final int SCROLLBAR_WIDTH = 4;
    /**
     * are we inside the header?
     */
    private boolean insideHeader = true;
    /**
     * are we inside the body?
     */
    private boolean insideBody = false;
    private BodyItem bodyItem;
    private boolean showHeader = true;
    private HeaderItem headerItem;
    private Command deleteCommand;
    private Command forwardCommand;
    private Command linksCommand;
    //private Command numbersCommand;
    private Command fullMessageCommand;
    private Command recipientsCommand;
    private Command replyCommand;
    private Command replyAllCommand;
    private Command saveSenderCommand;
    private Command markAsReadCommand;
    private Command followUpCommand;
    private Command editCommand;
    private Command sendCommand;
    private Command callCommand;
    private Command attachList;
    private Contact contactToCall;
    private int NEXT_MESSAGE = 1;
    private int PREVIOUS_MESSAGE = -1;
    private String theTitle = Localization.getMessages().MESSAGE_VIEW_FORM_TITLE;
    private boolean fullMsgPerformed;

    private Ads ad;
    
    /**
     * creates a new FunCanvasViewMessage with given message
     */
    public ViewMessage(Message message) {
        ad = UIController.getAd();
        this.message = message;
        this.drawer = UIController.getDrawer();
        this.theme = drawer.getGraphicalTheme();
        addCommands();
        setCommandListener(this);
        bodyFont = theme.getDefaultFont();
        this.bodyItem = new BodyItem();
        this.headerItem = new HeaderItem();

        repaint();
    }

    /**
     * paint the viewmessagteitem
     */
    protected void paint(Graphics graphics) {
        drawer.drawCanvasBackground(graphics, getWidth(), getHeight());
        //drawer.drawString("ciao", graphics, 0,0,false);
        int h= ad.paintSmallAd(graphics);
        graphics.translate(0,h);
        int titleheight = paintTitle(graphics);
        graphics.translate(0,-h);
        h+=titleheight;
        h = headerItem.draw(graphics, getWidth(), h);
        bodyItem.draw(graphics, getWidth() - SCROLLBAR_WIDTH - MARGIN, h);
        drawScrollBar(graphics, h);
    }

    /**
     * enable or disable the fullMessageCommand command
     * @param enable if true enable the command, if false disable the command
     */
    public void enableFullMessageCommand(boolean enable) {
        Log.debug(this, "Enable sync commands: " + enable);
        if (enable && !fullMsgPerformed) {
            addCommand(fullMessageCommand);
        } else {
            removeCommand(fullMessageCommand);
        }
    }

    public int paintTitle(Graphics g) {

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

        /*
         *
         *    int titleheight = drawer.getGraphicalTheme().getTitleFont().getHeight();
        drawer.drawTitleBackground(graphics, getWidth(), titleheight);
        drawer.drawString(graphics,1,1, false);
         */


        String s;

        Font titleFont = drawer.getGraphicalTheme().getTitleFont();
        // keep this because otherwhise some motorola phones may throw a
        // nullpointer exception
        if (theTitle == null) {
            //Log.debug("title null... setting to empty string");
            theTitle = "";
        }

        String shortTitle = UiUtils.cutString(theTitle, getWidth() - 1, titleFont);
        drawer.drawTitleBackground(g, getWidth(), titleFont.getHeight());
        g.setFont(titleFont);
        g.setColor(drawer.getGraphicalTheme().getTitleColor());
        g.drawString(shortTitle, 1, 1, Graphics.TOP | Graphics.LEFT);
        g.drawString(theTitle, 1, 1, Graphics.TOP | Graphics.LEFT);
        return drawer.getGraphicalTheme().getTitleFont().getHeight() + 1;
    }

    public void setTitle(String title) {

        //Log.error("setting title to " + title);

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

    //don't do a repaint here, or Nokia 6630 will crash.

    }

    /*    public String getTitle() {
    return this.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;
    }

    private void exitBody(int dir) {
        //  Log.debug("exit body");
        if (dir == Canvas.UP) {
            insideBody = false;
            enterHeader(dir);
        }
    }

    private void enterHeader(int dir) {
        // Log.debug("enter header");
        insideHeader = true;
        showHeader = true;
    }

    private void exitHeader(int dir) {
        //  Log.debug("exit header");
        if ((dir == Canvas.DOWN)) {
            insideHeader = false;
            showHeader = false;
            enterBody(dir);
        }

    }

    /**
     * Handle the key navigation
     * @param keyCode is the key that has been pressed
     */
    protected void keyPressed(int keyCode) {
    
        if (keyCode == Canvas.KEY_STAR) {
           if (ad.isEnabled()) {
               UIController.handleAdClick(ad);
           } 
           
        }
        
        
        // shortcut for deleting message
        if (keyCode == Canvas.KEY_NUM0) {
            deleteMessage();
        } else if (keyCode == -10) {
            // green key
            if (isValidForCall(contactToCall)) {
                UIController.phoneCall(contactToCall, this);
            }
        }

        int dir = getGameAction(keyCode);
        if (dir == Canvas.DOWN || dir == Canvas.UP) {

            if (insideBody) {
                //   Log.debug("sending traverse to body Item");
                bodyItem.traverse(dir);
            } else if (insideHeader) {
                // Log.debug("sending traverse to header Item");
                headerItem.traverse(dir);
            }
            repaint();
        } else if (dir == Canvas.RIGHT) {
            try {
                this.setTitle(Localization.getMessages().MESSAGE_VIEW_NEXT_MESSAGE);
                repaint();
                serviceRepaints();
                getNextMessage(NEXT_MESSAGE);
            } catch (NullPointerException nex) {
                Log.error(this, nex.getMessage());
            }
        } else if (dir == Canvas.LEFT) {
            try {
                this.setTitle(Localization.getMessages().MESSAGE_VIEW_PREVIOUS_MESSAGE);
                repaint();
                serviceRepaints();
                getNextMessage(PREVIOUS_MESSAGE);
            } catch (NullPointerException nex) {
                Log.error(this, nex.getMessage());
            }
        }
    }

    /**
     * Sets the correct message when left/rigth navigation is performed
     * @param shift is the
     */
    private void getNextMessage(int shift) {
        ad.toNextAd();
        MessageList fcml = UIController.getMessageList(message);
        int msgNumber = fcml.getNumMessages();
        int messageToSet = fcml.getActiveElementId() + shift;
        if (messageToSet >= msgNumber) {
            messageToSet = 0;
            fcml.rewindList();
            shift = 0;

⌨️ 快捷键说明

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