File Source: ParamModePanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.ParamModePanel__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;
24
25 import com.dmdirc.addons.ui_swing.UIUtilities;
26 import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
27 import com.dmdirc.config.ConfigManager;
28 import com.dmdirc.config.prefs.validator.RegexStringValidator;
29
30 import java.awt.Component;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33
34 import javax.swing.JCheckBox;
35 import javax.swing.JPanel;
36
37 import net.miginfocom.swing.MigLayout;
38
39 /**
40 * A component to encapsulate one parameter-requiring channel mode, displaying
41 * the user a checkbox, the mode's name, and a text field.
42 */
43 public final class ParamModePanel extends JPanel implements ActionListener {
44
45 /**
46 * A version number for this class. It should be changed whenever the class
47 * structure is changed (or anything else that would prevent serialized
48 * objects being unserialized with the new class).
49 */
50 private static final long serialVersionUID = 1;
51 /** The checkbox used in this mode panel. */
52 private final JCheckBox checkBox;
53 /** The textfield for the value of the mode. */
54 private final ValidatingJTextField textField;
55 /** the mode this component represents. */
56 private final String mode;
57 /** Original mode value. */
58 private final String originalValue;
59
60 /**
61 * Creates a new instance of ParamModePanel.
62 * @param thisMode The mode that this panel should deal with
63 * @param state The current state of the mode
64 * @param value The current value of the mode
65 * @param configManager The config manager to use to get mode names
66 */
67 public ParamModePanel(final String thisMode, final boolean state,
68 final String value, final ConfigManager configManager) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.ParamModePanel(String, bool, String, ConfigManager)
*
* Preconditions:
* configManager != null
*
* Postconditions:
* this.checkBox == &new JCheckBox(ParamModePanel#10)
* this.mode == thisMode
* init'ed(this.mode)
* this.originalValue == value
* init'ed(this.originalValue)
* this.textField == &new ValidatingJTextField(ParamModePanel#11)
* new JCheckBox(ParamModePanel#10) num objects == 1
* new ValidatingJTextField(ParamModePanel#11) num objects == 1
*
* Test Vectors:
* state: {1}, {0}
* com.dmdirc.config.ConfigManager:getOptionBool(...)@85: {0}, {1}
* com.dmdirc.config.ConfigManager:hasOptionString(...)@74: {0}, {1}
* com.dmdirc.config.ConfigManager:hasOptionString(...)@85: {0}, {1}
*/
69 super();
70 this.mode = thisMode;
71 this.originalValue = value;
72 String text;
73 String tooltip;
74 if (configManager.hasOptionString("server", "mode" + mode)) {
75 tooltip = "Mode " + mode + ": " + configManager.getOption(
76 "server", "mode" + mode);
77 } else {
78 tooltip = "Mode " + mode + ": Unknown";
79 }
80
81 setLayout(new MigLayout("fill"));
82
83 text = "Mode " + mode + ": ";
84
85 if (configManager.getOptionBool("server", "friendlymodes") &&
86 configManager.hasOptionString("server", "mode" + mode)) {
87 text = configManager.getOption("server", "mode" + mode) + ": ";
88 }
89
90 checkBox = new JCheckBox(text, state);
91 checkBox.setToolTipText(tooltip);
92 checkBox.setOpaque(UIUtilities.getTabbedPaneOpaque());
93 add(checkBox);
94
95 textField = new ValidatingJTextField(new RegexStringValidator("^[^ ]*$",
96 "Cannot contain spaces"));
97 textField.setText(value);
98 add(textField, "growx, pushx");
99
100 if (!state) {
101 textField.setEnabled(false);
102 }
103
104 checkBox.addActionListener(this);
105 }
106
107 /**
108 * Called when our checkbox is toggled.
109 * @param actionEvent associated action event
110 */
111 @Override
112 public void actionPerformed(final ActionEvent actionEvent) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* this.checkBox != null
* this.textField != null
*
* Test Vectors:
* javax.swing.JCheckBox:isSelected(...)@114: {0}, {1}
*/
113 textField.setEnabled(checkBox.isSelected());
114 if (checkBox.isSelected()) {
115 textField.requestFocusInWindow();
116 } else {
117 textField.setText(originalValue);
118 }
119
120 }
121
122 /**
123 * returns the state of this component.
124 * @return boolean state of mode
125 */
126 public boolean getState() {
/*
P/P * Method: bool getState()
*
* Preconditions:
* this.checkBox != null
* (soft) this.textField != null
*
* Postconditions:
* init'ed(return_value)
*/
127 return checkBox.isSelected() && textField.validateText();
128 }
129
130 /**
131 * returns the parameter of this mode if enabled, else returns an empty
132 * string.
133 * @return String mode parameter or "" if unset
134 */
135 public String getValue() {
/*
P/P * Method: String getValue()
*
* Preconditions:
* this.textField != null
*
* Postconditions:
* init'ed(return_value)
*/
136 return textField.getText();
137 }
138
139 /**
140 * Returns the name of the mode this component represents.
141 * @return String name of the mode
142 */
143 public String getModeName() {
/*
P/P * Method: String getModeName()
*
* Preconditions:
* this.checkBox != null
*
* Postconditions:
* init'ed(return_value)
*/
144 return checkBox.getText();
145 }
146
147 /**
148 * Returns the mode this component represents.
149 * @return String mode
150 */
151 public String getMode() {
/*
P/P * Method: String getMode()
*
* Postconditions:
* return_value == this.mode
* init'ed(return_value)
*/
152 return mode;
153 }
154
155 /**
156 * Returns the checkbox component.
157 *
158 * @return Checkbox component.
159 */
160 public Component getCheckboxComponent() {
/*
P/P * Method: Component getCheckboxComponent()
*
* Postconditions:
* return_value == this.checkBox
* init'ed(return_value)
*/
161 return checkBox;
162 }
163
164 /**
165 * Returns the value component.
166 *
167 * @return Value component
168 */
169 public Component getValueComponent() {
/*
P/P * Method: Component getValueComponent()
*
* Postconditions:
* return_value == this.textField
* init'ed(return_value)
*/
170 return textField;
171 }
172 }
SofCheck Inspector Build Version : 2.17854
| ParamModePanel.java |
2009-Jun-25 01:54:24 |
| ParamModePanel.class |
2009-Sep-02 17:04:15 |