1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| package edu.rice.cs.util.swing; |
38 |
| |
39 |
| import javax.swing.*; |
40 |
| import javax.swing.filechooser.FileFilter; |
41 |
| import java.awt.*; |
42 |
| import java.awt.event.ActionEvent; |
43 |
| import java.awt.event.ActionListener; |
44 |
| import java.awt.event.FocusListener; |
45 |
| import java.awt.event.FocusEvent; |
46 |
| import java.io.*; |
47 |
| |
48 |
| import edu.rice.cs.util.FileOps; |
49 |
| import edu.rice.cs.util.UnexpectedException; |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| public class FileSelectorComponent extends JPanel { |
57 |
| |
58 |
| |
59 |
| public static final int DEFAULT_NUM_COLS = 30; |
60 |
| |
61 |
| |
62 |
| public static final float DEFAULT_FONT_SIZE = 10f; |
63 |
| |
64 |
| |
65 |
| protected final SwingFrame _parent; |
66 |
| |
67 |
| |
68 |
| protected final JTextField _fileField; |
69 |
| |
70 |
| |
71 |
| protected final JButton _chooserButton; |
72 |
| |
73 |
| |
74 |
| protected final JFileChooser _chooser; |
75 |
| |
76 |
| |
77 |
| protected volatile FileFilter _fileFilter; |
78 |
| |
79 |
| |
80 |
| protected volatile File _file; |
81 |
| |
82 |
| |
83 |
| protected volatile boolean _mustExist; |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
0
| public FileSelectorComponent(SwingFrame parent, JFileChooser chooser) {
|
90 |
0
| this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE, true);
|
91 |
| } |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
155
| public FileSelectorComponent(SwingFrame parent, JFileChooser chooser, int numCols, float fontSize) {
|
100 |
155
| this(parent, chooser, numCols, fontSize, true);
|
101 |
| } |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
193
| public FileSelectorComponent(SwingFrame parent, JFileChooser chooser, int numCols, float fontSize, boolean mustExist) {
|
111 |
| |
112 |
0
| if (chooser == null) throw new UnexpectedException("Error: called new FileSelectorComponent(...) with a null chooser!");
|
113 |
| |
114 |
193
| _parent = parent;
|
115 |
193
| _chooser = chooser;
|
116 |
193
| _fileFilter = null;
|
117 |
193
| _mustExist = mustExist;
|
118 |
| |
119 |
193
| _fileField = new JTextField(numCols) {
|
120 |
76
| public Dimension getMaximumSize() {
|
121 |
76
| return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height);
|
122 |
| } |
123 |
| }; |
124 |
| |
125 |
193
| _fileField.setFont(_fileField.getFont().deriveFont(fontSize));
|
126 |
193
| _fileField.setPreferredSize(new Dimension(22, 22));
|
127 |
193
| _fileField.addActionListener(new ActionListener() {
|
128 |
0
| public void actionPerformed(ActionEvent e) { validateTextField(); }
|
129 |
| }); |
130 |
| |
131 |
193
| _fileField.addFocusListener(new FocusListener() {
|
132 |
0
| public void focusGained(FocusEvent e) { }
|
133 |
0
| public void focusLost(FocusEvent e) { validateTextField(); }
|
134 |
| }); |
135 |
| |
136 |
193
| _chooserButton = new JButton("...");
|
137 |
193
| _chooserButton.addActionListener(new ActionListener() {
|
138 |
0
| public void actionPerformed(ActionEvent e) { _chooseFile(); }
|
139 |
| }); |
140 |
| |
141 |
193
| _chooserButton.setMaximumSize(new Dimension(22, 22));
|
142 |
193
| _chooserButton.setMargin(new Insets(0, 5 ,0, 5));
|
143 |
| |
144 |
| |
145 |
193
| this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
|
146 |
193
| this.add(_fileField);
|
147 |
193
| this.add(_chooserButton);
|
148 |
| } |
149 |
| |
150 |
231
| public void setEnabled(boolean isEnabled) {
|
151 |
231
| _fileField.setEnabled(isEnabled);
|
152 |
231
| _chooserButton.setEnabled(isEnabled);
|
153 |
231
| super.setEnabled(isEnabled);
|
154 |
| } |
155 |
| |
156 |
| |
157 |
155
| public JTextField getFileField() { return _fileField; }
|
158 |
| |
159 |
| |
160 |
0
| public JFileChooser getFileChooser() { return _chooser; }
|
161 |
| |
162 |
| |
163 |
80
| public File getFileFromField() {
|
164 |
80
| String txt = _fileField.getText().trim();
|
165 |
80
| if (txt.equals("")) _file = FileOps.NULL_FILE;
|
166 |
0
| else _file = new File(txt);
|
167 |
| |
168 |
80
| return _file;
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
5
| public void setFileField(File file) {
|
175 |
5
| _file = file;
|
176 |
5
| if (file != null && !file.getPath().equals("")) {
|
177 |
0
| try {_file = file.getCanonicalFile(); }
|
178 |
| catch(IOException e) { } |
179 |
| } |
180 |
5
| resetFileField();
|
181 |
| } |
182 |
| |
183 |
5
| public void resetFileField() {
|
184 |
0
| if (_file == null) _fileField.setText("");
|
185 |
| else { |
186 |
5
| _fileField.setText(_file.getPath());
|
187 |
5
| _fileField.setCaretPosition(_fileField.getText().length());
|
188 |
| } |
189 |
| } |
190 |
| |
191 |
| |
192 |
152
| public void setFileFilter(FileFilter filter) { _fileFilter = filter; }
|
193 |
| |
194 |
152
| public void setToolTipText(String text) {
|
195 |
152
| super.setToolTipText(text);
|
196 |
152
| _fileField.setToolTipText(text);
|
197 |
152
| _chooser.setToolTipText(text);
|
198 |
| } |
199 |
| |
200 |
| |
201 |
0
| protected void _chooseFile() {
|
202 |
| |
203 |
0
| if (_file != null && _file.exists()) {
|
204 |
0
| _chooser.setCurrentDirectory(_file);
|
205 |
0
| _chooser.setSelectedFile(_file);
|
206 |
| } |
207 |
| |
208 |
| |
209 |
0
| if (_fileFilter != null) _chooser.setFileFilter(_fileFilter);
|
210 |
| |
211 |
| |
212 |
0
| int returnValue = _chooser.showDialog(_parent, null);
|
213 |
0
| if (returnValue == JFileChooser.APPROVE_OPTION) {
|
214 |
0
| File chosen = _chooser.getSelectedFile();
|
215 |
0
| if (chosen != null) setFileField(chosen);
|
216 |
| } |
217 |
| } |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
0
| public boolean validateTextField() {
|
228 |
| |
229 |
| |
230 |
| |
231 |
0
| String newValue = _fileField.getText().trim();
|
232 |
| |
233 |
0
| File newFile = FileOps.NULL_FILE;
|
234 |
0
| if (! newValue.equals(""))
|
235 |
0
| newFile = new File(newValue);
|
236 |
| |
237 |
0
| if (newFile != FileOps.NULL_FILE && _mustExist && !newFile.exists()) {
|
238 |
0
| JOptionPane.showMessageDialog(_parent, "The file '" + newValue + "'\nis invalid because it does not exist.",
|
239 |
| "Invalid File Name", JOptionPane.ERROR_MESSAGE); |
240 |
0
| if (_file != null && ! _file.exists()) _file = FileOps.NULL_FILE;
|
241 |
0
| resetFileField();
|
242 |
| |
243 |
| |
244 |
0
| return false;
|
245 |
| } |
246 |
| else { |
247 |
0
| setFileField(newFile);
|
248 |
| |
249 |
0
| return true;
|
250 |
| } |
251 |
| } |
252 |
| } |