📄 buddylisttree.java
字号:
Iterator i = ((TreeMap) buddyGroups.get(group)).keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); if (key.equals(name)) { break; } count++; } return count; } } /** * Checks to see if the group is already in the tree, and returns the index * of it * * @param group * the group to check * @return Description of the Return Value */ public AutomatedTreeNode checkGroup(BuddyGroup group) { boolean check = false; AutomatedTreeNode node = new AutomatedTreeNode(group); Enumeration children = root.children(); // find out if the group alread exists whileLoop: while (children.hasMoreElements()) { AutomatedTreeNode theNode = (AutomatedTreeNode) children .nextElement(); BuddyGroup g = (BuddyGroup) theNode.getUserObject(); if (g.getGroupName().equals(group.getGroupName())) { node = theNode; check = true; break whileLoop; } } if (root.isNodeChild(node)) { return node; } final String tempGroup = group.getGroupName(); final AutomatedTreeNode tempNode = node; if (!check) { int num = getGroupIndex(tempGroup); if (num >= 0) { root.insert(tempNode, num); } } group.setNode(node); return node; } private BuddyGroup getGroupObject(String name) { String temp = name.replaceAll(" ([^)]*)$", ""); if (!showUnfiledBuddies && temp.equals(resources.getString("contactsGroup"))) { return null; } if (!showAgentBuddies && temp.equals(resources.getString("transportsGroup"))) { return null; } BuddyGroup group = (BuddyGroup) groups.get(name); if (group == null) group = new BuddyGroup(name); groups.put(name, group); return group; } /** * finds out if the buddy should be displayed in the BuddyListTree. If so * the buddy is added to the tree * * @param buddy * the buddy to add */ public void checkAddEntry(final BuddyStatus buddy) { if (buddy == null) { return; } boolean add = false; // if we are set to show the offline buddies then add the buddy to the // tree if (showOfflineBuddies) { add = true; } else { // otherwise we have to find out if the buddy is online before we // add it if (buddy.size() > 0) { add = true; } } if(!showAwayBuddies) { Presence.Mode mode = buddy.getPresence(buddy.getHighestResource()); if( mode != null && mode != Presence.Mode.AVAILABLE ) add = false; } if (buddy.getRemoved()) { add = false; } String tempGroup = buddy.getTempGroup(); if (tempGroup == null) { tempGroup = buddy.getGroup(); } final String group = tempGroup; BuddyGroup gObj = getGroupObject(group); if (gObj == null) return; gObj.addBuddy(buddy); if (add) { final AutomatedTreeNode node = checkGroup(gObj); node.setUserObject(gObj); // find the group that the buddy belongs to int index = getBuddyIndex(gObj.getGroupName(), buddy); if (!isInTree(buddy)) { node.insert(new AutomatedTreeNode(buddy), index); } TreePath parent = new TreePath(root); tree.expandPath(parent); // find out if we need to expand this group String property = Settings.getInstance().getProperty( "groupExpandStatus_" + group); if (property == null || !property.equals("collapsed")) { tree.expandPath(parent.pathByAddingChild(node)); } } } /** * Returns whether or not the buddy is in the tree * * @param buddy * the buddy to check * @return true if the buddy is in the tree */ public boolean isInTree(BuddyStatus buddy) { String group = buddy.getGroup(); // loop through all the groups until we find the group that this // buddy belongs to Enumeration children = root.children(); while (children.hasMoreElements()) { AutomatedTreeNode node = (AutomatedTreeNode) children.nextElement(); // once we find it's group, loop through all the buddies in that // group if (((BuddyGroup) node.getUserObject()).getGroupName() .equals(group)) { Enumeration leafs = node.children(); while (leafs.hasMoreElements()) { AutomatedTreeNode leaf = (AutomatedTreeNode) leafs .nextElement(); if (leaf.getUserObject() == buddy) { return true; } } } } return false; } /** * Adds a buddy to the tree - if it's not already in the tree * * @param buddy * the buddy to add */ public void addBuddy(final BuddyStatus buddy) { checkAddEntry(buddy); validate(); repaint(); } /** * Removes the buddy from the tree * * @param buddy * the buddy to remove * @param group * the group the buddy is in */ public void removeBuddy(final BuddyStatus buddy, final String group, final boolean removeFromGroup) { // loop through all the groups until we find the group that this // buddy belongs to Enumeration children = root.children(); while (children.hasMoreElements()) { AutomatedTreeNode node = (AutomatedTreeNode) children.nextElement(); // once we find it's group, loop through all the buddies in that // group if (((BuddyGroup) node.getUserObject()).getGroupName() .equals(group)) { Enumeration leafs = node.children(); while (leafs.hasMoreElements()) { AutomatedTreeNode leaf = (AutomatedTreeNode) leafs .nextElement(); Object check = leaf.getUserObject(); if (check instanceof BuddyStatus) { BuddyStatus temp = (BuddyStatus) check; if (temp.getUser().equals(buddy.getUser())) { // once we find the buddy, remove it removeBuddyNode(node, leaf); if (removeFromGroup) { BuddyGroup g = getGroupObject(group); g.removeBuddy(buddy); } } } } } } } /** * removes this buddy from the JTree. If this was the last buddy in the * group then remove the group from being displayed * * @param node * the group node * @param leaf * the leaf node */ private void removeBuddyNode(final AutomatedTreeNode node, final AutomatedTreeNode leaf) { String group = ((BuddyGroup) node.getUserObject()).getGroupName(); if (group == null || group.equals(resources.getString("contactsGroup"))) { group = "zzz Contacts"; } else if (group.equals(resources.getString("transportsGroup"))) { group = "zzzz Agents/Transports"; } BuddyStatus buddy = (BuddyStatus) leaf.getUserObject(); String name = buddy.getName(); if (name == null) { name = buddy.getUser(); } String jid = buddy.getUser(); synchronized (buddyGroups) { TreeMap buddies = ((TreeMap) buddyGroups.get(group)); Iterator i = buddies.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); if (buddies.get(key).equals(jid)) { ((TreeMap) buddyGroups.get(group)).remove(key); break; } } } node.remove(leaf); if (node.getChildCount() <= 0) { buddyGroups.remove(group); root.remove(node); } tree.repaint(); } /** * Starts a conversation if someone double double clicks on a buddy */ public void initiateConversation(BuddyStatus buddy) { if (buddy == null) { if (tree.getSelectionPath() == null) { return; } TreePath path = tree.getSelectionPath(); AutomatedTreeNode node = (AutomatedTreeNode) path .getLastPathComponent(); if (model.isLeaf(node)) { try { buddy = (BuddyStatus) node.getUserObject(); } catch (ClassCastException ex) { return; } } } if (buddy == null) return; if (buddy.getConversation() == null) { ChatPanel conver = new ChatPanel(buddy); buddy.setConversation(conver); MessageDelegator.getInstance().showPanel(buddy.getConversation()); MessageDelegator.getInstance().frontFrame(buddy.getConversation()); } else { MessageDelegator.getInstance().showPanel(buddy.getConversation()); MessageDelegator.getInstance().frontFrame(buddy.getConversation()); } buddy.getConversation().stopTimer(); } /** * Listens for mouse events in the tree * * @author Adam Olsen * @created March 2, 2005 * @version 1.0 */ class PopupMouseListener extends MouseAdapter { /** * Description of the Method * * @param e * Description of the Parameter */ public void mousePressed(MouseEvent e) { checkPop(e); } /** * Description of the Method * * @param e * Description of the Parameter */ public void mouseReleased(MouseEvent e) { checkPop(e); } /** * Description of the Method * * @param e * Description of the Parameter */ public void mouseClicked(MouseEvent e) { checkPop(e); try { JTree tree = (JTree) e.getComponent(); } catch (ClassCastException ex) { return; } if (e.getClickCount() >= 2) { e.consume(); initiateConversation(null); } } /** * Checks if we need to display the buddy list popup menu Shows it if * needs be * * @param e * Description of the Parameter */ public void checkPop(MouseEvent e) { BuddyStatus buddy = null; if (e.isPopupTrigger()) { try { JTree tree = (JTree) e.getComponent(); TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) { throw new ClassCastException(); } tree.setSelectionPath(path); AutomatedTreeNode node = (AutomatedTreeNode) path .getLastPathComponent(); Object selectedUserObject = node.getUserObject(); if (selectedUserObject.getClass().equals(BuddyStatus.class)) { buddy = (BuddyStatus) node.getUserObject(); buddyPopupMenu.showMenu(e.getComponent(), e.getX(), e .getY(), buddy); } else if (selectedUserObject.getClass().equals( BuddyGroup.class)) { // buddies that are not in any group are put in // "Contacts" group by JBother // if we need to send a message to them, we need to // extract them from the tree // because there is no way to do it from the roster Enumeration iChildren = node.children(); String usersList = new String(); while (iChildren.hasMoreElements()) { AutomatedTreeNode o = (AutomatedTreeNode) iChildren .nextElement(); BuddyStatus buddyStatus = (BuddyStatus) o .getUserObject(); usersList += buddyStatus.getUser() + "; "; } groupPopupMenu.showMenu(e.getComponent(), e.getX(), e .getY(), usersList, (BuddyGroup) selectedUserObject); } buddy = (BuddyStatus) node.getUserObject(); if (buddy.getUser().indexOf("@") == -1) { buddyTransportMenu.showMenu(e.getComponent(), e.getX(), e.getY(), buddy); } else { buddyPopupMenu.showMenu(e.getComponent(), e.getX(), e .getY(), buddy); } } catch (ClassCastException ex) { /* * is not a buddy, so don't display the menu */ } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -