|
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 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| public class DirectorySelectorComponent extends JPanel { |
|
55 |
| |
|
56 |
| |
|
57 |
| public static final int DEFAULT_NUM_COLS = 30; |
|
58 |
| |
|
59 |
| |
|
60 |
| public static final float DEFAULT_FONT_SIZE = 10f; |
|
61 |
| |
|
62 |
| |
|
63 |
| protected final Component _parent; |
|
64 |
| |
|
65 |
| |
|
66 |
| protected final JTextField _fileField; |
|
67 |
| |
|
68 |
| |
|
69 |
| protected final JButton _chooserButton; |
|
70 |
| |
|
71 |
| |
|
72 |
| protected final DirectoryChooser _chooser; |
|
73 |
| |
|
74 |
| |
|
75 |
| protected File _file; |
|
76 |
| |
|
77 |
| |
|
78 |
| protected boolean _mustExist; |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
0
| public DirectorySelectorComponent(Component parent, DirectoryChooser chooser) {
|
|
85 |
0
| this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
|
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
76
| public DirectorySelectorComponent(Component parent, DirectoryChooser chooser, int numCols, float fontSize) {
|
|
95 |
76
| this(parent, chooser, numCols, fontSize, true);
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
114
| public DirectorySelectorComponent(Component parent, DirectoryChooser chooser, int numCols, float fontSize,
|
|
106 |
| boolean mustExist) { |
|
107 |
| |
|
108 |
114
| _parent = parent;
|
|
109 |
114
| _chooser = chooser;
|
|
110 |
114
| _file = FileOps.NULL_FILE;
|
|
111 |
114
| _mustExist = mustExist;
|
|
112 |
| |
|
113 |
114
| _fileField = new JTextField(numCols) {
|
|
114 |
76
| public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); }
|
|
115 |
| }; |
|
116 |
| |
|
117 |
114
| _fileField.setFont(_fileField.getFont().deriveFont(fontSize));
|
|
118 |
114
| _fileField.setPreferredSize(new Dimension(22,22));
|
|
119 |
114
| _fileField.addActionListener(new ActionListener() {
|
|
120 |
0
| public void actionPerformed(ActionEvent e) { validateTextField(); }
|
|
121 |
| }); |
|
122 |
| |
|
123 |
114
| _fileField.addFocusListener(new FocusListener() {
|
|
124 |
0
| public void focusGained(FocusEvent e) { }
|
|
125 |
0
| public void focusLost(FocusEvent e) { validateTextField(); }
|
|
126 |
| }); |
|
127 |
| |
|
128 |
114
| _chooserButton = new JButton("...");
|
|
129 |
114
| _chooserButton.addActionListener(new ActionListener() {
|
|
130 |
0
| public void actionPerformed(ActionEvent e) { _chooseFile(); }
|
|
131 |
| }); |
|
132 |
| |
|
133 |
114
| _chooserButton.setMaximumSize(new Dimension(22, 22));
|
|
134 |
114
| _chooserButton.setMargin(new Insets(0,5,0,5));
|
|
135 |
| |
|
136 |
| |
|
137 |
114
| this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
|
|
138 |
114
| this.add(_fileField);
|
|
139 |
114
| this.add(_chooserButton);
|
|
140 |
| } |
|
141 |
| |
|
142 |
76
| public void setEnabled(boolean isEnabled) {
|
|
143 |
76
| _fileField.setEnabled(isEnabled);
|
|
144 |
76
| _chooserButton.setEnabled(isEnabled);
|
|
145 |
76
| super.setEnabled(isEnabled);
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
76
| public JTextField getFileField() { return _fileField; }
|
|
150 |
| |
|
151 |
| |
|
152 |
0
| public DirectoryChooser getFileChooser() { return _chooser; }
|
|
153 |
| |
|
154 |
| |
|
155 |
38
| public File getFileFromField() {
|
|
156 |
38
| String txt = _fileField.getText().trim();
|
|
157 |
38
| if (txt.equals("")) _file = FileOps.NULL_FILE;
|
|
158 |
0
| else _file = new File(txt);
|
|
159 |
| |
|
160 |
38
| return _file;
|
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
76
| public void setFileField(File file) {
|
|
167 |
76
| _file = file;
|
|
168 |
76
| if (file != null && ! file.getPath().equals("")) {
|
|
169 |
0
| try { _file = file.getCanonicalFile(); }
|
|
170 |
| catch(IOException e) { } |
|
171 |
| } |
|
172 |
76
| resetFileField();
|
|
173 |
| } |
|
174 |
| |
|
175 |
76
| public void resetFileField() {
|
|
176 |
0
| if (_file == null) _fileField.setText("");
|
|
177 |
| else { |
|
178 |
76
| _fileField.setText(_file.toString());
|
|
179 |
76
| _fileField.setCaretPosition(_fileField.getText().length());
|
|
180 |
| } |
|
181 |
| } |
|
182 |
| |
|
183 |
0
| public void setToolTipText(String text) {
|
|
184 |
0
| super.setToolTipText(text);
|
|
185 |
0
| _fileField.setToolTipText(text);
|
|
186 |
0
| _chooserButton.setToolTipText(text);
|
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public void addChoosableFileFilter(FileFilter filter) { _chooser.addChoosableFileFilter(filter); }
|
|
191 |
| |
|
192 |
| |
|
193 |
0
| public void removeChoosableFileFilter(FileFilter filter) { _chooser.removeChoosableFileFilter(filter); }
|
|
194 |
| |
|
195 |
0
| public void clearChoosableFileFilters() { _chooser.resetChoosableFileFilters(); }
|
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| private boolean _validationInProgress = false; |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
0
| public boolean validateTextField() {
|
|
206 |
0
| if (_validationInProgress) return true;
|
|
207 |
0
| _validationInProgress = true;
|
|
208 |
| |
|
209 |
0
| String newValue = _fileField.getText().trim();
|
|
210 |
| |
|
211 |
0
| File newFile = FileOps.NULL_FILE;
|
|
212 |
0
| if (! newValue.equals("")) {
|
|
213 |
0
| newFile = new File(newValue);
|
|
214 |
0
| if (! newFile.isDirectory() && _chooser.isFileSelectionEnabled()) newFile = newFile.getParentFile();
|
|
215 |
| } |
|
216 |
| |
|
217 |
0
| if (newFile != FileOps.NULL_FILE && _mustExist && ! newFile.exists()) {
|
|
218 |
0
| JOptionPane.showMessageDialog(_parent, "The file '" + newValue + "'\nis invalid because it does not exist.",
|
|
219 |
| "Invalid File Name", JOptionPane.ERROR_MESSAGE); |
|
220 |
0
| resetFileField();
|
|
221 |
0
| _validationInProgress = false;
|
|
222 |
0
| return false;
|
|
223 |
| } |
|
224 |
| else { |
|
225 |
0
| setFileField(newFile);
|
|
226 |
0
| _validationInProgress = false;
|
|
227 |
0
| return true;
|
|
228 |
| } |
|
229 |
| } |
|
230 |
| |
|
231 |
| |
|
232 |
0
| protected void _chooseFile() {
|
|
233 |
| |
|
234 |
| |
|
235 |
0
| int returnValue = _chooser.showDialog(_file);
|
|
236 |
0
| if (returnValue == DirectoryChooser.APPROVE_OPTION) {
|
|
237 |
0
| File chosen = _chooser.getSelectedDirectory();
|
|
238 |
0
| if (chosen != null) setFileField(chosen);
|
|
239 |
| } |
|
240 |
| } |
|
241 |
| |
|
242 |
| } |