|
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.io.*; |
|
45 |
| |
|
46 |
| import edu.rice.cs.util.FileOps; |
|
47 |
| |
|
48 |
| |
|
49 |
| public class FileSelectorStringComponent extends JPanel { |
|
50 |
| |
|
51 |
| |
|
52 |
| public static final int DEFAULT_NUM_COLS = 30; |
|
53 |
| |
|
54 |
| |
|
55 |
| public static final float DEFAULT_FONT_SIZE = 10f; |
|
56 |
| |
|
57 |
| |
|
58 |
| protected final Component _parent; |
|
59 |
| |
|
60 |
| |
|
61 |
| protected final JTextField _textField; |
|
62 |
| |
|
63 |
| |
|
64 |
| protected final JButton _chooserButton; |
|
65 |
| |
|
66 |
| |
|
67 |
| protected final FileChooser _chooser; |
|
68 |
| |
|
69 |
| |
|
70 |
| protected volatile File _file; |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
0
| public FileSelectorStringComponent(Component parent, FileChooser chooser) {
|
|
77 |
0
| this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
|
|
78 |
| } |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
38
| public FileSelectorStringComponent(Component parent, FileChooser chooser, int numCols, float fontSize) {
|
|
87 |
38
| _parent = parent;
|
|
88 |
38
| _chooser = chooser;
|
|
89 |
38
| _file = FileOps.NULL_FILE;
|
|
90 |
| |
|
91 |
38
| _textField = new JTextField(numCols) {
|
|
92 |
76
| public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); }
|
|
93 |
| }; |
|
94 |
38
| _textField.setFont(_textField.getFont().deriveFont(fontSize));
|
|
95 |
38
| _textField.setPreferredSize(new Dimension(22,22));
|
|
96 |
| |
|
97 |
38
| _chooserButton = new JButton("...");
|
|
98 |
38
| _chooserButton.addActionListener(new ActionListener() {
|
|
99 |
0
| public void actionPerformed(ActionEvent e) { _chooseFile(); }
|
|
100 |
| }); |
|
101 |
38
| _chooserButton.setMaximumSize(new Dimension(22, 22));
|
|
102 |
38
| _chooserButton.setMargin(new Insets(0,5,0,5));
|
|
103 |
| |
|
104 |
38
| this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
|
|
105 |
38
| this.add(_textField);
|
|
106 |
38
| this.add(_chooserButton);
|
|
107 |
| } |
|
108 |
| |
|
109 |
76
| public void setEnabled(boolean isEnabled) {
|
|
110 |
76
| _textField.setEnabled(isEnabled);
|
|
111 |
76
| _chooserButton.setEnabled(isEnabled);
|
|
112 |
76
| super.setEnabled(isEnabled);
|
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
38
| public JTextField getTextField() { return _textField; }
|
|
117 |
| |
|
118 |
| |
|
119 |
0
| public FileChooser getFileChooser() { return _chooser; }
|
|
120 |
| |
|
121 |
| |
|
122 |
0
| public File convertStringToFile(String s) {
|
|
123 |
0
| s = s.trim();
|
|
124 |
0
| if (s.equals("")) return null;
|
|
125 |
0
| return new File(s);
|
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
0
| public String convertFileToString(File f) {
|
|
130 |
0
| if (f == null) return "";
|
|
131 |
0
| return f.toString();
|
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
0
| public File getFileFromField() {
|
|
136 |
| |
|
137 |
0
| String newValue = _textField.getText();
|
|
138 |
0
| File newFile = FileOps.NULL_FILE;
|
|
139 |
0
| if (! newValue.equals("")) {
|
|
140 |
0
| newFile = convertStringToFile(newValue);
|
|
141 |
0
| if (! newFile.isDirectory() && ! _chooser.isFileSelectionEnabled()) newFile = newFile.getParentFile();
|
|
142 |
| } |
|
143 |
| |
|
144 |
0
| if (newFile != null && ! newFile.exists()) newFile = _file;
|
|
145 |
| |
|
146 |
0
| return newFile;
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
0
| public String getText() { return _textField.getText(); }
|
|
151 |
| |
|
152 |
| |
|
153 |
0
| public void setText(String s) { _textField.setText(s); }
|
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
0
| public void setFileField(File file) {
|
|
159 |
0
| _file = file;
|
|
160 |
0
| if (file != null && ! file.getPath().equals("")) {
|
|
161 |
0
| try { _file = file.getCanonicalFile(); }
|
|
162 |
| catch(IOException e) { } |
|
163 |
| } |
|
164 |
0
| resetFileField();
|
|
165 |
| } |
|
166 |
| |
|
167 |
0
| public void resetFileField() {
|
|
168 |
0
| _textField.setText(convertFileToString(_file));
|
|
169 |
0
| _textField.setCaretPosition(_textField.getText().length());
|
|
170 |
| } |
|
171 |
| |
|
172 |
0
| public void setToolTipText(String text) {
|
|
173 |
0
| super.setToolTipText(text);
|
|
174 |
0
| _textField.setToolTipText(text);
|
|
175 |
0
| _chooserButton.setToolTipText(text);
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
0
| public void addChoosableFileFilter(FileFilter filter) {
|
|
180 |
0
| _chooser.addChoosableFileFilter(filter);
|
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
0
| public void removeChoosableFileFilter(FileFilter filter) {
|
|
185 |
0
| _chooser.removeChoosableFileFilter(filter);
|
|
186 |
| } |
|
187 |
| |
|
188 |
0
| public void clearChoosableFileFilters() {
|
|
189 |
0
| _chooser.resetChoosableFileFilters();
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
0
| protected void _chooseFile() {
|
|
194 |
0
| File f = getFileFromField();
|
|
195 |
0
| if (f != null && f.exists()) {
|
|
196 |
0
| _chooser.setCurrentDirectory(f);
|
|
197 |
0
| _chooser.setSelectedFile(f);
|
|
198 |
| } |
|
199 |
0
| int returnValue = _chooser.showDialog(_parent, null);
|
|
200 |
0
| if (returnValue == FileChooser.APPROVE_OPTION) {
|
|
201 |
0
| File chosen = _chooser.getSelectedFile();
|
|
202 |
0
| if (chosen != null) { setFileField(chosen); }
|
|
203 |
| } |
|
204 |
| } |
|
205 |
| |
|
206 |
| } |