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