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

📄 composemessageform.java

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

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.mailclient.mm.MessageInfo;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.loc.Localization;
import com.funambol.util.Log;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.ItemCommandListener;

public class ComposeMessageForm extends Form implements ItemCommandListener, CommandListener {
    
    public RecipientsStringItem recipientStringItem;
    public TextField body;
    public TextField subject;
    private final int MAX_BODY_SIZE=1000;
    private final int MAX_SUBJECT_SIZE=256;
    private Message message;
    private Command sendCommand;
    private Command addRecipientCommand;
    private Command sendLaterCommand;
    private Command saveCommand;
    private String toAppend;
    
    
    
    //indicate how flag should be changed when forwarding or replying to a message
    private MessageFlags flagsToSet;
    
    // the original message
    private MessageInfo originalMessage;
    
    
    /** Creates a new instance of ComposeMessageForm */
    public ComposeMessageForm(Message message, String toAppend) {
        super(Localization.getMessages().COMPOSE_MESSAGE_FORM_TITLE);
        recipientStringItem=new RecipientsStringItem(message);
        recipientStringItem.setItemCommandListener(this);
        this.append(recipientStringItem);
        this.setToAppend(toAppend);
        body =
                new TextField(Localization.getMessages().CMF_BODY_TF_LABEL,"",
                MAX_BODY_SIZE,TextField.ANY);
        subject =
                new TextField(Localization.getMessages().CMF_SUBJECT_TF_LABEL,"",
                MAX_SUBJECT_SIZE,TextField.ANY);
        
        this.append(subject);
        
        //TODO: Add something to set body size to a nicer dimension!
        this.append(body);
        
        /**
         * Adding commands
         */
        sendCommand =
                new Command(Localization.getMessages().SEND_COMMAND_LABEL,
                UIController.COMMAND_TYPE, 1);
        sendLaterCommand =
                new Command(Localization.getMessages().CMF_SEND_LATER_COMMAND,
                UIController.COMMAND_TYPE,3);
        // TODO: this already present in RecipientStringItem should we use also here?
        addRecipientCommand =
                new Command(Localization.getMessages().ADD_RECIPIENTS_COMMAND,
                UIController.COMMAND_TYPE,2);
        saveCommand =
                new Command(Localization.getMessages().SAVE_COMMAND,
                UIController.COMMAND_TYPE,3);
        
        if (!SyncClient.isBusy()) {
            addCommand(sendCommand);
        }
        
        addCommand(sendLaterCommand);
        // TODO: this already present in RecipientStringItem should we use also here?
        //addCommand(addRecipientCommand);
        
        addCommand(saveCommand);
        this.addCommand(UIController.cancelCommand);
        this.setCommandListener(this);
        this.setMessage(message);
        
        //start loading the contact list
        //  UIController.getThreadPool().startThread(new ContactListLoader());
        
    }
    
    private String getDefaultTitle() {
        return Localization.getMessages().COMPOSE_MESSAGE_FORM_TITLE;
    }
    
    public void setDefaultTitle() {
        this.setTitle(Localization.getMessages().COMPOSE_MESSAGE_FORM_TITLE);
    }
    
    public ComposeMessageForm(Message message) {
        this(message, "");
    }
    
    /**
     * calling other constructor with empy message
     */
    public ComposeMessageForm() {
        this(new Message());
    }
    
    /**
     * returns the message
     */
    public Message getMessage(){
        return message;
    }
    
    public Item getRecipientStringItem() {
        return recipientStringItem;
    }
    
    public void commandAction(Command command, Item item) {
        if (command == recipientStringItem.addRecipientsCommand) {
            openContactList();
        }
    }
    
    /**
     * Commandlistener implementation
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command==UIController.cancelCommand) {
           /** if ((message.getParent() != null) && (message.getParent().getFullName().equals("/Drafts"))){
                //Editing a message in DRAFT folder and clicking cancelCommand
                //return in DRAFT folder without show any alert
                UIController.updateDraftMessageList("");
                UIController.showBackScreen();
            }else { **/
                FunModalPopup popup =
                        new FunModalPopup("Cancel",
                        Localization.getMessages().CMF_CANCEL_COMMAND,
                        new PopupCancelCompose());
                UIController.showModalPopup(popup, this);
           // }
            
        } else if (command==sendCommand) {
            //Log.debug(this,"ComposeMessage: sendCommand");
            if (prepareMessage()) {
                
                addToFlagQueue();
                UIController.sendMessage(message);
                this.originalMessage = null;
            }
        } else if (command==sendLaterCommand) {
            //Log.debug(this, "Send later command pressed");
            if (prepareMessage()) {
                addToFlagQueue();
                UIController.sendLaterMessage(message);
                this.originalMessage = null;
            }
        } else if (command==addRecipientCommand){
            openContactList();
            
        } else if (command==saveCommand) {
            if (prepareMessage()) {
                UIController.saveMessage(message);
                this.setToAppend("");
            }
        }else {
            Log.error(this, "Error: unrecognized command");
        }
        
    }
    
    /**
     * open the recipent list
     */
    private void openContactList() {
        // Log.debug(this,"opening contact list");
        setTitle(Localization.getMessages().LOADING_CONTACTS);
        
        message.setSubject(subject.getString());
        try {
            message.setContent(body.getString());
        } catch (MailException ex) {
            ex.printStackTrace();
            Log.error(this, "Message body invalid!");
        }
        //    UIController.showAddressList(UIController.display.getCurrent(), message);
        
        UIController.showAddressList(this);
    }
    
    public void update() {
        if ((message.getTextContent()!=null) && (message.getTextContent().length() > MAX_BODY_SIZE)) {
            body.setString(message.getTextContent().substring(0, MAX_BODY_SIZE-5) + " ...");
        } else {
            body.setString(message.getTextContent());
        }
        
        if ((message.getSubject()!=null) && (message.getSubject().length() > MAX_SUBJECT_SIZE)) {
            subject.setString(message.getSubject().substring(0, MAX_SUBJECT_SIZE-5) + " ...");
        } else {
            subject.setString(message.getSubject());
        }
        
        recipientStringItem.setMessage(message);
        
        //  UIController.getThreadPool().startThread(new RMSContactLoader());
        UIController.getThreadPool().startThread(UIController.getContactListLoader(message));
    }
    
    private boolean prepareMessage() {
        try {
            if ((recipientStringItem.getText().equals(Localization.getMessages().SELECT_RECIPIENTS))) {
                UIController.showAlert(
                        Localization.getMessages().COMPOSE_MESSAGE_FORM_ERROR_MESSAGE,
                        UIController.display.getCurrent());
                return false;
            } else {
                message.setFrom(UIController.getMyFrom());
                message.setSubject(subject.getString());
                message.setContent(body.getString() + getToAppend());
                return true;
            }
        } catch (MailException ex) {
            ex.printStackTrace();
            Log.error(this, "Message body invalid!");
            return false;
        }
    }
    
    public void setMessage(Message message) {
        this.message=message;
        update();
    }
    
    
    public TextField getBody() {
        return body;
    }
    
    public TextField getSubject() {
        return subject;
    }
    
    public String getToAppend() {
        return toAppend;
    }
    
    public void setToAppend(String toAppend) {
        
        this.toAppend = toAppend;
        
        //Log.debug(this, "toAppend = " + getToAppend());
    }
    
    
    /**
     * enable or disable the send command
     * @param enable if true enable the command, if false disable the command
     */
    public void enableSendCommand(boolean enable) {
        if(enable) {
            addCommand(sendCommand);
        } else {
            removeCommand(sendCommand);
        }
    }
    
    
    
    
    /**
     * set the flags that will be added to the flagqueue for the original message
     * when this message will be sent
     * original message must be set using setOriginalMessage before addToFlagQueue
     * method is called (usually at the send or sendlater command action)
     */
    public void setFlagsToSet(MessageFlags flag) {
        
        // Log.info(this,  "setting new flag to " +flag.getFlags());
        flagsToSet = flag;
    }
    
    /**
     * if this method is called, with a non-null messageinfo,
     * when the send or send later command is pressed,
     * the message passed will have the flags given by
     * the value set by setFlagsToSet added to the flagqueue
     */
    public void setOriginalMessage(MessageInfo messageInfo) {
        
        //   Log.info(this, "setting original message to " +
        //           messageInfo.subject + "(" + messageInfo.messageId +")");
        this.originalMessage = messageInfo;
        
    }
    
    /**
     * this method adds the messagequeue
     */
    private void addToFlagQueue() {
        if (originalMessage != null) {
            //    Log.info(this, "original message is not null, updateing message flags");
            UIController.messageManager.updateMessageFlags(originalMessage, flagsToSet);
            originalMessage = null;
        }
    }
    
    private class PopupCancelCompose implements PopupAction {
        
        public PopupCancelCompose() {
            
        }
        
        public void cancel() {
            UIController.showBackScreen();
        }
        
        public void confirm() {
            // UIController.destroyAddressList();
            originalMessage = null;
            if((message.getParent() != null) && message.getParent().getFullName().equals("/Outbox")){
                //Editing a message in OUTBOX folder and clicking cancelCommand
                //return in OUTBOX folder
                UIController.updateOutboxMessageList("");
                UIController.showOutboxScreen(null);
            }else if ((message.getParent() != null) && (message.getParent().getFullName().equals("/Drafts"))){
                //Editing a message in DRAFT folder and clicking cancelCommand
                //return in DRAFT folder
                UIController.updateDraftMessageList("");
                UIController.showDraftScreen(null);
            }
            else{
                UIController.showInboxScreen();
            }
        }
    }
    
}

⌨️ 快捷键说明

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