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

📄 addcontactform.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.mailclient.cm.AlreadyExistentContactException;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.ui.controller.UIController;
//import com.funambol.mailclient.ui.view.FunAddressList;
import com.funambol.mailclient.cm.Contact;
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.StringItem;
import javax.microedition.lcdui.TextField;


public class AddContactForm extends Form implements CommandListener{
    private static final int MAX_TXTFIELD_SIZE=128;
    //TODO: change this according to contact fields
    private Command saveCommand=UIController.saveCommand;
    private Command advancedCommand =
            new Command(Localization.getMessages().ACF_ADVANCED_COMMAND_LABEL,
            UIController.COMMAND_TYPE, 5);
    private TextField txtVisibleName =
            new TextField(Localization.getMessages().ACF_VISIBLE_NAME_LABEL,
            "",MAX_TXTFIELD_SIZE,TextField.ANY);
    
    private TextField txtEmail1 =
            new TextField(Localization.getMessages().ACF_CONTACT_EMAIL_1_LABEL,
            "",MAX_TXTFIELD_SIZE,TextField.EMAILADDR);
    private Contact contact;
    private boolean isNew;
    private FunCanvasContactList contactList;
    private Contact existentContact = null;
    
    /**
     * Creates a new instance of AddContactForm
     */
    public AddContactForm(FunCanvasContactList contactList) {
        this(new Contact(), true);
        this.contactList=contactList;
        setTitle(Localization.getMessages().ACF_NEW_CONTACT_FORM_TITLE);
    }
    
    public AddContactForm(Contact contact, FunCanvasContactList contactList, boolean isNew ) {
        this(contact, isNew);
        this.contactList = contactList;
    }
    
    public AddContactForm(Contact contact, boolean isNew) {
        super(Localization.getMessages().ACF_EDIT_CONTACT_FORM_TITLE);
        this.isNew = isNew;
        this.contact=contact;
        this.addCommand(saveCommand);
        this.addCommand(advancedCommand);
        this.addCommand(UIController.cancelCommand);
        this.setCommandListener(this);
        txtVisibleName.setString(contact.getVisibleName());
        txtEmail1.setString(contact.getDefaultEmail());
        append(txtVisibleName);
        append(txtEmail1);
    }
    
    public void commandAction(Command command, Displayable displayable) {
        if (command==saveCommand) {
            
            if (txtEmail1.getString().indexOf('@')<=0) {
                UIController.showErrorAlert(
                        Localization.getMessages().INVALID_EMAIL_ADDRESS );
            } else {
                try {
                    
                    if((isValidContact())){
                        contact.setDefaultEmail(txtEmail1.getString());
                        contact.setNickName(txtVisibleName.getString());
                        UIController.saveContact(contact, isNew);

                        if ( this.contactList!=null ) {
                            if (isNew) {
                                contactList.addAddress(contact.getAddress(Address.TO));
                            } else {
                                contactList.resetActiveElementHeight();
                            }
                            //  contactList.reinitializeContactItems();
                        }

                    }
                    else{
                //} catch (AlreadyExistentContactException ex) {
                    
                    contactList.activateElement(existentContact);
                    
                    UIController.showAlert(
                            Localization.getMessages().CONTACT_ALREADY_EXISTS_EDITING_IT,
                            UIController.showEditContactScreen(
                            //null,ex.getExistentContact(),contactList, false)
                            null,existentContact,contactList, false)
                            );

                    
                    } 
                }catch (ContactManagerException ex) {
                    //ex.printStackTrace();
                    Log.error(this, "Error creating contact: " + ex.getMessage());
                    UIController.showErrorAlert(Localization.getMessages().INVALID_EMAIL_ADDRESS);
                } catch (MailException ex) {
                    Log.error(this, "Error adding contact to AddressList");
                    UIController.showErrorAlert(Localization.getMessages().ACF_ALERT_ERROR_SAVING_CONTACT_MESSAGE);
                    // contactList.reinitializeContactItems();
                    ex.printStackTrace();
                }
            }
            
            
        } else if (command==advancedCommand) {
            UIController.showAdvancedContactScreen(contact);
        } else if (command==UIController.cancelCommand) {
            UIController.showBackScreen();
        }
    }
    
    private boolean isValidContact(){ 
        existentContact = contactList.exists(txtEmail1.getString());
        
        if (isNew && existentContact==null){
            return true;
        }else if(isNew && existentContact!=null){
            return false;
        } else if (!isNew && contact.getDefaultEmail().equals(txtEmail1.getString())){
            return true;
        } else if (!isNew && existentContact==null){
            return true;
        }
        else{
            return false;
        }
    }

}

⌨️ 快捷键说明

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