⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsclusterlabel.java

📁 一款即时通讯软件
💻 JAVA
字号:
package edu.ou.kmi.buddyspace.plugins.maps.gui;

/*
 * BSClusterLabel.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 30 Sempember 2002, 16:06
 */

import javax.swing.*;
import java.util.*;

import org.jabber.jabberbeans.util.*;

import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.buddyspace.plugins.maps.xml.*;
import edu.ou.kmi.buddyspace.utils.*;

/**
 * <code>BSClusterLabel</code> is a icon label for specified cluster.
 * It is meant to display presence specific icon and tooltip.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSClusterLabel extends JLabel {

    protected ClusterTag cluster;
    private Hashtable presences;
    private Collection emphasizedJIDs = null;
    transient private boolean emphasis = true;
    //private String toolTipText = null;

    /** presence icons */
    private static Icon allOfflineIcon =  new ImageIcon(ClassLoader.getSystemResource("images/map_allOffline.gif"));
    private static Icon allOnlineIcon = new ImageIcon(ClassLoader.getSystemResource("images/map_allOnline.gif"));
    private static Icon moreOfflineIcon = new ImageIcon(ClassLoader.getSystemResource("images/map_moreOffline.gif"));
    private static Icon moreOnlineIcon = new ImageIcon(ClassLoader.getSystemResource("images/map_moreOnline.gif"));
    private static Icon [] icons = {
        allOfflineIcon, allOnlineIcon, moreOfflineIcon, moreOnlineIcon
    };
    private static Map xicons = new HashMap(); // emphasized icon lookup
    static
    {
        // Build a map of emphasized icons
        for (int i = 0; i < icons.length; i++) {
            xicons.put(icons[i], new IconX(icons[i]));
        }
    }

    /** Constructor */
    public BSClusterLabel(ClusterTag cluster, Icon img) {
        super(img);
        this.cluster = cluster;
        presences = new Hashtable();

        /*String toolTip = "cluster of";
        Enumeration jids = getJIDs();
        while (jids.hasMoreElements()) {
            JID j = (JID) jids.nextElement();
            toolTip += j.toString() + "\n";
        }
        setToolTipText(toolTip);*/

        if (cluster != null && cluster.getName() != null)
            setToolTipText(cluster.getName());
        else
            setToolTipText("cluster");
    }

    /** Sets label icon */
    public void setImg(Icon img) {
        setIcon(img);
    }

    /** Set the collection of JIDs that should be highlighted */
    public void setEmphasizedJIDs(Collection emphasizedJIDs) {
        this.emphasizedJIDs = emphasizedJIDs;
    }

    /** Returns all JIDs in cluster */
    public Enumeration getJIDs() {
        Vector jids = new Vector();
        addClusterJIDs(jids, cluster);
        return jids.elements();
    }

    // recursivelly adds jids of cluster into the vector
    private void addClusterJIDs(Vector jids, ClusterTag cluster) {
        Enumeration items = cluster.getItemsAndClusters();
        while (items.hasMoreElements()) {
            Object o = items.nextElement();
            if (o instanceof ItemTag)
                jids.add(((ItemTag)o).getJID());
            else if (o instanceof ClusterTag)
                addClusterJIDs(jids, (ClusterTag)o);
        }
    }

    /** Refreshes presences and updates label icon */
    public void refreshPresences(BSPresenceBean presenceBean) {
        emphasis = false;
        presences.clear();
        Enumeration jids = getJIDs();
        while(jids.hasMoreElements()) {
            JID j = (JID) jids.nextElement();
            BSPresenceInfo pi = presenceBean.getPresence(j);
            //String str = BSPresenceBean.getJIDHashString(j, true);
            if (pi == null) {
                pi = new BSPresenceInfo(j, false, null, null);
            }
            //presences.put(str, pi);
            presences.put(j, pi);
            if (emphasis == false && emphasizedJIDs != null) {
                String user = j.toString().toLowerCase();
                emphasis = emphasizedJIDs.contains(user);
            }
        }

        updateIcon();
    }

    /** Sets presence */
    public void setPresence(BSPresenceInfo pi) {
        if (pi == null) return;
        //String str = BSPresenceBean.getJIDHashString(pi.getJID(), true);
        JID key = pi.getJID();
        //if (presences.get(str) != null) {
        if (presences.get(key) != null) {
            //presences.put(str, pi);
            presences.put(key, pi);

            updateIcon();
        }
    }

    /** Clears all presences */
    public void clearPresences() {
        presences.clear();
        BSPresenceInfo pi = new BSPresenceInfo(null, false, null, null);
        Enumeration jids = getJIDs();
        while(jids.hasMoreElements()) {
            JID j = (JID) jids.nextElement();
            pi.setJID(j);
            //String str = BSPresenceBean.getJIDHashString(j, false);
            //presences.put(str, pi);
            presences.put(j, pi);
        }

        updateIcon();
    }

    /** Updates label icon according to presence */
    private void updateIcon() {
        Enumeration piEnum = presences.elements();
        int online = 0;
        int offline = 0;
        while(piEnum.hasMoreElements()) {
            BSPresenceInfo pi = (BSPresenceInfo) piEnum.nextElement();
            if (pi.isOnline()) online++;
            else offline++;
        }

        Icon icon = null;
        if (online == 0) {
            icon = allOfflineIcon;
        } else if (online < offline) {
            icon = moreOfflineIcon;
        } else if (offline != 0) {
            icon = moreOnlineIcon;
        } else {
            icon = allOnlineIcon;
        }
        if (emphasis) icon = (Icon)xicons.get(icon);

        setIcon(icon);
    }

}

⌨️ 快捷键说明

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