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

📄 bsrostertreeview.java

📁 一款即时通讯软件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**
     * Called when compose message is selected. Opens composing of message with selected item.
     */
    protected void composeMessage(JMenuItem mi) {
        // gets selected node
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node == null) return;
        
        // if jid node
        if (node.getUserObject() instanceof BSJIDItem) {
            // opens compose message
            BSJIDItem i = (BSJIDItem) node.getUserObject();
            JID jid = i.getJID();
            mainFrame.composeMessage(jid);
        }
        // if resource node
        else if (node instanceof BSResourceNode) {
            // opens chat
            BSResourceNode resNode = (BSResourceNode) node;
            JID jid = resNode.getJID();
            mainFrame.composeMessage(jid);
        }
        // if group node
        else if (node instanceof BSGroupNode) {
            // opens chat
            BSGroupNode grNode = (BSGroupNode) node;
            String groupName = grNode.getName();
            if (mi == msgWholeGroupMenuItem)
                mainFrame.composeMessage(groupName);
            else if (mi == msgGroupOnlineMenuItem) {
                Vector recipients = new Vector();
                Enumeration jidsEnum = rosterBean.getJIDsInGroup(groupName);
                while (jidsEnum.hasMoreElements()) {
                    JID jid = (JID) jidsEnum.nextElement();
                    BSPresenceInfo pi = presenceBean.getResourcePresence(jid);
                    if (pi != null && pi.isOnline())
                        recipients.add(jid);
                }
                mainFrame.composeMessage(recipients.elements(), 
                                         new Vector().elements());
            }
        }
        else
            return;
    }
    
    /**
     * Called when subscribe selected.
     */
    protected void subscribe() {
        // gets selected node
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node == null) return;
        
        JID jid = null;
        
        if (node.getUserObject() instanceof BSJIDItem) {
            BSJIDItem i = (BSJIDItem) node.getUserObject();
            jid = i.getJID();
        }
        else if (node instanceof BSComponentNode) {
            BSComponentNode n = (BSComponentNode) node;
            jid = n.getJID();
        }
            
        if (jid == null) return;
        
        // sends subscription request
        presenceBean.sendSubscriptionRequest(jid);
    }
    
    /**
     * Called when login a component selected.
     */
    protected void login() {
        // gets selected node
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node == null) return;
        
        if (!(node instanceof BSComponentNode)) return;
        
        // sends available presence
        BSComponentNode n = (BSComponentNode) node;
        JID jid = n.getJID();
        presenceBean.sendPresence(new BSPresenceInfo(null, true, null, null), jid);
    }
    
    /**
     * Called when logout a component selected.
     */
    protected void logout() {
        // gets selected node
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node == null) return;
        
        if (!(node instanceof BSComponentNode)) return;
        
        // sends available presence
        BSComponentNode n = (BSComponentNode) node;
        JID jid = n.getJID();
        presenceBean.sendPresence(new BSPresenceInfo(null, false, null, null), jid);
    }
    
    /**
     * If supposed to, shows popup menu.
     */
    protected void maybeShowPopup(MouseEvent e) {
        // if that was context popup event
        if (e.isPopupTrigger()) {
        //if (e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e)) {
            // gets the selected node
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            if (selRow != -1) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
                tree.addSelectionPath(selPath);
                // if we clicked on the group node
                if (node instanceof BSGroupNode) {
                    groupContextMenu.removeAll();
                    groupContextMenu.add(addContactMenuItem);
                    groupContextMenu.add(refreshMenuItem);
                    //groupContextMenu.add(msgMenuItem);
                    groupContextMenu.add(msgWholeGroupMenuItem);
                    groupContextMenu.add(msgGroupOnlineMenuItem);
                    updatePresenceMenu(node);
                    groupContextMenu.add(presenceMenu);
                    groupContextMenu.show(e.getComponent(), e.getX(), e.getY());
                    adjustPopupPosition(groupContextMenu);
                }
                // if we clicked on the buddy node
                else if (node.getUserObject() instanceof BSJIDItem) {
                    // checks the support for .plan
                    Vector namespaces = new Vector();
                    mainFrame.getSupportedNamespaces(namespaces, new Vector());
                    boolean supportsPlans = namespaces.contains("http://jabber.open.ac.uk/tags/plan#get");
                    // sets the menu
                    buddyContextMenu.removeAll();
                    buddyContextMenu.add(msgMenuItem);
                    buddyContextMenu.add(chatMenuItem);
                    buddyContextMenu.add(deleteMenuItem);
                    buddyContextMenu.add(changeMenuItem);
                    buddyContextMenu.add(subscribeMenuItem);
                    updatePresenceMenu(node);
                    buddyContextMenu.add(presenceMenu);
                    if (supportsPlans) buddyContextMenu.add(getPlanMenuItem);
                    buddyContextMenu.addSeparator();
                    buddyContextMenu.add(addContactMenuItem);
                    buddyContextMenu.add(refreshMenuItem);
                    // IX panels stuff
                    /*buddyContextMenu.addSeparator();
                    buddyContextMenu.add(relationsMenu);
                    relationsMenu.add(relSupMenuItem);
                    relationsMenu.add(relSubMenuItem);
                    relationsMenu.add(relPeerMenuItem);
                    relationsMenu.add(relContactMenuItem);
                    relationsMenu.add(relDeleteMenuItem);*/
                    // end of IX panels stuff
                    buddyContextMenu.show(e.getComponent(), e.getX(), e.getY());
                    adjustPopupPosition(buddyContextMenu);
                }
                // if we clicked on the resource node
                else if (node instanceof BSResourceNode) {
                    if (!mainFrame.isConnected())
                        return;
                    resourceContextMenu.removeAll();
                    resourceContextMenu.add(msgMenuItem);
                    resourceContextMenu.add(chatMenuItem);
                    resourceContextMenu.add(sendFileMenuItem);
                    resourceContextMenu.addSeparator();
                    resourceContextMenu.add(addContactMenuItem);
                    resourceContextMenu.add(refreshMenuItem);
                    resourceContextMenu.show(e.getComponent(), e.getX(), e.getY());
                    adjustPopupPosition(resourceContextMenu);
                }
                // if we clicked on the component node
                else if (node instanceof BSComponentNode) {
                    componentContextMenu.removeAll();
                    componentContextMenu.add(loginMenuItem);
                    componentContextMenu.add(logoutMenuItem);
                    componentContextMenu.addSeparator();
                    componentContextMenu.add(deleteMenuItem);
                    componentContextMenu.add(changeMenuItem);
                    componentContextMenu.add(subscribeMenuItem);
                    componentContextMenu.addSeparator();
                    componentContextMenu.add(addContactMenuItem);
                    componentContextMenu.add(refreshMenuItem);
                    componentContextMenu.show(e.getComponent(), e.getX(), e.getY());
                    adjustPopupPosition(componentContextMenu);
                }
            }
        }
    }
    
    /** Updates the individual presence menu before painting */
    protected void updatePresenceMenu(DefaultMutableTreeNode node) {
        onlinePresenceMenuItem.setSelected(false);
        chatPresenceMenuItem.setSelected(false);
        elsePresenceMenuItem.setSelected(false);
        busyPresenceMenuItem.setSelected(false);
        awayPresenceMenuItem.setSelected(false);
        xaPresenceMenuItem.setSelected(false);
        dndPresenceMenuItem.setSelected(false);
        invisiblePresenceMenuItem.setSelected(false);
        
        BSPresenceInfo curPi = null;
        if (node instanceof BSGroupNode) {
            String groupName = ((BSGroupNode)node).getName();
            curPi = presenceBean.getIndividualPresences().getPresenceSettingFor(groupName);
            presenceMenu.setText("Presence for this group");
        }
        else if (node.getUserObject() instanceof BSJIDItem) {
            JID jid = ((BSJIDItem)node.getUserObject()).getJID();
            curPi = presenceBean.getIndividualPresences().getPresenceSettingFor(jid);
            presenceMenu.setText("Presence for this JID");
        }
        if (curPi == null)
            ; // no individual presence set yet
        else if (!curPi.isOnline())
            invisiblePresenceMenuItem.setSelected(true);
        else if (BSPresenceInfo.SHOW_CHAT.equals(curPi.getShow()))
            chatPresenceMenuItem.setSelected(true);
        else if (BSPresenceInfo.SHOW_DND.equals(curPi.getShow()))
            dndPresenceMenuItem.setSelected(true);
        else if (BSPresenceInfo.SHOW_AWAY.equals(curPi.getShow()))
            awayPresenceMenuItem.setSelected(true);
        else if (BSPresenceInfo.SHOW_XA.equals(curPi.getShow()))
            xaPresenceMenuItem.setSelected(true);
        else if (BSPresenceInfo.SHOW_ONLINE.equals(curPi.getShow())) {
            String status = curPi.getStatus();
            if (status != null && status.startsWith(BSMainFrame.STATUS_ELSE_STR))
                elsePresenceMenuItem.setSelected(true);
            else if (status != null && status.startsWith(BSMainFrame.STATUS_BUSY_STR))
                busyPresenceMenuItem.setSelected(true);
            else
                onlinePresenceMenuItem.setSelected(true);
        }
        
        policyPresenceMenuItem.setSelected(BSIndividualPresences.WORSE_TAKES_POLICY == presenceBean.getIndividualPresences().getPolicy());
    }
    
    /** Adjusts popup menu position */
    protected void adjustPopupPosition(JPopupMenu menu) {
        Point point = menu.getLocationOnScreen();
        Dimension menuSize = menu.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        Rectangle oldRect = new Rectangle(point.x, point.y, menuSize.width, menuSize.height);
        Rectangle newRect = new Rectangle(Math.max(0, Math.min(screenSize.width - menuSize.width,
                                                               point.x)),
                                          Math.max(0, Math.min(screenSize.height - menuSize.height, 
                                                               point.y)),
                                                   menuSize.width, menuSize.height);

        if (!oldRect.equals(newRect)) {
            Window window = SwingUtilities.getWindowAncestor(menu);
            if(window != null){
                window.setLocation(newRect.x, newRect.y);
            }
        }
    }
    
    /** <code>ActionListener</code> function. Handles actions. */
    public void actionPerformed(ActionEvent e) {
        Object mi = e.getSource();

        // add contact
        if (mi == addContactMenuItem) {
            addContact();
        }
        // delete contact
        else if (mi == deleteMenuItem) {
            deleteContact((DefaultMutableTreeNode) 
                          tree.getSelectionPath().getLastPathComponent());
        }
        // change contact
        else if (mi == changeMenuItem) {
            changeContact((DefaultMutableTreeNode) 
                          tree.getSelectionPath().getLastPathComponent());
        }
        // get plan for contact
        else if (mi == getPlanMenuItem) {
            getPlan((DefaultMutableTreeNode) 
                          tree.getSelectionPath().getLastPathComponent());
        }
        // refresh roster
        else if (mi == refreshMenuItem) {
            rosterBean.refreshRoster();
        }
        // open chat
        else if (mi == msgMenuItem ||
                mi == msgWholeGroupMenuItem || mi == msgGroupOnlineMenuItem) {
            composeMessage((JMenuItem)mi);
        }
        // open chat
        else if (mi == chatMenuItem) {
            openChatWindow();
        }
        // send file
        else if (mi == sendFileMenuItem) {
            sendFileTo((DefaultMutableTreeNode) 
                          tree.getSelectionPath().getLastPathComponent());
        }
        // subscribe to presence
        else if (mi == subscribeMenuItem) {
            subscribe();
        }
        // log-in a component
        else if (mi == loginMenuItem) {
            login();
        }
        // log-off a component
        else if (mi == logoutMenuItem) {
            logout();
        }
        // sets presences
        else if (mi == onlinePresenceMenuItem ||
                 mi == chatPresenceMenuItem ||
                 mi == busyPresenceMenuItem ||
                 mi == elsePresenceMenuItem ||
                 mi == awayPresenceMenuItem ||
                 mi == xaPresenceMenuItem ||
                 mi == dndPresenceMenuItem ||
                 mi == invisiblePresenceMenuItem ||
                 mi == clearPresenceMenuItem ||      // clears individual presence setting for the item

⌨️ 快捷键说明

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