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

📄 bsdiscoplugin.java

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

/*
 * BSDiscoPlugin.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2003
 *
 *
 * Created on 18 August 2003, 12:58
 */

import java.util.*;
import javax.swing.*;
//import java.awt.*;
import java.awt.event.*;

import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;

import org.xml.sax.helpers.AttributeListImpl;

import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.buddyspace.gui.*;

import edu.ou.kmi.buddyspace.plugins.*;
import edu.ou.kmi.buddyspace.plugins.disco.core.*;
import edu.ou.kmi.buddyspace.plugins.disco.xml.*;
import edu.ou.kmi.buddyspace.plugins.disco.gui.*;

/**
 * <code>BSDiscoPlugin</code> is main class of disco plugin to BuddySpace.
 * It provides methods for plugin loading and user interaction.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSDiscoPlugin extends BSPlugin
                            implements ActionListener {
    
    private BSDiscoBean discoBean;
    private BSDiscoWinManager discoWinMan;
    //private JMenu discoMenu;
    private JMenuItem discoMenu;
    //private PacketID servedID = new PacketID(null);
    //private BrowseItem agents = null;
    
    /** Constructor */
    public BSDiscoPlugin(BSMainFrame mainFrame, JTabbedPane tabbedPane, BSCore core) {
        super(mainFrame, tabbedPane, core);
    }
    
    /** Inits the plugin core */
    protected void initCore() {
        discoBean = new BSDiscoBean();
    }
    
    /** Inits plugin GUI */
    protected void initGUI() {
        discoWinMan = new BSDiscoWinManager(mainFrame, tabbedPane);
    }
    
    /** Adds GUI controls into main window */
    protected void addGUIControls() {
        //discoMenu = new JMenu("Service discovery");
        discoMenu = new JMenuItem("Service discovery");
        discoMenu.addActionListener(this);
        discoMenu.setEnabled(false);
        mainFrame.addPluginJabberMenuItem(discoMenu);
    }
    
    /** Connects BuddySpace beans */
    protected void connectBeans() {
        if (core != null) {
            /*BSConnectionBean connBean = core.getConnectionBean();
            if (connBean != null)
                discoBean.setConnection(connBean.getConnection());*/
            discoWinMan.setDiscoBean(discoBean);
        }
    }
    
    /** Handles connection change to connected */
    public void connected() {
        super.connected();
        
        //initMenu();
        discoMenu.setEnabled(true);
        
        BSConnectionBean connBean = core.getConnectionBean();
        if (connBean != null)
            discoBean.setConnection(connBean.getConnection());
        
        //discoBean.addDiscoListener(this);
        //JID serverJID = new JID(null, mainFrame.server, null);
        //browseBean.browseTo(serverJID.toString(), servedID, false);
    }
    
    /** Handles connection change to disconnected */
    public void disconnected() {
        //discoBean.removeDiscoListener(this);
        discoWinMan.closeAllWindows();
        discoBean.disconnected();
        discoMenu.setEnabled(false);
        //discoMenu.removeAll();
    }
    
    /** Handles action from GUI controls in main window */
    public void actionPerformed(ActionEvent evt) {
        /*if (evt.getSource() instanceof BSJIDActionMenuItem) {
            BSJIDActionMenuItem mi = (BSJIDActionMenuItem)evt.getSource();
            if (mi.getNamespace() != null &&
                mi.getNamespace().equals("jabber:iq:browse")) {
                JID jid = mi.getJID();
                browseWinMan.openNewWindow((jid != null)? jid.toString() : null);
            }
            else {
                mainFrame.performAction(mi.getJID(), mi.getNamespace());
            }
        }*/
        if (evt.getSource() == discoMenu) {
            discoWinMan.openNewWindow(null, null);
        }
    }
    
    /** 
     * If namespace is <code>http://jabber.org/protocol/disco#items or #info</code>
     * disco's given JID and returns true otherwise returns false.
     */
    public boolean performAction(JID jid, String namespace) {
        if (namespace != null && namespace.equals("http://jabber.org/protocol/disco#items")) {
            /*if (!mainFrame.isConnected()) {
                JOptionPane.showMessageDialog(mainFrame, 
                                      "Cannot browse - not connected.",
                                      "Error", 
                                      JOptionPane.ERROR_MESSAGE);
                return false;
            }
            browseWinMan.openNewWindow((jid != null)? jid.toString() : null);*/
            return true;
        }
        else if (namespace != null && namespace.equals("http://jabber.org/protocol/disco#info")) {
            return true;
        }
        else
            return false;
    }
    
    /** Adds supported namespaces and their names */
    public void getSupportedNamespaces(Vector namespaces, Vector names) {
        namespaces.addElement(new String("http://jabber.org/protocol/disco#items"));
        names.addElement(new String("Jabber disco items"));
        namespaces.addElement(new String("http://jabber.org/protocol/disco#info"));
        names.addElement(new String("Jabber disco info"));
    }
    
    
    /** Inits browse menu for BSMainFrame. 
     * The menu includes <code>agents</code> and register shortcuts.
     */
    protected void initMenu() {
        /*BSJIDActionMenuItem menuItem;
        browseMenu.removeAll();
        
        JID serverJID = new JID(null, mainFrame.server, null);
        menuItem = new BSJIDActionMenuItem("Home server", serverJID, 
                                           "jabber:iq:browse");
        menuItem.addActionListener(this);
        browseMenu.add(menuItem);
        
        browseMenu.addSeparator();
        
        if (agents != null) {
            Enumeration agentsEnum = agents.children();
            while (agentsEnum.hasMoreElements()) {
                BrowseItem bi = (BrowseItem) agentsEnum.nextElement();
                //menuItem = new BSJIDActionMenuItem(bi.getName(), bi.getJID(), 
                //                                   "jabber:iq:browse");
                //browseMenu.add(menuItem);
                //menuItem.addActionListener(this);
                JMenu subMenu = new JMenu(bi.getName());
                browseMenu.add(subMenu);
                // adds the browse menu item
                menuItem = new BSJIDActionMenuItem("Browse", bi.getJID(), 
                                                   "jabber:iq:browse");
                subMenu.add(menuItem);
                menuItem.addActionListener(this);
                // if jabber:iq:register namespace supported, adds register menu item
                Enumeration namespaces = bi.namespaces();
                while (namespaces.hasMoreElements()) {
                    String ns = (String)namespaces.nextElement();
                    if (ns != null && ns.equals("jabber:iq:register")) {
                        menuItem = new BSJIDActionMenuItem("Register", bi.getJID(), 
                                                   "jabber:iq:register");
                        subMenu.add(menuItem);
                        menuItem.addActionListener(this);
                        break;
                    }
                }
            }
        }
        
        browseMenu.addSeparator();
        
        menuItem = new BSJIDActionMenuItem("Custom...", null, "jabber:iq:browse");
        menuItem.addActionListener(this);
        browseMenu.add(menuItem);*/
    }
    
    /** Loads preferences. Empty implementation. */
    protected void loadPreferences() {}
    
    /** Returns components and their names for display in preferences dialog.
     *  Empty implementation - returns null. 
     */
    public void getPreferencesTab(Vector components, Vector names) {}
    
    /** Stores preferences from preferences tab.
     *  Empty implementation. 
     */
    public boolean storePreferences() {
        return true;
    }
    
    /** Updates LAF of all plug-ins  */
    public void updateLAF() {
        discoWinMan.updateLAF();
    }
    
    
    /** Called when an error occured  */
    /*public void error(InfoQuery iq, String id) { 
        if (id == null && !id.equals(servedID.getID())) return;
        servedID.setID(null);
        browseBean.removeBrowseListener(this);
        String errCode = iq.getErrorCode();
        String errMsg = iq.getErrorText();
        JOptionPane.showMessageDialog(mainFrame, 
                                      "Error " + errCode + ": " + errMsg + "!",
                                      "Error", 
                                      JOptionPane.ERROR_MESSAGE);
    }*/
    
    /** Called when request for info received  */
    /*public void receivedInfoRequest(InfoQuery iq, String id) {
        System.out.println("received request for info from " + iq.getFromAddress().toString());
        System.out.println(iq.toString());
        
        try {
            IQDiscoInfoBuilder dib = new IQDiscoInfoBuilder();
            DiscoIdentityBuilder ib = new DiscoIdentityBuilder();
            DiscoFeatureBuilder fb = new DiscoFeatureBuilder();

            AttributeListImpl al = new AttributeListImpl();
            al.addAttribute("category", "CDATA", "conference");
            al.addAttribute("type", "CDATA", "text");
            al.addAttribute("name", "CDATA", "Romeo and Juliet, Act II, Scene II");
            ib.setAttributes(al);
            dib.addIdentity(new DiscoIdentity(ib));

            al = new AttributeListImpl();
            al.addAttribute("var", "CDATA", "gc-1.0");
            fb.setAttributes(al);
            dib.addFeature(new DiscoFeature(fb));

            al = new AttributeListImpl();
            al.addAttribute("var", "CDATA", "muc-password");
            fb.setAttributes(al);
            dib.addFeature(new DiscoFeature(fb));

            InfoQueryBuilder iqb = new InfoQueryBuilder();
            iqb.setType("result");
            iqb.setToAddress(iq.getFromAddress());
            iqb.setIdentifier(iq.getIdentifier());
            iqb.addExtension(new IQDiscoInfo(dib));
            
            discoBean.sendIQ(new InfoQuery(iqb));
        } catch(Exception e) {
            System.out.println("instantiation exception");
        }
    }*/
    
    /** Called when info received  */
    /*public void receivedInfo(IQDiscoInfo info, String id) {
        System.out.println("info received (id " + id + "):\n" + info.toString());
        if (id == null && !id.equals(servedID.getID())) return;
        servedID.setID(null);
        browseBean.removeBrowseListener(this);
        agents = browseItem;
        
        initMenu();
        
        PacketID pid = new PacketID();
        discoBean.discoverInfo(new JID("brouk", "jabber.org", "another"), "music", pid);
        System.out.println(pid.getID());
    }*/
    
    /** Called when items received  */
    /*public void receivedItems(IQDiscoItems items, String id) {
        System.out.println("items received (id " + id + "):\n" + items.toString());
        if (id == null && !id.equals(servedID.getID())) return;
        servedID.setID(null);
        browseBean.removeBrowseListener(this);
        agents = browseItem;
        
        initMenu();
        
        PacketID pid = new PacketID();
        discoBean.discoverItems(new JID("brouk", "jabber.org", "another"), "music", pid);
        //pid = new PacketID();
        //discoBean.discoverItems(new JID("brouk", "jabber.org", "another"), pid);
        System.out.println(pid.getID());
    }*/
    
    /** Called when request for items received  */
    /*public void receivedItemsRequest(InfoQuery iq, String id) {
        System.out.println("received request for items from " + iq.getFromAddress().toString());
        System.out.println(iq.toString());
        
        try {
            IQDiscoItemsBuilder dib = new IQDiscoItemsBuilder();
            DiscoItemBuilder ib = new DiscoItemBuilder();

            AttributeListImpl al = new AttributeListImpl();
            al.addAttribute("jid", "CDATA", "catalog.shakespeare.lit");
            al.addAttribute("node", "CDATA", "music/byrd-keyboard");
            al.addAttribute("name", "CDATA", "William Byrd - Keyboard Music");
            ib.setAttributes(al);
            dib.addItem(new DiscoItem(ib));

            al = new AttributeListImpl();
            al.addAttribute("jid", "CDATA", "catalog.shakespeare.lit");
            al.addAttribute("node", "CDATA", "music/dowland-first");
            al.addAttribute("name", "CDATA", "John Dowland - First Booke of Songes or Ayres");
            ib.setAttributes(al);
            dib.addItem(new DiscoItem(ib));

            al = new AttributeListImpl();
            al.addAttribute("jid", "CDATA", "catalog.shakespeare.lit");
            al.addAttribute("node", "CDATA", "music/morley-sixteen");
            al.addAttribute("name", "CDATA", "Thomas Morley - Sixteen Canzonets");
            ib.setAttributes(al);
            dib.addItem(new DiscoItem(ib));
            
            InfoQueryBuilder iqb = new InfoQueryBuilder();
            iqb.setType("result");
            iqb.setToAddress(iq.getFromAddress());
            iqb.setIdentifier(iq.getIdentifier());
            iqb.addExtension(new IQDiscoItems(dib));
            
            discoBean.sendIQ(new InfoQuery(iqb));
        } catch(Exception e) {
            System.out.println("instantiation exception");
        }
    }*/
    
    /** Called when a send fails  */
    /*public void sendFailed(InfoQuery iq, String id) {
        System.out.println("send failed");
    }*/
    
    /** Called when request for info received  */
    /*public void receivedEmptyResult(InfoQuery iq, String id) { }*/
    
}

⌨️ 快捷键说明

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