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

📄 funcanvascontactlist.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    
    
    protected void setContactItemList(ContactItem[] contactItems) {
        
        super.setElements(contactItems);
        this.contactItems = contactItems;
        setContactCount(getRmsCount());
        
        repaint();
    }
    
    public Contact getActiveContact() {
        return contactItems[getActiveElement()].getContact();
    }
    /**
     * set the search string array to the given array.
     *
     * @param string an array containing the strings to be used by
     * isVisible function ( if the contact first name, last name or visiblename
     * starts for one of the given strings, the element is visible )
     */
    public void setSearchString(String[] string) {
        this.searchString = string;
        //this find the first activable element
        rewindList();
        repaint();
        
    }
    
    void addAddress(Address address) {
        try {
            message.addRecipient(address);
            addCommand(okCommand);
        } catch (MailException ex) {
            ex.printStackTrace();
            Log.error("Unable to add recipient "+address.getEmail() + "to message");
        }
        this.initializeContactList();
        
    }
    
    void resetActiveElementHeight() {
        contactItems[getActiveElement()].resetHeight();
    }
    
    protected void enableDisableOKCommand() {
        
        for (int i=0; i< contactItems.length; i++) {
            if (contactItems[i].state != NONE){
                addCommand(okCommand);
                break;
                
            } else {
                removeCommand(okCommand);
            }
            
        }
    }
    
    private int getRmsCount() {
        return  UIController.getContactManager().getContactCount();
    }
    
    /**
     * set the contact count to be shown in the upper left corner
     * @param count the contact count to be shown in the upper left corner,
     * if a negative number is passed, the contact count used will be the
     * total number of contacts in rms
     */
    public void setContactCount(int count) {
        if (count < 0 ) count = getRmsCount();
        contactCount = count;
        
    }
    
   
    
    /**
     * enable or disable the sync command and the reset contacts command
     * @param enable if true enable the command, if false disable the command
     */
    public void enableSyncCommand(boolean enable){
        if (enable) {
            addCommand(syncCommand);
            addCommand(resetContactsCommand);
        } else {
            removeCommand(syncCommand);
            removeCommand(resetContactsCommand);
        }
    }
    
    /**
     * if the given contact is in the list, it is set as the active element.
     * @param contact the contact that must be set as active element
     * 
     */
    public void activateElement(Contact contact){
        int id = -1;
        for (int i=0; i< contactItems.length; i++) {
            if ((contactItems[i].getContact().getDefaultEmail()).equals(contact.getDefaultEmail()) ){
                id = i;// contactItems[i].getElementID();
                break;
            }
        }
        if (id > 0) {
            setActiveElement(id);
        }

    }
    
    /**
     * Scan the contact list looking for a contact with given 
     * email addres as the default email
     * @param email is the given contact mail
     * @return the Contact with the give email address , or null if
     * the contact is not present
     */
    public Contact exists(String  email){
        for (int i=0; i< contactItems.length; i++) {
            if ((contactItems[i].getContact().getDefaultEmail()).equals(email) ){
                return contactItems[contactItems[i].getElementID()].getContact();
            }
        }
        return null;
    }
    
    
    /**
     * remove the specified contat item from the 
     * contactitem vector. 
     * @param toRemove the position of the item 
     * to be removed in the elemnts vector 
     */
    private void removeContactItem(int toRemove){
        
        
        Vector helpVector = new Vector();
        for(int i=0; i< elements.length; i++){
            if(i!=toRemove){
                helpVector.addElement(elements[i]);
            }
        }
        contactItems = new ContactItem[elements.length -1];
        helpVector.copyInto(contactItems);
    }
  
    
    /**
     * get the rmscontact loader or create a new one if null
     *
     * @return the rmsContactLoader
     */
    private Runnable getRMSContactLoader() {
        if (rmsContactLoader == null) {
            rmsContactLoader = new RMSContactLoader();
        }
        
        return rmsContactLoader;
        
    }
    
//
// -------------------- RMSContactLoader Class -----------------------
//
    
    /**
     * inner class that loads contacts from the RMS
     */
    
    class RMSContactLoader implements Runnable {
        
        public RMSContactLoader() {
            
        }
        
        public void loadContacts() {
            try {
                //Log.debug(this, "Starting to load contacts from RMS!");
                
                setTitle(Localization.getMessages().LOADING_CONTACTS);
                //Remove command because Contact ID from cache is not the same with
                //Contact ID from RMS
                removeItemCommands();
                int count = UIController.getContactManager().getContactCount();
                Enumeration e = UIController.getContactManager().getContactList();
                
                Contact[] contactList = new Contact[count];
                ContactItem[] newContactItemList = new ContactItem[count];
                
                int i=0;
                
                while (e.hasMoreElements()) {
                    Contact c = (Contact) e.nextElement();
                    contactList[i] = c;
                    newContactItemList[i] = new ContactItem(c,i);
                    i++;
                }
                
                
                // orrible fix here. this is needed for safe thread sync. I suppose.
                // the problem is that sometimes the enumeration seems to be
                // smaller than the number of items returned by getContactCount,
                // so we need this to avoid nullpointers
                if (i == count) {
                    setContactItemList(newContactItemList);
                    Cache.saveContactSnapShot(Cache.VCARD, contactList);
                } else {
                    //if somethig went wrong, we try again!
                    loadContacts();
                }
                
                
            } catch (ContactManagerException ex) {
                Log.error(this, "Error loading contacts from rms");
                ex.printStackTrace();
            }  catch (RecordStoreException ex) {
                Log.error(this, "Error saving contact snapshot to rms");
                ex.printStackTrace();
            }  catch (IOException ex) {
                Log.error(this, "Error saving contact snapshot to rms");
                ex.printStackTrace();
            } finally {
                
                setTitle(getDefaultTitle());
                addItemCommands();
                //     Log.debug(this, "end loading contacts from rms!");
            }
        }
        
        public void run() {
            loadContacts();
        }
    }
    
    
    
//
// ----------------------------- AddressPointer Class --------------------------
//
    
    
    /**
     * return an object with index, addresstype and emailtype.
     * this class is used when searching for rms contacts mathching the ones
     * in the message given.
     */
    class AddressPointer {
        int index;
        int addressType;
        int emailType;
        
        public AddressPointer(int index, int addressType, int emailType) {
            this.index = index;
            this.addressType = addressType;
            this.emailType = emailType;
        }
        
    }
    
    
//
// ----------------------------- ContactItem Class --------------------------
//
    
    /**
     * item class to paint a contact and handle recipent settings
     *
     *<p> a contactItem has a selected Email amongst the email addresses of the given
     * contact multiple email addresses, and a state that can be one of
     *  <ul>
     *   <li>NONE</li>
     *   <li>Address.TO</li>
     *   <li>Address.CC</li>
     *   <li>Address.BCC</li>
     *  </ul>
     */
    class ContactItem {
        
        /**
         * states can be Address.TO, Address.CC, Address.BCC, or FunContactItem.NONE
         */
        private int state=NONE;
        
        
        private Contact contact;
        
        private int id;
        
        private int height =0;
        
        private int maxLabelWidth;
        
        private int selectedEmailIndex = Contact.DEFAULT_EMAIL;
        
        
        /**
         * create a new ContactItem with given data and default state and email
         *
         * @param contact the contact
         * @param elementId the elementID of this contact, is used by the
         * ContactItem to check if this is the active element
         */
        
        public ContactItem(Contact contact, int elementId) {
            this(contact, elementId, NONE, Contact.DEFAULT_EMAIL);
        }
        
        /**
         * create a new ContactItem with given data
         *
         * @param contact the contact
         * @param elementId the elementID of this contact, is used by the
         * ContactItem to check if this is the active element
         * @param state one of Address.TO Address.CC Address.BCC or
         * FunCanvasContactList.NONE
         *@param selectedEmail one of Contact.DEFAULT, Contact.SECONDARY_EMAIL
         * or Contact.TERTIARY_EMAIL
         */
        public ContactItem(Contact contact, int elementId,  int state, int selectedEmail) {
            
            
            this.contact  = contact;
            this.id = elementId;
            this.state = state;
            this.selectedEmailIndex = selectedEmail;
            
            
        }
        /**
         * reset the heigth of this contactItem.

⌨️ 快捷键说明

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