File Source: URLProtocolPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.URLProtocolPanel__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.config.IdentityManager;
26 import com.dmdirc.addons.ui_swing.dialogs.url.URLSubsitutionsPanel;
27 import com.dmdirc.util.URLHandler;
28
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.net.URI;
32 import java.util.Arrays;
33 import java.util.Enumeration;
34
35 import javax.swing.AbstractButton;
36 import javax.swing.ButtonGroup;
37 import javax.swing.JButton;
38 import javax.swing.JFileChooser;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JRadioButton;
42 import javax.swing.JTextField;
43 import javax.swing.event.DocumentEvent;
44 import javax.swing.event.DocumentListener;
45
46 import net.miginfocom.swing.MigLayout;
47
48 /**
49 * URL Protocol configuration panel.
50 */
51 public class URLProtocolPanel extends JPanel implements ActionListener,
52 DocumentListener {
53
54 /**
55 * A version number for this class. It should be changed whenever the class
56 * structure is changed (or anything else that would prevent serialized
57 * objects being unserialized with the new class).
58 */
59 private static final long serialVersionUID = 1;
60 /** Show file chooser. */
61 private JButton showFileChooser;
62 /** Command text field. */
63 private JTextField commandPath;
64 /** Option selection. */
65 private ButtonGroup optionType;
66 /** DMDirc choice. */
67 private JRadioButton dmdirc;
68 /** Browser choice. */
69 private JRadioButton browser;
70 /** Mail choice. */
71 private JRadioButton mail;
72 /** Custom command choice. */
73 private JRadioButton custom;
74 /** Substitutions label. */
75 private JLabel subsLabel;
76 /** example label. */
77 private JLabel exampleLabel;
78 /** URL. */
79 private final URI uri;
80 /** Show insets? */
81 private final boolean useInsets;
82 /** Substitutions panel. */
83 private URLSubsitutionsPanel subsPanel;
84
85 /**
86 * Instantiates the URLDialog.
87 *
88 * @param url URL to open once added
89 * @param useInsets Show insets?
90 */
91 public URLProtocolPanel(final URI url, final boolean useInsets) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.URLProtocolPanel(URI, bool)
*
* Postconditions:
* this.browser == &new JRadioButton(initComponents#5)
* this.commandPath == &new JTextField(initComponents#2)
* this.custom == &new JRadioButton(initComponents#7)
* this.dmdirc == &new JRadioButton(initComponents#4)
* this.exampleLabel == &new JLabel(initComponents#9)
* this.mail == &new JRadioButton(initComponents#6)
* this.optionType == &new ButtonGroup(initComponents#3)
* this.showFileChooser == &new JButton(initComponents#1)
* this.subsLabel == &new JLabel(initComponents#8)
* this.subsPanel == &new URLSubsitutionsPanel(initComponents#10)
* ...
*/
92 super();
93
94 this.uri = url;
95 this.useInsets = useInsets;
96
97 initComponents();
98 layoutComponents();
99 addListeners();
100 }
101
102 /** Initialises the components. */
103 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Postconditions:
* this.browser == &new JRadioButton(initComponents#5)
* this.commandPath == &new JTextField(initComponents#2)
* this.custom == &new JRadioButton(initComponents#7)
* this.dmdirc == &new JRadioButton(initComponents#4)
* this.exampleLabel == &new JLabel(initComponents#9)
* this.mail == &new JRadioButton(initComponents#6)
* this.optionType == &new ButtonGroup(initComponents#3)
* this.showFileChooser == &new JButton(initComponents#1)
* this.subsLabel == &new JLabel(initComponents#8)
* this.subsPanel == &new URLSubsitutionsPanel(initComponents#10)
* ...
*/
104 showFileChooser = new JButton("Browse");
105 commandPath = new JTextField();
106 optionType = new ButtonGroup();
107 dmdirc = new JRadioButton("Handle internally (irc links only)");
108 browser = new JRadioButton("Use browser (or system registered handler)");
109 mail = new JRadioButton("Use mail client");
110 custom = new JRadioButton("Custom command");
111 subsLabel = new JLabel();
112 exampleLabel = new JLabel();
113
114 commandPath.setEnabled(false);
115 showFileChooser.setEnabled(false);
116 subsLabel.setEnabled(false);
117 exampleLabel.setEnabled(false);
118
119 optionType.add(dmdirc);
120 optionType.add(browser);
121 optionType.add(mail);
122 optionType.add(custom);
123
124 subsPanel = new URLSubsitutionsPanel(Arrays.asList(new String[]{"url",
125 "fragment", "host", "path", "port", "query", "protocol", "username",
126 "password"
127 }));
128 subsPanel.setVisible(custom.isSelected());
129
130 updateSelection();
131 }
132
133 /** Lays out the components. */
134 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.browser)
* init'ed(this.commandPath)
* init'ed(this.custom)
* init'ed(this.dmdirc)
* init'ed(this.exampleLabel)
* init'ed(this.mail)
* init'ed(this.showFileChooser)
* init'ed(this.subsLabel)
* init'ed(this.subsPanel)
*
* Test Vectors:
* this.useInsets: {0}, {1}
*/
135 if (useInsets) {
136 setLayout(new MigLayout("fillx, wrap 1, hidemode 3"));
137 } else {
138 setLayout(new MigLayout("ins 0, fillx, wrap 1, hidemode 3"));
139 }
140
141 add(dmdirc, "growx, pushx");
142 add(browser, "growx, pushx");
143 add(mail, "growx, pushx");
144 add(custom, "growx, pushx");
145 add(commandPath, "split 2, growx, pushx, sgy line");
146 add(showFileChooser, "sgy line");
147 add(subsLabel, "growx, pushx");
148 add(exampleLabel, "width ::100%" + (useInsets ? "-2*u" : ""));
149 add(subsPanel, "growx, pushx");
150 }
151
152 /** Adds listeners to the components. */
153 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.browser != null
* this.commandPath != null
* this.custom != null
* this.dmdirc != null
* this.mail != null
* this.showFileChooser != null
*
* Presumptions:
* javax.swing.JTextField:getDocument(...)@159 != null
*/
154 showFileChooser.addActionListener(this);
155 dmdirc.addActionListener(this);
156 browser.addActionListener(this);
157 mail.addActionListener(this);
158 custom.addActionListener(this);
159 commandPath.getDocument().addDocumentListener(this);
160 }
161
162 /** Saves the settings. */
163 public void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* this.dmdirc != null
* this.optionType != null
* this.uri != null
* (soft) this.browser != null
* (soft) this.commandPath != null
* (soft) this.custom != null
* (soft) this.mail != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getConfigIdentity(...)@164 != null
*/
164 IdentityManager.getConfigIdentity().setOption("protocol",
165 uri.getScheme(), getSelection());
166 }
167
168 /**
169 * Returns the selected value.
170 *
171 * @return Selected value
172 */
173 public String getSelection() {
174 final String value;
/*
P/P * Method: String getSelection()
*
* Preconditions:
* this.dmdirc != null
* this.optionType != null
* (soft) this.browser != null
* (soft) this.commandPath != null
* (soft) this.custom != null
* (soft) this.mail != null
*
* Postconditions:
* init'ed(return_value)
*/
175 if (optionType.getSelection() == dmdirc.getModel()) {
176 value = "DMDIRC";
177 } else if (optionType.getSelection() == browser.getModel()) {
178 value = "BROWSER";
179 } else if (optionType.getSelection() == mail.getModel()) {
180 value = "MAIL";
181 } else if (optionType.getSelection() == custom.getModel()) {
182 value = commandPath.getText();
183 } else {
184 value = "";
185 }
186
187 return value;
188 }
189
190 /**
191 * Updates the example label.
192 */
193 private void updateExample() {
/*
P/P * Method: void updateExample()
*
* Preconditions:
* this.exampleLabel != null
* (soft) this.commandPath != null
* (soft) this.optionType != null
*
* Presumptions:
* com.dmdirc.util.URLHandler:getURLHander(...)@198 != null
*
* Test Vectors:
* this.uri: Inverse{null}, Addr_Set{null}
*/
194 if (uri == null) {
195 setEnabled(false);
196 exampleLabel.setText("Example: ");
197 } else {
198 exampleLabel.setText("Example: " + URLHandler.getURLHander().
199 substituteParams(uri, commandPath.getText()));
200 }
201 }
202
203 /**
204 * Updates the selection.
205 */
206 public void updateSelection() {
/*
P/P * Method: void updateSelection()
*
* Preconditions:
* this.optionType != null
* (soft) this.browser != null
* (soft) this.commandPath != null
* (soft) this.custom != null
* (soft) this.dmdirc != null
* (soft) this.exampleLabel != null
* (soft) this.mail != null
* (soft) this.showFileChooser != null
* (soft) this.subsLabel != null
* (soft) this.subsPanel != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@207 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@209 != null
*
* Test Vectors:
* this.uri: Addr_Set{null}, Inverse{null}
* com.dmdirc.config.ConfigManager:hasOptionString(...)@207: {0}, {1}
* java.lang.String:equals(...)@213: {0}, {1}
* java.lang.String:equals(...)@215: {0}, {1}
* java.lang.String:equals(...)@217: {0}, {1}
*/
207 if (uri != null && IdentityManager.getGlobalConfig().hasOptionString(
208 "protocol", uri.getScheme())) {
209 final String option =
210 IdentityManager.getGlobalConfig().getOption("protocol",
211 uri.getScheme());
212
213 if ("DMDIRC".equals(option)) {
214 optionType.setSelected(dmdirc.getModel(), true);
215 } else if ("BROWSER".equals(option)) {
216 optionType.setSelected(browser.getModel(), true);
217 } else if ("MAIL".equals(option)) {
218 optionType.setSelected(mail.getModel(), true);
219 } else {
220 optionType.setSelected(custom.getModel(), true);
221 commandPath.setText(option);
222 actionPerformed(null);
223 }
224 } else {
225 optionType.clearSelection();
226 commandPath.setText("");
227 }
228
229 updateExample();
230 }
231
232 /** {@inheritDoc} */
233 @Override
234 public void setEnabled(final boolean enabled) {
/*
P/P * Method: void setEnabled(bool)
*
* Preconditions:
* this.optionType != null
*
* Presumptions:
* java.util.Enumeration:nextElement(...)@238 != null
* javax.swing.ButtonGroup:getElements(...)@236 != null
*
* Test Vectors:
* java.util.Enumeration:hasMoreElements(...)@237: {0}, {1}
*/
235 super.setEnabled(enabled);
236 final Enumeration<AbstractButton> buttons = optionType.getElements();
237 while (buttons.hasMoreElements()) {
238 buttons.nextElement().setEnabled(enabled);
239 }
240 }
241
242 /**
243 * {@inheritDoc}
244 *
245 * @param e action event
246 */
247 @Override
248 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* (soft) this.commandPath != null
* (soft) this.custom != null
* (soft) this.exampleLabel != null
* (soft) this.optionType != null
* (soft) this.showFileChooser != null
* (soft) this.subsLabel != null
* (soft) this.subsPanel != null
*
* Presumptions:
* javax.swing.JFileChooser:getSelectedFile(...)@255 != null
*
* Test Vectors:
* e: Addr_Set{null}, Inverse{null}
* javax.swing.JFileChooser:showDialog(...)@253: {-231..-1, 1..232-1}, {0}
*/
249 if (e != null && e.getSource() == showFileChooser) {
250 final JFileChooser fileChooser = new JFileChooser();
251 fileChooser.addChoosableFileFilter(new ExecutableFileFilter());
252 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
253 if (fileChooser.showDialog(this, "Select") ==
254 JFileChooser.APPROVE_OPTION) {
255 commandPath.setText(fileChooser.getSelectedFile().toString());
256 }
257 } else {
258 subsPanel.setVisible(custom.isSelected());
259 if (optionType.getSelection() == custom.getModel()) {
260 commandPath.setEnabled(true);
261 showFileChooser.setEnabled(true);
262 subsLabel.setEnabled(true);
263 exampleLabel.setEnabled(true);
264 } else {
265 commandPath.setEnabled(false);
266 showFileChooser.setEnabled(false);
267 subsLabel.setEnabled(false);
268 exampleLabel.setEnabled(false);
269 }
270 validate();
271 }
272 }
273
274 /** {@inheritDoc} */
275 @Override
276 public void insertUpdate(final DocumentEvent e) {
/*
P/P * Method: void insertUpdate(DocumentEvent)
*
* Preconditions:
* this.exampleLabel != null
* (soft) this.commandPath != null
* (soft) this.optionType != null
*/
277 updateExample();
278 }
279
280 /** {@inheritDoc} */
281 @Override
282 public void removeUpdate(final DocumentEvent e) {
/*
P/P * Method: void removeUpdate(DocumentEvent)
*
* Preconditions:
* this.exampleLabel != null
* (soft) this.commandPath != null
* (soft) this.optionType != null
*/
283 updateExample();
284 }
285
286 /** {@inheritDoc} */
287 @Override
288 public void changedUpdate(final DocumentEvent e) {
289 //Ignore
/*
P/P * Method: void changedUpdate(DocumentEvent)
*/
290 }
291 }
SofCheck Inspector Build Version : 2.17854
| URLProtocolPanel.java |
2009-Jun-25 01:54:24 |
| URLProtocolPanel.class |
2009-Sep-02 17:04:15 |