attachmentslist.java

来自「moblie syncml mail javame」· Java 代码 · 共 365 行

JAVA
365
字号
/*
 * 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.mail.BodyPart;
import com.funambol.mail.Message;
import com.funambol.mail.Multipart;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.ui.utils.ImageAttachment;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.util.Log;
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 AttachmentsList extends List implements CommandListener {

    private Command backCommand;
    private Command openCommand;
    private ImageAttachment imgattach;
    private Multipart multipart;
    private int numberOfBodyPart;
    private Vector attachmentList;
    private static Font font;
    private static Font titleFont;
    private Drawer drawer;
    private String[] supportedMimeTypes = {
         "text/html", "text/vnd.wap.wml", "application/xhtml+xml", 
    //#ifndef Moto_Low
       "text/plain", "text/richtext", 
    //#endif
        "image/bmp", "image/gif", "image/png", "image/jpeg", "image/rgb", "text/vnd.sun.j2me.app-descriptor",
        "image/pjpeg"
    //#ifdef advancedAttachments
    //# ,"application/pdf","application/octet-stream","application/msword","application/excel",
    //# "application/vnd.ms-excel","application/x-excel","application/mspowerpoint",
    //# "application/vnd.ms-powerpoint","application/mspowerpoint","application/powerpoint",
    //# "application/vnd.ms-powerpoint","application/x-mspowerpoint","audio/", "video/"
    //#endif            
    };
    /**
     * the margin
     */
    private static final int MARGIN = 3;

    /** Creates a new instance of AttachmentsList */
    public AttachmentsList(Message mailMessage) {

        drawer = UIController.getDrawer();
        font = drawer.getGraphicalTheme().getDefaultFont();
        titleFont = drawer.getGraphicalTheme().getTitleFont();
        multipart = (Multipart) mailMessage.getContent();
        numberOfBodyPart = multipart.getCount();
        attachmentList = new Vector(numberOfBodyPart);

        setCommandListener(this);
        Vector tmpAttachList = new Vector(numberOfBodyPart);
        for (int i = 0; i < numberOfBodyPart; i++) {
            BodyPart bp = multipart.getBodyPart(i);

            String fileName = bp.getFileName();
            if (fileName != null && !fileName.startsWith("Part1.")) {

                tmpAttachList.addElement(bp);
            }
        }
        if (tmpAttachList.size() > 0) {
            setElements(tmpAttachList);
        }
        initializeAttachItems();

        setTitle("Attachments (" + (numberOfBodyPart - 1) + ")");

    }


    public void commandAction(Command command, Displayable displayable) {
        if (command == backCommand) {
            UIController.showBackScreen();
        }else if (command==openCommand) {
            String msg = Localization.getMessages().OPEN_BROWSER_MESSAGE_TEXT_3 +
                    " " + ((BodyPart) getActiveElement()).getFileName() +
                    " " + Localization.getMessages().OPEN_BROWSER_MESSAGE_TEXT_4;
          
            //#ifdef Exit 
            //we can avoid this popup, since we'll warn the user we need to exit
            //# UIController.openUrl(((BodyPart)getActiveElement()).getAttachUrl());
            //#else
            ModalPopup popup =
                    new ModalPopup(Localization.getMessages().OPEN_BROWSER_MESSAGE_TITLE,
                    msg,
                    new BrowserOpenPopupAction(((BodyPart)getActiveElement()).getAttachUrl()));
            UIController.showModalPopup(popup, UIController.display.getCurrent());
            //#endif
        }
    }

    int paintElement(int elementId, Graphics g) {
        Log.info("paint element");
        g.translate(0, current_paint_h);
        int h = ((CanvasAttachmentItem) attachmentList.elementAt(elementId)).paint(g);
        g.translate(0, -current_paint_h);

        return h;
    }

    /**
     * create the attachmentslist from the array of elements
     */
    private void initializeAttachItems() {
        attachmentList.removeAllElements();
        for (int i = 0; i < elements.size(); i++) {
            attachmentList.addElement(new CanvasAttachmentItem(
                    (BodyPart) elements.elementAt(i), i));
        }
    }

    void handleKey(int keyCode) {
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
    }

    int paintTitle(Graphics g) {
        Log.debug("paint title!");

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

        if (title == null) {
            title = "";
        }


        drawer.drawTitleBackground(g, getWidth(), titleFont.getHeight());
        g.setFont(titleFont);
        g.setColor(drawer.getGraphicalTheme().getTitleColor());

        g.drawString(title, 0, 0, Graphics.TOP | Graphics.LEFT);

        int h = titleFont.getHeight();

        g.translate(0, h);

        g.translate(0, -h);
        return h;

    }

    /**
     * paint the background
     */
    void paintBackground(Graphics graphics) {
        Log.debug("paint background!");
        drawer.drawCanvasBackground(graphics, getWidth(), getHeight());
    }

    protected void initCommands() {
        backCommand = new Command(Localization.getMessages().BACK_COMMAND,Command.BACK,5);
        addCommand( backCommand ); 
        openCommand = new Command("Open",Command.OK,1);
    }
    
    /**
     * called by implementation when a list of elements with lenght >0 is set
     */
    protected void addItemCommands() {
        addCommand(openCommand);
        setFireCommand(openCommand);
    }

    /**
     * called by implementation when a list of elements with lenght = 0 is set
     */
    protected void removeItemCommands() {
        removeCommand(openCommand);
        setFireCommand(null);
    }

    protected void activate(int activeElement) {
        super.activate(activeElement);
        BodyPart bp = (BodyPart) getActiveElement();
        removeItemCommands();
        if (bp != null && bp.getAttachUrl() != null) {
            if (mimeSupported(bp.getContentType())) {
                addItemCommands();
            }
        }
    }
    boolean isVisible(int elementId) {
        return elements.elementAt(elementId) != null;
    }

    boolean isActivable(int elementId) {
        return isVisible(elementId);
    }

    private boolean mimeSupported(String bpcontentType) {

        for (int i = 0; i < supportedMimeTypes.length; i++) {
            String CTSupported = supportedMimeTypes[i];
            if (bpcontentType.toLowerCase().indexOf(CTSupported) != -1) {
                return true;
            }
        }
        return false;
    }
    //------------------------ CanvasAttachmentItem ---------------------------------//
    /**
     * this class draws the attach item
     */
    class CanvasAttachmentItem {
        BodyPart bodypart;
        private int id;
        /**
         * the attachment file name that fits on a single line
         */
        private String shortName;
        /**
         * the attachment file size that fits on a single line
         */
        private String size;
        CanvasAttachmentItem(BodyPart bp, int id) {
            bodypart = bp;
            this.id = id;
        }

        private boolean isActive() {
            return id == getActiveElementId();
        }

        /**
         * paint the attachmentitem
         */
        public int paint(Graphics graphics) {

            //we need to calculate the short strings
            int w = getWidth();
            int h = calculateHeight();
            calculateShortStrings();

            //we draw the background and size
            drawer.drawBackground(graphics, w, h, isActive());

            drawer.drawString(getSize(), graphics, w - MARGIN, MARGIN, Graphics.TOP | Graphics.RIGHT, isActive());


            //we draw the attachment name
            drawer.drawString(shortName, graphics, //Theme.getIMAGE_PART_WIDTH()+
                    2 * MARGIN, MARGIN, isActive());
            return h;
        }

        public BodyPart getBodyPart() {
            return bodypart;
        }
        /**
         * calculate the short version of address and subject and
         * store them in shortAddress and shortSubject
         */
        private void calculateShortStrings() {

            if (shortName == null) {

                String fileName = bodypart.getFileName();
                String fileExtension = fileName.substring(fileName.length() - 4, fileName.length());
                int w = getWidth();
                int firstlineSpace = w - 4 * MARGIN //- Theme.getIMAGE_PART_WIDTH() 
                        - font.stringWidth(getSize()) - font.stringWidth(fileExtension);

                shortName = UiUtils.cutString(bodypart.getFileName(), firstlineSpace, font);
                if (!shortName.equals(fileName)) {
                    shortName += fileExtension;
                }

            } else {
                //   Log.error("no need to calculate shortstring");
            }


        }
        /**
         * return the size of attachment
         */
        private String getSize() {
            String s = bodypart.getSize();
            if (s != null) {
                if (size == null) {
                    size = formatSize(bodypart.getSize());
                }
                return size;
            } else {
                return "";
            }
        }

        private String formatSize(String size) {
            int sizeInt = Integer.parseInt(size);

            if (sizeInt < 1024) {
                return "(" + size + " b)";
            } else {
                sizeInt = sizeInt / 1024;
                if (sizeInt < 1024) {
                    return "(" + (String.valueOf(sizeInt)) + " K)";
                } else {
                    return "(" + (String.valueOf(sizeInt)) + " M)";
                }
            }
        }

        private void setId(int newId) {
            this.id = newId;
        }

        /*
         * compute height using information from the bean,
         * the drawer and the icon size
         */
        private int calculateHeight() {
            return font.getHeight() + 2 * MARGIN;
        }
    }
}

⌨️ 快捷键说明

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