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

📄 bsmappubsubcore.java

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

/*
 * BSMapPubsubCore.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2003
 *
 *
 * Created on 22 October 2003, 13:51
 */

/*import javax.swing.*;

import org.jabber.jabberbeans.util.*;

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

import edu.ou.kmi.buddyspace.plugins.maps.*;
*/

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

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

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

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

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

/**
 * <code>BSMapPubsubCore</code> provides access to pubsub for maps.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSMapPubsubCore extends BSPubsubAdapter {

    BSMainFrame mainFrame = null;
    BSPubsubBean pubsubBean = null;
    BSMapBean mapBean = null;
    PacketID servedID = null;

    protected JID pubsubJID = null;
    protected String pubsubNode = null;

    public static final String PUBSUB_DIR_PREFIX = "published_on_";

    public BSMapPubsubCore(BSMainFrame mainFrame, BSMapBean mapBean) {
        this.mainFrame = mainFrame;
        this.mapBean = mapBean;
        servedID = new PacketID(null);
    }

    public void setPubsubJID(JID pubsubJID) {
        this.pubsubJID = pubsubJID;
    }

    public void setPubsubNode(String pubsubNode) {
        this.pubsubNode = pubsubNode;
    }

    public void conected() {
        if (pubsubBean != null)
            pubsubBean.removePubsubListener(this);
        BSPluginLoader pluginLoader = mainFrame.getPluginLoader();
        if (pluginLoader != null) {
            BSPubsubPlugin pubsubPlugin = pluginLoader.getPubsubPlugin();
            pubsubBean = (pubsubPlugin != null)? pubsubPlugin.getPubsubBean() : null;
        }
        else
            pubsubBean = null;

        if (pubsubBean != null)
            pubsubBean.addPubsubListener(this);
    }


    public boolean getPublishedMaps() {
        System.out.println("BSMapPubsubCore.getPublishedMaps");
        if (pubsubBean == null) return false;
        /*if (pubsubJID == null) {
            showPubsubJIDNotSetError();
            return false;
        }*/

        //if (jid == null) {
            BSPubsubGetItemsDlg dlg = new BSPubsubGetItemsDlg(mainFrame,
                                        "Get published maps", pubsubJID, pubsubNode);
            dlg.show();
            if (dlg.pubsubJID == null) return true;
        //}
        String pubsubNode = dlg.pubsubNode;
        if (!pubsubNode.startsWith("/")) pubsubNode = "/" + pubsubNode;
        return pubsubBean.requestItems(dlg.pubsubJID, pubsubNode, servedID);
    }


    /** Called when received items event - published or retracted items. */
    public void itemsEvent(Message m, PubsubEventItems items, String id) {
        if (items == null || m == null) return;

        Enumeration ie = items.items();
        String node = items.getNode();

        handleReceivedItems(m, node, ie);
    }


    /** Called when result of items request received. */
    public void itemsReceived(InfoQuery iq, PubsubItems items, String id) {
        if (id == null || !id.equals(servedID.getID())) return;
        if (items == null || iq == null) return;

        Enumeration ie = items.items();
        String node = items.getNode();

        handleReceivedItems(iq, node, ie);
    }


    protected void handleReceivedItems(ContentPacket p, String node, Enumeration itemsEnum) {

        if (itemsEnum == null) return;

        JID jid = p.getFromAddress();
        String originID = PUBSUB_DIR_PREFIX + (((jid != null)? jid.toString() : "unknown")
                          + ((node != null)? ("%" + node) : ""));
        originID = originID.replace('/', '%');
        originID = originID.replace('\\', '%');

        Vector maps = new Vector();
        Vector oobs = new Vector();
        while (itemsEnum.hasMoreElements()) {
            PubsubItem i = (PubsubItem) itemsEnum.nextElement();
            if (!i.isItem()) continue;
            XMLData xml = i.getXML();
            if (xml == null) continue;
            if (xml instanceof MapTag) {
                System.out.println("map item received");
                maps.add(xml);
            }
            else if (xml instanceof OOB) {
                System.out.println("oob item received");
                oobs.add(xml);
            }
        }

        if (maps.size() == 0 && oobs.size() == 0) return;

        BSMapPubsubSaveConfirmDialog dlg = new BSMapPubsubSaveConfirmDialog(
                    mainFrame, jid, node, maps, oobs);
        dlg.show();
        if (dlg.maps == null) return;

        Enumeration oobsEnum = dlg.oobs.elements();
        while (oobsEnum.hasMoreElements()) {
            OOB oob = (OOB) oobsEnum.nextElement();
            try {
                mapBean.handleMapOOB(oob, originID);
            } catch (IOException e) { continue; }
        }

        Enumeration mapsEnum = dlg.maps.elements();
        while (mapsEnum.hasMoreElements()) {
            MapTag map = (MapTag) mapsEnum.nextElement();
            mapBean.saveMap(map, originID);
        }

        mapsEnum = dlg.maps.elements();
        while (mapsEnum.hasMoreElements()) {
            MapTag map = (MapTag) mapsEnum.nextElement();
            mapBean.loadMap(map.getID(), originID);
        }
    }


    /** Called when an error occured */
    public void error(InfoQuery iq, String id) {
        if (id == null || !id.equals(servedID.getID())) return;

        String errCode = iq.getErrorCode();
        String errMsg = iq.getErrorText();
        JOptionPane.showMessageDialog(mainFrame,
                                      "Get published maps failed. Error "
                                      + errCode + ": " + errMsg + "!",
                                      "Error",
                                      JOptionPane.ERROR_MESSAGE);
    }


    protected void showPubsubJIDNotSetError() {
        JOptionPane.showMessageDialog(mainFrame,
                                      "JID of pub/sub component or node used for maps not set.",
                                      "Maps pubsub error",
                                      JOptionPane.ERROR_MESSAGE);
    }


    public static String getFriendlyOrigin(String originID) {
        if (originID != null && originID.startsWith(PUBSUB_DIR_PREFIX)) {
            /*submenuName = "Published";
            int firstPercIndex = dirName.indexOf('%');
            if (firstPercIndex == -1) firstPercIndex = dirName.length();
            String server = dirName.substring(BSMapPubsubCore.PUBSUB_DIR_PREFIX.length(),
                                              firstPercIndex);
            //submenu += " on " + server;
            int lastPercIndex = dirName.lastIndexOf('%');
            if (lastPercIndex != -1)
                submenuName += " in .../" + dirName.substring(lastPercIndex+1);
            else
                submenuName += " on " + server;*/
            String result = "#";
            int firstPercIndex = originID.indexOf('%');
            if (firstPercIndex == -1) firstPercIndex = PUBSUB_DIR_PREFIX.length() - 1;
            result += originID.substring(firstPercIndex+1);
            result = result.replace('%', '/');
            return result;
        }
        else
            return originID;
    }

}

⌨️ 快捷键说明

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