File Source: PluginPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.pluginpanel.PluginPanel__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.addons.ui_swing.components.pluginpanel;
24
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.text.TextLabel;
30 import com.dmdirc.addons.ui_swing.components.renderers.AddonCellRenderer;
31 import com.dmdirc.addons.ui_swing.dialogs.prefs.SwingPreferencesDialog;
32 import com.dmdirc.util.URLHandler;
33
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.Collections;
37 import java.util.List;
38
39 import javax.swing.DefaultListModel;
40 import javax.swing.JButton;
41 import javax.swing.JLabel;
42 import javax.swing.JList;
43 import javax.swing.JPanel;
44 import javax.swing.JScrollPane;
45 import javax.swing.event.ListSelectionEvent;
46 import javax.swing.event.ListSelectionListener;
47
48 import net.miginfocom.swing.MigLayout;
49
50 /**
51 * Plugin manager dialog. Allows the user to manage their plugins.
52 */
/*
P/P * Method: JScrollPane access$200(PluginPanel)
*
* Preconditions:
* x0 != null
* init'ed(x0.scrollPane)
*
* Postconditions:
* return_value == x0.scrollPane
* init'ed(return_value)
*/
53 public final class PluginPanel extends JPanel implements
54 ActionListener, ListSelectionListener, PreferencesInterface {
55
56 /**
57 * A version number for this class. It should be changed whenever the class
58 * structure is changed (or anything else that would prevent serialized
59 * objects being unserialized with the new class).
60 */
61 private static final long serialVersionUID = 3;
62 /** List of plugins. */
63 private JList pluginList;
64 /** plugin list scroll pane. */
65 private JScrollPane scrollPane;
66 /** Button to enable/disable plugin. */
67 private JButton toggleButton;
68 /** Currently selected plugin. */
69 private int selectedPlugin;
70 /** Blurb label. */
71 private TextLabel blurbLabel;
72
73 /** Creates a new instance of PluginDialog. */
74 public PluginPanel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.pluginpanel.PluginPanel()
*
* Preconditions:
* init'ed(com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.CLIENT_HEIGHT)
*
* Postconditions:
* this.blurbLabel == &new TextLabel(initComponents#7)
* this.pluginList == &new JList(initComponents#1)
* this.scrollPane == &new JScrollPane(initComponents#4)
* this.selectedPlugin == 0
* this.toggleButton == &new JButton(initComponents#6)
* new JButton(initComponents#6) num objects == 1
* new JList(initComponents#1) num objects == 1
* new JScrollPane(initComponents#4) num objects == 1
* new SimpleAttributeSet(TextLabel#6) num objects == 1
* new TextLabel(initComponents#7) num objects == 1
* ...
*/
75 super();
76
77 initComponents();
78 addListeners();
79 layoutComponents();
80
81 pluginList.setSelectedIndex(0);
82 selectedPlugin = 0;
83 }
84
85 /** Initialises the components. */
86 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Postconditions:
* this.blurbLabel == &new TextLabel(initComponents#7)
* this.pluginList == &new JList(initComponents#1)
* this.scrollPane == &new JScrollPane(initComponents#4)
* this.toggleButton == &new JButton(initComponents#6)
* new JButton(initComponents#6) num objects == 1
* new JList(initComponents#1) num objects == 1
* new JScrollPane(initComponents#4) num objects == 1
* new SimpleAttributeSet(TextLabel#6) num objects == 1
* new TextLabel(initComponents#7) num objects == 1
* this.blurbLabel.sas == &new SimpleAttributeSet(TextLabel#6)
*/
87 pluginList = new JList(new DefaultListModel());
88 pluginList.setCellRenderer(new AddonCellRenderer());
89
90 scrollPane = new JScrollPane(new JLabel("Loading plugins..."));
91 scrollPane.setHorizontalScrollBarPolicy(
92 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
93 scrollPane.setVerticalScrollBarPolicy(
94 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
95
96 toggleButton = new JButton("Enable");
97 toggleButton.setEnabled(false);
98
99 blurbLabel = new TextLabel(
100 "Plugins allow you to extend the functionality of DMDirc.");
101
102 /** {@inheritDoc}. */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.pluginpanel.PluginPanel$1(PluginPanel)
*/
103 new LoggingSwingWorker() {
104
105 /** {@inheritDoc}. */
106 @Override
107 protected Object doInBackground() {
/*
P/P * Method: Object doInBackground()
*
* Preconditions:
* this.pluginList != null
*
* Postconditions:
* return_value == this.pluginList
* return_value != null
*/
108 return populateList();
109 }
110
111 /** {@inheritDoc}. */
112 @Override
113 protected void done() {
/*
P/P * Method: void done()
*
* Preconditions:
* init'ed(this.pluginList)
* this.scrollPane != null
*/
114 super.done();
115 scrollPane.setViewportView(pluginList);
116 }
117 }.execute();
118 }
119
120 /** Lays out the dialog. */
121 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.CLIENT_HEIGHT)
* init'ed(this.blurbLabel)
* init'ed(this.scrollPane)
* init'ed(this.toggleButton)
*/
122 setLayout(new MigLayout("ins 0, fill, h " +
123 SwingPreferencesDialog.CLIENT_HEIGHT));
124
125 add(blurbLabel, "wrap 10, growx, pushx");
126
127 add(scrollPane, "wrap 5, grow, push");
128
129 add(toggleButton, "split 2, growx, pushx, sg button");
130
131 final JButton button = new JButton("Get more plugins");
132 button.addActionListener(this);
133 add(button, "growx, pushx, sg button");
134 }
135
136 /**
137 * Populates the plugins list with plugins from the plugin manager.
138 *
139 * @return Populated list
140 */
141 private JList populateList() {
/*
P/P * Method: JList populateList()
*
* Preconditions:
* this.pluginList != null
*
* Presumptions:
* com.dmdirc.plugins.PluginManager:getPluginManager(...)@142 != null
* com.dmdirc.plugins.PluginManager:getPossiblePluginInfos(...)@142 != null
* javax.swing.JList:getModel(...)@146 != null
* javax.swing.JList:getModel(...)@148 != null
*
* Postconditions:
* return_value == this.pluginList
* return_value != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@147: {0}, {1}
*/
142 final List<PluginInfo> list =
143 PluginManager.getPluginManager().getPossiblePluginInfos(true);
144 Collections.sort(list);
145
146 ((DefaultListModel) pluginList.getModel()).clear();
147 for (PluginInfo plugin : list) {
148 ((DefaultListModel) pluginList.getModel()).addElement(new PluginInfoToggle(
149 plugin));
150 }
151 pluginList.repaint();
152 return pluginList;
153 }
154
155 /** Adds listeners to components. */
156 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.pluginList != null
* this.toggleButton != null
*/
157 toggleButton.addActionListener(this);
158 pluginList.addListSelectionListener(this);
159 }
160
161 /**
162 * Invoked when an action occurs.
163 *
164 * @param e The event related to this action.
165 */
166 @Override
167 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* (soft) this.pluginList != null
* (soft) init'ed(this.selectedPlugin)
* (soft) this.toggleButton != null
*
* Presumptions:
* com.dmdirc.util.URLHandler:getURLHander(...)@182 != null
* javax.swing.JList:getSelectedValue(...)@169 != null
* pluginInfo.pi@169 != null
*
* Test Vectors:
* this.selectedPlugin: {-231..-1}, {0..232-1}
*/
168 if (e.getSource() == toggleButton && selectedPlugin >= 0) {
169 final PluginInfoToggle pluginInfo = (PluginInfoToggle) pluginList.
170 getSelectedValue();
171
172 pluginInfo.toggle();
173
174 if (pluginInfo.getState()) {
175 toggleButton.setText("Disable");
176 } else {
177 toggleButton.setText("Enable");
178 }
179
180 pluginList.repaint();
181 } else if (e.getSource() != toggleButton) {
182 URLHandler.getURLHander().launchApp("http://addons.dmdirc.com/");
183 }
184 }
185
186 /** {@inheritDoc}. */
187 @Override
188 public void valueChanged(final ListSelectionEvent e) {
/*
P/P * Method: void valueChanged(ListSelectionEvent)
*
* Preconditions:
* e != null
* (soft) this.toggleButton != null
*
* Presumptions:
* javax.swing.JList:getSelectedValue(...)@192 != null
* javax.swing.event.ListSelectionEvent:getSource(...)@190 != null
* javax.swing.event.ListSelectionEvent:getSource(...)@192 != null
* pluginInfo.pi@192 != null
*
* Postconditions:
* possibly_updated(this.selectedPlugin)
*
* Test Vectors:
* javax.swing.JList:getSelectedIndex(...)@190: {-231..-1}, {0..232-1}
* javax.swing.event.ListSelectionEvent:getValueIsAdjusting(...)@189: {1}, {0}
* pluginInfo.toggle@192 ^ com.dmdirc.plugins.PluginInfo:isLoaded(...)@65: {0}, {1}
*/
189 if (!e.getValueIsAdjusting()) {
190 final int selected = ((JList) e.getSource()).getSelectedIndex();
191 if (selected >= 0) {
192 final PluginInfoToggle pluginInfo =
193 (PluginInfoToggle) ((JList) e.getSource()).
194 getSelectedValue();
195 toggleButton.setEnabled(true);
196
197 if (pluginInfo.getState()) {
198 toggleButton.setEnabled(pluginInfo.getPluginInfo().isUnloadable());
199 toggleButton.setText("Disable");
200 } else {
201 toggleButton.setText("Enable");
202 }
203 }
204 selectedPlugin = selected;
205 }
206 }
207
208 /** {@inheritDoc} */
209 @Override
210 public void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* this.pluginList != null
*
* Presumptions:
* arr$.length@211 <= 232-1
* arr$[i$]@211 != null
* javax.swing.DefaultListModel:toArray(...)@211 != null
* javax.swing.JList:getModel(...)@211 != null
* pit.pi@211 != null
*/
211 for (Object pit : ((DefaultListModel) pluginList.getModel()).toArray()) {
212 ((PluginInfoToggle) pit).apply();
213 }
214 }
215 }
SofCheck Inspector Build Version : 2.17854
| PluginPanel.java |
2009-Jun-25 01:54:24 |
| PluginPanel.class |
2009-Sep-02 17:04:15 |
| PluginPanel$1.class |
2009-Sep-02 17:04:15 |