Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PluginPanel.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components.pluginpanel;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.config.prefs.PreferencesInterface;
  26. import com.dmdirc.plugins.PluginInfo;
  27. import com.dmdirc.plugins.PluginManager;
  28. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  29. import com.dmdirc.addons.ui_swing.components.addonbrowser.DownloaderWindow;
  30. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  31. import com.dmdirc.addons.ui_swing.components.renderers.AddonCellRenderer;
  32. import java.awt.Window;
  33. import java.awt.event.ActionEvent;
  34. import java.awt.event.ActionListener;
  35. import java.util.Collections;
  36. import java.util.List;
  37. import javax.swing.DefaultListModel;
  38. import javax.swing.JButton;
  39. import javax.swing.JLabel;
  40. import javax.swing.JList;
  41. import javax.swing.JPanel;
  42. import javax.swing.JScrollPane;
  43. import javax.swing.event.ListSelectionEvent;
  44. import javax.swing.event.ListSelectionListener;
  45. import net.miginfocom.swing.MigLayout;
  46. /**
  47. * Plugin manager dialog. Allows the user to manage their plugins.
  48. */
  49. public final class PluginPanel extends JPanel implements
  50. ActionListener, ListSelectionListener, PreferencesInterface {
  51. /**
  52. * A version number for this class. It should be changed whenever the class
  53. * structure is changed (or anything else that would prevent serialized
  54. * objects being unserialized with the new class).
  55. */
  56. private static final long serialVersionUID = 3;
  57. /** List of plugins. */
  58. private JList pluginList;
  59. /** plugin list scroll pane. */
  60. private JScrollPane scrollPane;
  61. /** Button to enable/disable plugin. */
  62. private JButton toggleButton;
  63. /** Currently selected plugin. */
  64. private int selectedPlugin;
  65. /** Blurb label. */
  66. private TextLabel blurbLabel;
  67. /** Parent Window. */
  68. private Window parentWindow;
  69. /** Swing Controller. */
  70. private SwingController controller;
  71. /**
  72. * Creates a new instance of PluginDialog.
  73. *
  74. * @param parentWindow Parent window
  75. * @param controller Swing Controller
  76. */
  77. public PluginPanel(final Window parentWindow,
  78. final SwingController controller) {
  79. super();
  80. this.parentWindow = parentWindow;
  81. initComponents();
  82. addListeners();
  83. layoutComponents();
  84. pluginList.setSelectedIndex(0);
  85. selectedPlugin = 0;
  86. }
  87. /** Initialises the components. */
  88. private void initComponents() {
  89. pluginList = new JList(new DefaultListModel());
  90. pluginList.setCellRenderer(new AddonCellRenderer());
  91. scrollPane = new JScrollPane(new JLabel("Loading plugins..."));
  92. scrollPane.setHorizontalScrollBarPolicy(
  93. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  94. scrollPane.setVerticalScrollBarPolicy(
  95. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  96. toggleButton = new JButton("Enable");
  97. toggleButton.setEnabled(false);
  98. blurbLabel = new TextLabel(
  99. "Plugins allow you to extend the functionality of DMDirc.");
  100. /** {@inheritDoc}. */
  101. new LoggingSwingWorker() {
  102. /** {@inheritDoc}. */
  103. @Override
  104. protected Object doInBackground() {
  105. return populateList();
  106. }
  107. /** {@inheritDoc}. */
  108. @Override
  109. protected void done() {
  110. super.done();
  111. scrollPane.setViewportView(pluginList);
  112. }
  113. }.execute();
  114. }
  115. /** Lays out the dialog. */
  116. private void layoutComponents() {
  117. final int panelHeight;
  118. if (controller == null) {
  119. panelHeight = ((SwingController) Main.getUI()).getPrefsDialog().
  120. getPanelHeight();
  121. } else {
  122. panelHeight = controller.getPrefsDialog().getPanelHeight();
  123. }
  124. setLayout(new MigLayout("ins 0, fill, hmax " + panelHeight));
  125. add(blurbLabel, "wrap 10, growx, pushx");
  126. add(scrollPane, "wrap 5, grow, push");
  127. add(toggleButton, "split 2, growx, pushx, sg button");
  128. final JButton button = new JButton("Get more plugins");
  129. button.addActionListener(this);
  130. add(button, "growx, pushx, sg button");
  131. }
  132. /**
  133. * Populates the plugins list with plugins from the plugin manager.
  134. *
  135. * @return Populated list
  136. */
  137. private JList populateList() {
  138. final List<PluginInfo> list =
  139. PluginManager.getPluginManager().getPossiblePluginInfos(true);
  140. Collections.sort(list);
  141. ((DefaultListModel) pluginList.getModel()).clear();
  142. for (PluginInfo plugin : list) {
  143. ((DefaultListModel) pluginList.getModel()).addElement(new PluginInfoToggle(
  144. plugin));
  145. }
  146. pluginList.repaint();
  147. return pluginList;
  148. }
  149. /** Adds listeners to components. */
  150. private void addListeners() {
  151. toggleButton.addActionListener(this);
  152. pluginList.addListSelectionListener(this);
  153. }
  154. /**
  155. * Invoked when an action occurs.
  156. *
  157. * @param e The event related to this action.
  158. */
  159. @Override
  160. public void actionPerformed(final ActionEvent e) {
  161. if (e.getSource() == toggleButton && selectedPlugin >= 0) {
  162. final PluginInfoToggle pluginInfo = (PluginInfoToggle) pluginList.
  163. getSelectedValue();
  164. pluginInfo.toggle();
  165. if (pluginInfo.getState()) {
  166. toggleButton.setText("Disable");
  167. } else {
  168. toggleButton.setText("Enable");
  169. }
  170. pluginList.repaint();
  171. } else if (e.getSource() != toggleButton) {
  172. new DownloaderWindow(parentWindow);
  173. }
  174. }
  175. /** {@inheritDoc}. */
  176. @Override
  177. public void valueChanged(final ListSelectionEvent e) {
  178. if (!e.getValueIsAdjusting()) {
  179. final int selected = ((JList) e.getSource()).getSelectedIndex();
  180. if (selected >= 0) {
  181. final PluginInfoToggle pluginInfo =
  182. (PluginInfoToggle) ((JList) e.getSource()).
  183. getSelectedValue();
  184. toggleButton.setEnabled(true);
  185. if (pluginInfo.getState()) {
  186. toggleButton.setEnabled(pluginInfo.getPluginInfo().
  187. isUnloadable());
  188. toggleButton.setText("Disable");
  189. } else {
  190. toggleButton.setText("Enable");
  191. }
  192. }
  193. selectedPlugin = selected;
  194. }
  195. }
  196. /** {@inheritDoc} */
  197. @Override
  198. public void save() {
  199. for (Object pit : ((DefaultListModel) pluginList.getModel()).toArray()) {
  200. ((PluginInfoToggle) pit).apply();
  201. }
  202. }
  203. }