contactitemcollection.java
来自「moblie syncml mail javame」· Java 代码 · 共 779 行 · 第 1/2 页
JAVA
779 行
/*
* 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.mail.Message;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManagerEvent;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.ContactManagerListener;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.util.Log;
import java.util.Enumeration;
import java.util.Vector;
/**
* class for handling a group of contact items
*/
public class ContactItemCollection implements ContactManagerListener {
private Vector contactItems;
private boolean toBeFiltered;
// alphabetical comparator
private ContactItemComparator alphabeticalComparator;
// state comparator
private ContactItemComparator stateComparator;
//the current comparator
private ContactItemComparator comparator;
// current state of contacts
private int state ;
// items are sorted alphabetically
public static int BY_STATE = 0;
// items are sorted by state
public static int ALPHABETICALLY = 1;
// items are not sorted
public int NONE = 2;
private ContactItemFilter filter;
private Vector filteredVector;
private Message message;
private int messageRecCount;
private ContactItemCollectionListener listener;
/**
* Creates a new instance of ContactItemCollection
*/
public ContactItemCollection(Message m) {
// Log.debug("creating ContactItemCollection");
this.message = m;
this.messageRecCount = m.getNumberOfRecipients();
alphabeticalComparator = new BasicContactItemComparator();
stateComparator = new StateContactItemComparator();
initCollection();
//this.setComparator(new StateContactItemComparator());
this.setFilter(new T9Search());
UIController.getContactManager().setListener(this);
}
/**
* this method initializes the collection getting first the messageAdresses,
* than adding the contacts from rms comparing them one by one with the
* addresses presents in the message
* <br/>
* at the end of the method, the class is ready to return the contactitem
* collection sorted aphabetically
*/
// TODO: memory optimization to avoid recreating objects each time.
private void initCollection() {
// Log.debug("init contactitem collection");
// getting addresses from message
Vector messageAddresses = UiUtils.getMessageAddresses(message);
// Log.info("messageAddresses: " + getMessageAddresses().size());
// adding contacts from rms, setting them to the right state
// (to, cc, bcc) if they're present in messageAddresses array
Enumeration e;
try {
e = UIController.getContactManager().getContactList();
if (contactItems == null) {
contactItems = new Vector(UIController.getContactManager().
getContactCount());
} else {
contactItems.removeAllElements();
}
String email;
while(e.hasMoreElements()) {
Contact c = (Contact)e.nextElement();
if (messageAddresses.size() > 0) {
//Log.info("messageAddresses size =" + messageAddresses.size() + ". looping");
// we need to search the messageAddresses for a
// matching email address
boolean found = false;
for (int i=0; i<messageAddresses.size(); i++) {
Address currentAddress =
(Address)messageAddresses.elementAt(i);
email = currentAddress.getEmail();
/* Log.info("looking for email: " + email + "\n" +
"contact emails are: \n" +
"1] " + c.getDefaultEmail() + "\n" +
"2] " + c.getEmail_2() + "\n" +
"3] " + c.getEmail_3() + "\n"); */
if (email.equals(c.getDefaultEmail())) {
contactItems.addElement(new ContactItem(
c, currentAddress.getType(),
Contact.DEFAULT_EMAIL) );
messageAddresses.removeElementAt(i);
found = true;
break;
/*Log.info("found default address, removing from messageaddresses. " +
"now messageaddresses.size is " + messageAddresses.size()); */
} else if (email.equals(c.getEmail_2())) {
contactItems.addElement(new ContactItem(
c, currentAddress.getType(),
Contact.SECONDARY_EMAIL) );
messageAddresses.removeElementAt(i);
found = true;
break;
/* Log.info("found secondary address, removing from messageaddresses. " +
"now messageaddresses.size is " + messageAddresses.size());
*/
} else if (email.equals(c.getEmail_3())) {
contactItems.addElement(new ContactItem(
c, currentAddress.getType(),
Contact.TERTIARY_EMAIL) );
messageAddresses.removeElementAt(i);
found = true;
break;
/* Log.info("found tertiary address, removing from messageaddresses. " +
"now messageaddresses.size is " + messageAddresses.size());
*/
}
}
if(!found) {
// contact is not in messageAddresses
// Log.info("address not found, adding it from rms");
contactItems.addElement(new ContactItem(c));
}
} else {
// Log.info( "direct add: messageaddresses.size is " + getMessageAddresses().size());
contactItems.addElement(new ContactItem(c));
}
}
// we've addedd all the contacts in the contactmanager
if (messageAddresses.size() > 0) {
/*Log.info("some addresses left! I have to add " +
messageAddresses.size() + " addresses!"); */
//we need to add some more addresses
Enumeration em = messageAddresses.elements();
while (em.hasMoreElements()) {
Address a = (Address) em.nextElement();
//Log.info("adding address " + a.getEmail());
Contact c = new Contact(a.getName(), a.getEmail());
/* Log.debug("before adding the contactItem for " + c.getVisibleName() +
"we had " + contactItems.size() + " contactitems");
*/ contactItems.addElement(new ContactItem(c, a.getType(),
Contact.DEFAULT_EMAIL));
// Log.debug("after: " + contactItems.size());
}
}
this.state = ALPHABETICALLY;
} catch (ContactManagerException ex) {
ex.printStackTrace();
} finally {
toBeFiltered = true;
if (listener != null) {
ContactItemCollectionEvent event =
new ContactItemCollectionEvent();
event.setEventCode(
ContactItemCollectionEvent.EVENT_CODE_RESET_CONTACTITEM_LIST);
listener.update(event);
}
}
}
/**
* remove a contact item from the contact list
* @return true if the item was found, false if not.
*/
public boolean remove(ContactItem c) {
boolean found = contactItems.removeElement(c);
if (found) {
// remove from the filtered vector if present
getFilteredVector().removeElement(c);
if (listener != null) {
ContactItemCollectionEvent event =
new ContactItemCollectionEvent();
event.setEventCode(
ContactItemCollectionEvent.EVENT_CODE_DELETE_CONTACTITEM);
event.setContactItem(c);
listener.update(event);
}
}
return found;
}
/**
* set the filter to be used
* if the given filter is different from the current one, the tobefiltered
* property is set to true
*/
public void setFilter(ContactItemFilter filter) {
if (this.filter != filter) {
this.toBeFiltered = true;
this.filteredVector = null;
}
this.filter = filter;
}
/**
* set the message
*/
public void setMessage(Message newMessage) {
// try to be smart here to avoid useless reinit
if (newMessage.equals(message)) {
// we may consider a reset here...
return;
}
int newRecCount = newMessage.getNumberOfRecipients();
this.message=newMessage;
// if newRecCount and oldRecCoun are 0, we don't need to
// reinit everything (this avoid the slow resort operation )
if (newRecCount != 0 || messageRecCount != 0) {
// Log.debug("need to reload collection");
initCollection();
} else {
// but we need to reset the state and email of each element
resetItems();
}
messageRecCount = newRecCount;
if (listener != null) {
ContactItemCollectionEvent event =
new ContactItemCollectionEvent();
event.setEventCode(
ContactItemCollectionEvent.EVENT_CODE_MESSAGE_CHANGED);
listener.update(event);
}
}
/**
* set the item to state ADDRES_STATE_NONE and the email to DEFAULT_EMAIL,
*
*/
private void resetItems() {
Enumeration e = contactItems.elements() ;
ContactItem ci;
while (e.hasMoreElements()){
ci= ((ContactItem)e.nextElement());
ci.setState(ContactItem.ADDRESS_STATE_NONE);
ci.setSelectedEmailIndex(Contact.DEFAULT_EMAIL);
}
if (listener != null) {
ContactItemCollectionEvent event =
new ContactItemCollectionEvent();
event.setEventCode(
ContactItemCollectionEvent.EVENT_CODE_RESET_CONTACTITEM_LIST);
listener.update(event);
}
}
/**
* @return the items sorted with the given criterion.
* @param sortingMode one of ALPHABETICALLY, BY_STATE, NONE
*/
public Vector getItems(int sortingMode) throws NoMatchingContactsException {
if (sortingMode != NONE) {
if (state == BY_STATE && sortingMode == ALPHABETICALLY) {
Log.info(" sorting from state to alphabetical ") ;
// going from state to alphabetical sort
//counting number of items to reinsert
if (contactItems.size() > 0) {
Vector toInsert = new Vector(5);
ContactItem ci = ((ContactItem)contactItems.elementAt(0));
while (ci.getState() != ContactItem.ADDRESS_STATE_NONE) {
ci.setState(ContactItem.ADDRESS_STATE_NONE);
contactItems.removeElementAt(0);
toInsert.addElement(ci);
if (!(contactItems.size() > 0)) {
// we can take the next element
Log.debug("exiting while loop");
break;
} else {
Log.debug("we can take the next one!");
ci = ((ContactItem)contactItems.elementAt(0));
}
Log.info("got contact " + ci.getContact().getVisibleName());
}
// toInsert now contains elements to be inserted in
// contactItems
Enumeration e = toInsert.elements();
while (e.hasMoreElements()) {
insertItem((ContactItem)e.nextElement(), alphabeticalComparator);
}
}
} else if (sortingMode == BY_STATE) {
// going to state sort
Log.info(" sorting by state ") ;
ContactItem ci;
int p =0;
for (int i = 0; i < contactItems.size(); i++) {
ci = (ContactItem)contactItems.elementAt(i);
if (ci.getState() != ContactItem.ADDRESS_STATE_NONE) {
Log.info("moving contact " + ci.getContact().getVisibleName() +
" from position " + i + " to position " + p);
contactItems.removeElementAt(i);
contactItems.insertElementAt(ci, p);
p++;
}
}
}
notifyListener(ContactItemCollectionEvent.EVENT_CODE_SORT_CONTACTITEM_LIST);
this.state = sortingMode;
}
if (toBeFiltered) {
filter();
}
return filteredVector;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?