📄 buddylisttree.java
字号:
/* Copyright (C) 2003 Adam Olsen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;//includesimport java.awt.Color;import java.awt.GridLayout;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Locale;import java.util.ResourceBundle;import java.util.TreeMap;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.swing.SwingUtilities;import javax.swing.ToolTipManager;import javax.swing.event.TreeExpansionEvent;import javax.swing.event.TreeExpansionListener;import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;import org.dotuseful.ui.tree.AutomatedTreeModel;import org.dotuseful.ui.tree.AutomatedTreeNode;import org.jivesoftware.smack.Roster;import org.jivesoftware.smack.RosterEntry;import org.jivesoftware.smack.XMPPConnection;import org.jivesoftware.smack.packet.Presence;import com.valhalla.jbother.jabber.BuddyGroup;import com.valhalla.jbother.jabber.BuddyStatus;import com.valhalla.jbother.menus.BuddyListPopupMenu;import com.valhalla.jbother.menus.BuddyListTransportMenu;import com.valhalla.jbother.menus.GroupPopupMenu;import com.valhalla.settings.Settings;/** * BuddyListTree is the part of the buddy list dialog that draws the buddies and * their groups from your Jabber roster. It also displays different pictures for * different statuses. * * @author Adam Olsen * @created March 2, 2005 * @version 1.0 */public class BuddyListTree extends JPanel { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private XMPPConnection connection; private Roster roster; private AutomatedTreeNode root = new AutomatedTreeNode("Buddies"); private AutomatedTreeModel model = new AutomatedTreeModel(root); private JTree tree = new JTree(model); private JScrollPane scrollPane = new JScrollPane(tree); private BuddyListPopupMenu buddyPopupMenu = new BuddyListPopupMenu(); private GroupPopupMenu groupPopupMenu = new GroupPopupMenu(); private BuddyListTransportMenu buddyTransportMenu = new BuddyListTransportMenu(); private boolean showOfflineBuddies = Settings.getInstance().getBoolean( "showOfflineBuddies"); private boolean showUnfiledBuddies = Settings.getInstance().getBoolean( "showUnfiledBuddies"); private boolean showAgentBuddies = Settings.getInstance().getBoolean( "showAgentBuddies"); private boolean sortByStatus = Settings.getInstance().getBoolean( "sortByStatus"); private boolean showAwayBuddies = !Settings.getInstance().getBoolean("dontShowAwayBuddies"); private BuddyListRenderer renderer = new BuddyListRenderer(); protected Hashtable totalEntriesPerGroup = new Hashtable(); protected Hashtable onlineEntriesPerGroup = new Hashtable(); protected Hashtable groups = new Hashtable(); private BuddyListExpansionListener expandListener = new BuddyListExpansionListener(); private TreeMap buddyGroups = new TreeMap(); // to sort the buddy groups /** * Sets up the tree */ public BuddyListTree() { setLayout(new GridLayout(0, 1)); setBackground(Color.WHITE); tree.setCellRenderer(renderer); tree.setRootVisible(false); tree.setRowHeight(0); tree.setShowsRootHandles(true); tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addMouseListener(new PopupMouseListener()); tree.addTreeExpansionListener(expandListener); ToolTipManager.sharedInstance().registerComponent(tree); add(scrollPane); } public void updateUI() { super.updateUI(); if (buddyPopupMenu != null) { buddyPopupMenu = new BuddyListPopupMenu(); groupPopupMenu = new GroupPopupMenu(); } } /** * Gets the popMenu attribute of the BuddyListTree object * * @return The popMenu value */ public BuddyListPopupMenu getPopMenu() { return buddyPopupMenu; } /** * Returns the JTree * * @return the actual JTree swing component */ public JTree getTree() { return this.tree; } /** * Sets the JTree's XMPPConnection * * @param connection * the current connection */ public void setConnection(XMPPConnection connection) { this.connection = connection; if (connection == null) { return; } this.roster = ConnectorThread.getInstance().getRoster(); this.roster.reload(); } /** * Sets whether or not to show the offline buddies * * @param show * true to show the offline buddies */ public void setShowOfflineBuddies(boolean show) { this.showOfflineBuddies = show; Settings.getInstance().setBoolean("showOfflineBuddies", show); reloadBuddies(); } public void setShowAwayBuddies(boolean show) { this.showAwayBuddies = show; Settings.getInstance().setBoolean("doneShowAwayBuddies", !show); reloadBuddies(); } /** * Sets whether or not to show the unfiled buddies * * @param show * true to show unfiled buddies */ public void setShowUnfiledBuddies(boolean show) { this.showUnfiledBuddies = show; Settings.getInstance().setBoolean("showUnfiledBuddies", show); reloadBuddies(); } /** * Sets whether or not to show agents/transports * * @param show * true to show agents and transports */ public void setShowAgentBuddies(boolean show) { this.showAgentBuddies = show; Settings.getInstance().setBoolean("showAgentBuddies", show); reloadBuddies(); } /** * set to true if you want to sort your buddies by status * * @param show * true to sort by status */ public void setSortByStatus(boolean show) { this.sortByStatus = show; Settings.getInstance().setBoolean("sortByStatus", show); reloadBuddies(); } /** * @return true if sort by status is set */ public boolean getSortByStatus() { return this.sortByStatus; } /** * To listen to when a group gets expanded. Saves it in the settings for * session restoration * * @author Adam Olsen * @created March 2, 2005 * @version 1.0 */ static class BuddyListExpansionListener implements TreeExpansionListener { /** * Listens for a tree collapse event * * @param e * Description of the Parameter */ public void treeCollapsed(TreeExpansionEvent e) { TreePath path = e.getPath(); AutomatedTreeNode node = (AutomatedTreeNode) path .getLastPathComponent(); if (node.getUserObject() instanceof BuddyGroup) { Settings.getInstance().setProperty( "groupExpandStatus_" + ((BuddyGroup) node.getUserObject()) .getGroupName(), "collapsed"); } } /** * Listens for a tree expand event * * @param e * Description of the Parameter */ public void treeExpanded(TreeExpansionEvent e) { TreePath path = e.getPath(); AutomatedTreeNode node = (AutomatedTreeNode) path .getLastPathComponent(); if (node.getUserObject() instanceof BuddyGroup) { Settings.getInstance().remove( "groupExpandStatus_" + ((BuddyGroup) node.getUserObject()) .getGroupName()); } } } /** * Redraws the JTree */ public void reloadBuddies() { reloadBuddies(false); } /** * Shows all the current offline buddies */ public void loadOfflineBuddies() { reloadBuddies(true); } /** * Redraws the JTree * * @param loadOffline * whether or not to just load the offline buddies */ public void reloadBuddies(final boolean loadOffline) { showOfflineBuddies = Settings.getInstance().getBoolean( "showOfflineBuddies"); SwingUtilities.invokeLater(new Runnable() { public void run() { if (connection == null) { return; } if (roster == null) { roster = ConnectorThread.getInstance().getRoster(); roster.reload(); } if (!loadOffline) { clearBuddies(); } // loop through all the RosterEntries and see if they need to be // added to the // BuddyList tree Iterator it = roster.getEntries(); while (it.hasNext()) { RosterEntry entry = (RosterEntry) it.next(); BuddyStatus buddy = BuddyList.getInstance().getBuddyStatus( entry.getUser()); checkAddEntry(buddy); } tree.repaint(); tree.validate(); } }); } /** * Clears all the buddies from the JTree */ public void clearBuddies() { com.valhalla.Logger.debug("Clearing Buddies"); // clear the JTree and the BuddyGroups TreeMap root.removeAllChildren(); buddyGroups.clear(); totalEntriesPerGroup.clear(); } /** * We store all of the Group names in a TreeMap so that we can get the sort * index when inserting the TreeNode for this group into the root node * * @param group * the name of the group * @return The groupIndex value */ private int getGroupIndex(String group) { synchronized (buddyGroups) { if (buddyGroups.containsKey(group)) { return -1; } // we want General Contacts and Agents/Transports to always // sort at the bottom if (group.equals(resources.getString("contactsGroup"))) { group = "zzz Contacts"; } else if (group.equals(resources.getString("transportsGroup"))) { group = "zzzz Agents/Transports"; } buddyGroups.put(group, new TreeMap()); int count = 0; // find the index of the newly sorted group Iterator i = buddyGroups.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); if (key.equals(group)) { break; } count++; } return count; } } /** * Finds out the sort index of this particular buddy * * @param group * the group the buddy is in * @param buddy * the buddy to find the index of * @return The buddyIndex value */ private int getBuddyIndex(String group, BuddyStatus buddy) { synchronized (buddyGroups) { if (group.equals(resources.getString("contactsGroup"))) { group = "zzz Contacts"; } else if (group.equals(resources.getString("transportsGroup"))) { group = "zzzz Agents/Transports"; } else if (group.equals(resources.getString("notInRoster"))) { group = "zzzz Not In Roster"; } String name = buddy.getName(); if (name == null) { name = buddy.getUser(); } name = name.toLowerCase() + buddy.getUser().toLowerCase(); if (sortByStatus) { // sort by status Presence.Mode mode = buddy.getPresence(buddy .getHighestResource()); if (mode == null) { name = "zzz9555 " + name; } else if (mode == Presence.Mode.AVAILABLE) { name = "aa " + name; } else if (mode == Presence.Mode.CHAT) { name = "zzz9111 " + name; } else if (mode == Presence.Mode.AWAY) { name = "zzz9222 " + name; } else if (mode == Presence.Mode.EXTENDED_AWAY) { name = "zzz9333 " + name; } else if (mode == Presence.Mode.DO_NOT_DISTURB) { name = "zzz9444 " + name; } if (buddy.size() <= 0) { name = "zzz9555 " + name; } } if (buddyGroups.get(group) == null) { buddyGroups.put(group, new TreeMap()); } ((TreeMap) buddyGroups.get(group)).put(name, buddy.getUser()); int count = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -