contactgroup.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 899 行 · 第 1/2 页

SVN-BASE
899
字号
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.spark.ui;

import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.VerticalFlowLayout;
import org.jivesoftware.spark.component.panes.CollapsiblePane;
import org.jivesoftware.spark.component.renderer.JPanelRenderer;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * Container representing a RosterGroup within the Contact List.
 */
public class ContactGroup extends CollapsiblePane implements MouseListener {
    private List<ContactItem> contactItems = new ArrayList<ContactItem>();
    private List<ContactGroup> contactGroups = new ArrayList<ContactGroup>();
    private List<ContactGroupListener> listeners = new ArrayList<ContactGroupListener>();
    private List<ContactItem> offlineContacts = new ArrayList<ContactItem>();

    private String groupName;
    private DefaultListModel model;
    private JList contactItemList;
    private boolean sharedGroup;
    private JPanel listPanel;

    // Used to display no contacts in list.
    private final ContactItem noContacts = new ContactItem("There are no online contacts in this group.", null);

    private final ListMotionListener motionListener = new ListMotionListener();

    private boolean canShowPopup;

    private MouseEvent mouseEvent;

    private LocalPreferences preferences;


    /**
     * Create a new ContactGroup.
     *
     * @param groupName the name of the new ContactGroup.
     */
    public ContactGroup(String groupName) {
        // Initialize Model and UI
        model = new DefaultListModel();
        contactItemList = new JList(model);

        preferences = SettingsManager.getLocalPreferences();

        setTitle(getGroupTitle(groupName));

        // Use JPanel Renderer
        contactItemList.setCellRenderer(new JPanelRenderer());

        this.groupName = groupName;

        listPanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false));
        listPanel.add(contactItemList, listPanel);
        this.setContentPane(listPanel);

        if (!isOfflineGroup()) {
            contactItemList.setDragEnabled(true);
            contactItemList.setTransferHandler(new ContactGroupTransferHandler());
        }

        // Allow for mouse events to take place on the title bar
        getTitlePane().addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                checkPopup(e);
            }

            public void mouseReleased(MouseEvent e) {
                checkPopup(e);
            }

            public void checkPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    e.consume();
                    fireContactGroupPopupEvent(e);
                }
            }
        });

        // Items should have selection listener
        contactItemList.addMouseListener(this);

        contactItemList.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent keyEvent) {

            }

            public void keyPressed(KeyEvent keyEvent) {
                if (keyEvent.getKeyChar() == KeyEvent.VK_ENTER) {
                    ContactItem item = (ContactItem)contactItemList.getSelectedValue();
                    fireContactItemDoubleClicked(item);
                }

                ContactList.activeKeyEvent = keyEvent;
            }

            public void keyReleased(KeyEvent keyEvent) {
                ContactList.activeKeyEvent = null;
            }
        });

        noContacts.getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, 11));
        noContacts.getNicknameLabel().setForeground(Color.GRAY);
        model.addElement(noContacts);

        // Add Popup Window
        addPopupWindow();
    }

    /**
     * Adds a new offline contact.
     *
     * @param nickname the nickname of the offline contact.
     * @param jid      the jid of the offline contact.
     */
    public void addOfflineContactItem(String nickname, String jid, String status) {
        // Build new ContactItem
        final ContactItem offlineItem = new ContactItem(nickname, jid);
        offlineItem.setGroupName(getGroupName());

        final Presence offlinePresence = PresenceManager.getPresence(jid);
        offlineItem.setPresence(offlinePresence);

        // set offline icon
        offlineItem.setIcon(PresenceManager.getIconFromPresence(offlinePresence));

        // Set status if applicable.
        if (ModelUtil.hasLength(status)) {
            offlineItem.setStatusText(status);
        }

        // Add to offlien contacts.
        offlineContacts.add(offlineItem);

        insertOfflineContactItem(offlineItem);
    }

    /**
     * Inserts a new offline <code>ContactItem</code> into the ui model.
     *
     * @param offlineItem the ContactItem to add.
     */
    public void insertOfflineContactItem(ContactItem offlineItem) {
        if (model.contains(offlineItem)) {
            return;
        }

        if (!preferences.isOfflineGroupVisible()) {
            Collections.sort(offlineContacts, itemComparator);
            int index = offlineContacts.indexOf(offlineItem);

            int totalListSize = contactItems.size();
            int newPos = totalListSize + index;

            if (newPos > model.size()) {
                newPos = model.size();
            }

            model.insertElementAt(offlineItem, newPos);

            if (model.contains(noContacts)) {
                model.removeElement(noContacts);
            }
        }
    }

    /**
     * Removes an offline <code>ContactItem</code> from the Offline contact
     * model and ui.
     *
     * @param item the offline contact item to remove.
     */
    public void removeOfflineContactItem(ContactItem item) {
        offlineContacts.remove(item);
        removeContactItem(item);
    }

    /**
     * Removes an offline <code>ContactItem</code> from the offline contact model and ui.
     *
     * @param jid the offline contact item to remove.
     */
    public void removeOfflineContactItem(String jid) {
        final List<ContactItem> items = new ArrayList<ContactItem>(offlineContacts);
        for (ContactItem item : items) {
            if (item.getJID().equals(jid)) {
                removeOfflineContactItem(item);
            }
        }
    }

    /**
     * Toggles the visibility of Offline Contacts.
     *
     * @param show true if offline contacts should be shown, otherwise false.
     */
    public void toggleOfflineVisibility(boolean show) {
        final List<ContactItem> items = new ArrayList<ContactItem>(offlineContacts);
        for (ContactItem item : items) {
            if (show) {
                insertOfflineContactItem(item);
            }
            else {
                model.removeElement(item);
            }
        }


    }


    /**
     * Adds a <code>ContactItem</code> to the ContactGroup.
     *
     * @param item the ContactItem.
     */
    public void addContactItem(ContactItem item) {
        // Remove from offline group if it exists
        removeOfflineContactItem(item.getJID());

        if (model.contains(noContacts)) {
            model.remove(0);
        }

        if ("Offline Group".equals(groupName)) {
            item.getNicknameLabel().setFont(new Font("Dialog", Font.PLAIN, 11));
            item.getNicknameLabel().setForeground(Color.GRAY);
        }

        item.setGroupName(getGroupName());
        contactItems.add(item);

        List<ContactItem> tempItems = getContactItems();


        Collections.sort(tempItems, itemComparator);


        int index = tempItems.indexOf(item);


        Object[] objs = contactItemList.getSelectedValues();

        model.insertElementAt(item, index);

        int[] intList = new int[objs.length];
        for (int i = 0; i < objs.length; i++) {
            ContactItem contact = (ContactItem)objs[i];
            intList[i] = model.indexOf(contact);
        }

        if (intList.length > 0) {
            contactItemList.setSelectedIndices(intList);
        }

        fireContactItemAdded(item);
    }

    /**
     * Call whenever the UI needs to be updated.
     */
    public void fireContactGroupUpdated() {
        contactItemList.validate();
        contactItemList.repaint();
        updateTitle();
    }

    public void addContactGroup(ContactGroup contactGroup) {
        final JPanel panel = new JPanel(new GridBagLayout());
        panel.add(contactGroup, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 15, 0, 0), 0, 0));
        panel.setBackground(Color.white);
        contactGroup.setSubPane(true);

        // contactGroup.setStyle(CollapsiblePane.TREE_STYLE);
        listPanel.add(panel);
        contactGroups.add(contactGroup);
    }

    /**
     * Removes a child ContactGroup.
     *
     * @param contactGroup the contact group to remove.
     */
    public void removeContactGroup(ContactGroup contactGroup) {
        Component[] comps = listPanel.getComponents();
        for (int i = 0; i < comps.length; i++) {
            Component comp = comps[i];
            if (comp instanceof JPanel) {
                JPanel panel = (JPanel)comp;
                ContactGroup group = (ContactGroup)panel.getComponent(0);
                if (group == contactGroup) {
                    listPanel.remove(panel);
                    break;
                }
            }
        }


        contactGroups.remove(contactGroup);
    }

    public void setPanelBackground(Color color) {
        Component[] comps = listPanel.getComponents();
        for (int i = 0; i < comps.length; i++) {
            Component comp = comps[i];
            if (comp instanceof JPanel) {
                JPanel panel = (JPanel)comp;
                panel.setBackground(color);
            }
        }

    }

    /**
     * Returns a ContactGroup based on it's name.
     *
     * @param groupName the name of the group.
     * @return the ContactGroup.
     */
    public ContactGroup getContactGroup(String groupName) {
        final Iterator groups = new ArrayList(contactGroups).iterator();
        while (groups.hasNext()) {
            ContactGroup group = (ContactGroup)groups.next();
            if (group.getGroupName().equals(groupName)) {
                return group;
            }
        }

        return null;
    }

    /**
     * Removes a <code>ContactItem</code>.
     *
     * @param item the ContactItem to remove.
     */
    public void removeContactItem(ContactItem item) {
        contactItems.remove(item);

        model.removeElement(item);
        updateTitle();

        fireContactItemRemoved(item);
    }

    /**
     * Returns a <code>ContactItem</code> by the nickname the user has been assigned.
     *
     * @param nickname the nickname of the user.
     * @return the ContactItem.
     */
    public ContactItem getContactItemByNickname(String nickname) {
        final Iterator iter = new ArrayList(contactItems).iterator();
        while (iter.hasNext()) {
            ContactItem item = (ContactItem)iter.next();
            if (item.getNickname().equals(nickname)) {
                return item;
            }
        }
        return null;
    }

    /**
     * Returns a <code>ContactItem</code> by the users bare bareJID.
     *
     * @param bareJID the bareJID of the user.
     * @return the ContactItem.
     */
    public ContactItem getContactItemByJID(String bareJID) {
        final Iterator iter = new ArrayList(contactItems).iterator();
        while (iter.hasNext()) {
            ContactItem item = (ContactItem)iter.next();
            if (item.getJID().equals(bareJID)) {
                return item;
            }
        }
        return null;
    }

    /**
     * Returns all <code>ContactItem</cod>s in the ContactGroup.
     *
     * @return all ContactItems.
     */
    public List<ContactItem> getContactItems() {
        final List<ContactItem> list = new ArrayList<ContactItem>(contactItems);
        Collections.sort(list, itemComparator);
        return list;
    }

    /**
     * Returns the name of the ContactGroup.
     *
     * @return the name of the ContactGroup.
     */
    public String getGroupName() {
        return groupName;
    }


    public void mouseClicked(MouseEvent e) {

        Object o = contactItemList.getSelectedValue();
        if (!(o instanceof ContactItem)) {

⌨️ 快捷键说明

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