|
@@ -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
|
+}
|