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

📄 accountsettingsform.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.mailclient.mm.SyncEventInfo;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.config.*;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.ui.view.FunModalPopup;
import com.funambol.mailclient.ui.view.PopupAction;
import com.funambol.mailclient.loc.Localization;
import com.funambol.util.Log;
import com.funambol.util.StringUtil;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;


/**
 * This form shows all the settings related to the
 * Account functions
 * The user will be able to configure username, password,
 * sync server url.
 * The form shows also the deviceId used internally to
 * synchronize
 */
public class AccountSettingsForm extends Form implements CommandListener {
    
    private Command backCommand;
    private Command saveCommand;
    
    private TextField serverUrlTF;
    private TextField userNameTF;
    private TextField emailRemoteNameTF;
    private TextField passwordTF;
    private StringItem deviceIdSringItem;
    private TextField emailAddressTF;
    private TextField visibleNameTF;
    private TextField contactsRemoteNameTF;
    
    private MailClientConfig mailClientConfig;
    
    private boolean syncIsBusy;
    
    /**
     * Creates a new instance of AccountSettingsForm
     */
    public AccountSettingsForm() {
        super(Localization.getMessages().CONNECTION_SETTINGS_FORM_TITLE);

        mailClientConfig = UIController.getConfig();
        
        backCommand = UIController.backCommand;
        saveCommand = UIController.saveCommand;
        
        syncIsBusy = SyncClient.isBusy();
        
        append(getServerUrlTF());
        append(getUserNameTF());
        append(getPasswordTF());
        append(getVisibleNameTF());
        append(getEmailAddressTF());
        
        append(getEmailRemoteNameTF());
        append(getContactsRemoteNameTF());
        
        addCommand(backCommand);
        
        if (!SyncClient.isBusy()){
            addCommand(saveCommand);
        }
        
        setCommandListener(this);
    }
    
    /**
     * command handling method
     */ 
    public void commandAction(Command command, Displayable displayable) {
        if(command == backCommand) {
            UIController.showBackScreen();

        } else if(command == saveCommand) {
            // save settings

            // Sanity checks: empty url or default and empty credentials
            //                are not valid.
            // FIXME: why can these be null??
            if( StringUtil.isNullOrEmpty(userNameTF.getString())    ||
                StringUtil.isNullOrEmpty(serverUrlTF.getString())   ||
                StringUtil.isNullOrEmpty(passwordTF.getString())    ||
                Account.DEFAULT_URL.equals(serverUrlTF.getString()) ) {

                Log.info("Empty Url or credentials field found: fill it!!");
                UIController.showAlert(
                    Localization.getMessages().CSF_EMPTY_CREDENTIALS_ERROR);
                return;
            }
            
            // Get the current account
            Account mailAccount = UIController.getConfig().getMailAccount();

            // If the account was already set
            if(mailAccount.isSet()) {
                // If username or url are changed, we must alert the user
                // and reset the account.
                boolean userChanged = 
                        !userNameTF.getString().equals(mailAccount.getUser());
                boolean urlChanged = 
                        !serverUrlTF.getString().equals(mailAccount.getUrl());

                if(userChanged || urlChanged) {
                    Log.info("Saving new account. Account has changed");
                    UIController.showModalPopup(
                            new FunModalPopup(
                                Localization.getMessages().SAVING_ACCOUNT_POPUP_TITLE,
                                Localization.getMessages().SAVING_ACCOUNT_POPUP_MSG,
                                new SavingAccountAction()),this);
                } else {
                    mailClientConfig = getNewConfigFromUser();
                    UIController.saveNewConfig(mailClientConfig);
                    UIController.showBackScreen();
                }
            } else {
                // if it's the first start, save the config and
                // start the initial sync.
                mailClientConfig = getNewConfigFromUser();
                UIController.saveNewConfig(mailClientConfig);
                UIController.showInboxScreen();
                UIController.sync(true);
            }       
        }
    }
    
    private MailClientConfig getNewConfigFromUser() {
        Account userAccount = new Account();
        userAccount.setUser(userNameTF.getString());
        userAccount.setPassword(passwordTF.getString());
        userAccount.setUrl(serverUrlTF.getString());
        userAccount.setRemoteURI(emailRemoteNameTF.getString());
        userAccount.setPimRemoteURI(contactsRemoteNameTF.getString());
        userAccount.setAddress(emailAddressTF.getString());
        userAccount.setName(visibleNameTF.getString());
        mailClientConfig.setMailAccount(userAccount);
        return mailClientConfig;
        
    }
    
    private TextField getServerUrlTF() {
        if (serverUrlTF == null) {
            serverUrlTF = new TextField(Localization.getMessages().CSF_SERVER_URL_TF_LABEL,
                    mailClientConfig.getMailAccount().getUrl(), 120, TextField.ANY);
            serverUrlTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        
        setUneditableTextField(serverUrlTF);
        return serverUrlTF;
    }
    
    private TextField getUserNameTF() {
        if (userNameTF == null) {
            userNameTF = new TextField(Localization.getMessages().CSF_USERNAME_TF_LABEL,
                    mailClientConfig.getMailAccount().getUser(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
            userNameTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
            
        }
        setUneditableTextField(userNameTF);
        return userNameTF;
    }
    
    private TextField getEmailRemoteNameTF() {
        if (emailRemoteNameTF == null) {
            emailRemoteNameTF = new TextField(Localization.getMessages().CSF_EMAIL_REMOTE_NAME_TF_LABEL,
                    mailClientConfig.getMailAccount().getRemoteURI(), 120, TextField.ANY);
            emailRemoteNameTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(emailRemoteNameTF);
        return emailRemoteNameTF;
    }
    
    private TextField getPasswordTF() {
        if (passwordTF == null) {
            passwordTF = new TextField(Localization.getMessages().CSF_PASSWORD_TF_LABEL,
                    mailClientConfig.getMailAccount().getPassword(), 120, TextField.PASSWORD);
            passwordTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(passwordTF);
        return passwordTF;
    }
    
    private TextField getContactsRemoteNameTF() {
        if (contactsRemoteNameTF == null){
            contactsRemoteNameTF =
                    new TextField(Localization.getMessages().CSF_CONTACTS_REMOTE_NAME_TF_LABEL,
                    mailClientConfig.getMailAccount().getPimRemoteURI(), 120, TextField.ANY);
            getContactsRemoteNameTF().setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(contactsRemoteNameTF);
        return contactsRemoteNameTF;
    }
    
    private TextField getEmailAddressTF() {
        if (emailAddressTF == null) {
            emailAddressTF = new TextField(Localization.getMessages().CSF_EMAIL_ADDRESS_TF_LABEL, mailClientConfig.getMailAccount().getAddress(), 120, TextField.ANY);
        emailAddressTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(emailAddressTF);
        return emailAddressTF;
    }
    
    private TextField getVisibleNameTF() {
        if (visibleNameTF == null) {
            visibleNameTF = new TextField(Localization.getMessages().CSF_VISIBLE_NAME_TF_LABEL, mailClientConfig.getMailAccount().getName(), 120, TextField.ANY);
            visibleNameTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(visibleNameTF);
        return visibleNameTF;
    }
    
    //This method disable editing of TextFields when
    //a sync process was started
    private void setUneditableTextField(TextField textField) {
        if (syncIsBusy){
            textField.setConstraints(TextField.UNEDITABLE|textField.getConstraints());
        }
    }
    
    
    private class SavingAccountAction implements PopupAction {
        
        public SavingAccountAction() {
        }
        
        public void cancel() {
            UIController.showBackScreen();
        }
        
        public void confirm() {
            mailClientConfig = getNewConfigFromUser();
            UIController.saveNewConfig(mailClientConfig);
            UIController.resetAccount();
        }
        
    }
}

⌨️ 快捷键说明

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