File Source: PrefsComponentFactory.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.PrefsComponentFactory$5__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;
24
25 import com.dmdirc.config.prefs.PreferencesSetting;
26 import com.dmdirc.config.prefs.validator.NumericalValidator;
27 import com.dmdirc.addons.ui_swing.components.ColourChooser;
28 import com.dmdirc.addons.ui_swing.components.FontPicker;
29 import com.dmdirc.addons.ui_swing.components.OptionalColourChooser;
30 import com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay;
31 import com.dmdirc.addons.ui_swing.components.durationeditor.DurationListener;
32 import com.dmdirc.addons.ui_swing.components.renderers.MapEntryRenderer;
33 import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
34
35 import java.awt.Dimension;
36 import java.awt.Font;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.KeyAdapter;
40 import java.awt.event.KeyEvent;
41 import java.util.Map;
42
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JComponent;
46 import javax.swing.JSpinner;
47 import javax.swing.JTextField;
48 import javax.swing.SpinnerNumberModel;
49 import javax.swing.event.ChangeEvent;
50 import javax.swing.event.ChangeListener;
51
52 /**
53 * Provides methods for constructing a JComponent from a PreferencesSetting.
54 */
55 public final class PrefsComponentFactory {
56
57 /**
58 * Creates a new instance of PrefsComponentFactory.
59 */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory()
*/
60 private PrefsComponentFactory() {
61 // Shouldn't be initialised
62 }
63
64 /**
65 * Retrieves the component for the specified setting. Components are
66 * initialised with the current value(s) of the setting, and have listeners
67 * added to update the setting whenever the components are changed.
68 *
69 * @param setting The setting whose component is being requested
70 * @return An appropriate JComponent descendant
71 */
72 public static JComponent getComponent(final PreferencesSetting setting) {
73 JComponent option;
74
/*
P/P * Method: JComponent getComponent(PreferencesSetting)
*
* Preconditions:
* setting != null
* (soft) com.dmdirc.addons.ui_swing.PrefsComponentFactory$9__static_init.new int[](PrefsComponentFactory$9__static_init#1)[...] in {1..8}
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesSetting:getType(...)@75 != null
* com.dmdirc.config.prefs.PreferencesType:ordinal(...)@75 >= 0
* com.dmdirc.config.prefs.PreferencesType:values(...).length >= 1
* com.dmdirc.config.prefs.PreferencesType:ordinal(...)@75 < com.dmdirc.config.prefs.PreferencesType:values(...).length
* javax.swing.JComponent:getFont(...)@105 != null
*
* Postconditions:
* java.lang.String:substring(...)._tainted == 0
* return_value in Addr_Set{&new ValidatingJTextField(getTextOption#1),&new JCheckBox(getBooleanOption#1),&new JComboBox(getComboOption#1),&new JSpinner(getIntegerOption#1),&new JSpinner(getIntegerOption#3),&new JSpinner(getIntegerOption#5),&new DurationDisplay(getDurationOption#1),&new DurationDisplay(getDurationOption#2),&new ColourChooser(getColourOption#1),&new OptionalColourChooser(getOptionalColourOption#1),&new FontPicker(getFontOption#1)}
* new ColourChooser(getColourOption#1) num objects <= 1
* new ColourChooser(getColourOption#1).command == &""
* new ColourChooser(getColourOption#1).editButton == &new JButton(ColourChooser#2)
* new ColourChooser(getColourOption#1).listeners == &new EventListenerList(ColourChooser#1)
* new ColourChooser(getColourOption#1).previewPanel == &new JPanel(ColourChooser#5)
* new ColourChooser(getColourOption#1).showHex == 1
* new ColourChooser(getColourOption#1).showIRC == 1
* init'ed(new ColourChooser(getColourOption#1).value)
* ...
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.PrefsComponentFactory$9__static_init.new int[](PrefsComponentFactory$9__static_init#1)[...]: {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}
*/
75 switch (setting.getType()) {
76 case TEXT:
77 option = getTextOption(setting);
78 break;
79 case BOOLEAN:
80 option = getBooleanOption(setting);
81 break;
82 case MULTICHOICE:
83 option = getComboOption(setting);
84 break;
85 case INTEGER:
86 option = getIntegerOption(setting);
87 break;
88 case DURATION:
89 option = getDurationOption(setting);
90 break;
91 case COLOUR:
92 option = getColourOption(setting);
93 break;
94 case OPTIONALCOLOUR:
95 option = getOptionalColourOption(setting);
96 break;
97 case FONT:
98 option = getFontOption(setting);
99 break;
100 default:
101 throw new IllegalArgumentException(setting.getType()
102 + " is not a valid option type");
103 }
104
105 option.setPreferredSize(new Dimension(Short.MAX_VALUE, option.getFont().
106 getSize()));
107
108 return option;
109 }
110
111 /**
112 * Initialises and returns a ValidatingJTextField for the specified setting.
113 *
114 * @param setting The setting to create the component for
115 * @return A JComponent descendent for the specified setting
116 */
117 private static JComponent getTextOption(final PreferencesSetting setting) {
/*
P/P * Method: JComponent getTextOption(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Postconditions:
* return_value == &new ValidatingJTextField(getTextOption#1)
* new ValidatingJTextField(getTextOption#1) num objects == 1
*/
118 final ValidatingJTextField option = new ValidatingJTextField(setting.getValidator());
119 option.setText(setting.getValue());
120
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$1(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
121 option.addKeyListener(new KeyAdapter() {
122 @Override
123 public void keyReleased(final KeyEvent e) {
/*
P/P * Method: void keyReleased(KeyEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* java.awt.event.KeyEvent:getSource(...)@124 != null
*/
124 setting.setValue(((JTextField) e.getSource()).getText());
125 }
126 });
127
128 return option;
129 }
130
131 /**
132 * Initialises and returns a JCheckBox for the specified setting.
133 *
134 * @param setting The setting to create the component for
135 * @return A JComponent descendent for the specified setting
136 */
137 private static JComponent getBooleanOption(final PreferencesSetting setting) {
/*
P/P * Method: JComponent getBooleanOption(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Postconditions:
* return_value == &new JCheckBox(getBooleanOption#1)
* new JCheckBox(getBooleanOption#1) num objects == 1
*/
138 final JCheckBox option = new JCheckBox();
139 option.setSelected(Boolean.parseBoolean(setting.getValue()));
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$2(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
140 option.addChangeListener(new ChangeListener() {
141
142 /** {@inheritDoc} */
143 @Override
144 public void stateChanged(final ChangeEvent e) {
/*
P/P * Method: void stateChanged(ChangeEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* javax.swing.event.ChangeEvent:getSource(...)@145 != null
*/
145 setting.setValue(String.valueOf(((JCheckBox) e.getSource()).isSelected()));
146 }
147 });
148
149 return option;
150 }
151
152 /**
153 * Initialises and returns a JComboBox for the specified setting.
154 *
155 * @param setting The setting to create the component for
156 * @return A JComponent descendent for the specified setting
157 */
158 private static JComponent getComboOption(final PreferencesSetting setting) {
/*
P/P * Method: JComponent getComboOption(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesSetting:getComboOptions(...)@159 != null
* com.dmdirc.config.prefs.PreferencesSetting:getComboOptions(...)@163 != null
* java.util.Iterator:next(...)@163 != null
* java.util.Map:entrySet(...)@159 != null
* java.util.Map:entrySet(...)@163 != null
* ...
*
* Postconditions:
* return_value == &new JComboBox(getComboOption#1)
* new JComboBox(getComboOption#1) num objects == 1
*
* Test Vectors:
* java.lang.String:equals(...)@164: {0}, {1}
* java.util.Iterator:hasNext(...)@163: {0}, {1}
*/
159 final JComboBox option = new JComboBox(setting.getComboOptions().entrySet().toArray());
160 option.setRenderer(new MapEntryRenderer());
161 option.setEditable(false);
162
163 for (Map.Entry<String, String> entry : setting.getComboOptions().entrySet()) {
164 if (entry.getKey().equals(setting.getValue())) {
165 option.setSelectedItem(entry);
166 break;
167 }
168 }
169
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$3(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
170 option.addActionListener(new ActionListener() {
171
172 /** {@inheritDoc} */
173 @Override
174 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@175 != null
* javax.swing.JComboBox:getSelectedItem(...)@175 != null
*/
175 setting.setValue((String) ((Map.Entry)
176 ((JComboBox) e.getSource()).getSelectedItem()).getKey());
177 }
178 });
179
180 return option;
181 }
182
183 /**
184 * Initialises and returns a JSpinner for the specified setting.
185 *
186 * @param setting The setting to create the component for
187 * @return A JComponent descendent for the specified setting
188 */
189 private static JComponent getIntegerOption(final PreferencesSetting setting) {
190 JSpinner option;
191
192 try {
/*
P/P * Method: JComponent getIntegerOption(PreferencesSetting)
*
* Preconditions:
* (soft) setting != null
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesSetting:getValidator(...)@194 != null
*
* Postconditions:
* return_value == One-of{&new JSpinner(getIntegerOption#1), &new JSpinner(getIntegerOption#3), &new JSpinner(getIntegerOption#5)}
* return_value in Addr_Set{&new JSpinner(getIntegerOption#3),&new JSpinner(getIntegerOption#1),&new JSpinner(getIntegerOption#5)}
* new JSpinner(getIntegerOption#1) num objects <= 1
* new JSpinner(getIntegerOption#3) num objects <= 1
* new JSpinner(getIntegerOption#5) num objects <= 1
*/
193 if (setting.getValidator() instanceof NumericalValidator) {
194 option = new JSpinner(
195 new SpinnerNumberModel(Integer.parseInt(setting.getValue()),
196 ((NumericalValidator) setting.getValidator()).getMin(),
197 ((NumericalValidator) setting.getValidator()).getMax(),
198 1));
199 } else {
200 option = new JSpinner(new SpinnerNumberModel());
201 option.setValue(Integer.parseInt(setting.getValue()));
202 }
203 } catch (NumberFormatException ex) {
204 option = new JSpinner(new SpinnerNumberModel());
205 }
206
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$4(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
207 option.addChangeListener(new ChangeListener() {
208
209 /** {@inheritDoc} */
210 @Override
211 public void stateChanged(final ChangeEvent e) {
/*
P/P * Method: void stateChanged(ChangeEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* javax.swing.JSpinner:getValue(...)@212 != null
* javax.swing.event.ChangeEvent:getSource(...)@212 != null
*/
212 setting.setValue(((JSpinner) e.getSource()).getValue().
213 toString());
214 }
215 });
216
217 return option;
218 }
219
220 /**
221 * Initialises and returns a DurationDisplay for the specified setting.
222 *
223 * @param setting The setting to create the component for
224 * @return A JComponent descendent for the specified setting
225 */
226 private static JComponent getDurationOption(final PreferencesSetting setting) {
227 DurationDisplay option;
228
229 try {
/*
P/P * Method: JComponent getDurationOption(PreferencesSetting)
*
* Preconditions:
* (soft) setting != null
*
* Postconditions:
* return_value == One-of{&new DurationDisplay(getDurationOption#1), &new DurationDisplay(getDurationOption#2)}
* return_value in Addr_Set{&new DurationDisplay(getDurationOption#1),&new DurationDisplay(getDurationOption#2)}
* new DurationDisplay(getDurationOption#1) num objects <= 1
* new DurationDisplay(getDurationOption#1).button == &new JButton(initComponents#1)
* init'ed(new DurationDisplay(getDurationOption#1).duration)
* new DurationDisplay(getDurationOption#1).durationLabel == &new JLabel(initComponents#2)
* new DurationDisplay(getDurationOption#1).listeners == &new ListenerList(DurationDisplay#1)
* new DurationDisplay(getDurationOption#1).window == null
* new DurationDisplay(getDurationOption#2) num objects <= 1
* new DurationDisplay(getDurationOption#2).button == &new JButton(initComponents#1)
* ...
*/
230 option = new DurationDisplay(Integer.parseInt(setting.getValue()));
231 } catch (NumberFormatException ex) {
232 option = new DurationDisplay();
233 }
234
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$5(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
235 option.addDurationListener(new DurationListener() {
236
237 /** {@inheritDoc} */
238 @Override
239 public void durationUpdated(final int newDuration) {
/*
P/P * Method: void durationUpdated(int)
*
* Preconditions:
* this.val$setting != null
*/
240 setting.setValue(String.valueOf(newDuration));
241 }
242 });
243
244 return option;
245 }
246
247 /**
248 * Initialises and returns a ColourChooser for the specified setting.
249 *
250 * @param setting The setting to create the component for
251 * @return A JComponent descendent for the specified setting
252 */
253 private static JComponent getColourOption(final PreferencesSetting setting) {
/*
P/P * Method: JComponent getColourOption(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Postconditions:
* return_value == &new ColourChooser(getColourOption#1)
* new ColourChooser(getColourOption#1) num objects == 1
* return_value.showHex == 1
* return_value.showIRC == 1
* new EventListenerList(ColourChooser#1) num objects == 1
* new JButton(ColourChooser#2) num objects == 1
* new JPanel(ColourChooser#5) num objects == 1
* return_value.command == &""
* return_value.editButton == &new JButton(ColourChooser#2)
* return_value.listeners == &new EventListenerList(ColourChooser#1)
* ...
*/
254 final ColourChooser option = new ColourChooser(setting.getValue(), true, true);
255
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$6(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
256 option.addActionListener(new ActionListener() {
257
258 /** {@inheritDoc} */
259 @Override
260 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@261 != null
*/
261 setting.setValue(((ColourChooser) e.getSource()).getColour());
262 }
263 });
264
265 return option;
266 }
267
268 /**
269 * Initialises and returns an OptionalColourChooser for the specified setting.
270 *
271 * @param setting The setting to create the component for
272 * @return A JComponent descendent for the specified setting
273 */
274 private static JComponent getOptionalColourOption(final PreferencesSetting setting) {
/*
P/P * Method: JComponent getOptionalColourOption(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesSetting:getValue(...)@275 != null
* com.dmdirc.config.prefs.PreferencesSetting:getValue(...)@277 != null
* java.lang.String:indexOf(...)@277 <= 232-2
*
* Postconditions:
* java.lang.String:substring(...)._tainted == 0
* return_value == &new OptionalColourChooser(getOptionalColourOption#1)
* new JButton(OptionalColourChooser#2) num objects == 1
* new JCheckBox(OptionalColourChooser#7) num objects == 1
* new JPanel(OptionalColourChooser#5) num objects == 1
* new ListenerList(OptionalColourChooser#1) num objects == 1
* new OptionalColourChooser(getOptionalColourOption#1) num objects == 1
* return_value.showHex == 1
* return_value.showIRC == 1
* return_value.editButton == &new JButton(OptionalColourChooser#2)
* ...
*/
275 final boolean state = setting.getValue() != null
276 && !setting.getValue().startsWith("false:");
277 final String colour = setting.getValue() == null ? "0" : setting.getValue().
278 substring(1 + setting.getValue().indexOf(':'));
279
280 final OptionalColourChooser option = new OptionalColourChooser(colour, state, true, true);
281
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$7(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
282 option.addActionListener(new ActionListener() {
283
284 /** {@inheritDoc} */
285 @Override
286 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...).enabled@287 != null
* java.awt.event.ActionEvent:getSource(...)@287 != null
*/
287 setting.setValue(
288 ((OptionalColourChooser) e.getSource()).isEnabled() + ":"
289 + ((OptionalColourChooser) e.getSource()).getColour());
290 }
291 });
292
293 return option;
294 }
295
296 /**
297 * Initialises and returns an Font Chooser for the specified setting.
298 *
299 * @param setting The setting to create the component for
300 * @return A JComponent descendent for the specified setting
301 */
302 private static JComponent getFontOption(final PreferencesSetting setting) {
/*
P/P * Method: JComponent getFontOption(PreferencesSetting)
*
* Preconditions:
* setting != null
*
* Postconditions:
* return_value == &new FontPicker(getFontOption#1)
* new FontPicker(getFontOption#1) num objects == 1
*/
303 final String value = setting.getValue();
304
305 final FontPicker option = new FontPicker(value);
306
/*
P/P * Method: void com.dmdirc.addons.ui_swing.PrefsComponentFactory$8(PreferencesSetting)
*
* Postconditions:
* this.val$setting == Param_1
* init'ed(this.val$setting)
*/
307 option.addActionListener(new ActionListener() {
308
309 /** {@inheritDoc} */
310 @Override
311 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* this.val$setting != null
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@312 != null
*/
312 Object value = ((FontPicker) e.getSource()).getSelectedItem();
313 if (value instanceof Font) {
314 setting.setValue(((Font) value).getFamily());
315 } else {
316 setting.setValue(null);
317 }
318 }
319 });
320
321 return option;
322 }
323
324 }
SofCheck Inspector Build Version : 2.17854
| PrefsComponentFactory.java |
2009-Jun-25 01:54:24 |
| PrefsComponentFactory.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$1.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$2.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$3.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$4.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$5.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$6.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$7.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$8.class |
2009-Sep-02 17:04:15 |
| PrefsComponentFactory$9.class |
2009-Sep-02 17:04:15 |