123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /*
- * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.pluginpanel;
-
- import com.dmdirc.Main;
- import com.dmdirc.addons.ui_swing.SwingController;
- import com.dmdirc.config.prefs.PreferencesInterface;
- import com.dmdirc.plugins.PluginInfo;
- import com.dmdirc.plugins.PluginManager;
- import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
- import com.dmdirc.addons.ui_swing.components.addonbrowser.DownloaderWindow;
- import com.dmdirc.addons.ui_swing.components.text.TextLabel;
- import com.dmdirc.addons.ui_swing.components.renderers.AddonCellRenderer;
-
- import java.awt.Window;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Collections;
- import java.util.List;
-
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Plugin manager dialog. Allows the user to manage their plugins.
- */
- public final class PluginPanel extends JPanel implements
- ActionListener, ListSelectionListener, PreferencesInterface {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 3;
- /** List of plugins. */
- private JList pluginList;
- /** plugin list scroll pane. */
- private JScrollPane scrollPane;
- /** Button to enable/disable plugin. */
- private JButton toggleButton;
- /** Currently selected plugin. */
- private int selectedPlugin;
- /** Blurb label. */
- private TextLabel blurbLabel;
- /** Parent Window. */
- private Window parentWindow;
- /** Swing Controller. */
- private SwingController controller;
-
- /**
- * Creates a new instance of PluginDialog.
- *
- * @param parentWindow Parent window
- * @param controller Swing Controller
- */
- public PluginPanel(final Window parentWindow,
- final SwingController controller) {
- super();
-
- this.parentWindow = parentWindow;
-
- initComponents();
- addListeners();
- layoutComponents();
-
- pluginList.setSelectedIndex(0);
- selectedPlugin = 0;
- }
-
- /** Initialises the components. */
- private void initComponents() {
- pluginList = new JList(new DefaultListModel());
- pluginList.setCellRenderer(new AddonCellRenderer());
-
- scrollPane = new JScrollPane(new JLabel("Loading plugins..."));
- scrollPane.setHorizontalScrollBarPolicy(
- JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- scrollPane.setVerticalScrollBarPolicy(
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
-
- toggleButton = new JButton("Enable");
- toggleButton.setEnabled(false);
-
- blurbLabel = new TextLabel(
- "Plugins allow you to extend the functionality of DMDirc.");
-
- /** {@inheritDoc}. */
- new LoggingSwingWorker() {
-
- /** {@inheritDoc}. */
- @Override
- protected Object doInBackground() {
- return populateList();
- }
-
- /** {@inheritDoc}. */
- @Override
- protected void done() {
- super.done();
- scrollPane.setViewportView(pluginList);
- }
- }.execute();
- }
-
- /** Lays out the dialog. */
- private void layoutComponents() {
- final int panelHeight;
- if (controller == null) {
- panelHeight = ((SwingController) Main.getUI()).getPrefsDialog().
- getPanelHeight();
- } else {
- panelHeight = controller.getPrefsDialog().getPanelHeight();
- }
- setLayout(new MigLayout("ins 0, fill, hmax " + panelHeight));
-
- add(blurbLabel, "wrap 10, growx, pushx");
-
- add(scrollPane, "wrap 5, grow, push");
-
- add(toggleButton, "split 2, growx, pushx, sg button");
-
- final JButton button = new JButton("Get more plugins");
- button.addActionListener(this);
- add(button, "growx, pushx, sg button");
- }
-
- /**
- * Populates the plugins list with plugins from the plugin manager.
- *
- * @return Populated list
- */
- private JList populateList() {
- final List<PluginInfo> list =
- PluginManager.getPluginManager().getPossiblePluginInfos(true);
- Collections.sort(list);
-
- ((DefaultListModel) pluginList.getModel()).clear();
- for (PluginInfo plugin : list) {
- ((DefaultListModel) pluginList.getModel()).addElement(new PluginInfoToggle(
- plugin));
- }
- pluginList.repaint();
- return pluginList;
- }
-
- /** Adds listeners to components. */
- private void addListeners() {
- toggleButton.addActionListener(this);
- pluginList.addListSelectionListener(this);
- }
-
- /**
- * Invoked when an action occurs.
- *
- * @param e The event related to this action.
- */
- @Override
- public void actionPerformed(final ActionEvent e) {
- if (e.getSource() == toggleButton && selectedPlugin >= 0) {
- final PluginInfoToggle pluginInfo = (PluginInfoToggle) pluginList.
- getSelectedValue();
-
- pluginInfo.toggle();
-
- if (pluginInfo.getState()) {
- toggleButton.setText("Disable");
- } else {
- toggleButton.setText("Enable");
- }
-
- pluginList.repaint();
- } else if (e.getSource() != toggleButton) {
- new DownloaderWindow(parentWindow);
- }
- }
-
- /** {@inheritDoc}. */
- @Override
- public void valueChanged(final ListSelectionEvent e) {
- if (!e.getValueIsAdjusting()) {
- final int selected = ((JList) e.getSource()).getSelectedIndex();
- if (selected >= 0) {
- final PluginInfoToggle pluginInfo =
- (PluginInfoToggle) ((JList) e.getSource()).
- getSelectedValue();
- toggleButton.setEnabled(true);
-
- if (pluginInfo.getState()) {
- toggleButton.setEnabled(pluginInfo.getPluginInfo().
- isUnloadable());
- toggleButton.setText("Disable");
- } else {
- toggleButton.setText("Enable");
- }
- }
- selectedPlugin = selected;
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void save() {
- for (Object pit : ((DefaultListModel) pluginList.getModel()).toArray()) {
- ((PluginInfoToggle) pit).apply();
- }
- }
- }
|