contactgroup.java.svn-base

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

SVN-BASE
899
字号
            return;
        }

        // Iterator through rest
        ContactItem item = (ContactItem)o;

        if (e.getClickCount() == 2) {
            fireContactItemDoubleClicked(item);
        }
        else if (e.getClickCount() == 1) {
            fireContactItemClicked(item);
        }
    }

    public void mouseEntered(MouseEvent e) {
        int loc = contactItemList.locationToIndex(e.getPoint());

        Object o = model.getElementAt(loc);
        if (!(o instanceof ContactItem)) {
            return;
        }

        ContactItem item = (ContactItem)o;
        if (item == null) {
            return;
        }

        contactItemList.setCursor(GraphicUtils.HAND_CURSOR);
    }

    public void mouseExited(MouseEvent e) {
        Object o = null;
        try {
            int loc = contactItemList.locationToIndex(e.getPoint());
            if (loc == -1) {
                return;
            }

            o = model.getElementAt(loc);
            if (!(o instanceof ContactItem)) {
                ContactInfoWindow.getInstance().dispose();
                return;
            }
        }
        catch (Exception e1) {
            Log.error(e1);
            return;
        }

        ContactItem item = (ContactItem)o;
        if (item == null) {
            return;
        }
        contactItemList.setCursor(GraphicUtils.DEFAULT_CURSOR);

    }

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

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

    private void checkPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            // Otherwise, handle single selection
            int index = contactItemList.locationToIndex(e.getPoint());
            if (index != -1) {
                int[] indexes = contactItemList.getSelectedIndices();
                boolean selected = false;
                for (int i = 0; i < indexes.length; i++) {
                    int o = indexes[i];
                    if (index == o) {
                        selected = true;
                    }
                }

                if (!selected) {
                    contactItemList.setSelectedIndex(index);
                    fireContactItemClicked((ContactItem)contactItemList.getSelectedValue());
                }
            }


            final Collection selectedItems = SparkManager.getChatManager().getSelectedContactItems();
            if (selectedItems.size() > 1) {
                firePopupEvent(e, selectedItems);
                return;
            }
            else if (selectedItems.size() == 1) {
                final ContactItem contactItem = (ContactItem)selectedItems.iterator().next();
                firePopupEvent(e, contactItem);
            }
        }
    }

    /**
     * Add a <code>ContactGroupListener</code>.
     *
     * @param listener the ContactGroupListener.
     */
    public void addContactGroupListener(ContactGroupListener listener) {
        listeners.add(listener);
    }

    /**
     * Removes a <code>ContactGroupListener</code>.
     *
     * @param listener the ContactGroupListener.
     */
    public void removeContactGroupListener(ContactGroupListener listener) {
        listeners.remove(listener);
    }

    private void fireContactItemClicked(ContactItem item) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).contactItemClicked(item);
        }
    }

    private void fireContactItemDoubleClicked(ContactItem item) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).contactItemDoubleClicked(item);
        }
    }


    private void firePopupEvent(MouseEvent e, ContactItem item) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).showPopup(e, item);
        }
    }

    private void firePopupEvent(MouseEvent e, Collection items) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).showPopup(e, items);
        }
    }

    private void fireContactGroupPopupEvent(MouseEvent e) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).contactGroupPopup(e, this);
        }
    }

    private void fireContactItemAdded(ContactItem item) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).contactItemAdded(item);
        }
    }

    private void fireContactItemRemoved(ContactItem item) {
        final Iterator iter = new ArrayList(listeners).iterator();
        while (iter.hasNext()) {
            ((ContactGroupListener)iter.next()).contactItemRemoved(item);
        }
    }

    private void updateTitle() {
        if ("Offline Group".equals(groupName)) {
            setTitle("Offline Group");
            return;
        }

        int count = 0;
        List list = new ArrayList(getContactItems());
        int size = list.size();
        for (int i = 0; i < size; i++) {
            ContactItem it = (ContactItem)list.get(i);
            if (it.isAvailable()) {
                count++;
            }
        }

        setTitle(getGroupTitle(groupName) + " (" + count + " online)");


        if (model.getSize() == 0) {
            model.addElement(noContacts);
        }
    }

    /**
     * Returns the containing <code>JList</code> of the ContactGroup.
     *
     * @return the JList.
     */
    public JList getList() {
        return contactItemList;
    }

    /**
     * Clears all selections within this group.
     */
    public void clearSelection() {
        contactItemList.clearSelection();
    }

    public void removeAllContacts() {
        // Remove all users from online group.
        Iterator contactItems = new ArrayList(getContactItems()).iterator();
        while (contactItems.hasNext()) {
            ContactItem item = (ContactItem)contactItems.next();
            removeContactItem(item);
        }

        // Remove all users from offline group.
        for (ContactItem item : getOfflineContacts()) {
            removeOfflineContactItem(item);
        }
    }

    /**
     * Returns true if the ContactGroup contains available users.
     *
     * @return true if the ContactGroup contains available users.
     */
    public boolean hasAvailableContacts() {
        final Iterator iter = contactGroups.iterator();
        while (iter.hasNext()) {
            ContactGroup group = (ContactGroup)iter.next();

            if (group.hasAvailableContacts()) {
                return true;
            }
        }

        Iterator contacts = getContactItems().iterator();
        while (contacts.hasNext()) {
            ContactItem item = (ContactItem)contacts.next();
            if (item.getPresence() != null) {
                return true;
            }
        }
        return false;
    }

    public Collection<ContactItem> getOfflineContacts() {
        return new ArrayList<ContactItem>(offlineContacts);
    }

    /**
     * Sorts ContactItems.
     */
    final Comparator<ContactItem> itemComparator = new Comparator() {
        public int compare(Object contactItemOne, Object contactItemTwo) {
            final ContactItem item1 = (ContactItem)contactItemOne;
            final ContactItem item2 = (ContactItem)contactItemTwo;
            return item1.getNickname().toLowerCase().compareTo(item2.getNickname().toLowerCase());
        }
    };

    /**
     * Returns true if this ContactGroup is the Offline Group.
     *
     * @return true if OfflineGroup.
     */
    public boolean isOfflineGroup() {
        return "Offline Group".equals(getGroupName());
    }

    /**
     * Returns true if this ContactGroup is the Unfiled Group.
     *
     * @return true if UnfiledGroup.
     */
    public boolean isUnfiledGroup() {
        return "Unfiled".equals(getGroupName());
    }

    public String toString() {
        return getGroupName();
    }

    /**
     * Returns true if ContactGroup is a Shared Group.
     *
     * @return true if Shared Group.
     */
    public boolean isSharedGroup() {
        return sharedGroup;
    }

    /**
     * Set to true if this ContactGroup is a shared Group.
     *
     * @param sharedGroup true if shared group.
     */
    protected void setSharedGroup(boolean sharedGroup) {
        this.sharedGroup = sharedGroup;
        if (sharedGroup) {
            setToolTipText(Res.getString("message.is.shared.group", getGroupName()));
        }
    }

    /**
     * Returns all Selected Contacts within the ContactGroup.
     *
     * @return all selected ContactItems.
     */
    public List getSelectedContacts() {
        final List items = new ArrayList();
        Object[] selections = contactItemList.getSelectedValues();
        final int no = selections != null ? selections.length : 0;
        for (int i = 0; i < no; i++) {
            ContactItem item = (ContactItem)selections[i];
            items.add(item);
        }
        return items;
    }

    public JPanel getContainerPanel() {
        return listPanel;
    }

    public Collection getContactGroups() {
        return contactGroups;
    }

    /**
     * Lets make sure that the panel doesn't stretch past the
     * scrollpane view pane.
     *
     * @return the preferred dimension
     */
    public Dimension getPreferredSize() {
        final Dimension size = super.getPreferredSize();
        size.width = 0;
        return size;
    }

    /**
     * Sets the name of group.
     *
     * @param groupName the contact group name.
     */
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    /**
     * Returns the "pretty" title of the ContactGroup.
     *
     * @param title the title.
     * @return the new title.
     */
    public String getGroupTitle(String title) {
        int lastIndex = title.lastIndexOf("::");
        if (lastIndex != -1) {
            title = title.substring(lastIndex + 2);
        }

        return title;
    }

    /**
     * Returns true if the group is nested.
     *
     * @param groupName the name of the group.
     * @return true if the group is nested.
     */
    public boolean isSubGroup(String groupName) {
        return groupName.indexOf("::") != -1;
    }

    /**
     * Returns true if this group is nested.
     *
     * @return true if nested.
     */
    public boolean isSubGroup() {
        return isSubGroup(getGroupName());
    }

    /**
     * Returns the underlying container for the JList.
     *
     * @return the underlying container of the JList.
     */
    public JPanel getListPanel() {
        return listPanel;
    }

    /**
     * Adds an internal popup listesner.
     */
    private void addPopupWindow() {
        final Timer timer = new Timer(500, new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                canShowPopup = true;
            }
        });

        contactItemList.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent mouseEvent) {
                timer.start();
            }

            public void mouseExited(MouseEvent mouseEvent) {
                timer.stop();
                canShowPopup = false;
                ContactInfoWindow.getInstance().dispose();
            }
        });


        contactItemList.addMouseMotionListener(motionListener);
    }


    private class ListMotionListener extends MouseMotionAdapter {

        public void mouseMoved(MouseEvent e) {
            if (e != null) {
                mouseEvent = e;
            }

            if (!canShowPopup) {
                return;
            }

            if (e == null) {
                return;
            }

            displayWindow(e);
        }
    }

    /**
     * Displays the <code>ContactInfoWindow</code>.
     *
     * @param e the mouseEvent that triggered this event.
     */
    private void displayWindow(MouseEvent e) {
        ContactInfoWindow.getInstance().display(this, e);
    }
}


⌨️ 快捷键说明

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