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

📄 bsmapsplugin.java

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

/*
 * BSMapsPlugin.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 27 September 2002, 11:53
 */

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

import org.jabber.jabberbeans.util.*;

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

/**
 * <code>BSMapsPlugin</code> is main class of maping plugin to BuddySpace.
 * It provides methods for plugin loading and user interaction.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 * BuddyFinder additions by Chris Denham
 */
public class BSMapsPlugin extends BSPlugin
                          implements ActionListener, MenuListener {

    final static String DEF_MAP_PATH                 = "./maps";
    public static final String PREF_NAME_MAP_PATH    = "mapPath";
    public static final String PREF_NAME_PUBSUB_JID  = "mapPubsubJid";
    public static final String PREF_NAME_PUBSUB_NODE = "mapPubsubNode";
    public static String mapPath                     = DEF_MAP_PATH;
    public static JID pubsubJID                      = null;
    public static String pubsubNode                  = null;

    private BSMapBean mapBean;
    private BSBuddyFinderBean buddyFinderBean;
    private BSMapWinManager mapWinMan;
    private BSMapPubsubCore mapPubsub;
    //private JMenuItem testMenuItem;
    private JMenu mapsMenu;
    private JMenuItem getPubsubMapsMenuItem;

    private JTextField mapPathTextField;
    private JTextField pubsubJidTextField;
    private JTextField pubsubNodeTextField;

    /** Constructor */
    public BSMapsPlugin(BSMainFrame mainFrame, JTabbedPane tabbedPane, BSCore core) {
        super(mainFrame, tabbedPane, core);
    }

    /** Inits plugin core */
    protected void initCore() {
        mapBean = new BSMapBean(mapPath);
        mapBean.setProxy(mainFrame.proxy, mainFrame.proxyPort);
        mapBean.setFileServer(mainFrame.fileServer, mainFrame.fileServerPath, mainFrame.fileServerPort);

        mapPubsub = new BSMapPubsubCore(mainFrame, mapBean);
        mapPubsub.setPubsubJID(pubsubJID);
        mapPubsub.setPubsubNode(pubsubNode);

        buddyFinderBean = BSBuddyFinderBean.getInstance();

    }

    /** Inits plugin GUI */
    protected void initGUI() {
        mapWinMan = new BSMapWinManager(mainFrame, tabbedPane, mapPath);
        mapWinMan.setMapBean(mapBean);
        mapWinMan.setBuddyFinderBean(buddyFinderBean);
    }

    protected void loadPreferences() {
        String[] names = {PREF_NAME_MAP_PATH};
        String[] defaults = {DEF_MAP_PATH};
        Vector prefs = mainFrame.loadPreferences(names, defaults);
        if (prefs != null && prefs.size() == 1)
            mapPath = (String) prefs.elementAt(0);

        names = new String[] {PREF_NAME_PUBSUB_JID, PREF_NAME_PUBSUB_NODE};
        defaults = new String[] {"",""};
        prefs = mainFrame.loadPreferences(names, defaults);
        if (prefs != null && prefs.size() == 2) {
            String pubsubJidStr = (String) prefs.elementAt(0);
            if (pubsubJidStr == null || "".equals(pubsubJidStr)) pubsubJID = null;
            else pubsubJID = JID.fromString(pubsubJidStr);
            pubsubNode = (String) prefs.elementAt(1);
        }
        if (mapPubsub != null) {
            mapPubsub.setPubsubJID(pubsubJID);
            mapPubsub.setPubsubNode(pubsubNode);
        }
    }

    /** Adds plugin GUI controls */
    protected void addGUIControls() {
        /*testMenuItem = new JMenuItem("Test local maps");
        testMenuItem.addActionListener(this);
        //testMenuItem.setEnabled(false);
        mainFrame.addActionMenuItem(testMenuItem);*/

        mapsMenu = new JMenu("Maps");
        mapsMenu.setMnemonic('m');
        mapsMenu.addMenuListener(this);
        mainFrame.addMenu(mapsMenu);

        getPubsubMapsMenuItem = new JMenuItem("Get published maps");
        getPubsubMapsMenuItem.setEnabled(false);
        getPubsubMapsMenuItem.addActionListener(this);
        mapsMenu.add(getPubsubMapsMenuItem);
    }

    /** Connects underlying beans */
    protected void connectBeans() {
        mapWinMan.setMapBean(mapBean);
        if (core != null) {

            mapWinMan.setPresenceBean(core.getPresenceBean());
            mapWinMan.setRosterBean(core.getRosterBean());

            BSInfoQueryBean iqBean = core.getInfoQueryBean();
            if (iqBean != null)
                mapBean.setIQBean(iqBean.getIQBean());

            BSMessengerBean msgBean = core.getMessengerBean();
            if (msgBean != null)
            {
                mapBean.setMessengerBean(msgBean.getMessengerBean());
                buddyFinderBean.setMessengerBean(msgBean.getMessengerBean());
            }
        }
    }

    /** Handles connection change to connected */
    public void connected() {
        super.connected();
        //joinMenuItem.setEnabled(true);
        mapPubsub.conected();

        Vector namespaces = new Vector();
        mainFrame.getSupportedNamespaces(namespaces, new Vector());
        mapWinMan.setSupportPlans(namespaces.contains("http://jabber.open.ac.uk/tags/plan#get"));

        getPubsubMapsMenuItem.setEnabled(true);
    }


    /** Handles connection change to disconnected */
    public void disconnected() {
        //mapWinMan.closeAllWindows();
        //confBean.disconnected();
        //joinMenuItem.setEnabled(false);
        getPubsubMapsMenuItem.setEnabled(false);
    }

    /** Adds menu items into map menu */
    private void addMapsMenuItems() {
        mapsMenu.removeAll();

        mapsMenu.add(getPubsubMapsMenuItem);
        mapsMenu.add(new JSeparator());

        // gets all subdirs in mapPath
        File mapDir = new File(mapPath);
        File dirs[] = mapDir.listFiles(new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
        });
        // for all subdirs
        if (dirs == null) return;
        for (int i=0; i<dirs.length; i++) {
            // adds submenu for subdir
            //JMenu submenu = new JMenu(dirs[i].getName());
            String dirName = dirs[i].getName();
            JMenu submenu = new JMenu();
            String submenuName;
            submenuName = BSMapPubsubCore.getFriendlyOrigin(dirName);
            // if this is a directory of maps received from a pubsub
            if (dirName.startsWith(BSMapPubsubCore.PUBSUB_DIR_PREFIX))
                submenu.setToolTipText(dirName.substring(BSMapPubsubCore.PUBSUB_DIR_PREFIX.length()));
            submenu.setText(submenuName);
            mapsMenu.add(submenu);
            // gets all map files in subdir
            File maps[] = dirs[i].listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".ygf");
                }
            });
            // adds menuItems for all maps
            if (maps == null) return;
            for (int j=0; j<maps.length; j++) {
                // adds menuItem for map
                String label = maps[j].getName();
                if (label.length() >= 4) label = label.substring(0, label.length()-4);
                JMenuItem menuItem = new BSMapMenuItem(label, maps[j].getName(), dirs[i].getName());
                menuItem.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        loadMapForItem((BSMapMenuItem)e.getSource());
                    }
                });
                submenu.add(menuItem);
            }
        }
    }

    /** Loads map for given menu item */
    private void loadMapForItem(BSMapMenuItem i) {
        String mapName = i.getMapName();
        String originID = i.getOriginID();
        mapBean.loadMap(mapName, originID);
    }

    /** Handles actions from GUI controls */
    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == getPubsubMapsMenuItem) {
            mapPubsub.getPublishedMaps();
        }
        // a map menu item - load the map
        else if (evt.getSource() instanceof BSMapMenuItem) {
            BSMapMenuItem i = (BSMapMenuItem) evt.getSource();
            String mapName = i.getMapName();
            String originID = i.getOriginID();
            mapBean.loadMap(mapName, originID);
        }
    }

    /** Fills the map menu on its selection */
    public void menuSelected(MenuEvent evt) {
        Object source = evt.getSource();
        if (source == mapsMenu) {
            addMapsMenuItems();
        }
    }

    /** Empty */
    public void menuCanceled(MenuEvent menuEvent) { }
    /** Empty */
    public void menuDeselected(MenuEvent menuEvent) { }


    /** Returns components and their names for display in preferences dialog */
    public void getPreferencesTab(Vector components, Vector names) {
        GridBagConstraints gbc;

        JPanel prefsPanel = new JPanel(new GridBagLayout());

        JLabel label = new JLabel("Local path for maps:");
        gbc = new GridBagConstraints();
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        prefsPanel.add(label, gbc);
        mapPathTextField = new JTextField(mapPath);
        gbc = new GridBagConstraints();
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        prefsPanel.add(mapPathTextField, gbc);
        label = new JLabel("JID of pubsub server component for maps:");
        gbc = new GridBagConstraints();
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        prefsPanel.add(label, gbc);
        pubsubJidTextField = new JTextField(((pubsubJID == null)? "" : pubsubJID.toString()));
        gbc = new GridBagConstraints();
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        prefsPanel.add(pubsubJidTextField, gbc);
        label = new JLabel("Maps node in the pubsub server component (e.g. group/maps):");
        gbc = new GridBagConstraints();
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.WEST;
        prefsPanel.add(label, gbc);
        pubsubNodeTextField = new JTextField(((pubsubNode != null)? pubsubNode : ""));
        gbc = new GridBagConstraints();
        gbc.gridy = 6;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        prefsPanel.add(pubsubNodeTextField, gbc);

        label = new JLabel("\nSome of the changes may take effect" +
                                  " after restart of BuddySpace");
        gbc = new GridBagConstraints();
        gbc.gridy = 7;
        gbc.anchor = GridBagConstraints.WEST;
        prefsPanel.add(label, gbc);

        components.add(prefsPanel);
        names.add("Mapping");
    }

    /** Stores preferences from preferences tab.
     */
    public boolean storePreferences() {
        mapPath = mapPathTextField.getText();
        String pubsubJidStr = pubsubJidTextField.getText();
        if (pubsubJidStr == null || "".equals(pubsubJidStr)) pubsubJID = null;
        else pubsubJID = JID.fromString(pubsubJidStr);
        pubsubNode = pubsubNodeTextField.getText();
        String[] names = {PREF_NAME_MAP_PATH,
                          PREF_NAME_PUBSUB_JID,
                          PREF_NAME_PUBSUB_NODE};
        String[] values = {mapPath,
                           pubsubJidStr,
                           pubsubNode};
        mainFrame.addAndSavePreferences(names, values);

        mapPubsub.setPubsubJID(pubsubJID);
        mapPubsub.setPubsubNode(pubsubNode);

        return true;
    }

    /** Updates LAF of all plug-ins  */
    public void updateLAF() {
        mapWinMan.updateLAF();
    }


    /**
     * Performs action within given namespace for given JID.
     * Returns if action was performed.
     */
    public boolean performAction(JID jid, String namespace) {
        if (namespace != null && namespace.equals("http://jabber.open.ac.uk/tags/map")) {
            if (mapWinMan == null) return false;
            return mapWinMan.openMap(jid);
        }
        else
            return false;
    }


    /** Adds supported namespaces and their names */
    public void getSupportedNamespaces(Vector namespaces, Vector names) {
        namespaces.addElement(new String("http://jabber.open.ac.uk/tags/map"));
        names.addElement(new String("BuddySpace map"));
    }


    /** Opens windows specified in vector. */
    public void openTheWindows(Vector openWindows, boolean connected) {
        if (mapWinMan != null)
            mapWinMan.openTheWindows(openWindows, connected);
    }


    /** Add its open windows into the vector (for opening when starting next time). */
    public void addOpenWindows(Vector openWindows, boolean connected) {
        if (mapWinMan != null)
            mapWinMan.addOpenWindows(openWindows, connected);
    }

}

⌨️ 快捷键说明

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