author | rupashka |
Wed, 27 Aug 2008 20:49:35 +0400 | |
changeset 1302 | ac8b63707542 |
parent 1299 | 027d966d5658 |
child 1840 | 984ba55ac827 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing.plaf.basic; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.filechooser.*; |
|
30 |
import javax.swing.filechooser.FileFilter; |
|
31 |
import javax.swing.event.*; |
|
32 |
import javax.swing.plaf.*; |
|
33 |
import java.awt.*; |
|
34 |
import java.awt.event.*; |
|
35 |
import java.awt.datatransfer.*; |
|
36 |
import java.beans.*; |
|
37 |
import java.io.*; |
|
38 |
import java.util.*; |
|
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
39 |
import java.util.List; |
2 | 40 |
import java.util.regex.*; |
41 |
import sun.awt.shell.ShellFolder; |
|
42 |
import sun.swing.*; |
|
43 |
import sun.swing.SwingUtilities2; |
|
44 |
||
45 |
/** |
|
46 |
* Basic L&F implementation of a FileChooser. |
|
47 |
* |
|
48 |
* @author Jeff Dinkins |
|
49 |
*/ |
|
50 |
public class BasicFileChooserUI extends FileChooserUI { |
|
51 |
||
52 |
/* FileView icons */ |
|
53 |
protected Icon directoryIcon = null; |
|
54 |
protected Icon fileIcon = null; |
|
55 |
protected Icon computerIcon = null; |
|
56 |
protected Icon hardDriveIcon = null; |
|
57 |
protected Icon floppyDriveIcon = null; |
|
58 |
||
59 |
protected Icon newFolderIcon = null; |
|
60 |
protected Icon upFolderIcon = null; |
|
61 |
protected Icon homeFolderIcon = null; |
|
62 |
protected Icon listViewIcon = null; |
|
63 |
protected Icon detailsViewIcon = null; |
|
64 |
protected Icon viewMenuIcon = null; |
|
65 |
||
66 |
protected int saveButtonMnemonic = 0; |
|
67 |
protected int openButtonMnemonic = 0; |
|
68 |
protected int cancelButtonMnemonic = 0; |
|
69 |
protected int updateButtonMnemonic = 0; |
|
70 |
protected int helpButtonMnemonic = 0; |
|
71 |
||
72 |
/** |
|
73 |
* The mnemonic keycode used for the approve button when a directory |
|
74 |
* is selected and the current selection mode is FILES_ONLY. |
|
75 |
* |
|
76 |
* @since 1.4 |
|
77 |
*/ |
|
78 |
protected int directoryOpenButtonMnemonic = 0; |
|
79 |
||
80 |
protected String saveButtonText = null; |
|
81 |
protected String openButtonText = null; |
|
82 |
protected String cancelButtonText = null; |
|
83 |
protected String updateButtonText = null; |
|
84 |
protected String helpButtonText = null; |
|
85 |
||
86 |
/** |
|
87 |
* The label text displayed on the approve button when a directory |
|
88 |
* is selected and the current selection mode is FILES_ONLY. |
|
89 |
* |
|
90 |
* @since 1.4 |
|
91 |
*/ |
|
92 |
protected String directoryOpenButtonText = null; |
|
93 |
||
94 |
private String openDialogTitleText = null; |
|
95 |
private String saveDialogTitleText = null; |
|
96 |
||
97 |
protected String saveButtonToolTipText = null; |
|
98 |
protected String openButtonToolTipText = null; |
|
99 |
protected String cancelButtonToolTipText = null; |
|
100 |
protected String updateButtonToolTipText = null; |
|
101 |
protected String helpButtonToolTipText = null; |
|
102 |
||
103 |
/** |
|
104 |
* The tooltip text displayed on the approve button when a directory |
|
105 |
* is selected and the current selection mode is FILES_ONLY. |
|
106 |
* |
|
107 |
* @since 1.4 |
|
108 |
*/ |
|
109 |
protected String directoryOpenButtonToolTipText = null; |
|
110 |
||
111 |
// Some generic FileChooser functions |
|
112 |
private Action approveSelectionAction = new ApproveSelectionAction(); |
|
113 |
private Action cancelSelectionAction = new CancelSelectionAction(); |
|
114 |
private Action updateAction = new UpdateAction(); |
|
115 |
private Action newFolderAction; |
|
116 |
private Action goHomeAction = new GoHomeAction(); |
|
117 |
private Action changeToParentDirectoryAction = new ChangeToParentDirectoryAction(); |
|
118 |
||
119 |
private String newFolderErrorSeparator = null; |
|
120 |
private String newFolderErrorText = null; |
|
121 |
private String newFolderParentDoesntExistTitleText = null; |
|
122 |
private String newFolderParentDoesntExistText = null; |
|
123 |
private String fileDescriptionText = null; |
|
124 |
private String directoryDescriptionText = null; |
|
125 |
||
126 |
private JFileChooser filechooser = null; |
|
127 |
||
128 |
private boolean directorySelected = false; |
|
129 |
private File directory = null; |
|
130 |
||
131 |
private PropertyChangeListener propertyChangeListener = null; |
|
132 |
private AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter(); |
|
133 |
private FileFilter actualFileFilter = null; |
|
134 |
private GlobFilter globFilter = null; |
|
135 |
private BasicDirectoryModel model = null; |
|
136 |
private BasicFileView fileView = new BasicFileView(); |
|
137 |
private boolean usesSingleFilePane; |
|
138 |
private boolean readOnly; |
|
139 |
||
140 |
// The accessoryPanel is a container to place the JFileChooser accessory component |
|
141 |
private JPanel accessoryPanel = null; |
|
142 |
private Handler handler; |
|
143 |
||
144 |
||
145 |
public BasicFileChooserUI(JFileChooser b) { |
|
146 |
} |
|
147 |
||
148 |
public void installUI(JComponent c) { |
|
149 |
accessoryPanel = new JPanel(new BorderLayout()); |
|
150 |
filechooser = (JFileChooser) c; |
|
151 |
||
152 |
createModel(); |
|
153 |
||
154 |
clearIconCache(); |
|
155 |
||
156 |
installDefaults(filechooser); |
|
157 |
installComponents(filechooser); |
|
158 |
installListeners(filechooser); |
|
159 |
filechooser.applyComponentOrientation(filechooser.getComponentOrientation()); |
|
160 |
} |
|
161 |
||
162 |
public void uninstallUI(JComponent c) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
163 |
uninstallListeners(filechooser); |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
164 |
uninstallComponents(filechooser); |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
165 |
uninstallDefaults(filechooser); |
2 | 166 |
|
167 |
if(accessoryPanel != null) { |
|
168 |
accessoryPanel.removeAll(); |
|
169 |
} |
|
170 |
||
171 |
accessoryPanel = null; |
|
172 |
getFileChooser().removeAll(); |
|
173 |
||
174 |
handler = null; |
|
175 |
} |
|
176 |
||
177 |
public void installComponents(JFileChooser fc) { |
|
178 |
} |
|
179 |
||
180 |
public void uninstallComponents(JFileChooser fc) { |
|
181 |
} |
|
182 |
||
183 |
protected void installListeners(JFileChooser fc) { |
|
184 |
propertyChangeListener = createPropertyChangeListener(fc); |
|
185 |
if(propertyChangeListener != null) { |
|
186 |
fc.addPropertyChangeListener(propertyChangeListener); |
|
187 |
} |
|
188 |
fc.addPropertyChangeListener(getModel()); |
|
189 |
||
190 |
InputMap inputMap = getInputMap(JComponent. |
|
191 |
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
|
192 |
SwingUtilities.replaceUIInputMap(fc, JComponent. |
|
193 |
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); |
|
194 |
ActionMap actionMap = getActionMap(); |
|
195 |
SwingUtilities.replaceUIActionMap(fc, actionMap); |
|
196 |
} |
|
197 |
||
198 |
InputMap getInputMap(int condition) { |
|
199 |
if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { |
|
200 |
return (InputMap)DefaultLookup.get(getFileChooser(), this, |
|
201 |
"FileChooser.ancestorInputMap"); |
|
202 |
} |
|
203 |
return null; |
|
204 |
} |
|
205 |
||
206 |
ActionMap getActionMap() { |
|
207 |
return createActionMap(); |
|
208 |
} |
|
209 |
||
210 |
ActionMap createActionMap() { |
|
211 |
ActionMap map = new ActionMapUIResource(); |
|
212 |
||
213 |
Action refreshAction = new UIAction(FilePane.ACTION_REFRESH) { |
|
214 |
public void actionPerformed(ActionEvent evt) { |
|
215 |
getFileChooser().rescanCurrentDirectory(); |
|
216 |
} |
|
217 |
}; |
|
218 |
||
219 |
map.put(FilePane.ACTION_APPROVE_SELECTION, getApproveSelectionAction()); |
|
220 |
map.put(FilePane.ACTION_CANCEL, getCancelSelectionAction()); |
|
221 |
map.put(FilePane.ACTION_REFRESH, refreshAction); |
|
222 |
map.put(FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY, |
|
223 |
getChangeToParentDirectoryAction()); |
|
224 |
return map; |
|
225 |
} |
|
226 |
||
227 |
||
228 |
protected void uninstallListeners(JFileChooser fc) { |
|
229 |
if(propertyChangeListener != null) { |
|
230 |
fc.removePropertyChangeListener(propertyChangeListener); |
|
231 |
} |
|
232 |
fc.removePropertyChangeListener(getModel()); |
|
233 |
SwingUtilities.replaceUIInputMap(fc, JComponent. |
|
234 |
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); |
|
235 |
SwingUtilities.replaceUIActionMap(fc, null); |
|
236 |
} |
|
237 |
||
238 |
||
239 |
protected void installDefaults(JFileChooser fc) { |
|
240 |
installIcons(fc); |
|
241 |
installStrings(fc); |
|
242 |
usesSingleFilePane = UIManager.getBoolean("FileChooser.usesSingleFilePane"); |
|
243 |
readOnly = UIManager.getBoolean("FileChooser.readOnly"); |
|
244 |
TransferHandler th = fc.getTransferHandler(); |
|
245 |
if (th == null || th instanceof UIResource) { |
|
246 |
fc.setTransferHandler(defaultTransferHandler); |
|
247 |
} |
|
248 |
LookAndFeel.installProperty(fc, "opaque", Boolean.FALSE); |
|
249 |
} |
|
250 |
||
251 |
protected void installIcons(JFileChooser fc) { |
|
252 |
directoryIcon = UIManager.getIcon("FileView.directoryIcon"); |
|
253 |
fileIcon = UIManager.getIcon("FileView.fileIcon"); |
|
254 |
computerIcon = UIManager.getIcon("FileView.computerIcon"); |
|
255 |
hardDriveIcon = UIManager.getIcon("FileView.hardDriveIcon"); |
|
256 |
floppyDriveIcon = UIManager.getIcon("FileView.floppyDriveIcon"); |
|
257 |
||
258 |
newFolderIcon = UIManager.getIcon("FileChooser.newFolderIcon"); |
|
259 |
upFolderIcon = UIManager.getIcon("FileChooser.upFolderIcon"); |
|
260 |
homeFolderIcon = UIManager.getIcon("FileChooser.homeFolderIcon"); |
|
261 |
detailsViewIcon = UIManager.getIcon("FileChooser.detailsViewIcon"); |
|
262 |
listViewIcon = UIManager.getIcon("FileChooser.listViewIcon"); |
|
263 |
viewMenuIcon = UIManager.getIcon("FileChooser.viewMenuIcon"); |
|
264 |
} |
|
265 |
||
266 |
protected void installStrings(JFileChooser fc) { |
|
267 |
||
268 |
Locale l = fc.getLocale(); |
|
269 |
newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l); |
|
270 |
newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l); |
|
271 |
||
272 |
newFolderParentDoesntExistTitleText = UIManager.getString("FileChooser.newFolderParentDoesntExistTitleText", l); |
|
273 |
newFolderParentDoesntExistText = UIManager.getString("FileChooser.newFolderParentDoesntExistText", l); |
|
274 |
||
275 |
fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText",l); |
|
276 |
directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText",l); |
|
277 |
||
278 |
saveButtonText = UIManager.getString("FileChooser.saveButtonText",l); |
|
279 |
openButtonText = UIManager.getString("FileChooser.openButtonText",l); |
|
280 |
saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText",l); |
|
281 |
openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText",l); |
|
282 |
cancelButtonText = UIManager.getString("FileChooser.cancelButtonText",l); |
|
283 |
updateButtonText = UIManager.getString("FileChooser.updateButtonText",l); |
|
284 |
helpButtonText = UIManager.getString("FileChooser.helpButtonText",l); |
|
285 |
directoryOpenButtonText = UIManager.getString("FileChooser.directoryOpenButtonText",l); |
|
286 |
||
287 |
saveButtonMnemonic = getMnemonic("FileChooser.saveButtonMnemonic", l); |
|
288 |
openButtonMnemonic = getMnemonic("FileChooser.openButtonMnemonic", l); |
|
289 |
cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic", l); |
|
290 |
updateButtonMnemonic = getMnemonic("FileChooser.updateButtonMnemonic", l); |
|
291 |
helpButtonMnemonic = getMnemonic("FileChooser.helpButtonMnemonic", l); |
|
292 |
directoryOpenButtonMnemonic = getMnemonic("FileChooser.directoryOpenButtonMnemonic", l); |
|
293 |
||
294 |
saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText",l); |
|
295 |
openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText",l); |
|
296 |
cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText",l); |
|
297 |
updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText",l); |
|
298 |
helpButtonToolTipText = UIManager.getString("FileChooser.helpButtonToolTipText",l); |
|
299 |
directoryOpenButtonToolTipText = UIManager.getString("FileChooser.directoryOpenButtonToolTipText",l); |
|
300 |
} |
|
301 |
||
302 |
protected void uninstallDefaults(JFileChooser fc) { |
|
303 |
uninstallIcons(fc); |
|
304 |
uninstallStrings(fc); |
|
305 |
if (fc.getTransferHandler() instanceof UIResource) { |
|
306 |
fc.setTransferHandler(null); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
protected void uninstallIcons(JFileChooser fc) { |
|
311 |
directoryIcon = null; |
|
312 |
fileIcon = null; |
|
313 |
computerIcon = null; |
|
314 |
hardDriveIcon = null; |
|
315 |
floppyDriveIcon = null; |
|
316 |
||
317 |
newFolderIcon = null; |
|
318 |
upFolderIcon = null; |
|
319 |
homeFolderIcon = null; |
|
320 |
detailsViewIcon = null; |
|
321 |
listViewIcon = null; |
|
322 |
viewMenuIcon = null; |
|
323 |
} |
|
324 |
||
325 |
protected void uninstallStrings(JFileChooser fc) { |
|
326 |
saveButtonText = null; |
|
327 |
openButtonText = null; |
|
328 |
cancelButtonText = null; |
|
329 |
updateButtonText = null; |
|
330 |
helpButtonText = null; |
|
331 |
directoryOpenButtonText = null; |
|
332 |
||
333 |
saveButtonToolTipText = null; |
|
334 |
openButtonToolTipText = null; |
|
335 |
cancelButtonToolTipText = null; |
|
336 |
updateButtonToolTipText = null; |
|
337 |
helpButtonToolTipText = null; |
|
338 |
directoryOpenButtonToolTipText = null; |
|
339 |
} |
|
340 |
||
341 |
protected void createModel() { |
|
342 |
if (model != null) { |
|
343 |
model.invalidateFileCache(); |
|
344 |
} |
|
345 |
model = new BasicDirectoryModel(getFileChooser()); |
|
346 |
} |
|
347 |
||
348 |
public BasicDirectoryModel getModel() { |
|
349 |
return model; |
|
350 |
} |
|
351 |
||
352 |
public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) { |
|
353 |
return null; |
|
354 |
} |
|
355 |
||
356 |
public String getFileName() { |
|
357 |
return null; |
|
358 |
} |
|
359 |
||
360 |
public String getDirectoryName() { |
|
361 |
return null; |
|
362 |
} |
|
363 |
||
364 |
public void setFileName(String filename) { |
|
365 |
} |
|
366 |
||
367 |
public void setDirectoryName(String dirname) { |
|
368 |
} |
|
369 |
||
370 |
public void rescanCurrentDirectory(JFileChooser fc) { |
|
371 |
} |
|
372 |
||
373 |
public void ensureFileIsVisible(JFileChooser fc, File f) { |
|
374 |
} |
|
375 |
||
376 |
public JFileChooser getFileChooser() { |
|
377 |
return filechooser; |
|
378 |
} |
|
379 |
||
380 |
public JPanel getAccessoryPanel() { |
|
381 |
return accessoryPanel; |
|
382 |
} |
|
383 |
||
384 |
protected JButton getApproveButton(JFileChooser fc) { |
|
385 |
return null; |
|
386 |
} |
|
387 |
||
680
eaff686e34f7
5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents:
2
diff
changeset
|
388 |
public JButton getDefaultButton(JFileChooser fc) { |
eaff686e34f7
5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents:
2
diff
changeset
|
389 |
return getApproveButton(fc); |
eaff686e34f7
5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents:
2
diff
changeset
|
390 |
} |
eaff686e34f7
5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents:
2
diff
changeset
|
391 |
|
2 | 392 |
public String getApproveButtonToolTipText(JFileChooser fc) { |
393 |
String tooltipText = fc.getApproveButtonToolTipText(); |
|
394 |
if(tooltipText != null) { |
|
395 |
return tooltipText; |
|
396 |
} |
|
397 |
||
398 |
if(fc.getDialogType() == JFileChooser.OPEN_DIALOG) { |
|
399 |
return openButtonToolTipText; |
|
400 |
} else if(fc.getDialogType() == JFileChooser.SAVE_DIALOG) { |
|
401 |
return saveButtonToolTipText; |
|
402 |
} |
|
403 |
return null; |
|
404 |
} |
|
405 |
||
406 |
public void clearIconCache() { |
|
407 |
fileView.clearIconCache(); |
|
408 |
} |
|
409 |
||
410 |
||
411 |
// ******************************************** |
|
412 |
// ************ Create Listeners ************** |
|
413 |
// ******************************************** |
|
414 |
||
415 |
private Handler getHandler() { |
|
416 |
if (handler == null) { |
|
417 |
handler = new Handler(); |
|
418 |
} |
|
419 |
return handler; |
|
420 |
} |
|
421 |
||
422 |
protected MouseListener createDoubleClickListener(JFileChooser fc, |
|
423 |
JList list) { |
|
424 |
return new Handler(list); |
|
425 |
} |
|
426 |
||
427 |
public ListSelectionListener createListSelectionListener(JFileChooser fc) { |
|
428 |
return getHandler(); |
|
429 |
} |
|
430 |
||
431 |
private class Handler implements MouseListener, ListSelectionListener { |
|
432 |
JList list; |
|
433 |
||
434 |
Handler() { |
|
435 |
} |
|
436 |
||
437 |
Handler(JList list) { |
|
438 |
this.list = list; |
|
439 |
} |
|
440 |
||
441 |
public void mouseClicked(MouseEvent evt) { |
|
442 |
// Note: we can't depend on evt.getSource() because of backward |
|
443 |
// compatability |
|
444 |
if (list != null && |
|
445 |
SwingUtilities.isLeftMouseButton(evt) && |
|
446 |
(evt.getClickCount()%2 == 0)) { |
|
447 |
||
448 |
int index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint()); |
|
449 |
if (index >= 0) { |
|
450 |
File f = (File)list.getModel().getElementAt(index); |
|
451 |
try { |
|
452 |
// Strip trailing ".." |
|
453 |
f = ShellFolder.getNormalizedFile(f); |
|
454 |
} catch (IOException ex) { |
|
455 |
// That's ok, we'll use f as is |
|
456 |
} |
|
457 |
if(getFileChooser().isTraversable(f)) { |
|
458 |
list.clearSelection(); |
|
459 |
changeDirectory(f); |
|
460 |
} else { |
|
461 |
getFileChooser().approveSelection(); |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
465 |
} |
|
466 |
||
467 |
public void mouseEntered(MouseEvent evt) { |
|
468 |
if (list != null) { |
|
469 |
TransferHandler th1 = getFileChooser().getTransferHandler(); |
|
470 |
TransferHandler th2 = list.getTransferHandler(); |
|
471 |
if (th1 != th2) { |
|
472 |
list.setTransferHandler(th1); |
|
473 |
} |
|
474 |
if (getFileChooser().getDragEnabled() != list.getDragEnabled()) { |
|
475 |
list.setDragEnabled(getFileChooser().getDragEnabled()); |
|
476 |
} |
|
477 |
} |
|
478 |
} |
|
479 |
||
480 |
public void mouseExited(MouseEvent evt) { |
|
481 |
} |
|
482 |
||
483 |
public void mousePressed(MouseEvent evt) { |
|
484 |
} |
|
485 |
||
486 |
public void mouseReleased(MouseEvent evt) { |
|
487 |
} |
|
488 |
||
489 |
public void valueChanged(ListSelectionEvent evt) { |
|
490 |
if(!evt.getValueIsAdjusting()) { |
|
491 |
JFileChooser chooser = getFileChooser(); |
|
492 |
FileSystemView fsv = chooser.getFileSystemView(); |
|
493 |
JList list = (JList)evt.getSource(); |
|
494 |
||
495 |
int fsm = chooser.getFileSelectionMode(); |
|
496 |
boolean useSetDirectory = usesSingleFilePane && |
|
497 |
(fsm == JFileChooser.FILES_ONLY); |
|
498 |
||
499 |
if (chooser.isMultiSelectionEnabled()) { |
|
500 |
File[] files = null; |
|
501 |
Object[] objects = list.getSelectedValues(); |
|
502 |
if (objects != null) { |
|
503 |
if (objects.length == 1 |
|
504 |
&& ((File)objects[0]).isDirectory() |
|
505 |
&& chooser.isTraversable(((File)objects[0])) |
|
506 |
&& (useSetDirectory || !fsv.isFileSystem(((File)objects[0])))) { |
|
507 |
setDirectorySelected(true); |
|
508 |
setDirectory(((File)objects[0])); |
|
509 |
} else { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
510 |
ArrayList<File> fList = new ArrayList<File>(objects.length); |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
511 |
for (Object object : objects) { |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
512 |
File f = (File) object; |
2 | 513 |
boolean isDir = f.isDirectory(); |
514 |
if ((chooser.isFileSelectionEnabled() && !isDir) |
|
515 |
|| (chooser.isDirectorySelectionEnabled() |
|
516 |
&& fsv.isFileSystem(f) |
|
517 |
&& isDir)) { |
|
518 |
fList.add(f); |
|
519 |
} |
|
520 |
} |
|
521 |
if (fList.size() > 0) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
522 |
files = fList.toArray(new File[fList.size()]); |
2 | 523 |
} |
524 |
setDirectorySelected(false); |
|
525 |
} |
|
526 |
} |
|
527 |
chooser.setSelectedFiles(files); |
|
528 |
} else { |
|
529 |
File file = (File)list.getSelectedValue(); |
|
530 |
if (file != null |
|
531 |
&& file.isDirectory() |
|
532 |
&& chooser.isTraversable(file) |
|
533 |
&& (useSetDirectory || !fsv.isFileSystem(file))) { |
|
534 |
||
535 |
setDirectorySelected(true); |
|
536 |
setDirectory(file); |
|
537 |
if (usesSingleFilePane) { |
|
538 |
chooser.setSelectedFile(null); |
|
539 |
} |
|
540 |
} else { |
|
541 |
setDirectorySelected(false); |
|
542 |
if (file != null) { |
|
543 |
chooser.setSelectedFile(file); |
|
544 |
} |
|
545 |
} |
|
546 |
} |
|
547 |
} |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
protected class DoubleClickListener extends MouseAdapter { |
|
552 |
// NOTE: This class exists only for backward compatability. All |
|
553 |
// its functionality has been moved into Handler. If you need to add |
|
554 |
// new functionality add it to the Handler, but make sure this |
|
555 |
// class calls into the Handler. |
|
556 |
Handler handler; |
|
557 |
public DoubleClickListener(JList list) { |
|
558 |
handler = new Handler(list); |
|
559 |
} |
|
560 |
||
561 |
/** |
|
562 |
* The JList used for representing the files is created by subclasses, but the |
|
563 |
* selection is monitored in this class. The TransferHandler installed in the |
|
564 |
* JFileChooser is also installed in the file list as it is used as the actual |
|
565 |
* transfer source. The list is updated on a mouse enter to reflect the current |
|
566 |
* data transfer state of the file chooser. |
|
567 |
*/ |
|
568 |
public void mouseEntered(MouseEvent e) { |
|
569 |
handler.mouseEntered(e); |
|
570 |
} |
|
571 |
||
572 |
public void mouseClicked(MouseEvent e) { |
|
573 |
handler.mouseClicked(e); |
|
574 |
} |
|
575 |
} |
|
576 |
||
577 |
protected class SelectionListener implements ListSelectionListener { |
|
578 |
// NOTE: This class exists only for backward compatability. All |
|
579 |
// its functionality has been moved into Handler. If you need to add |
|
580 |
// new functionality add it to the Handler, but make sure this |
|
581 |
// class calls into the Handler. |
|
582 |
public void valueChanged(ListSelectionEvent e) { |
|
583 |
getHandler().valueChanged(e); |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Property to remember whether a directory is currently selected in the UI. |
|
589 |
* |
|
590 |
* @return <code>true</code> iff a directory is currently selected. |
|
591 |
* @since 1.4 |
|
592 |
*/ |
|
593 |
protected boolean isDirectorySelected() { |
|
594 |
return directorySelected; |
|
595 |
} |
|
596 |
||
597 |
/** |
|
598 |
* Property to remember whether a directory is currently selected in the UI. |
|
599 |
* This is normally called by the UI on a selection event. |
|
600 |
* |
|
601 |
* @param b iff a directory is currently selected. |
|
602 |
* @since 1.4 |
|
603 |
*/ |
|
604 |
protected void setDirectorySelected(boolean b) { |
|
605 |
directorySelected = b; |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Property to remember the directory that is currently selected in the UI. |
|
610 |
* |
|
611 |
* @return the value of the <code>directory</code> property |
|
612 |
* @see #setDirectory |
|
613 |
* @since 1.4 |
|
614 |
*/ |
|
615 |
protected File getDirectory() { |
|
616 |
return directory; |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* Property to remember the directory that is currently selected in the UI. |
|
621 |
* This is normally called by the UI on a selection event. |
|
622 |
* |
|
623 |
* @param f the <code>File</code> object representing the directory that is |
|
624 |
* currently selected |
|
625 |
* @since 1.4 |
|
626 |
*/ |
|
627 |
protected void setDirectory(File f) { |
|
628 |
directory = f; |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* Returns the mnemonic for the given key. |
|
633 |
*/ |
|
634 |
private int getMnemonic(String key, Locale l) { |
|
635 |
return SwingUtilities2.getUIDefaultsInt(key, l); |
|
636 |
} |
|
637 |
||
638 |
// ******************************************************* |
|
639 |
// ************ FileChooser UI PLAF methods ************** |
|
640 |
// ******************************************************* |
|
641 |
||
642 |
/** |
|
643 |
* Returns the default accept all file filter |
|
644 |
*/ |
|
645 |
public FileFilter getAcceptAllFileFilter(JFileChooser fc) { |
|
646 |
return acceptAllFileFilter; |
|
647 |
} |
|
648 |
||
649 |
||
650 |
public FileView getFileView(JFileChooser fc) { |
|
651 |
return fileView; |
|
652 |
} |
|
653 |
||
654 |
||
655 |
/** |
|
656 |
* Returns the title of this dialog |
|
657 |
*/ |
|
658 |
public String getDialogTitle(JFileChooser fc) { |
|
659 |
String dialogTitle = fc.getDialogTitle(); |
|
660 |
if (dialogTitle != null) { |
|
661 |
return dialogTitle; |
|
662 |
} else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) { |
|
663 |
return openDialogTitleText; |
|
664 |
} else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) { |
|
665 |
return saveDialogTitleText; |
|
666 |
} else { |
|
667 |
return getApproveButtonText(fc); |
|
668 |
} |
|
669 |
} |
|
670 |
||
671 |
||
672 |
public int getApproveButtonMnemonic(JFileChooser fc) { |
|
673 |
int mnemonic = fc.getApproveButtonMnemonic(); |
|
674 |
if (mnemonic > 0) { |
|
675 |
return mnemonic; |
|
676 |
} else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) { |
|
677 |
return openButtonMnemonic; |
|
678 |
} else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) { |
|
679 |
return saveButtonMnemonic; |
|
680 |
} else { |
|
681 |
return mnemonic; |
|
682 |
} |
|
683 |
} |
|
684 |
||
685 |
public String getApproveButtonText(JFileChooser fc) { |
|
686 |
String buttonText = fc.getApproveButtonText(); |
|
687 |
if (buttonText != null) { |
|
688 |
return buttonText; |
|
689 |
} else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) { |
|
690 |
return openButtonText; |
|
691 |
} else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) { |
|
692 |
return saveButtonText; |
|
693 |
} else { |
|
694 |
return null; |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
||
699 |
// ***************************** |
|
700 |
// ***** Directory Actions ***** |
|
701 |
// ***************************** |
|
702 |
||
703 |
public Action getNewFolderAction() { |
|
704 |
if (newFolderAction == null) { |
|
705 |
newFolderAction = new NewFolderAction(); |
|
706 |
// Note: Don't return null for readOnly, it might |
|
707 |
// break older apps. |
|
708 |
if (readOnly) { |
|
709 |
newFolderAction.setEnabled(false); |
|
710 |
} |
|
711 |
} |
|
712 |
return newFolderAction; |
|
713 |
} |
|
714 |
||
715 |
public Action getGoHomeAction() { |
|
716 |
return goHomeAction; |
|
717 |
} |
|
718 |
||
719 |
public Action getChangeToParentDirectoryAction() { |
|
720 |
return changeToParentDirectoryAction; |
|
721 |
} |
|
722 |
||
723 |
public Action getApproveSelectionAction() { |
|
724 |
return approveSelectionAction; |
|
725 |
} |
|
726 |
||
727 |
public Action getCancelSelectionAction() { |
|
728 |
return cancelSelectionAction; |
|
729 |
} |
|
730 |
||
731 |
public Action getUpdateAction() { |
|
732 |
return updateAction; |
|
733 |
} |
|
734 |
||
735 |
||
736 |
/** |
|
737 |
* Creates a new folder. |
|
738 |
*/ |
|
739 |
protected class NewFolderAction extends AbstractAction { |
|
740 |
protected NewFolderAction() { |
|
741 |
super(FilePane.ACTION_NEW_FOLDER); |
|
742 |
} |
|
743 |
public void actionPerformed(ActionEvent e) { |
|
744 |
if (readOnly) { |
|
745 |
return; |
|
746 |
} |
|
747 |
JFileChooser fc = getFileChooser(); |
|
748 |
File currentDirectory = fc.getCurrentDirectory(); |
|
749 |
||
750 |
if (!currentDirectory.exists()) { |
|
751 |
JOptionPane.showMessageDialog( |
|
752 |
fc, |
|
753 |
newFolderParentDoesntExistText, |
|
754 |
newFolderParentDoesntExistTitleText, JOptionPane.WARNING_MESSAGE); |
|
755 |
return; |
|
756 |
} |
|
757 |
||
758 |
File newFolder; |
|
759 |
try { |
|
760 |
newFolder = fc.getFileSystemView().createNewFolder(currentDirectory); |
|
761 |
if (fc.isMultiSelectionEnabled()) { |
|
762 |
fc.setSelectedFiles(new File[] { newFolder }); |
|
763 |
} else { |
|
764 |
fc.setSelectedFile(newFolder); |
|
765 |
} |
|
766 |
} catch (IOException exc) { |
|
767 |
JOptionPane.showMessageDialog( |
|
768 |
fc, |
|
769 |
newFolderErrorText + newFolderErrorSeparator + exc, |
|
770 |
newFolderErrorText, JOptionPane.ERROR_MESSAGE); |
|
771 |
return; |
|
772 |
} |
|
773 |
||
774 |
fc.rescanCurrentDirectory(); |
|
775 |
} |
|
776 |
} |
|
777 |
||
778 |
/** |
|
779 |
* Acts on the "home" key event or equivalent event. |
|
780 |
*/ |
|
781 |
protected class GoHomeAction extends AbstractAction { |
|
782 |
protected GoHomeAction() { |
|
783 |
super("Go Home"); |
|
784 |
} |
|
785 |
public void actionPerformed(ActionEvent e) { |
|
786 |
JFileChooser fc = getFileChooser(); |
|
787 |
changeDirectory(fc.getFileSystemView().getHomeDirectory()); |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
protected class ChangeToParentDirectoryAction extends AbstractAction { |
|
792 |
protected ChangeToParentDirectoryAction() { |
|
793 |
super("Go Up"); |
|
794 |
putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY); |
|
795 |
} |
|
796 |
public void actionPerformed(ActionEvent e) { |
|
797 |
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); |
|
798 |
if (focusOwner == null || !(focusOwner instanceof javax.swing.text.JTextComponent)) { |
|
799 |
getFileChooser().changeToParentDirectory(); |
|
800 |
} |
|
801 |
} |
|
802 |
} |
|
803 |
||
804 |
/** |
|
805 |
* Responds to an Open or Save request |
|
806 |
*/ |
|
807 |
protected class ApproveSelectionAction extends AbstractAction { |
|
808 |
protected ApproveSelectionAction() { |
|
809 |
super(FilePane.ACTION_APPROVE_SELECTION); |
|
810 |
} |
|
811 |
public void actionPerformed(ActionEvent e) { |
|
812 |
if (isDirectorySelected()) { |
|
813 |
File dir = getDirectory(); |
|
814 |
if (dir != null) { |
|
815 |
try { |
|
816 |
// Strip trailing ".." |
|
817 |
dir = ShellFolder.getNormalizedFile(dir); |
|
818 |
} catch (IOException ex) { |
|
819 |
// Ok, use f as is |
|
820 |
} |
|
821 |
changeDirectory(dir); |
|
822 |
return; |
|
823 |
} |
|
824 |
} |
|
825 |
||
826 |
JFileChooser chooser = getFileChooser(); |
|
827 |
||
828 |
String filename = getFileName(); |
|
829 |
FileSystemView fs = chooser.getFileSystemView(); |
|
830 |
File dir = chooser.getCurrentDirectory(); |
|
831 |
||
832 |
if (filename != null) { |
|
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
833 |
// Remove whitespaces from end of filename |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
834 |
int i = filename.length() - 1; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
835 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
836 |
while (i >=0 && filename.charAt(i) <= ' ') { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
837 |
i--; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
838 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
839 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
840 |
filename = filename.substring(0, i + 1); |
2 | 841 |
} |
842 |
||
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
843 |
if (filename == null || filename.length() == 0) { |
2 | 844 |
// no file selected, multiple selection off, therefore cancel the approve action |
845 |
resetGlobFilter(); |
|
846 |
return; |
|
847 |
} |
|
848 |
||
849 |
File selectedFile = null; |
|
850 |
File[] selectedFiles = null; |
|
851 |
||
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
852 |
// Unix: Resolve '~' to user's home directory |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
853 |
if (File.separatorChar == '/') { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
854 |
if (filename.startsWith("~/")) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
855 |
filename = System.getProperty("user.home") + filename.substring(1); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
856 |
} else if (filename.equals("~")) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
857 |
filename = System.getProperty("user.home"); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
858 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
859 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
860 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
861 |
if (chooser.isMultiSelectionEnabled() && filename.length() > 1 && |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
862 |
filename.charAt(0) == '"' && filename.charAt(filename.length() - 1) == '"') { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
863 |
List<File> fList = new ArrayList<File>(); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
864 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
865 |
String[] files = filename.substring(1, filename.length() - 1).split("\" \""); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
866 |
// Optimize searching files by names in "children" array |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
867 |
Arrays.sort(files); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
868 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
869 |
File[] children = null; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
870 |
int childIndex = 0; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
871 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
872 |
for (String str : files) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
873 |
File file = fs.createFileObject(str); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
874 |
if (!file.isAbsolute()) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
875 |
if (children == null) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
876 |
children = fs.getFiles(dir, false); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
877 |
Arrays.sort(children); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
878 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
879 |
for (int k = 0; k < children.length; k++) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
880 |
int l = (childIndex + k) % children.length; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
881 |
if (children[l].getName().equals(str)) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
882 |
file = children[l]; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
883 |
childIndex = l + 1; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
884 |
break; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
885 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
886 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
887 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
888 |
fList.add(file); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
889 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
890 |
|
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
891 |
if (!fList.isEmpty()) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
892 |
selectedFiles = fList.toArray(new File[fList.size()]); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
893 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
894 |
resetGlobFilter(); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
895 |
} else { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
896 |
selectedFile = fs.createFileObject(filename); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
897 |
if (!selectedFile.isAbsolute()) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
898 |
selectedFile = fs.getChild(dir, filename); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
899 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
900 |
// check for wildcard pattern |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
901 |
FileFilter currentFilter = chooser.getFileFilter(); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
902 |
if (!selectedFile.exists() && isGlobPattern(filename)) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
903 |
changeDirectory(selectedFile.getParentFile()); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
904 |
if (globFilter == null) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
905 |
globFilter = new GlobFilter(); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
906 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
907 |
try { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
908 |
globFilter.setPattern(selectedFile.getName()); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
909 |
if (!(currentFilter instanceof GlobFilter)) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
910 |
actualFileFilter = currentFilter; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
911 |
} |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
912 |
chooser.setFileFilter(null); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
913 |
chooser.setFileFilter(globFilter); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
914 |
return; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
915 |
} catch (PatternSyntaxException pse) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
916 |
// Not a valid glob pattern. Abandon filter. |
2 | 917 |
} |
918 |
} |
|
919 |
||
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
920 |
resetGlobFilter(); |
2 | 921 |
|
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
922 |
// Check for directory change action |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
923 |
boolean isDir = (selectedFile != null && selectedFile.isDirectory()); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
924 |
boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile)); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
925 |
boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled(); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
926 |
boolean isFileSelEnabled = chooser.isFileSelectionEnabled(); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
927 |
boolean isCtrl = (e != null && (e.getModifiers() & ActionEvent.CTRL_MASK) != 0); |
2 | 928 |
|
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
929 |
if (isDir && isTrav && (isCtrl || !isDirSelEnabled)) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
930 |
changeDirectory(selectedFile); |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
931 |
return; |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
932 |
} else if ((isDir || !isFileSelEnabled) |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
933 |
&& (!isDir || !isDirSelEnabled) |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
934 |
&& (!isDirSelEnabled || selectedFile.exists())) { |
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
935 |
selectedFile = null; |
2 | 936 |
} |
937 |
} |
|
1302
ac8b63707542
6351074: JFileChooser removes leading space in filename
rupashka
parents:
1299
diff
changeset
|
938 |
|
2 | 939 |
if (selectedFiles != null || selectedFile != null) { |
940 |
if (selectedFiles != null || chooser.isMultiSelectionEnabled()) { |
|
941 |
if (selectedFiles == null) { |
|
942 |
selectedFiles = new File[] { selectedFile }; |
|
943 |
} |
|
944 |
chooser.setSelectedFiles(selectedFiles); |
|
945 |
// Do it again. This is a fix for bug 4949273 to force the |
|
946 |
// selected value in case the ListSelectionModel clears it |
|
947 |
// for non-existing file names. |
|
948 |
chooser.setSelectedFiles(selectedFiles); |
|
949 |
} else { |
|
950 |
chooser.setSelectedFile(selectedFile); |
|
951 |
} |
|
952 |
chooser.approveSelection(); |
|
953 |
} else { |
|
954 |
if (chooser.isMultiSelectionEnabled()) { |
|
955 |
chooser.setSelectedFiles(null); |
|
956 |
} else { |
|
957 |
chooser.setSelectedFile(null); |
|
958 |
} |
|
959 |
chooser.cancelSelection(); |
|
960 |
} |
|
961 |
} |
|
962 |
} |
|
963 |
||
964 |
||
965 |
private void resetGlobFilter() { |
|
966 |
if (actualFileFilter != null) { |
|
967 |
JFileChooser chooser = getFileChooser(); |
|
968 |
FileFilter currentFilter = chooser.getFileFilter(); |
|
969 |
if (currentFilter != null && currentFilter.equals(globFilter)) { |
|
970 |
chooser.setFileFilter(actualFileFilter); |
|
971 |
chooser.removeChoosableFileFilter(globFilter); |
|
972 |
} |
|
973 |
actualFileFilter = null; |
|
974 |
} |
|
975 |
} |
|
976 |
||
977 |
private static boolean isGlobPattern(String filename) { |
|
978 |
return ((File.separatorChar == '\\' && (filename.indexOf('*') >= 0 |
|
979 |
|| filename.indexOf('?') >= 0)) |
|
980 |
|| (File.separatorChar == '/' && (filename.indexOf('*') >= 0 |
|
981 |
|| filename.indexOf('?') >= 0 |
|
982 |
|| filename.indexOf('[') >= 0))); |
|
983 |
} |
|
984 |
||
985 |
||
986 |
/* A file filter which accepts file patterns containing |
|
987 |
* the special wildcards *? on Windows and *?[] on Unix. |
|
988 |
*/ |
|
989 |
class GlobFilter extends FileFilter { |
|
990 |
Pattern pattern; |
|
991 |
String globPattern; |
|
992 |
||
993 |
public void setPattern(String globPattern) { |
|
994 |
char[] gPat = globPattern.toCharArray(); |
|
995 |
char[] rPat = new char[gPat.length * 2]; |
|
996 |
boolean isWin32 = (File.separatorChar == '\\'); |
|
997 |
boolean inBrackets = false; |
|
998 |
int j = 0; |
|
999 |
||
1000 |
this.globPattern = globPattern; |
|
1001 |
||
1002 |
if (isWin32) { |
|
1003 |
// On windows, a pattern ending with *.* is equal to ending with * |
|
1004 |
int len = gPat.length; |
|
1005 |
if (globPattern.endsWith("*.*")) { |
|
1006 |
len -= 2; |
|
1007 |
} |
|
1008 |
for (int i = 0; i < len; i++) { |
|
1009 |
switch(gPat[i]) { |
|
1010 |
case '*': |
|
1011 |
rPat[j++] = '.'; |
|
1012 |
rPat[j++] = '*'; |
|
1013 |
break; |
|
1014 |
||
1015 |
case '?': |
|
1016 |
rPat[j++] = '.'; |
|
1017 |
break; |
|
1018 |
||
1019 |
case '\\': |
|
1020 |
rPat[j++] = '\\'; |
|
1021 |
rPat[j++] = '\\'; |
|
1022 |
break; |
|
1023 |
||
1024 |
default: |
|
1025 |
if ("+()^$.{}[]".indexOf(gPat[i]) >= 0) { |
|
1026 |
rPat[j++] = '\\'; |
|
1027 |
} |
|
1028 |
rPat[j++] = gPat[i]; |
|
1029 |
break; |
|
1030 |
} |
|
1031 |
} |
|
1032 |
} else { |
|
1033 |
for (int i = 0; i < gPat.length; i++) { |
|
1034 |
switch(gPat[i]) { |
|
1035 |
case '*': |
|
1036 |
if (!inBrackets) { |
|
1037 |
rPat[j++] = '.'; |
|
1038 |
} |
|
1039 |
rPat[j++] = '*'; |
|
1040 |
break; |
|
1041 |
||
1042 |
case '?': |
|
1043 |
rPat[j++] = inBrackets ? '?' : '.'; |
|
1044 |
break; |
|
1045 |
||
1046 |
case '[': |
|
1047 |
inBrackets = true; |
|
1048 |
rPat[j++] = gPat[i]; |
|
1049 |
||
1050 |
if (i < gPat.length - 1) { |
|
1051 |
switch (gPat[i+1]) { |
|
1052 |
case '!': |
|
1053 |
case '^': |
|
1054 |
rPat[j++] = '^'; |
|
1055 |
i++; |
|
1056 |
break; |
|
1057 |
||
1058 |
case ']': |
|
1059 |
rPat[j++] = gPat[++i]; |
|
1060 |
break; |
|
1061 |
} |
|
1062 |
} |
|
1063 |
break; |
|
1064 |
||
1065 |
case ']': |
|
1066 |
rPat[j++] = gPat[i]; |
|
1067 |
inBrackets = false; |
|
1068 |
break; |
|
1069 |
||
1070 |
case '\\': |
|
1071 |
if (i == 0 && gPat.length > 1 && gPat[1] == '~') { |
|
1072 |
rPat[j++] = gPat[++i]; |
|
1073 |
} else { |
|
1074 |
rPat[j++] = '\\'; |
|
1075 |
if (i < gPat.length - 1 && "*?[]".indexOf(gPat[i+1]) >= 0) { |
|
1076 |
rPat[j++] = gPat[++i]; |
|
1077 |
} else { |
|
1078 |
rPat[j++] = '\\'; |
|
1079 |
} |
|
1080 |
} |
|
1081 |
break; |
|
1082 |
||
1083 |
default: |
|
1084 |
//if ("+()|^$.{}<>".indexOf(gPat[i]) >= 0) { |
|
1085 |
if (!Character.isLetterOrDigit(gPat[i])) { |
|
1086 |
rPat[j++] = '\\'; |
|
1087 |
} |
|
1088 |
rPat[j++] = gPat[i]; |
|
1089 |
break; |
|
1090 |
} |
|
1091 |
} |
|
1092 |
} |
|
1093 |
this.pattern = Pattern.compile(new String(rPat, 0, j), Pattern.CASE_INSENSITIVE); |
|
1094 |
} |
|
1095 |
||
1096 |
public boolean accept(File f) { |
|
1097 |
if (f == null) { |
|
1098 |
return false; |
|
1099 |
} |
|
1100 |
if (f.isDirectory()) { |
|
1101 |
return true; |
|
1102 |
} |
|
1103 |
return pattern.matcher(f.getName()).matches(); |
|
1104 |
} |
|
1105 |
||
1106 |
public String getDescription() { |
|
1107 |
return globPattern; |
|
1108 |
} |
|
1109 |
} |
|
1110 |
||
1111 |
/** |
|
1112 |
* Responds to a cancel request. |
|
1113 |
*/ |
|
1114 |
protected class CancelSelectionAction extends AbstractAction { |
|
1115 |
public void actionPerformed(ActionEvent e) { |
|
1116 |
getFileChooser().cancelSelection(); |
|
1117 |
} |
|
1118 |
} |
|
1119 |
||
1120 |
/** |
|
1121 |
* Rescans the files in the current directory |
|
1122 |
*/ |
|
1123 |
protected class UpdateAction extends AbstractAction { |
|
1124 |
public void actionPerformed(ActionEvent e) { |
|
1125 |
JFileChooser fc = getFileChooser(); |
|
1126 |
fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName())); |
|
1127 |
fc.rescanCurrentDirectory(); |
|
1128 |
} |
|
1129 |
} |
|
1130 |
||
1131 |
||
1132 |
private void changeDirectory(File dir) { |
|
1133 |
JFileChooser fc = getFileChooser(); |
|
1134 |
// Traverse shortcuts on Windows |
|
1135 |
if (dir != null && File.separatorChar == '\\' && dir.getPath().endsWith(".lnk")) { |
|
1136 |
try { |
|
1137 |
File linkedTo = ShellFolder.getShellFolder(dir).getLinkLocation(); |
|
1138 |
if (linkedTo != null && fc.isTraversable(linkedTo)) { |
|
1139 |
dir = linkedTo; |
|
1140 |
} else { |
|
1141 |
return; |
|
1142 |
} |
|
1143 |
} catch (FileNotFoundException ex) { |
|
1144 |
return; |
|
1145 |
} |
|
1146 |
} |
|
1147 |
fc.setCurrentDirectory(dir); |
|
1148 |
if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES && |
|
1149 |
fc.getFileSystemView().isFileSystem(dir)) { |
|
1150 |
||
1151 |
setFileName(dir.getAbsolutePath()); |
|
1152 |
} |
|
1153 |
} |
|
1154 |
||
1155 |
||
1156 |
// ***************************************** |
|
1157 |
// ***** default AcceptAll file filter ***** |
|
1158 |
// ***************************************** |
|
1159 |
protected class AcceptAllFileFilter extends FileFilter { |
|
1160 |
||
1161 |
public AcceptAllFileFilter() { |
|
1162 |
} |
|
1163 |
||
1164 |
public boolean accept(File f) { |
|
1165 |
return true; |
|
1166 |
} |
|
1167 |
||
1168 |
public String getDescription() { |
|
1169 |
return UIManager.getString("FileChooser.acceptAllFileFilterText"); |
|
1170 |
} |
|
1171 |
} |
|
1172 |
||
1173 |
||
1174 |
// *********************** |
|
1175 |
// * FileView operations * |
|
1176 |
// *********************** |
|
1177 |
protected class BasicFileView extends FileView { |
|
1178 |
/* FileView type descriptions */ |
|
1179 |
// PENDING(jeff) - pass in the icon cache size |
|
1180 |
protected Hashtable<File,Icon> iconCache = new Hashtable<File,Icon>(); |
|
1181 |
||
1182 |
public BasicFileView() { |
|
1183 |
} |
|
1184 |
||
1185 |
public void clearIconCache() { |
|
1186 |
iconCache = new Hashtable<File,Icon>(); |
|
1187 |
} |
|
1188 |
||
1189 |
public String getName(File f) { |
|
1190 |
// Note: Returns display name rather than file name |
|
1191 |
String fileName = null; |
|
1192 |
if(f != null) { |
|
1193 |
fileName = getFileChooser().getFileSystemView().getSystemDisplayName(f); |
|
1194 |
} |
|
1195 |
return fileName; |
|
1196 |
} |
|
1197 |
||
1198 |
||
1199 |
public String getDescription(File f) { |
|
1200 |
return f.getName(); |
|
1201 |
} |
|
1202 |
||
1203 |
public String getTypeDescription(File f) { |
|
1204 |
String type = getFileChooser().getFileSystemView().getSystemTypeDescription(f); |
|
1205 |
if (type == null) { |
|
1206 |
if (f.isDirectory()) { |
|
1207 |
type = directoryDescriptionText; |
|
1208 |
} else { |
|
1209 |
type = fileDescriptionText; |
|
1210 |
} |
|
1211 |
} |
|
1212 |
return type; |
|
1213 |
} |
|
1214 |
||
1215 |
public Icon getCachedIcon(File f) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
1216 |
return iconCache.get(f); |
2 | 1217 |
} |
1218 |
||
1219 |
public void cacheIcon(File f, Icon i) { |
|
1220 |
if(f == null || i == null) { |
|
1221 |
return; |
|
1222 |
} |
|
1223 |
iconCache.put(f, i); |
|
1224 |
} |
|
1225 |
||
1226 |
public Icon getIcon(File f) { |
|
1227 |
Icon icon = getCachedIcon(f); |
|
1228 |
if(icon != null) { |
|
1229 |
return icon; |
|
1230 |
} |
|
1231 |
icon = fileIcon; |
|
1232 |
if (f != null) { |
|
1233 |
FileSystemView fsv = getFileChooser().getFileSystemView(); |
|
1234 |
||
1235 |
if (fsv.isFloppyDrive(f)) { |
|
1236 |
icon = floppyDriveIcon; |
|
1237 |
} else if (fsv.isDrive(f)) { |
|
1238 |
icon = hardDriveIcon; |
|
1239 |
} else if (fsv.isComputerNode(f)) { |
|
1240 |
icon = computerIcon; |
|
1241 |
} else if (f.isDirectory()) { |
|
1242 |
icon = directoryIcon; |
|
1243 |
} |
|
1244 |
} |
|
1245 |
cacheIcon(f, icon); |
|
1246 |
return icon; |
|
1247 |
} |
|
1248 |
||
1249 |
public Boolean isHidden(File f) { |
|
1250 |
String name = f.getName(); |
|
1251 |
if(name != null && name.charAt(0) == '.') { |
|
1252 |
return Boolean.TRUE; |
|
1253 |
} else { |
|
1254 |
return Boolean.FALSE; |
|
1255 |
} |
|
1256 |
} |
|
1257 |
} |
|
1258 |
||
1259 |
private static final TransferHandler defaultTransferHandler = new FileTransferHandler(); |
|
1260 |
||
1261 |
/** |
|
1262 |
* Data transfer support for the file chooser. Since files are currently presented |
|
1263 |
* as a list, the list support is reused with the added flavor of DataFlavor.javaFileListFlavor |
|
1264 |
*/ |
|
1265 |
static class FileTransferHandler extends TransferHandler implements UIResource { |
|
1266 |
||
1267 |
/** |
|
1268 |
* Create a Transferable to use as the source for a data transfer. |
|
1269 |
* |
|
1270 |
* @param c The component holding the data to be transfered. This |
|
1271 |
* argument is provided to enable sharing of TransferHandlers by |
|
1272 |
* multiple components. |
|
1273 |
* @return The representation of the data to be transfered. |
|
1274 |
* |
|
1275 |
*/ |
|
1276 |
protected Transferable createTransferable(JComponent c) { |
|
1277 |
Object[] values = null; |
|
1278 |
if (c instanceof JList) { |
|
1279 |
values = ((JList)c).getSelectedValues(); |
|
1280 |
} else if (c instanceof JTable) { |
|
1281 |
JTable table = (JTable)c; |
|
1282 |
int[] rows = table.getSelectedRows(); |
|
1283 |
if (rows != null) { |
|
1284 |
values = new Object[rows.length]; |
|
1285 |
for (int i=0; i<rows.length; i++) { |
|
1286 |
values[i] = table.getValueAt(rows[i], 0); |
|
1287 |
} |
|
1288 |
} |
|
1289 |
} |
|
1290 |
if (values == null || values.length == 0) { |
|
1291 |
return null; |
|
1292 |
} |
|
1293 |
||
1294 |
StringBuffer plainBuf = new StringBuffer(); |
|
1295 |
StringBuffer htmlBuf = new StringBuffer(); |
|
1296 |
||
1297 |
htmlBuf.append("<html>\n<body>\n<ul>\n"); |
|
1298 |
||
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
1299 |
for (Object obj : values) { |
2 | 1300 |
String val = ((obj == null) ? "" : obj.toString()); |
1301 |
plainBuf.append(val + "\n"); |
|
1302 |
htmlBuf.append(" <li>" + val + "\n"); |
|
1303 |
} |
|
1304 |
||
1305 |
// remove the last newline |
|
1306 |
plainBuf.deleteCharAt(plainBuf.length() - 1); |
|
1307 |
htmlBuf.append("</ul>\n</body>\n</html>"); |
|
1308 |
||
1309 |
return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values); |
|
1310 |
} |
|
1311 |
||
1312 |
public int getSourceActions(JComponent c) { |
|
1313 |
return COPY; |
|
1314 |
} |
|
1315 |
||
1316 |
static class FileTransferable extends BasicTransferable { |
|
1317 |
||
1318 |
Object[] fileData; |
|
1319 |
||
1320 |
FileTransferable(String plainData, String htmlData, Object[] fileData) { |
|
1321 |
super(plainData, htmlData); |
|
1322 |
this.fileData = fileData; |
|
1323 |
} |
|
1324 |
||
1325 |
/** |
|
1326 |
* Best format of the file chooser is DataFlavor.javaFileListFlavor. |
|
1327 |
*/ |
|
1328 |
protected DataFlavor[] getRicherFlavors() { |
|
1329 |
DataFlavor[] flavors = new DataFlavor[1]; |
|
1330 |
flavors[0] = DataFlavor.javaFileListFlavor; |
|
1331 |
return flavors; |
|
1332 |
} |
|
1333 |
||
1334 |
/** |
|
1335 |
* The only richer format supported is the file list flavor |
|
1336 |
*/ |
|
1337 |
protected Object getRicherData(DataFlavor flavor) { |
|
1338 |
if (DataFlavor.javaFileListFlavor.equals(flavor)) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
1339 |
ArrayList<Object> files = new ArrayList<Object>(); |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
1340 |
for (Object file : this.fileData) { |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
680
diff
changeset
|
1341 |
files.add(file); |
2 | 1342 |
} |
1343 |
return files; |
|
1344 |
} |
|
1345 |
return null; |
|
1346 |
} |
|
1347 |
||
1348 |
} |
|
1349 |
} |
|
1350 |
} |