File Source: PrefsCategoryLoader.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.prefs.PrefsCategoryLoader__static_init
*/
1 /*
2 *
3 * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 package com.dmdirc.addons.ui_swing.dialogs.prefs;
25
26 import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
27 import com.dmdirc.addons.ui_swing.components.ColourChooser;
28 import com.dmdirc.addons.ui_swing.components.OptionalColourChooser;
29 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
30 import com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay;
31 import com.dmdirc.config.prefs.PreferencesCategory;
32 import com.dmdirc.config.prefs.PreferencesSetting;
33
34 import com.dmdirc.logger.ErrorLevel;
35 import com.dmdirc.logger.Logger;
36 import java.util.concurrent.ExecutionException;
37 import javax.swing.BorderFactory;
38 import javax.swing.JComponent;
39 import javax.swing.JPanel;
40 import javax.swing.SwingWorker;
41
42 import net.miginfocom.layout.PlatformDefaults;
43 import net.miginfocom.swing.MigLayout;
44
45 /**
46 * Loads a preferences panel for a specified preferences category in the
47 * background.
48 */
/*
P/P * Method: Object doInBackground()
*
* Preconditions:
* this.category != null
* (soft) this.categoryPanel != null
* (soft) init'ed(this.categoryPanel.parent)
* (soft) this.categoryPanel.tooltip != null
*
* Postconditions:
* return_value == &new JPanel(addCategory#1)
* new JPanel(addCategory#1) num objects == 1
*/
49 public class PrefsCategoryLoader extends SwingWorker<JPanel, Object> {
50
51 /** Panel gap. */
52 private final int padding = (int) PlatformDefaults.getUnitValueX("related").
53 getValue();
54 /** Panel left padding. */
55 private final int leftPadding = (int) PlatformDefaults.getPanelInsets(1).
56 getValue();
57 /** Panel right padding. */
58 private final int rightPadding = (int) PlatformDefaults.getPanelInsets(3).
59 getValue();
60 /** Error panel. */
61 private JPanel errorCategory;
62 private CategoryPanel categoryPanel;
63 private PreferencesCategory category;
64
65 /**
66 * Instantiates a new preferences category loader.
67 *
68 * @param categoryPanel Parent Category panel
69 * @param category Preferences Category to load
70 */
71 public PrefsCategoryLoader(final CategoryPanel categoryPanel,
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.PrefsCategoryLoader(CategoryPanel, PreferencesCategory)
*
* Presumptions:
* (int) (net.miginfocom.layout.UnitValue:getValue(...)@52) in {-231..232-1}
* (int) (net.miginfocom.layout.UnitValue:getValue(...)@55) in {-231..232-1}
* (int) (net.miginfocom.layout.UnitValue:getValue(...)@58) in {-231..232-1}
* net.miginfocom.layout.PlatformDefaults:getPanelInsets(...)@55 != null
* net.miginfocom.layout.PlatformDefaults:getPanelInsets(...)@58 != null
* ...
*
* Postconditions:
* this.category == category
* init'ed(this.category)
* this.categoryPanel == categoryPanel
* init'ed(this.categoryPanel)
* this.errorCategory == &new JPanel(PrefsCategoryLoader#1)
* init'ed(this.leftPadding)
* init'ed(this.padding)
* init'ed(this.rightPadding)
* new JPanel(PrefsCategoryLoader#1) num objects == 1
*/
72 final PreferencesCategory category) {
73 this.categoryPanel = categoryPanel;
74 this.category = category;
75
76 errorCategory = new JPanel(new MigLayout("fillx"));
77 errorCategory.add(new TextLabel("There was an error loading this category."));
78 }
79
80 /**
81 * {@inheritDoc}
82 *
83 * @throws Exception if unable to compute a result
84 */
85 @Override
86 protected JPanel doInBackground() throws Exception {
/*
P/P * Method: JPanel doInBackground()
*
* Preconditions:
* this.category != null
* (soft) this.categoryPanel != null
* (soft) init'ed(this.categoryPanel.parent)
* (soft) this.categoryPanel.tooltip != null
*
* Postconditions:
* return_value == &new JPanel(addCategory#1)
* new JPanel(addCategory#1) num objects == 1
*/
87 return addCategory(category);
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 protected void done() {
/*
P/P * Method: void done()
*
* Preconditions:
* (soft) init'ed(this.category)
* (soft) this.categoryPanel != null
* (soft) init'ed(this.categoryPanel.category)
* (soft) this.categoryPanel.panels != null
* (soft) init'ed(this.errorCategory)
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.dialogs.prefs.PrefsCategoryLoader:isCancelled(...)@93: {0}, {1}
*/
93 if (isCancelled()) {
94 return;
95 }
96 categoryPanel.categoryLoaded(this, category);
97 }
98
99 /**
100 * Returns the panel for this loader.
101 *
102 * @return Loaded panel
103 */
104 public JPanel getPanel() {
105 JPanel panel;
106 try {
/*
P/P * Method: JPanel getPanel()
*
* Preconditions:
* (soft) init'ed(this.errorCategory)
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
*
* Postconditions:
* init'ed(return_value)
*/
107 panel = super.get();
108 } catch (InterruptedException ex) {
109 panel = errorCategory;
110 } catch (ExecutionException ex) {
111 Logger.appError(ErrorLevel.MEDIUM, "Error loading prefs panel", ex);
112 panel = errorCategory;
113 }
114 return panel;
115 }
116
117 /**
118 * Initialises the specified category.
119 *
120 * @since 0.6.3m1
121 * @param category The category that is being initialised
122 * @param panel The panel to which we're adding its contents
123 * @param path The textual path of this category
124 */
125 private void initCategory(final PreferencesCategory category,
126 final JPanel panel, final String path) {
127
/*
P/P * Method: void initCategory(PreferencesCategory, JPanel, String)
*
* Preconditions:
* category != null
* (soft) panel != null
* (soft) this.categoryPanel != null
* (soft) init'ed(this.categoryPanel.parent)
* (soft) this.categoryPanel.tooltip != null
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesCategory:getDescription(...)@128 != null
* com.dmdirc.config.prefs.PreferencesCategory:getSettings(...)@153 != null
* com.dmdirc.config.prefs.PreferencesCategory:getSubcats(...)@133 != null
* com.dmdirc.config.prefs.PreferencesCategory:getSubcats(...)@158 != null
* java.util.Iterator:next(...)@133 != null
* ...
*
* Test Vectors:
* com.dmdirc.config.prefs.PreferencesCategory:hasObject(...)@141: {0}, {1}
* com.dmdirc.config.prefs.PreferencesCategory:isInline(...)@134: {0}, {1}
* com.dmdirc.config.prefs.PreferencesCategory:isInline(...)@136: {1}, {0}
* com.dmdirc.config.prefs.PreferencesCategory:isInline(...)@159: {0}, {1}
* com.dmdirc.config.prefs.PreferencesCategory:isInlineBefore(...)@134: {0}, {1}
* com.dmdirc.config.prefs.PreferencesCategory:isInlineBefore(...)@157: {1}, {0}
* java.lang.String:isEmpty(...)@128: {1}, {0}
* java.util.Iterator:hasNext(...)@133: {0}, {1}
* java.util.Iterator:hasNext(...)@153: {0}, {1}
* java.util.Iterator:hasNext(...)@158: {0}, {1}
*/
128 if (!category.getDescription().isEmpty()) {
129 panel.add(new TextLabel(category.getDescription()), "span, " +
130 "growx, pushx, wrap 2*unrel");
131 }
132
133 for (PreferencesCategory child : category.getSubcats()) {
134 if (child.isInline() && category.isInlineBefore()) {
135 addInlineCategory(child, panel);
136 } else if (!child.isInline()) {
137 addCategory(child);
138 }
139 }
140
141 if (category.hasObject()) {
142 if (!(category.getObject() instanceof JPanel)) {
143 throw new IllegalArgumentException(
144 "Custom preferences objects" +
145 " for this UI must extend JPanel.");
146 }
147
148 panel.add((JPanel) category.getObject(), "growx, pushx");
149
150 return;
151 }
152
153 for (PreferencesSetting setting : category.getSettings()) {
154 addComponent(category, setting, panel);
155 }
156
157 if (!category.isInlineBefore()) {
158 for (PreferencesCategory child : category.getSubcats()) {
159 if (child.isInline()) {
160 addInlineCategory(child, panel);
161 }
162 }
163 }
164 }
165
166 /**panel.
167 * Initialises and adds a component to a panel.
168 *
169 * @param category The category the setting is being added to
170 * @param setting The setting to be used
171 * @param panel The panel to add the component to
172 */
173 private void addComponent(final PreferencesCategory category,
174 final PreferencesSetting setting,
175 final JPanel panel) {
176
/*
P/P * Method: void addComponent(PreferencesCategory, PreferencesSetting, JPanel)
*
* Preconditions:
* panel != null
* setting != null
* this.categoryPanel != null
* this.categoryPanel.tooltip != null
* (soft) init'ed(this.categoryPanel.parent)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.PrefsComponentFactory:getComponent(...)@179 != null
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.components.ColourChooser:instanceof(...)@187: {0}, {1}
* com.dmdirc.addons.ui_swing.components.OptionalColourChooser:instanceof(...)@189: {0}, {1}
* com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay:instanceof(...)@185: {0}, {1}
*/
177 final TextLabel label = getLabel(setting);
178
179 JComponent option = PrefsComponentFactory.getComponent(setting);
180 option.setToolTipText(null);
181 categoryPanel.getToolTipPanel().registerTooltipHandler(label);
182 categoryPanel.getToolTipPanel().registerTooltipHandler(option,
183 setting.getHelptext());
184
185 if (option instanceof DurationDisplay) {
186 ((DurationDisplay) option).setWindow(categoryPanel.getParentWindow());
187 } else if (option instanceof ColourChooser) {
188 ((ColourChooser) option).setWindow(categoryPanel.getParentWindow());
189 } else if (option instanceof OptionalColourChooser) {
190 ((OptionalColourChooser) option).setWindow(categoryPanel.
191 getParentWindow());
192 }
193
194 panel.add(label, "align label, wmax 40%");
195 panel.add(option, "growx, pushx, w 60%");
196 }
197
198 /**
199 * Retrieves the title label for the specified setting.
200 *
201 * @param setting The setting whose label is being requested
202 * @return A TextLabel with the appropriate text and tooltip
203 */
204 private TextLabel getLabel(final PreferencesSetting setting) {
/*
P/P * Method: TextLabel getLabel(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesSetting:getHelptext(...)@207 != null
*
* Postconditions:
* return_value == &new TextLabel(getLabel#1)
* new SimpleAttributeSet(TextLabel#6) num objects == 1
* new TextLabel(getLabel#1) num objects == 1
* return_value.sas == &new SimpleAttributeSet(TextLabel#6)
*
* Test Vectors:
* java.lang.String:isEmpty(...)@207: {0}, {1}
*/
205 final TextLabel label = new TextLabel(setting.getTitle() + ": ", false);
206
207 if (setting.getHelptext().isEmpty()) {
208 label.setToolTipText("No help available.");
209 } else {
210 label.setToolTipText(setting.getHelptext());
211 }
212
213 return label;
214 }
215
216 /**
217 * Adds a new inline category.
218 *
219 * @param category The category to be added
220 * @param parent The panel to add the category to
221 */
222 private void addInlineCategory(final PreferencesCategory category,
223 final JPanel parent) {
/*
P/P * Method: void addInlineCategory(PreferencesCategory, JPanel)
*
* Preconditions:
* category != null
* parent != null
* (soft) this.categoryPanel != null
* (soft) init'ed(this.categoryPanel.parent)
* (soft) this.categoryPanel.tooltip != null
*/
224 final JPanel panel =
225 new JPanel(new MigLayout("fillx, gap unrel, wrap 2, hidemode 3, pack, " +
226 "wmax 470-" + leftPadding + "-" +
227 rightPadding + "-2*" + padding));
228 panel.setBorder(BorderFactory.createTitledBorder(category.getTitle()));
229
230 parent.add(panel, "span, growx, pushx, wrap");
231
232 initCategory(category, panel, "");
233 }
234
235 /**
236 * Adds the specified category to the preferences dialog.
237 *
238 * @since 0.6.3m1
239 * @param category The category to be added
240 * @param namePrefix Category name prefix
241 */
242 private JPanel addCategory(final PreferencesCategory category) {
/*
P/P * Method: JPanel addCategory(PreferencesCategory)
*
* Preconditions:
* category != null
* (soft) this.categoryPanel != null
* (soft) init'ed(this.categoryPanel.parent)
* (soft) this.categoryPanel.tooltip != null
*
* Postconditions:
* return_value == &new JPanel(addCategory#1)
* new JPanel(addCategory#1) num objects == 1
*/
243 final JPanel panel =
244 new JPanel(new MigLayout("fillx, gap unrel, " +
245 "wrap 2, hidemode 3, wmax 470-" + leftPadding + "-" +
246 rightPadding + "-2*" + padding));
247 final String path = category.getPath();
248
249 initCategory(category, panel, path);
250
251 return panel;
252 }
253 }
SofCheck Inspector Build Version : 2.17854
| PrefsCategoryLoader.java |
2009-Jun-25 01:54:24 |
| PrefsCategoryLoader.class |
2009-Sep-02 17:04:16 |