Parcourir la source

Add FileBrowser component to PrefsComponentFactory

Fixes issue 1279

Change-Id: I5a0241e72db64a39b571fb76d2eee96b8a017ed1
Reviewed-on: http://gerrit.dmdirc.com/869
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.4
Simon Mott il y a 14 ans
Parent
révision
86a9b06e6f

+ 52
- 10
src/com/dmdirc/addons/ui_swing/PrefsComponentFactory.java Voir le fichier

22
 
22
 
23
 package com.dmdirc.addons.ui_swing;
23
 package com.dmdirc.addons.ui_swing;
24
 
24
 
25
+import com.dmdirc.addons.ui_swing.components.FileBrowser;
25
 import com.dmdirc.config.prefs.PreferencesSetting;
26
 import com.dmdirc.config.prefs.PreferencesSetting;
26
 import com.dmdirc.config.prefs.validator.NumericalValidator;
27
 import com.dmdirc.config.prefs.validator.NumericalValidator;
27
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
28
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
46
 import javax.swing.JCheckBox;
47
 import javax.swing.JCheckBox;
47
 import javax.swing.JComboBox;
48
 import javax.swing.JComboBox;
48
 import javax.swing.JComponent;
49
 import javax.swing.JComponent;
50
+import javax.swing.JFileChooser;
49
 import javax.swing.JSpinner;
51
 import javax.swing.JSpinner;
50
 import javax.swing.JTextField;
52
 import javax.swing.JTextField;
51
 import javax.swing.SpinnerNumberModel;
53
 import javax.swing.SpinnerNumberModel;
105
             case FONT:
107
             case FONT:
106
                 option = getFontOption(setting);
108
                 option = getFontOption(setting);
107
                 break;
109
                 break;
110
+            case FILE:
111
+                option = getFileBrowseOption(setting, JFileChooser.FILES_ONLY);
112
+                break;
113
+            case DIRECTORY:
114
+                option = getFileBrowseOption(setting, JFileChooser.DIRECTORIES_ONLY);
115
+                break;
116
+            case FILES_AND_DIRECTORIES:
117
+                option = getFileBrowseOption(setting, JFileChooser.FILES_AND_DIRECTORIES);
118
+                break;
108
             default:
119
             default:
109
                 throw new IllegalArgumentException(setting.getType()
120
                 throw new IllegalArgumentException(setting.getType()
110
                         + " is not a valid option type");
121
                         + " is not a valid option type");
146
         final JCheckBox option = new JCheckBox();
157
         final JCheckBox option = new JCheckBox();
147
         option.setSelected(Boolean.parseBoolean(setting.getValue()));
158
         option.setSelected(Boolean.parseBoolean(setting.getValue()));
148
         option.addChangeListener(new ChangeListener() {
159
         option.addChangeListener(new ChangeListener() {
149
-            
160
+
150
             /** {@inheritDoc} */
161
             /** {@inheritDoc} */
151
             @Override
162
             @Override
152
             public void stateChanged(final ChangeEvent e) {
163
             public void stateChanged(final ChangeEvent e) {
176
         }
187
         }
177
 
188
 
178
         option.addActionListener(new ActionListener() {
189
         option.addActionListener(new ActionListener() {
179
-            
190
+
180
             /** {@inheritDoc} */
191
             /** {@inheritDoc} */
181
             @Override
192
             @Override
182
             public void actionPerformed(final ActionEvent e) {
193
             public void actionPerformed(final ActionEvent e) {
238
                 && !setting.getValue().startsWith("false:");
249
                 && !setting.getValue().startsWith("false:");
239
         final String integer = setting.getValue() == null ? "0" : setting.getValue().
250
         final String integer = setting.getValue() == null ? "0" : setting.getValue().
240
                 substring(1 + setting.getValue().indexOf(':'));
251
                 substring(1 + setting.getValue().indexOf(':'));
241
-        
252
+
242
         OptionalJSpinner option;
253
         OptionalJSpinner option;
243
         Validator optionalValidator = setting.getValidator();
254
         Validator optionalValidator = setting.getValidator();
244
         Validator numericalValidator = null;
255
         Validator numericalValidator = null;
296
         }
307
         }
297
 
308
 
298
         option.addDurationListener(new DurationListener() {
309
         option.addDurationListener(new DurationListener() {
299
-            
310
+
300
             /** {@inheritDoc} */
311
             /** {@inheritDoc} */
301
             @Override
312
             @Override
302
             public void durationUpdated(final int newDuration) {
313
             public void durationUpdated(final int newDuration) {
317
         final ColourChooser option = new ColourChooser(setting.getValue(), true, true);
328
         final ColourChooser option = new ColourChooser(setting.getValue(), true, true);
318
 
329
 
319
         option.addActionListener(new ActionListener() {
330
         option.addActionListener(new ActionListener() {
320
-            
331
+
321
             /** {@inheritDoc} */
332
             /** {@inheritDoc} */
322
             @Override
333
             @Override
323
             public void actionPerformed(final ActionEvent e) {
334
             public void actionPerformed(final ActionEvent e) {
343
         final OptionalColourChooser option = new OptionalColourChooser(colour, state, true, true);
354
         final OptionalColourChooser option = new OptionalColourChooser(colour, state, true, true);
344
 
355
 
345
         option.addActionListener(new ActionListener() {
356
         option.addActionListener(new ActionListener() {
346
-            
357
+
347
             /** {@inheritDoc} */
358
             /** {@inheritDoc} */
348
             @Override
359
             @Override
349
             public void actionPerformed(final ActionEvent e) {
360
             public void actionPerformed(final ActionEvent e) {
355
 
366
 
356
         return option;
367
         return option;
357
     }
368
     }
358
-    
369
+
359
     /**
370
     /**
360
      * Initialises and returns an Font Chooser for the specified setting.
371
      * Initialises and returns an Font Chooser for the specified setting.
361
      *
372
      *
366
         final String value = setting.getValue();
377
         final String value = setting.getValue();
367
 
378
 
368
         final FontPicker option = new FontPicker(value);
379
         final FontPicker option = new FontPicker(value);
369
-        
380
+
370
         option.addActionListener(new ActionListener() {
381
         option.addActionListener(new ActionListener() {
371
 
382
 
372
             /** {@inheritDoc} */
383
             /** {@inheritDoc} */
379
                     setting.setValue(null);
390
                     setting.setValue(null);
380
                 }
391
                 }
381
             }
392
             }
382
-        });        
383
-        
393
+        });
394
+
395
+        return option;
396
+    }
397
+
398
+    /**
399
+     * Initialises and returns a FileBrowser for the specified setting.
400
+     *
401
+     * @param setting The setting to create the component for
402
+     * @param type The type of filechooser we want (Files/Directories/Both)
403
+     *
404
+     * @return A JComponent descendent for the specified setting
405
+     */
406
+    private static JComponent getFileBrowseOption(final PreferencesSetting setting,
407
+            final int type) {
408
+        final FileBrowser option = new FileBrowser(setting, type);
409
+
410
+        option.addActionListener(new ActionListener() {
411
+            /** {@inheritDoc} */
412
+            @Override
413
+            public void actionPerformed(final ActionEvent e) {
414
+                setting.setValue(option.getPath());
415
+            }
416
+        });
417
+
418
+        option.addKeyListener(new KeyAdapter() {
419
+            /** {@inheritDoc} */
420
+            @Override
421
+            public void keyReleased(final KeyEvent e) {
422
+                setting.setValue(((JTextField) e.getSource()).getText());
423
+            }
424
+        });
425
+
384
         return option;
426
         return option;
385
     }
427
     }
386
 
428
 

+ 137
- 0
src/com/dmdirc/addons/ui_swing/components/FileBrowser.java Voir le fichier

1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes,
3
+ * Simon Mott
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.components;
25
+
26
+import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
27
+import com.dmdirc.config.prefs.PreferencesSetting;
28
+import com.dmdirc.util.ListenerList;
29
+
30
+import java.awt.event.ActionEvent;
31
+import java.awt.event.ActionListener;
32
+
33
+import java.awt.event.KeyListener;
34
+import javax.swing.JButton;
35
+import javax.swing.JFileChooser;
36
+import javax.swing.JPanel;
37
+import javax.swing.JTextField;
38
+
39
+import net.miginfocom.swing.MigLayout;
40
+
41
+/**
42
+ * File browser component.
43
+ *
44
+ * @author Simon Mott
45
+ * @since 0.6.3
46
+ */
47
+public class FileBrowser extends JPanel implements ActionListener {
48
+
49
+    /**
50
+     * A version number for this class. It should be changed whenever the class
51
+     * structure is changed (or anything else that would prevent serialized
52
+     * objects being unserialized with the new class).
53
+     */
54
+    private static final long serialVersionUID = 1;
55
+
56
+    /** Browse button. */
57
+    private JButton browseButton;
58
+    /** Text field to show path of chosen file. */
59
+    private ValidatingJTextField pathField;
60
+    /** File browsing window. */
61
+    private JFileChooser fileChooser = new JFileChooser();
62
+    /** Our listeners. */
63
+    private final ListenerList listeners = new ListenerList();
64
+
65
+    /**
66
+     * Creates a new File Browser.
67
+     *
68
+     * @param setting The setting to create the component for
69
+     * @param type The type of filechooser we want (Files/Directories/Both)
70
+     */
71
+    public FileBrowser(final PreferencesSetting setting, final int type) {
72
+        fileChooser.setFileSelectionMode(type);
73
+        
74
+        browseButton = new JButton("Browse");
75
+        browseButton.addActionListener(this);
76
+
77
+        pathField = new ValidatingJTextField(new JTextField(setting.getValue()),
78
+                setting.getValidator());
79
+
80
+        setLayout(new MigLayout("ins 0, fill"));
81
+        add(pathField, "growx, pushx, sgy all");
82
+        add(browseButton, "sgy all");
83
+    }
84
+
85
+    /**
86
+     * {@inheritDoc}.
87
+     *
88
+     * @param e Action event
89
+     */
90
+    @Override
91
+    public void actionPerformed(ActionEvent e) {
92
+        fileChooser.showOpenDialog(this);
93
+
94
+        if (fileChooser.getSelectedFile() != null) {
95
+            pathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
96
+        }
97
+        fireActionEvent();
98
+    }
99
+
100
+    /**
101
+     * Adds an action listener to this file browser. Action
102
+     * listeners are notified whenever the path changes.
103
+     *
104
+     * @param l The listener to be added
105
+     */
106
+    public void addActionListener(final ActionListener l) {
107
+        listeners.add(ActionListener.class, l);
108
+    }
109
+
110
+    /**
111
+     * {@inheritDoc}.
112
+     *
113
+     * @param l The listener to be added
114
+     */
115
+    @Override
116
+    public void addKeyListener(final KeyListener l) {
117
+        pathField.addKeyListener(l);
118
+    }
119
+
120
+    /**
121
+     * Returns the current path selected by this file browser.
122
+     *
123
+     * @return Path selected by this filebrowser
124
+     */
125
+    public String getPath() {
126
+        return pathField.getText();
127
+    }
128
+
129
+    /**
130
+     * Informs all action listeners that an action has occured.
131
+     */
132
+    protected void fireActionEvent() {
133
+        for (ActionListener listener : listeners.get(ActionListener.class)) {
134
+            listener.actionPerformed(new ActionEvent(this, 1, getPath()));
135
+        }
136
+    }
137
+}

Chargement…
Annuler
Enregistrer