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

📄 funcanvasviewmessage.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * 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
 *
 */

/*
 * FunCanvasViewMessage.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.RMSStore;
import com.funambol.mail.Store;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.sm.SyncClient;
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 FunCanvasViewMessage extends Canvas implements CommandListener,
        Runnable {
    /**
     * 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 int NEXT_MESSAGE = 1;
    
    private int PREVIOUS_MESSAGE = -1;
    
    private String theTitle = Localization.getMessages().MESSAGE_VIEW_FORM_TITLE ;
    /**
     * creates a new FunCanvasViewMessage with given message
     */
    public FunCanvasViewMessage(Message message) {
        
        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();
        //setTitle(Localization.getMessages().MESSAGE_VIEW_FORM_TITLE);
        repaint();
    }
    
    /**
     * paint the viewmessagteitem
     */
    protected void paint(Graphics graphics) {
        drawer.drawCanvasBackground(graphics, getWidth(), getHeight());
        //drawer.drawString("ciao", graphics, 0,0,false);
        int h= paintTitle(graphics);
        h = headerItem.draw(graphics,getWidth(),h);
        bodyItem.draw(graphics,getWidth()- SCROLLBAR_WIDTH - MARGIN ,h);
        drawScrollBar(graphics, h);
    }
    
    
    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;
    }
    
    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) {
        
        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) {
            this.setTitle(Localization.getMessages().MESSAGE_VIEW_NEXT_MESSAGE);
            repaint();
            serviceRepaints();
            getNextMessage(NEXT_MESSAGE);
        } else if (dir==Canvas.LEFT) {
            this.setTitle(Localization.getMessages().MESSAGE_VIEW_PREVIOUS_MESSAGE);
            repaint();
            serviceRepaints();
            getNextMessage(PREVIOUS_MESSAGE);
        }
    }
    
    
    
    
    /**
     * Sets the correct message when left/rigth navigation is performed
     * @param shift is the
     */
    private void getNextMessage(int shift) {
        FunCanvasMessageList fcml = UIController.getMessageList(message);
        int msgNumber = fcml.getNumMessages();
        int messageToSet = fcml.getActiveElement()+shift;
        if (messageToSet>=msgNumber) {
            messageToSet=0;
            fcml.rewindList();
            shift=0;
        } else if (messageToSet<0) {
            messageToSet=msgNumber-1;
            shift=fcml.elements.length;
        }
        
        if (Math.abs(shift)>1) {
            for (int i=0; i<shift; i++) {
                fcml.scroll(1);
                fcml.repaint();
            }
        } else {
            fcml.scroll(shift);
        }
        fcml.setActiveElement(messageToSet);
        cleanup();
        UIController.showViewMessageScreen(
                fcml, fcml.getSelectedMessageInfo().getMessage());
        //repaint();
    }
    
    
    /**
     * this method creates a vector of strings that can fit
     * on a single line of screenWidth pixel with given font.
     */
    public String[] getStringArray(String text, int screenWidth, Font font) {
        // Log.debug(this, "GetStingArray() " + text.substring(0,10));
        
        Vector lines = new Vector();
        String [] ret;
        
        ChunkedString csLine = new ChunkedString(text);
        ChunkedString csWord;
        String[] lineSep = {"\r\n", "\n\r", "\r", "\n" };
        String[] wordSep = {" ", "\t"};
        
        
        //ChunkedString line;
        StringBuffer wordStringBuffer = new StringBuffer();
        String word;
        StringBuffer myLine=new StringBuffer();
        int widthLeft;
        while ( (csWord=csLine.getNextChunk(lineSep)) != null ) {
            //csWord = new ChunkedString(line);
            widthLeft = screenWidth;
            myLine.setLength(0);
            
            // get the words
            while ( (word = csWord.getNextString(wordSep)) != null ) {
                wordStringBuffer = new StringBuffer(word);
                
                if ( myLine.length() != 0 ) {
                    //if this is not the first word of the line we add a space
                    wordStringBuffer.insert(0," ");
                }
                int width = font.stringWidth(wordStringBuffer.toString());
                //if it fits inside the line
                if (width < widthLeft) {
                    //we append the word at the line and update widthleft
                    myLine.append(wordStringBuffer.toString());
                    widthLeft -=width;
                } else {
                    //if we've a space we can cut it out since this word
                    // will go to the next line
                    if (wordStringBuffer.charAt(0) == ' ') {
                        wordStringBuffer.deleteCharAt(0);
                    }
                    width = font.stringWidth(wordStringBuffer.toString());
                    //if can go on a new line
                    if ( width < screenWidth ) {
                        //we put it on a new line
                        lines.addElement(myLine.toString());
                        myLine.setLength(0);
                        myLine.append(wordStringBuffer.toString());
                        widthLeft = screenWidth - width;
                        continue;
                    } else {
                        int start = 0;
                        int end = 0;
                        int length = wordStringBuffer.length();
                        //String aString = myLine.toString();
                        while (start < length) {
                            // check length of the substring, adding a single char each time
                            for (int w = 0; w < widthLeft && end < length; end++) {
                                w = font.substringWidth( wordStringBuffer.toString(), start, (end - start) +1 );
                            }
                            myLine.append(wordStringBuffer.toString().substring(start,end));
                            if (end < length) {
                                lines.addElement(myLine.toString());
                                myLine.setLength(0);
                                widthLeft = screenWidth;
                            } else {
                                widthLeft = screenWidth - font.stringWidth(myLine.toString());
                            }
                            start = end;
                        }
                    }
                }
            }
            lines.addElement(myLine.toString());
        }
        ret = new String[lines.size()];
        //        Log.debug(this, "created vector, copying it");
        lines.copyInto(ret);
        lines=null;
        //       Log.debug(this, "returning string array");
        return ret;
    }
    
    private void addCommands() {
        addCommand(UIController.backCommand);
        deleteCommand =new Command(Localization.getMessages().DELETE_COMMAND_LABEL,
                UIController.COMMAND_TYPE,10);
        forwardCommand=new Command(Localization.getMessages().MVF_FORWARD_COMMAND,UIController.COMMAND_TYPE,25);

⌨️ 快捷键说明

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