addcontactform.java
来自「moblie syncml mail javame」· Java 代码 · 共 298 行
JAVA
298 行
/*
* 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.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.cm.ContactManager;
import com.funambol.mailclient.loc.Localization;
import com.funambol.util.Log;
import com.funambol.util.StringUtil;
import javax.microedition.lcdui.AlertType;
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.ANY | TextField.NON_PREDICTIVE);
private TextField txtFirstName =
new TextField(Localization.getMessages().ACF_CONTACT_FIRSTNAME_LABEL, "", MAX_TXTFIELD_SIZE, TextField.ANY);
private TextField txtLastName =
new TextField(Localization.getMessages().ACF_CONTACT_LASTNAME_LABEL, "", MAX_TXTFIELD_SIZE, TextField.ANY);
private Contact contact;
private boolean isNew;
private ContactList contactList;
private Contact existentContact = null;
/**
* Creates a new instance of AddContactForm
*/
public AddContactForm(ContactList contactList) {
this(new Contact(), true);
this.contactList = contactList;
setTitle(Localization.getMessages().ACF_NEW_CONTACT_FORM_TITLE);
}
public AddContactForm(Contact contact, ContactList 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);
//#ifdef isBlackberry
//# this.addCommand( UIController.blackberryExitCommand );
//#endif
txtEmail1.setInitialInputMode("MIDP_LOWERCASE_LATIN");
txtVisibleName.setString(contact.getVisibleName());
txtEmail1.setString(contact.getDefaultEmail());
if (!StringUtil.isNullOrEmpty(contact.getFirstName())) {
txtFirstName.setString(contact.getFirstName());
}
if (!StringUtil.isNullOrEmpty(contact.getLastName())) {
txtLastName.setString(contact.getLastName());
}
if (isNameToGuess(contact)) {
String[] names = getFirstAndLastNames(contact);
txtFirstName.setString(names[0]);
txtLastName.setString(names[1]);
}
append(txtFirstName);
append(txtLastName);
append(txtEmail1);
append(txtVisibleName);
}
/**
* this method implement an euristic algorithm that tries to guess first
* and last name from a given contact with only visiblename
* @param contact a contact with non null visible name and empty first and
* last name
* @return an array of size 2 composed by first name and last name
*/
private String[] getFirstAndLastNames(Contact contact) {
String[] names = StringUtil.split(contact.getVisibleName(), ",");
String fname = null;
String lname = null;
if (names.length == 2) {
// we're in the case of "Doe, John" so we
// can guess that first name is the second part
fname = names[1];
lname = names[0];
} else if (names.length > 2) {
// we're in the case of "Huey, Dewey, Louie"
// so we use it as first name and live happy everafter
fname = contact.getVisibleName();
} else {
// splitting with spaces
names = StringUtil.split(contact.getVisibleName(), " ");
// we're in the case of "John Doe", we can suppose that
// the last word is the last name, and the remaining is
// the first name
lname = names[names.length-1];
if (names.length > 1) {
// putting what remains in fname
fname = contact.getVisibleName().substring(0, contact.getVisibleName().length() - lname.length() -1);
}
}
return new String[] {fname, lname};
}
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())) {
disableCommands();
setTitle(Localization.getMessages().SAVING_CONTACT);
UIController.getThreadPool().startThread(
new Thread() {
public void run() {
try {
contact.setDefaultEmail(txtEmail1.getString());
contact.setNickName(txtVisibleName.getString().replace('\n', ' ').trim());
contact.setFirstName(txtFirstName.getString().replace('\n', ' ').trim());
contact.setLastName(txtLastName.getString().replace('\n', ' ').trim());
if (contact.getContactId() == 0) {
// the flag new can be true also for contact
// not in rms, e.g. if they came from a
// reply message.
UIController.saveContact(contact, true);
} else {
UIController.saveContact(contact, isNew);
}
if (contactList != null) {
if (isNew &&
!(UIController.backStack.peek() instanceof ViewMessage) &&
!(UIController.backStack.peek() instanceof RecipientList)) {
contactList.addAddress(contact.getAddress(Address.TO));
contactList.SortByState();
} else {
contactList.resetActiveElementHeight();
}
// contactList.reinitializeContactItems();
}
UIController.showBackScreen();
} catch (ContactManagerException ex) {
Log.error(this, "Error creating contact: " + ex.getMessage());
if (UIController.checkContactListFull()) {
UIController.showErrorAlert(
Localization.getMessages().AB_LIMIT_ERROR_MSG);
} else {
UIController.showErrorAlert(Localization.getMessages().INVALID_EMAIL_ADDRESS);
ex.printStackTrace();
}
} //catch (InterruptedException e) {}
}
});
} 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 (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();
} else if (command == UIController.blackberryExitCommand) {
UIController.midlet.destroyApp(false);
}
}
private void disableCommands() {
this.removeCommand(saveCommand);
this.removeCommand(advancedCommand);
this.removeCommand(UIController.cancelCommand);
}
private boolean isValidContact() {
try {
existentContact = UIController.getContactManager().
exists(txtEmail1.getString());
} catch (Exception ex) {
ex.printStackTrace();
Log.error("[AddContactForm] Error validating contact" + ex.getMessage());
}
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;
}
}
/**
* method to find out if we need to guess first name and last name for the
* given contact
* @param contact
* @return true if we should guess the name (i.e.
* if visible name is not empty but first and last name are empty)
*
*/
private boolean isNameToGuess(Contact contact) {
//return true if we have visible name but no first and last name
return (!StringUtil.isNullOrEmpty(contact.getVisibleName()) &&
StringUtil.isNullOrEmpty(contact.getFirstName()) &&
StringUtil.isNullOrEmpty(contact.getLastName()));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?