contactitem.java.svn-base

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

SVN-BASE
493
字号
/**
 * $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.resource.SparkRes;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
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 javax.swing.*;
import java.awt.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Represent a single contact within the <code>ContactList</code>.
 */
public class ContactItem extends JPanel {
    private JLabel imageLabel;
    private JLabel nicknameLabel;
    private JLabel descriptionLabel;
    private String nickname;
    private String fullyQualifiedJID;
    private Icon icon;

    private String status;
    private String groupName;

    boolean available;

    private Presence presence;

    private String hash = "";

    private File contactsDir;

    private JLabel sideIcon;

    private int fontSize;

    private int iconSize;

    private boolean avatarsShowing;

    /**
     * Creates a new instance of a contact.
     *
     * @param nickname          the nickname of the contact.
     * @param fullyQualifiedJID the fully-qualified jid of the contact (ex. derek@jivesoftware.com)
     */
    public ContactItem(String nickname, String fullyQualifiedJID) {
        setLayout(new GridBagLayout());

        // Set Default Font
        final LocalPreferences pref = SettingsManager.getLocalPreferences();
        fontSize = pref.getContactListFontSize();
        iconSize = pref.getContactListIconSize();
        avatarsShowing = pref.areAvatarsVisible();

        // Set default presence
        presence = new Presence(Presence.Type.unavailable);

        contactsDir = new File(SparkManager.getUserDirectory(), "contacts");

        nicknameLabel = new JLabel();
        descriptionLabel = new JLabel();
        imageLabel = new JLabel();
        sideIcon = new JLabel();
        if (avatarsShowing) {
            sideIcon.setMinimumSize(new Dimension(iconSize, iconSize));
            sideIcon.setMaximumSize(new Dimension(iconSize, iconSize));
            sideIcon.setPreferredSize(new Dimension(iconSize, iconSize));
        }

        nicknameLabel.setHorizontalTextPosition(JLabel.LEFT);
        nicknameLabel.setHorizontalAlignment(JLabel.LEFT);
        nicknameLabel.setText(nickname);


        descriptionLabel.setFont(new Font("Dialog", Font.PLAIN, fontSize));
        descriptionLabel.setForeground((Color)UIManager.get("ContactItemDescription.foreground"));
        descriptionLabel.setHorizontalTextPosition(JLabel.LEFT);
        descriptionLabel.setHorizontalAlignment(JLabel.LEFT);


        this.setOpaque(true);

        add(imageLabel, new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
        add(nicknameLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
        add(descriptionLabel, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 2, 0), 0, 0));
        add(sideIcon, new GridBagConstraints(3, 0, 1, 2, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));

        setNickname(nickname);

        this.fullyQualifiedJID = fullyQualifiedJID;
    }

    /**
     * Returns the nickname of the contact.
     *
     * @return the nickname.
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * Sets the nickname of the contact.
     *
     * @param nickname the contact nickname.
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
        nicknameLabel.setText(StringUtils.unescapeNode(nickname));
    }

    /**
     * Returns the fully qualified JID of the contact. (If available). Otherwise will
     * return the bare jid.
     *
     * @return the fully qualified jid (ex. derek@jivesoftware.com).
     */
    public String getJID() {
        return fullyQualifiedJID;
    }

    /**
     * Returns the icon showing the contacts current state or presence.
     *
     * @return the icon.
     */
    public Icon getIcon() {
        return icon;
    }

    /**
     * Sets the current icon to use.
     *
     * @param icon the current icon to use.
     */
    public void setIcon(Icon icon) {
        this.icon = icon;
        imageLabel.setIcon(icon);
    }

    /**
     * Returns the contacts current status based on their presence.
     *
     * @return the contacts current status.
     */
    public String getStatus() {
        return status;
    }

    /**
     * Sets the contacts current status.
     *
     * @param status the contacts current status.
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * Returns the name of the <code>ContactGroup</code> that this contact belongs to.
     *
     * @return the name of the <code>ContactGroup</code>.
     */
    public String getGroupName() {
        return groupName;
    }

    /**
     * Sets the name of the <code>ContactGrouop</code> that this contact belongs to.
     *
     * @param groupName the name of the ContactGroup.
     */
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    /**
     * Returns the <code>JLabel</code> showing the users nickname.
     *
     * @return the nickname label.
     */
    public JLabel getNicknameLabel() {
        return nicknameLabel;
    }

    /**
     * Returns the <code>JLabel</code> representing the description.
     *
     * @return the description label.
     */
    public JLabel getDescriptionLabel() {
        return descriptionLabel;
    }

    /**
     * Returns the current presence of the contact.
     *
     * @return the users current presence.
     */
    public Presence getPresence() {
        return presence;
    }

    /**
     * Sets the current presence on this contact item.
     *
     * @param presence the presence.
     */
    public void setPresence(Presence presence) {

        this.presence = presence;

        final PacketExtension packetExtension = presence.getExtension("x", "vcard-temp:x:update");

⌨️ 快捷键说明

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