contactlist.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 1,679 行 · 第 1/5 页

JAVA
1,679
字号
            if (item != null) {
                int numberOfMillisecondsInTheFuture = 3000;
                Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);

                // Only run through if the users presence was online before.
                if (item.getPresence().isAvailable()) {
                    item.showUserGoingOfflineOnline();
                    item.setIcon(SparkRes.getImageIcon(SparkRes.CLEAR_BALL_ICON));
                    group.fireContactGroupUpdated();

                    final Timer offlineTimer = new Timer();
                    offlineTimer.schedule(new TimerTask() {
                        public void run() {
                            // Check to see if the user is offline, if so, move them to the offline group.
                            Presence userPresence = PresenceManager.getPresence(bareJID);
                            if (userPresence.isAvailable()) {
                                return;
                            }

                            item.setPresence(presence);

                            // Check for ContactItemHandler.
                            group.removeContactItem(item);
                            checkGroup(group);

                            if (offlineGroup.getContactItemByJID(item.getJID()) == null) {
                                moveToOffline(item);
                                offlineGroup.fireContactGroupUpdated();
                            }
                        }
                    }, timeToRun);
                }
            }
            else {
                final ContactItem offlineItem = offlineGroup.getContactItemByJID(bareJID);
                if (offlineItem != null) {
                    offlineItem.setPresence(presence);
                }
            }
        }
    }

    /**
     * Moves a user to each group they belong to.
     *
     * @param bareJID  the bareJID of the user to show as online.
     * @param entry    the <code>RosterEntry</code> of the user.
     * @param presence the users presence.
     */
    private void changeOfflineToOnline(String bareJID, final RosterEntry entry, Presence presence) {
        // Move out of offline group. Add to all groups.
        ContactItem offlineItem = offlineGroup.getContactItemByJID(bareJID);
        if (offlineItem == null) {
            return;
        }

        offlineGroup.removeContactItem(offlineItem);

        // Add To all groups it belongs to.
        boolean isFiled = false;
        for (RosterGroup rosterGroup : entry.getGroups()) {
            isFiled = true;
            ContactGroup contactGroup = getContactGroup(rosterGroup.getName());
            if (contactGroup == null) {
                // Create Contact Group
                contactGroup = addContactGroup(rosterGroup.getName());
            }

            if (contactGroup != null) {
                String name = entry.getName();
                if (name == null) {
                    name = entry.getUser();
                }

                ContactItem contactItem = null;
                if (contactGroup.getContactItemByJID(entry.getUser()) == null) {
                    contactItem = new ContactItem(name, entry.getUser());
                    contactGroup.addContactItem(contactItem);
                }
                else if (contactGroup.getContactItemByJID(entry.getUser()) != null) {
                    // If the user is in their, but without a nice presence.
                    contactItem = contactGroup.getContactItemByJID(entry.getUser());
                }

                contactItem.setPresence(presence);
                contactItem.setAvailable(true);
                toggleGroupVisibility(contactGroup.getGroupName(), true);

                contactItem.showUserComingOnline();
                contactGroup.fireContactGroupUpdated();


                int numberOfMillisecondsInTheFuture = 5000;
                Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);
                Timer timer = new Timer();

                final ContactItem staticItem = contactItem;
                final ContactGroup staticGroup = contactGroup;
                timer.schedule(new TimerTask() {
                    public void run() {
                        staticItem.updatePresenceIcon(staticItem.getPresence());
                        staticGroup.fireContactGroupUpdated();
                    }
                }, timeToRun);
            }
        }

        if (!isFiled) {
            String name = entry.getName();
            if (name == null) {
                name = entry.getUser();
            }
            ContactItem contactItem = new ContactItem(name, entry.getUser());
            unfiledGroup.addContactItem(contactItem);
            contactItem.setPresence(presence);
            contactItem.setAvailable(true);

            unfiledGroup.setVisible(true);
            unfiledGroup.fireContactGroupUpdated();
        }
    }


    /**
     * Called to build the initial ContactList.
     */
    private void buildContactList() {
        XMPPConnection con = SparkManager.getConnection();
        final Roster roster = con.getRoster();


        roster.addRosterListener(this);

        // Add All Groups to List
        for (RosterGroup group : roster.getGroups()) {
            addContactGroup(group.getName());
        }

        for (RosterGroup group : roster.getGroups()) {
            ContactGroup contactGroup = getContactGroup(group.getName());

            for (RosterEntry entry : group.getEntries()) {
                String name = entry.getName();
                if (!ModelUtil.hasLength(name)) {
                    name = entry.getUser();
                }

                ContactItem contactItem = new ContactItem(name, entry.getUser());
                contactItem.setPresence(new Presence(Presence.Type.unavailable));
                if ((entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from)
                    && RosterPacket.ItemStatus.SUBSCRIPTION_PENDING == entry.getStatus()) {
                    // Add to contact group.
                    contactGroup.addContactItem(contactItem);
                    contactGroup.setVisible(true);
                }
                else {
                    if (offlineGroup.getContactItemByJID(entry.getUser()) == null) {
                        moveToOffline(contactItem);
                    }
                }
            }

        }

        // Add Unfiled Group
        // addContactGroup(unfiledGroup);
        for (RosterEntry entry : roster.getUnfiledEntries()) {
            String name = entry.getName();
            if (!ModelUtil.hasLength(name)) {
                name = entry.getUser();
            }

            ContactItem contactItem = new ContactItem(name, entry.getUser());
            moveToOffline(contactItem);
        }
        unfiledGroup.setVisible(false);
    }

    /**
     * Called when NEW entries are added.
     *
     * @param addresses the address added.
     */
    public void entriesAdded(final Collection addresses) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Roster roster = SparkManager.getConnection().getRoster();

                Iterator jids = addresses.iterator();
                while (jids.hasNext()) {
                    String jid = (String)jids.next();
                    RosterEntry entry = roster.getEntry(jid);
                    addUser(entry);
                }
            }
        });
    }

    /**
     * Adds a single user to the ContactList.
     *
     * @param entry the <code>RosterEntry</code> of the the user.
     */
    private void addUser(RosterEntry entry) {
        String name = entry.getName();
        if (!ModelUtil.hasLength(name)) {
            name = entry.getUser();
        }

        String nickname = entry.getName();
        if (!ModelUtil.hasLength(nickname)) {
            nickname = entry.getUser();
        }

        ContactItem newContactItem = new ContactItem(nickname, entry.getUser());

        if (entry != null && (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from)) {
            // Ignore, since the new user is pending to be added.
            for (RosterGroup group : entry.getGroups()) {
                ContactGroup contactGroup = getContactGroup(group.getName());
                if (contactGroup == null) {
                    contactGroup = addContactGroup(group.getName());
                }

                boolean isPending = entry != null && (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from)
                    && RosterPacket.ItemStatus.SUBSCRIPTION_PENDING == entry.getStatus();
                if (isPending) {
                    contactGroup.setVisible(true);
                }
                contactGroup.addContactItem(newContactItem);

            }
            return;
        }
        else {
            moveToOffline(newContactItem);
        }

        // Update users icon
        Presence presence = SparkManager.getConnection().getRoster().getPresence(entry.getUser());
        try {
            updateUserPresence(presence);
        }
        catch (Exception e) {
            Log.error(e);
        }
    }

    /**
     * Handle when the Roster changes based on subscription notices.
     *
     * @param addresses
     */
    public void entriesUpdated(final Collection addresses) {
        handleEntriesUpdated(addresses);
    }

    /**
     * Called when users are removed from the roster.
     *
     * @param addresses the addresses removed from the roster.
     */
    public void entriesDeleted(final Collection addresses) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Iterator jids = addresses.iterator();
                while (jids.hasNext()) {
                    String jid = (String)jids.next();
                    removeContactItem(jid);
                }
            }
        });

    }

    /**
     * Handles any presence modifications of a user(s).
     *
     * @param addresses the Collection of addresses that have been modified within the Roster.
     */
    private synchronized void handleEntriesUpdated(final Collection addresses) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Roster roster = SparkManager.getConnection().getRoster();

                Iterator jids = addresses.iterator();
                while (jids.hasNext()) {
                    String jid = (String)jids.next();
                    RosterEntry rosterEntry = roster.getEntry(jid);
                    if (rosterEntry != null) {
                        // Check for new Roster Groups and add them if they do not exist.
                        boolean isUnfiled = true;
                        for (RosterGroup group : rosterEntry.getGroups()) {
                            isUnfiled = false;

                            // Handle if this is a new Entry in a new Group.
                            if (getContactGroup(group.getName()) == null) {
                                // Create group.
                                ContactGroup contactGroup = addContactGroup(group.getName());
                                contactGroup.setVisible(false);
                                contactGroup = getContactGroup(group.getName());
                                String name = rosterEntry.getName();
                                if (name == null) {
                                    name = rosterEntry.getUser();
                                }

                                ContactItem contactItem = new ContactItem(name, rosterEntry.getUser());
                                contactGroup.addContactItem(contactItem);
                                Presence presence = PresenceManager.getPresence(jid);
                                contactItem.setPresence(presence);
                                if (presence.isAvailable()) {
                                    contactGroup.setVisible(true);
                                }
                            }
                            else {
                                ContactGroup contactGroup = getContactGroup(group.getName());
                                ContactItem item = offlineGroup.getContactItemByJID(jid);
                                if (item == null) {
                                    item = contactGroup.getContactItemByJID(jid);
                                }
                                // Check to see if this entry is new to a pre-existing group.
                                if (item == null) {
                                    String name = rosterEntry.getName();
                                    if (name == null) {
                                        name = rosterEntry.getUser();
                                    }
                                    item = new ContactItem(name, rosterEntry.getUser());
                                    Presence presence = PresenceManager.getPresence(jid);
                                    item.setPresence(presence);
                                    if (presence.isAvailable()) {
                                        contactGroup.addContactItem(item);
                                        contactGroup.fireContactGroupUpdated();
                                    }
                                    else {
                                        moveToOffline(item);
                                        offlineGroup.fireContactGroupUpdated();

⌨️ 快捷键说明

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