Explorar el Código

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 hace 14 años
padre
commit
86a9b06e6f

+ 52
- 10
src/com/dmdirc/addons/ui_swing/PrefsComponentFactory.java Ver fichero

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.FileBrowser;
25 26
 import com.dmdirc.config.prefs.PreferencesSetting;
26 27
 import com.dmdirc.config.prefs.validator.NumericalValidator;
27 28
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
@@ -46,6 +47,7 @@ import java.util.Map;
46 47
 import javax.swing.JCheckBox;
47 48
 import javax.swing.JComboBox;
48 49
 import javax.swing.JComponent;
50
+import javax.swing.JFileChooser;
49 51
 import javax.swing.JSpinner;
50 52
 import javax.swing.JTextField;
51 53
 import javax.swing.SpinnerNumberModel;
@@ -105,6 +107,15 @@ public final class PrefsComponentFactory {
105 107
             case FONT:
106 108
                 option = getFontOption(setting);
107 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 119
             default:
109 120
                 throw new IllegalArgumentException(setting.getType()
110 121
                         + " is not a valid option type");
@@ -146,7 +157,7 @@ public final class PrefsComponentFactory {
146 157
         final JCheckBox option = new JCheckBox();
147 158
         option.setSelected(Boolean.parseBoolean(setting.getValue()));
148 159
         option.addChangeListener(new ChangeListener() {
149
-            
160
+
150 161
             /** {@inheritDoc} */
151 162
             @Override
152 163
             public void stateChanged(final ChangeEvent e) {
@@ -176,7 +187,7 @@ public final class PrefsComponentFactory {
176 187
         }
177 188
 
178 189
         option.addActionListener(new ActionListener() {
179
-            
190
+
180 191
             /** {@inheritDoc} */
181 192
             @Override
182 193
             public void actionPerformed(final ActionEvent e) {
@@ -238,7 +249,7 @@ public final class PrefsComponentFactory {
238 249
                 && !setting.getValue().startsWith("false:");
239 250
         final String integer = setting.getValue() == null ? "0" : setting.getValue().
240 251
                 substring(1 + setting.getValue().indexOf(':'));
241
-        
252
+
242 253
         OptionalJSpinner option;
243 254
         Validator optionalValidator = setting.getValidator();
244 255
         Validator numericalValidator = null;
@@ -296,7 +307,7 @@ public final class PrefsComponentFactory {
296 307
         }
297 308
 
298 309
         option.addDurationListener(new DurationListener() {
299
-            
310
+
300 311
             /** {@inheritDoc} */
301 312
             @Override
302 313
             public void durationUpdated(final int newDuration) {
@@ -317,7 +328,7 @@ public final class PrefsComponentFactory {
317 328
         final ColourChooser option = new ColourChooser(setting.getValue(), true, true);
318 329
 
319 330
         option.addActionListener(new ActionListener() {
320
-            
331
+
321 332
             /** {@inheritDoc} */
322 333
             @Override
323 334
             public void actionPerformed(final ActionEvent e) {
@@ -343,7 +354,7 @@ public final class PrefsComponentFactory {
343 354
         final OptionalColourChooser option = new OptionalColourChooser(colour, state, true, true);
344 355
 
345 356
         option.addActionListener(new ActionListener() {
346
-            
357
+
347 358
             /** {@inheritDoc} */
348 359
             @Override
349 360
             public void actionPerformed(final ActionEvent e) {
@@ -355,7 +366,7 @@ public final class PrefsComponentFactory {
355 366
 
356 367
         return option;
357 368
     }
358
-    
369
+
359 370
     /**
360 371
      * Initialises and returns an Font Chooser for the specified setting.
361 372
      *
@@ -366,7 +377,7 @@ public final class PrefsComponentFactory {
366 377
         final String value = setting.getValue();
367 378
 
368 379
         final FontPicker option = new FontPicker(value);
369
-        
380
+
370 381
         option.addActionListener(new ActionListener() {
371 382
 
372 383
             /** {@inheritDoc} */
@@ -379,8 +390,39 @@ public final class PrefsComponentFactory {
379 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 426
         return option;
385 427
     }
386 428
 

+ 137
- 0
src/com/dmdirc/addons/ui_swing/components/FileBrowser.java Ver fichero

@@ -0,0 +1,137 @@
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
+}

Loading…
Cancelar
Guardar