📄 pluginmanager.java
字号:
/* Copyright (C) 2003 Adam Olsen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.pluginmanager;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.util.ArrayList;import java.util.Hashtable;import java.util.Locale;import java.util.Properties;import java.util.ResourceBundle;import javax.swing.*;import javax.swing.SwingUtilities;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import com.valhalla.gui.DialogTracker;import com.valhalla.gui.Standard;/** * Allows the user to download, install, and upgrade plugins all from within the * application that the plugins are used in * * @author Adam Olsen * @version 1.0 */public class PluginManager extends JFrame { private ResourceBundle resources = ResourceBundle.getBundle( "PluginManager", Locale.getDefault()); private String mirror; private String script; private String installDir; private ArrayList pluginList = null; private ArrayList installedPlugins = null; private JLabel statusLabel = new JLabel("<html><b>" + resources.getString("status") + "</b>: "); private JTabbedPane pane = new JTabbedPane(); private JButton closeButton = new JButton(resources .getString("closeButton")); private JPanel mainPanel; private JPanel bottomPanel = new JPanel(new BorderLayout(5, 5)); private PluginManagerPanel listPanel = new PluginManagerPanel(this, false); private PluginManagerPanel managePanel = new PluginManagerPanel(this, true); private JButton unloadButton = new JButton(resources.getString("unload")); private JButton loadButton = new JButton(resources.getString("load")); private PluginManager thisPointer = this; /** * Constructs the Plugin Manager * * @param mirror * the mirror to download the plugins from * @param script * the script to get after connecting * @param installDir * the location to install the plugins to */ public PluginManager(String mirror, String script, String installDir) { setTitle(resources.getString("pluginManager")); this.mirror = mirror; this.script = script; this.installDir = installDir; initComponents(); DialogTracker.addDialog(this, false, true); downloadPluginList(); } /** * @return the download mirror */ String getMirror() { return mirror; } /** * @return the download script */ String getScript() { return script; } /** * @return the install dir */ String getInstallDir() { return installDir; } /** * Sets up UI components */ private void initComponents() { closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DialogTracker.removeDialog(thisPointer); } }); mainPanel = (JPanel) getContentPane(); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainPanel.setLayout(new BorderLayout(5, 5)); loadButton.setEnabled(false); unloadButton.setEnabled(false); ListActionListener listener = new ListActionListener(); managePanel.getButton().addActionListener(listener); managePanel.getTable().getSelectionModel().addListSelectionListener( new HighlightListener()); unloadButton.addActionListener(listener); loadButton.addActionListener(listener); listPanel.getButton().addActionListener(listener); // set up the manage panel managePanel.setButtonText(resources.getString("removePlugins")); JPanel bPanel = managePanel.getButtonPanel(); bPanel.add(Box.createRigidArea(new Dimension(5, 0))); bPanel.add(unloadButton); bPanel.add(loadButton); bPanel.validate(); pane.add(managePanel, resources.getString("managePlugins")); pane.add(listPanel, resources.getString("installUpgradePlugins")); mainPanel.add(pane, BorderLayout.CENTER); mainPanel.add(bottomPanel, BorderLayout.SOUTH); bottomPanel.add(statusLabel, BorderLayout.CENTER); bottomPanel.add(closeButton, BorderLayout.EAST); pack(); setSize(new Dimension(520, 470)); setLocationRelativeTo(null); } class HighlightListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { int rows[] = managePanel.getTable().getSelectedRows(); boolean selected = (rows.length > 0); managePanel.getButton().setEnabled(selected); loadButton.setEnabled(selected); unloadButton.setEnabled(selected); } } /** * Listens for a button to be clicked in one of the list panels */ class ListActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == managePanel.getButton()) removeHandler(); else if (e.getSource() == listPanel.getButton()) installHandler(pluginList); else if (e.getSource() == unloadButton) { unloadPluginHandler(installedPlugins, true, managePanel); managePanel.setPlugins(installedPlugins); } else if (e.getSource() == loadButton) { loadPluginHandler(installedPlugins, managePanel); managePanel.setPlugins(installedPlugins); } } } private boolean checkSelectedRow(PluginManagerPanel panel, int current) { if (panel == null) return false; JTable table = panel.getTable(); int rows[] = table.getSelectedRows(); for (int i = 0; i < rows.length; i++) { if (current == rows[i]) return true; } return false; } /** * loads the checked plugins */ private void loadPluginHandler(ArrayList list, PluginManagerPanel panel) { for (int i = 0; i < list.size(); i++) { Properties props = (Properties) list.get(i); if (props.getProperty("selected") != null || checkSelectedRow(panel, i)) { Hashtable loadedPlugins = PluginLoader.getInstance() .getLoadedPlugins(); PluginJAR jar = PluginLoader.getInstance().getPlugin( props.getProperty("name")); if (jar != null && !jar.getLoaded()) { jar.loadPlugin(); loadedPlugins.put(props.getProperty("name"), jar); props.remove("selected"); } } } } /** * Unloads the checked plugins */ private void unloadPluginHandler(ArrayList list, boolean mark, PluginManagerPanel panel) { for (int i = 0; i < list.size(); i++) { Properties props = (Properties) list.get(i); if (props.getProperty("selected") != null || checkSelectedRow(panel, i)) { Hashtable loadedPlugins = PluginLoader.getInstance() .getLoadedPlugins(); PluginJAR jar = PluginLoader.getInstance().getPlugin( props.getProperty("name")); if (jar != null) { if (jar.getLoaded()) jar.unloadPlugin(); loadedPlugins.remove(props.getProperty("name")); jar.close(); jar = null; System.gc(); } if (mark) props.remove("selected"); } } } /** * Installs the plugins selected in the install panel * * @param list * the list to download */ private void installHandler(ArrayList list) { setStatusText(resources.getString("downloading")); Thread thread = new Thread(new PluginDownloaderThread(this, list)); thread.start(); } /** * Removes the plugins that are selected in the manage panel */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -