File Source: SettingsPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel__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.expandingsettings;
24
25 import com.dmdirc.config.Identity;
26 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
27 import com.dmdirc.addons.ui_swing.UIUtilities;
28
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import javax.swing.BorderFactory;
34 import javax.swing.JPanel;
35 import javax.swing.JScrollPane;
36
37 import net.miginfocom.swing.MigLayout;
38
39 /**
40 * Settings panel.
41 */
42 public final class SettingsPanel extends JPanel {
43
44 /**
45 * A version number for this class. It should be changed whenever the class
46 * structure is changed (or anything else that would prevent serialized
47 * objects being unserialized with the new class).
48 */
49 private static final long serialVersionUID = 2;
50 /** Config manager. */
51 private final transient Identity config;
52
53 /** Valid option types. */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel$OptionType(String, int)
*/
54 public enum OptionType {
55
56 /** Text field. */
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel$OptionType__static_init
*
* Postconditions:
* $VALUES == &new SettingsPanel$OptionType[](SettingsPanel$OptionType__static_init#5)
* CHECKBOX == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#2)
* $VALUES[1] == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#2)
* COLOUR == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#3)
* $VALUES[2] == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#3)
* SPINNER == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#4)
* $VALUES[3] == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#4)
* TEXTFIELD == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#1)
* $VALUES[0] == &new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#1)
* new SettingsPanel$OptionType(SettingsPanel$OptionType__static_init#1) num objects == 1
* ...
*/
57 TEXTFIELD,
58 /** Check box. */
59 CHECKBOX,
60 /** Colour chooser. */
61 COLOUR,
62 /** Number spinner. */
63 SPINNER
64 }
65 /** config option -> name. */
66 private Map<String, String> names;
67 /** config option -> type. */
68 private Map<String, OptionType> types;
69 /** Info label. */
70 private TextLabel infoLabel;
71 /** Current options panel. */
72 private CurrentOptionsPanel currentOptionsPanel;
73 /** Add option panel. */
74 private AddOptionPanel addOptionPanel;
75 /** Current options scroll pane. */
76 private JScrollPane scrollPane;
77
78 /**
79 * Creates a new instance of SettingsPanel.
80 *
81 * @param config Config to use
82 * @param infoText Info blurb.
83 */
84 public SettingsPanel(final Identity config, final String infoText) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel(Identity, String)
*
* Postconditions:
* this.addOptionPanel == &new AddOptionPanel(initComponents#4)
* this.config == config
* init'ed(this.config)
* this.currentOptionsPanel == &new CurrentOptionsPanel(initComponents#5)
* this.infoLabel == &new TextLabel(initComponents#3)
* this.names == &new LinkedHashMap(initComponents#1)
* this.scrollPane == &new JScrollPane(initComponents#6)
* this.types == &new LinkedHashMap(initComponents#2)
* new AddOptionPanel(initComponents#4) num objects == 1
* new ColourChooser(initComponents#5) num objects == 1
* ...
*/
85 super();
86
87 this.setOpaque(UIUtilities.getTabbedPaneOpaque());
88 this.config = config;
89
90 initComponents(infoText);
91 layoutComponents();
92 }
93
94 /**
95 * Initialises the components.
96 *
97 * @param infoText Info blurb.
98 */
99 private void initComponents(final String infoText) {
/*
P/P * Method: void initComponents(String)
*
* Presumptions:
* javax.swing.JScrollPane:getViewport(...)@115 != null
*
* Postconditions:
* this.addOptionPanel == &new AddOptionPanel(initComponents#4)
* this.currentOptionsPanel == &new CurrentOptionsPanel(initComponents#5)
* this.infoLabel == &new TextLabel(initComponents#3)
* this.names == &new LinkedHashMap(initComponents#1)
* this.scrollPane == &new JScrollPane(initComponents#6)
* this.types == &new LinkedHashMap(initComponents#2)
* new AddOptionPanel(initComponents#4) num objects == 1
* new ColourChooser(initComponents#5) num objects == 1
* new ColourChooser(initComponents#5).showHex == 1
* new ColourChooser(initComponents#5).showIRC == 1
* ...
*/
100 names = new LinkedHashMap<String, String>();
101 types = new LinkedHashMap<String, OptionType>();
102
103 infoLabel = new TextLabel(infoText);
104
105 addOptionPanel =
106 new AddOptionPanel(this);
107 currentOptionsPanel =
108 new CurrentOptionsPanel(this);
109 scrollPane = new JScrollPane(currentOptionsPanel);
110
111 scrollPane.setBorder(BorderFactory.createTitledBorder("Current settings"));
112 addOptionPanel.setBorder(BorderFactory.createTitledBorder("Add new setting"));
113
114 scrollPane.setOpaque(UIUtilities.getTabbedPaneOpaque());
115 scrollPane.getViewport().setOpaque(UIUtilities.getTabbedPaneOpaque());
116 }
117
118 /**
119 * Lays out the components.
120 */
121 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.addOptionPanel)
* init'ed(this.infoLabel)
* init'ed(this.scrollPane)
*/
122 setLayout(new MigLayout("fill, wrap 1"));
123
124 add(infoLabel, "growx, pushx");
125 add(scrollPane, "grow, push");
126 add(addOptionPanel, "growx, pushx");
127 }
128
129 /**
130 * Adds an option to the settings panel.
131 *
132 * @param optionName Option name
133 * @param displayName Display name
134 * @param type Option type
135 */
136 public void addOption(final String optionName,
137 final String displayName,
138 final OptionType type) {
/*
P/P * Method: void addOption(String, String, SettingsPanel$OptionType)
*
* Preconditions:
* (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
* (soft) optionName != null
* (soft) this...names != null
* (soft) this.addOptionPanel != null
* (soft) this.addOptionPanel.addOptionButton != null
* (soft) this.addOptionPanel.addOptionComboBox != null
* (soft) this.currentOptionsPanel != null
* (soft) this.currentOptionsPanel.checkBoxes != null
* (soft) this.currentOptionsPanel.colours != null
* (soft) this.currentOptionsPanel.parent != null
* ...
*
* Test Vectors:
* this.config: Inverse{null}, Addr_Set{null}
* com.dmdirc.config.Identity:hasOptionString(...)@151: {0}, {1}
* java.lang.String:indexOf(...)@143: {-231..-2, 0..232-1}, {-1}
*/
139 if (config == null) {
140 return;
141 }
142
143 if (optionName.indexOf('.') == -1) {
144 return;
145 }
146 final String[] splitOption = optionName.split("\\.");
147
148 names.put(optionName, displayName);
149 types.put(optionName, type);
150
151 if (config.hasOptionString(splitOption[0], splitOption[1])) {
152 addCurrentOption(optionName, type,
153 config.getOption(splitOption[0], splitOption[1]));
154 } else {
155 addAddableOption(optionName);
156 }
157 }
158
159 /** Updates the options. */
160 public void update() {
/*
P/P * Method: void update()
*
* Preconditions:
* this.addOptionPanel != null
* init'ed(this.addOptionPanel.addInputNone)
* this.addOptionPanel.addOptionComboBox != null
* this.currentOptionsPanel != null
* this.currentOptionsPanel.checkBoxes != null
* this.currentOptionsPanel.colours != null
* this.currentOptionsPanel.spinners != null
* this.currentOptionsPanel.textFields != null
* this.types != null
* (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
* ...
*
* Presumptions:
* java.util.Iterator:next(...)@164 != null
* java.util.Map:entrySet(...)@164 != null
* java.util.Map_Entry:getKey(...)@165 != null
* java.util.Map_Entry:getValue(...)@168 != null
*
* Postconditions:
* this.addOptionPanel.addInputCurrent == this.addOptionPanel.addInputNone
* init'ed(this.addOptionPanel.addInputCurrent)
*
* Test Vectors:
* com.dmdirc.config.Identity:hasOptionString(...)@167: {0}, {1}
* java.util.Iterator:hasNext(...)@164: {0}, {1}
*/
161 addOptionPanel.clearOptions();
162 currentOptionsPanel.clearOptions();
163
164 for (Entry<String, OptionType> entry : types.entrySet()) {
165 final String[] splitOption = entry.getKey().split("\\.");
166
167 if (config.hasOptionString(splitOption[0], splitOption[1])) {
168 addCurrentOption(entry.getKey(), entry.getValue(),
169 config.getOption(splitOption[0], splitOption[1]));
170 } else {
171 addAddableOption(entry.getKey());
172 }
173 }
174 }
175
176 /** Saves the options to the config. */
177 public void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* this.types != null
* (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
* (soft) this.config != null
* (soft) this.currentOptionsPanel != null
* (soft) this.currentOptionsPanel.checkBoxes != null
* (soft) this.currentOptionsPanel.colours != null
* (soft) this.currentOptionsPanel.spinners != null
* (soft) this.currentOptionsPanel.textFields != null
*
* Presumptions:
* java.util.Iterator:next(...)@178 != null
* java.util.Map:entrySet(...)@178 != null
* java.util.Map_Entry:getKey(...)@182 != null
* java.util.Map_Entry:getValue(...)@179 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@178: {0}, {1}
*/
178 for (Entry<String, OptionType> entry : types.entrySet()) {
179 final String value =
180 currentOptionsPanel.getOption(entry.getKey(),
181 entry.getValue());
182 final String[] splitOption = entry.getKey().split("\\.");
183 if (value == null) {
184 config.unsetOption(splitOption[0], splitOption[1]);
185 } else {
186 config.setOption(splitOption[0], splitOption[1], value);
187 }
188 }
189 }
190
191 /**
192 * Adds a current option.
193 *
194 * @param optionName option to add
195 * @param type Option type
196 * @param value Option value
197 */
198 protected void addCurrentOption(final String optionName,
199 final OptionType type, final String value) {
/*
P/P * Method: void addCurrentOption(String, SettingsPanel$OptionType, String)
*
* Preconditions:
* this.currentOptionsPanel != null
* this.currentOptionsPanel.checkBoxes != null
* this.currentOptionsPanel.colours != null
* this.currentOptionsPanel.spinners != null
* this.currentOptionsPanel.textFields != null
* type != null
* (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
* (soft) this...names != null
* (soft) this.currentOptionsPanel.parent != null
*/
200 currentOptionsPanel.addOption(optionName, type, value);
201 }
202
203 /**
204 * Deletes a current option.
205 *
206 * @param optionName Option to delete
207 * @param type Option type
208 */
209 protected void removeCurrentOption(final String optionName,
210 final OptionType type) {
/*
P/P * Method: void removeCurrentOption(String, SettingsPanel$OptionType)
*
* Preconditions:
* this.currentOptionsPanel != null
* this.currentOptionsPanel.checkBoxes != null
* this.currentOptionsPanel.colours != null
* this.currentOptionsPanel.spinners != null
* this.currentOptionsPanel.textFields != null
* type != null
* (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
* (soft) this...names != null
* (soft) this.currentOptionsPanel.parent != null
*/
211 currentOptionsPanel.delOption(optionName, type);
212 }
213
214 /**
215 * Adds an addable option.
216 *
217 * @param optionName Option name
218 */
219 protected void addAddableOption(final String optionName) {
/*
P/P * Method: void addAddableOption(String)
*
* Preconditions:
* this.addOptionPanel != null
* this.addOptionPanel.addOptionButton != null
* this.addOptionPanel.addOptionComboBox != null
*/
220 addOptionPanel.addOption(optionName);
221 }
222
223 /**
224 * Returns the display name for a config option.
225 *
226 * @param optionName Option name to return the name for
227 *
228 * @return Display name for a specified option
229 */
230 public String getOptionName(final String optionName) {
/*
P/P * Method: String getOptionName(String)
*
* Preconditions:
* this.names != null
*
* Postconditions:
* init'ed(return_value)
*/
231 return names.get(optionName);
232 }
233
234 /**
235 * Returns the option type for a config option.
236 *
237 * @param optionName Option name to return the type of
238 *
239 * @return Option type for a specified option
240 */
241 public OptionType getOptionType(final String optionName) {
/*
P/P * Method: SettingsPanel$OptionType getOptionType(String)
*
* Preconditions:
* this.types != null
*
* Postconditions:
* init'ed(return_value)
*/
242 return types.get(optionName);
243 }
244 }
SofCheck Inspector Build Version : 2.17854
| SettingsPanel.java |
2009-Jun-25 01:54:24 |
| SettingsPanel.class |
2009-Sep-02 17:04:15 |
| SettingsPanel$OptionType.class |
2009-Sep-02 17:04:15 |