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

📄 pluginsimview.java

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

import javax.swing.*;
import java.net.*;
import java.applet.*;
import java.util.*;
import edu.ou.kmi.simlink.system.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.JID;
import edu.ou.kmi.buddyspace.gui.BSMainFrame;

public class PluginSimView extends JPanel implements ActionListener, PacketListener
{
    private Map plugins = new HashMap();
    private JComboBox simulationList;
    private JButton loadSimulation;
    private ParameterChangeListener parameterChangeListener;
    private SimLinkPlugin plugin;
    private MessengerBean msgBean;
    private JID roomjid = null;
    private JID userjid = null;
    private JID linkjid = new JID(BSMainFrame.simLinkBotJID);

    public PluginSimView(ParameterChangeListener parameterChangeListener, JID roomjid, JID userjid)
    {
        this.parameterChangeListener = parameterChangeListener;
        this.roomjid = roomjid;
        this.userjid = userjid;

        simulationList = new JComboBox();
        simulationList.addItem("No plugins available");
        add(simulationList);

        loadSimulation = new JButton("Load plugin");
        loadSimulation.addActionListener(this);
        add(loadSimulation);

    }

    private void setMenu(String menuString)
    {
        StringTokenizer st = new StringTokenizer(menuString, "\n");
        simulationList.removeAllItems();
        simulationList.addItem("Select a plugin");
        plugins.clear();
        while (st.hasMoreTokens())
        {
            String name = st.nextToken();
            String className = st.nextToken();
            String url = st.nextToken();
            Vector v = new Vector();
            v.add(className);
            v.add(url);
            plugins.put(name, v);
            simulationList.addItem(name);
        }
        validate();
    }

    public void requestMenu(MessengerBean msgBean)
    {
        this.msgBean = msgBean;
        if (msgBean == null) return;
        msgBean.addPacketListener(this);
        try {
            MessageBuilder mb = new MessageBuilder();
            mb.setToAddress(linkjid);
            String room = "null";
            if (roomjid != null) room = roomjid.toString().toLowerCase();
            mb.setBody(room);
            mb.setType("private");
            mb.setSubject("menu");
            msgBean.send((Message)mb.build());
        } catch (InstantiationException e) {
            msgBean.delPacketListener(this); // might as well dump the listener
            JOptionPane.showMessageDialog(null,
                                          "Error requesting plugin menu.",
                                          "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private String getSimID()
    {
        String room = "null";
        if (roomjid != null) room = roomjid.toString().toLowerCase();
        return room + plugin.getClass().getName();
    }

    public void requestPluginParameters()
    {
        if (msgBean == null) return;
        msgBean.addPacketListener(this);
        try {
            MessageBuilder mb = new MessageBuilder();
            mb.setToAddress(linkjid);
            mb.setBody(getSimID());
            mb.setType("private");
            mb.setSubject("getparameters");
            msgBean.send((Message)mb.build());
        } catch (InstantiationException e) {
            msgBean.delPacketListener(this); // might as well dump the listener
            JOptionPane.showMessageDialog(null,
                                          "Error requesting plugin initialization.",
                                          "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void postPluginParameters(String parameters)
    {
        if (msgBean == null || roomjid == null) return;
        try {
            MessageBuilder mb = new MessageBuilder();
            mb.setToAddress(linkjid);
            mb.setBody(getSimID() + "\n" + parameters);
            mb.setType("private");
            mb.setSubject("setparameters");
            msgBean.send((Message)mb.build());
        } catch (InstantiationException e) {
            JOptionPane.showMessageDialog(null,
                                          "Error posting plugin parameters.",
                                          "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private static Map classLoaderMap = new HashMap();
    public void loadPlugin(String className, String url)
    {
        plugin = null;
        Component gui = null;
        removeAll();

        try {
            // Using cached class loader because native libs can not
            // be simultaneously be loaded by more than one class loader
            ClassLoader loader = (ClassLoader)classLoaderMap.get(url);
            if (loader == null)
            {
                loader = new URLClassLoader(new URL[] {new URL(url)});
                classLoaderMap.put(url, loader);
            }
            Class pluginClass = Class.forName(className, true, loader);
            Object instance = pluginClass.newInstance();

            if (instance instanceof Applet)
            {
                Applet applet = (Applet) instance;
                add(new JScrollPane(applet));
                applet.init();
                applet.start();
            } else if (instance instanceof SimLinkPlugin) {
                plugin = (SimLinkPlugin) instance;
                String userID = null;
                if (userjid != null) userID = userjid.toString();
                gui = plugin.createGUI(userID);
                plugin.setParameterChangeListener(parameterChangeListener);
                this.requestPluginParameters();
            } else if (instance instanceof Component) {
                gui = (Component) instance;
            } else {
                throw new Exception("Unsupported plugin class.");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            JTextPane errorPanel = new JTextPane();
            errorPanel.setEditable(false);
            errorPanel.setText("Plugin load failed.\n" +
                               "Class name: " + className + "\n" +
                               "URL: " + url + "\n" +
                               "Reason:\n" +
                               ex.toString());
            add(errorPanel);
        }

        if (gui != null)
        {
            setLayout(new BorderLayout());
            if (gui instanceof Container)
            {
                if (((Container)gui).getLayout() instanceof BorderLayout)
                {
                } else {
                    JPanel p = new JPanel();
                    p.add(gui);
                    gui = new JScrollPane(p);
                }
            } else {
                add(new JScrollPane(gui));
            }
        }

        if (gui != null) add(gui);

        validate();

    }

    public void update(String parameters)
    {
        if (plugin != null) plugin.setPackedParameters(parameters);
    }

    public void actionPerformed(ActionEvent evt)
    {
        Object source = evt.getSource();
        if (source == loadSimulation) {
            if (simulationList.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(this, "No plugin was selected");
                return;
            }
            Vector plugin = (Vector) plugins.get(simulationList.getSelectedItem());
            String className = (String) plugin.get(0);
            String url = (String) plugin.get(1);
            loadPlugin(className, url);
            validate();
        }
    }

    // Intercepts the message from the link bot to build the menu and init plugin.
    public void receivedPacket(PacketEvent packetEvent)
    {
        Packet packet = packetEvent.getPacket();
        if (packet instanceof Message)
        {
            Message msg = (Message)packet;
            String from = msg.getFromAddress().toString().toLowerCase();
            if (from.startsWith(linkjid.toString()))
            {
                msgBean.delPacketListener(this); // don't need it anymore
                String subject = msg.getSubject();
                if (subject.equals("menu")) setMenu(msg.getBody());
                if (subject.equals("getparameters")) update(msg.getBody());
            }
        }
    }

    public void sendFailed(PacketEvent packetEvent) {  }
    public void sentPacket(PacketEvent packetEvent) {  }


}

⌨️ 快捷键说明

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