<commit_before>/**
 * $RCSfile: ,v $
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.spark.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

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.plugin.layout.LayoutSettings;
import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

/**
 * Represent a single contact within the <code>ContactList</code>.
 */
public class ContactItem extends JPanel {

	private static final long serialVersionUID = 1514044406550293152L;
	private JLabel imageLabel;
    private JLabel displayNameLabel;
    private JLabel descriptionLabel;
    private String nickname;
    private String alias;
    private final String fullyQualifiedJID;
	private JLabel specialImageLabel;
    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;

	public ContactItem(String alias, String nickname, String fullyQualifiedJID) {
		this(alias, nickname, fullyQualifiedJID, true);
	}

    /**
     * Creates a new instance of a contact.
     *
     * @param alias             the alias of the contact
     * @param nickname          the nickname of the contact.
     * @param fullyQualifiedJID the fully-qualified jid of the contact (ex. <EMAIL>)
     */
    public ContactItem(String alias, String nickname, String fullyQualifiedJID, boolean initUi) {
        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");

        this.alias = alias;
        this.nickname = nickname;
        this.fullyQualifiedJID = fullyQualifiedJID;

        if (initUi) {
		displayNameLabel = new JLabel();
		descriptionLabel = new JLabel();
		imageLabel = new JLabel();
		specialImageLabel = 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));
		}

		displayNameLabel.setHorizontalTextPosition(JLabel.LEFT);
		displayNameLabel.setHorizontalAlignment(JLabel.LEFT);
		//displayNameLabel.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(displayNameLabel, 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));

		setDisplayName();
        }
    }

	/**
	 * Returns the name that should be displayed to represent the the contact.
	 * If an alias has been set, this alias will be returned. If no alias has
	 * been set, the nickname will be returned. If that hasn't been set either,
	 * the JID will be returned.
	 *
	 * @return a name suitable to be displayed
	 */
    public String getDisplayName() {
    	final String displayName;
		if (alias != null) {
			displayName = alias;
		} else if (nickname != null) {
			displayName = nickname;
		} else {
			displayName = getJID();
		}
<commit_msg>SPARK-1461: Doesn't show transport's icon near a contact<commit_after>/**
 * $RCSfile: ,v $
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.spark.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

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.plugin.layout.LayoutSettings;
import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

/**
 * Represent a single contact within the <code>ContactList</code>.
 */
public class ContactItem extends JPanel {

	private static final long serialVersionUID = 1514044406550293152L;
	private JLabel imageLabel;
    private JLabel displayNameLabel;
    private JLabel descriptionLabel;
    private String nickname;
    private String alias;
    private final String fullyQualifiedJID;
	private JLabel specialImageLabel;
    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;

	public ContactItem(String alias, String nickname, String fullyQualifiedJID) {
		this(alias, nickname, fullyQualifiedJID, true);
	}

    /**
     * Creates a new instance of a contact.
     *
     * @param alias             the alias of the contact
     * @param nickname          the nickname of the contact.
     * @param fullyQualifiedJID the fully-qualified jid of the contact (ex. <EMAIL>)
     */
    public ContactItem(String alias, String nickname, String fullyQualifiedJID, boolean initUi) {
        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");

        this.alias = alias;
        this.nickname = nickname;
        this.fullyQualifiedJID = fullyQualifiedJID;

        if (initUi) {
		displayNameLabel = new JLabel();
		descriptionLabel = new JLabel();
		imageLabel = new JLabel();
		specialImageLabel = 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));
		}

		displayNameLabel.setHorizontalTextPosition(JLabel.LEFT);
		displayNameLabel.setHorizontalAlignment(JLabel.LEFT);
		//displayNameLabel.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(displayNameLabel, 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(specialImageLabel, new GridBagConstraints(3, 0, 1, 2, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
		add(sideIcon, new GridBagConstraints(4, 0, 1, 2, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));

		setDisplayName();
        }
    }

	/**
	 * Returns the name that should be displayed to represent the the contact.
	 * If an alias has been set, this alias will be returned. If no alias has
	 * been set, the nickname will be returned. If that hasn't been set either,
	 * the JID will be returned.
	 *
	 * @return a name suitable to be displayed
	 */
    public String getDisplayName() {
    	final String displayName;
		if (alias != null) {
			displayName = alias;
		} else if (nickname != null) {
			displayName = nickname;
		} else {
			displayName = getJID();
		}

{'fraction_non_alphanumeric': 0.09851686138039606, 'fraction_numerical': 0.01607423978788632, 'mean_word_length': 4.202586206896552, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4037931', 'n_tokens_mistral': 3905, 'n_tokens_neox': 3598, 'n_words': 1006}