author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 458 | 54413b296bdf |
child 1837 | efd004b550b7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-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 |
package com.sun.java.swing.plaf.gtk; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.beans.*; |
|
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
32 |
import java.text.MessageFormat; |
|
33 |
import java.util.*; |
|
34 |
||
35 |
import javax.swing.*; |
|
36 |
import javax.swing.border.*; |
|
37 |
import javax.swing.filechooser.*; |
|
38 |
import javax.swing.event.*; |
|
39 |
import javax.swing.plaf.*; |
|
40 |
import javax.swing.plaf.basic.BasicDirectoryModel; |
|
41 |
import javax.swing.table.*; |
|
42 |
import javax.accessibility.*; |
|
43 |
||
44 |
import sun.swing.SwingUtilities2; |
|
45 |
||
46 |
import sun.swing.plaf.synth.*; |
|
47 |
import sun.swing.FilePane; |
|
48 |
import sun.awt.shell.ShellFolder; |
|
49 |
||
50 |
/** |
|
51 |
* GTK FileChooserUI. |
|
52 |
* |
|
53 |
* @author Leif Samuelsson |
|
54 |
* @author Jeff Dinkins |
|
55 |
*/ |
|
56 |
class GTKFileChooserUI extends SynthFileChooserUI { |
|
57 |
||
58 |
// The accessoryPanel is a container to place the JFileChooser accessory component |
|
59 |
private JPanel accessoryPanel = null; |
|
60 |
||
61 |
private String newFolderButtonText = null; |
|
62 |
private String newFolderErrorSeparator = null; |
|
63 |
private String newFolderErrorText = null; |
|
64 |
private String newFolderDialogText = null; |
|
65 |
private String newFolderNoDirectoryErrorTitleText = null; |
|
66 |
private String newFolderNoDirectoryErrorText = null; |
|
67 |
||
68 |
private String deleteFileButtonText = null; |
|
69 |
private String renameFileButtonText = null; |
|
70 |
||
71 |
private String newFolderButtonToolTipText = null; |
|
72 |
private String deleteFileButtonToolTipText = null; |
|
73 |
private String renameFileButtonToolTipText = null; |
|
74 |
||
75 |
private int newFolderButtonMnemonic = 0; |
|
76 |
private int deleteFileButtonMnemonic = 0; |
|
77 |
private int renameFileButtonMnemonic = 0; |
|
78 |
private int foldersLabelMnemonic = 0; |
|
79 |
private int filesLabelMnemonic = 0; |
|
80 |
||
81 |
private String renameFileDialogText = null; |
|
82 |
private String renameFileErrorTitle = null; |
|
83 |
private String renameFileErrorText = null; |
|
84 |
||
85 |
private JComboBox filterComboBox; |
|
86 |
private FilterComboBoxModel filterComboBoxModel; |
|
87 |
||
88 |
// From Motif |
|
89 |
||
90 |
private JPanel rightPanel; |
|
91 |
private JList directoryList; |
|
92 |
private JList fileList; |
|
93 |
||
94 |
private JLabel pathField; |
|
95 |
private JTextField fileNameTextField; |
|
96 |
||
97 |
private static final Dimension hstrut3 = new Dimension(3, 1); |
|
98 |
private static final Dimension vstrut10 = new Dimension(1, 10); |
|
99 |
||
100 |
private static Dimension prefListSize = new Dimension(75, 150); |
|
101 |
||
102 |
private static Dimension PREF_SIZE = new Dimension(435, 360); |
|
103 |
private static Dimension MIN_SIZE = new Dimension(200, 300); |
|
104 |
||
105 |
private static Dimension ZERO_ACC_SIZE = new Dimension(1, 1); |
|
106 |
||
107 |
private static Dimension MAX_SIZE = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); |
|
108 |
||
109 |
private static final Insets buttonMargin = new Insets(3, 3, 3, 3); |
|
110 |
||
111 |
private String filesLabelText = null; |
|
112 |
private String foldersLabelText = null; |
|
113 |
private String pathLabelText = null; |
|
114 |
private String filterLabelText = null; |
|
115 |
||
116 |
private int pathLabelMnemonic = 0; |
|
117 |
private int filterLabelMnemonic = 0; |
|
118 |
||
119 |
private JComboBox directoryComboBox; |
|
120 |
private DirectoryComboBoxModel directoryComboBoxModel; |
|
121 |
private Action directoryComboBoxAction = new DirectoryComboBoxAction(); |
|
122 |
private JPanel bottomButtonPanel; |
|
123 |
private GTKDirectoryModel model = null; |
|
124 |
private Action newFolderAction; |
|
125 |
private boolean readOnly; |
|
126 |
private boolean showDirectoryIcons; |
|
127 |
private boolean showFileIcons; |
|
128 |
private GTKFileView fileView = new GTKFileView(); |
|
129 |
private PropertyChangeListener gtkFCPropertyChangeListener; |
|
130 |
private Action approveSelectionAction = new GTKApproveSelectionAction(); |
|
131 |
private GTKDirectoryListModel directoryListModel; |
|
132 |
||
133 |
public GTKFileChooserUI(JFileChooser filechooser) { |
|
134 |
super(filechooser); |
|
135 |
} |
|
136 |
||
137 |
public String getFileName() { |
|
138 |
JFileChooser fc = getFileChooser(); |
|
139 |
String typedInName = fileNameTextField != null ? |
|
140 |
fileNameTextField.getText() : null; |
|
141 |
||
142 |
if (!fc.isMultiSelectionEnabled()) { |
|
143 |
return typedInName; |
|
144 |
} |
|
145 |
||
146 |
int mode = fc.getFileSelectionMode(); |
|
147 |
JList list = mode == JFileChooser.DIRECTORIES_ONLY ? |
|
148 |
directoryList : fileList; |
|
149 |
Object[] files = list.getSelectedValues(); |
|
150 |
int len = files.length; |
|
151 |
Vector result = new Vector(len + 1); |
|
152 |
||
153 |
// we return all selected file names |
|
154 |
for (int i = 0; i < len; i++) { |
|
155 |
File file = (File)files[i]; |
|
156 |
result.add(file.getName()); |
|
157 |
} |
|
158 |
// plus the file name typed into the text field, if not already there |
|
159 |
if (typedInName != null && !result.contains(typedInName)) { |
|
160 |
result.add(typedInName); |
|
161 |
} |
|
162 |
||
163 |
StringBuffer buf = new StringBuffer(); |
|
164 |
len = result.size(); |
|
165 |
||
166 |
// construct the resulting string |
|
167 |
for (int i=0; i<len; i++) { |
|
168 |
if (len > 1) { |
|
169 |
buf.append(" \""); |
|
170 |
} |
|
171 |
buf.append(result.get(i)); |
|
172 |
if (len > 1) { |
|
173 |
buf.append("\""); |
|
174 |
} |
|
175 |
} |
|
176 |
return buf.toString(); |
|
177 |
} |
|
178 |
||
179 |
public void setFileName(String fileName) { |
|
180 |
if (fileNameTextField != null) { |
|
181 |
fileNameTextField.setText(fileName); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
// public String getDirectoryName() { |
|
186 |
// return pathField.getText(); |
|
187 |
// } |
|
188 |
||
189 |
public void setDirectoryName(String dirname) { |
|
190 |
pathField.setText(dirname); |
|
191 |
} |
|
192 |
||
193 |
public void ensureFileIsVisible(JFileChooser fc, File f) { |
|
194 |
// PENDING |
|
195 |
} |
|
196 |
||
197 |
public void rescanCurrentDirectory(JFileChooser fc) { |
|
198 |
getModel().validateFileCache(); |
|
199 |
} |
|
200 |
||
201 |
public JPanel getAccessoryPanel() { |
|
202 |
return accessoryPanel; |
|
203 |
} |
|
204 |
||
205 |
// *********************** |
|
206 |
// * FileView operations * |
|
207 |
// *********************** |
|
208 |
||
209 |
public FileView getFileView(JFileChooser fc) { |
|
210 |
return fileView; |
|
211 |
} |
|
212 |
||
213 |
private class GTKFileView extends BasicFileView { |
|
214 |
public GTKFileView() { |
|
215 |
iconCache = null; |
|
216 |
} |
|
217 |
||
218 |
public void clearIconCache() { |
|
219 |
} |
|
220 |
||
221 |
public Icon getCachedIcon(File f) { |
|
222 |
return null; |
|
223 |
} |
|
224 |
||
225 |
public void cacheIcon(File f, Icon i) { |
|
226 |
} |
|
227 |
||
228 |
public Icon getIcon(File f) { |
|
229 |
return (f != null && f.isDirectory()) ? directoryIcon : fileIcon; |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
||
234 |
private void updateDefaultButton() { |
|
235 |
JFileChooser filechooser = getFileChooser(); |
|
236 |
JRootPane root = SwingUtilities.getRootPane(filechooser); |
|
237 |
if (root == null) { |
|
238 |
return; |
|
239 |
} |
|
240 |
||
241 |
if (filechooser.getControlButtonsAreShown()) { |
|
242 |
if (root.getDefaultButton() == null) { |
|
243 |
root.setDefaultButton(getApproveButton(filechooser)); |
|
244 |
getCancelButton(filechooser).setDefaultCapable(false); |
|
245 |
} |
|
246 |
} else { |
|
247 |
if (root.getDefaultButton() == getApproveButton(filechooser)) { |
|
248 |
root.setDefaultButton(null); |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
protected void doSelectedFileChanged(PropertyChangeEvent e) { |
|
254 |
super.doSelectedFileChanged(e); |
|
255 |
File f = (File) e.getNewValue(); |
|
256 |
if (f != null) { |
|
257 |
setFileName(getFileChooser().getName(f)); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
protected void doDirectoryChanged(PropertyChangeEvent e) { |
|
262 |
directoryList.clearSelection(); |
|
263 |
ListSelectionModel sm = directoryList.getSelectionModel(); |
|
264 |
if (sm instanceof DefaultListSelectionModel) { |
|
265 |
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0); |
|
266 |
((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0); |
|
267 |
} |
|
268 |
fileList.clearSelection(); |
|
269 |
sm = fileList.getSelectionModel(); |
|
270 |
if (sm instanceof DefaultListSelectionModel) { |
|
271 |
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0); |
|
272 |
((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0); |
|
273 |
} |
|
274 |
||
275 |
File currentDirectory = getFileChooser().getCurrentDirectory(); |
|
276 |
if (currentDirectory != null) { |
|
277 |
try { |
|
278 |
setDirectoryName(ShellFolder.getNormalizedFile((File)e.getNewValue()).getPath()); |
|
279 |
} catch (IOException ioe) { |
|
280 |
setDirectoryName(((File)e.getNewValue()).getAbsolutePath()); |
|
281 |
} |
|
282 |
if ((getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) && !getFileChooser().isMultiSelectionEnabled()) { |
|
283 |
setFileName(pathField.getText()); |
|
284 |
} |
|
285 |
directoryComboBoxModel.addItem(currentDirectory); |
|
286 |
directoryListModel.directoryChanged(); |
|
287 |
} |
|
288 |
super.doDirectoryChanged(e); |
|
289 |
} |
|
290 |
||
291 |
protected void doAccessoryChanged(PropertyChangeEvent e) { |
|
292 |
if (getAccessoryPanel() != null) { |
|
293 |
if (e.getOldValue() != null) { |
|
294 |
getAccessoryPanel().remove((JComponent)e.getOldValue()); |
|
295 |
} |
|
296 |
JComponent accessory = (JComponent)e.getNewValue(); |
|
297 |
if (accessory != null) { |
|
298 |
getAccessoryPanel().add(accessory, BorderLayout.CENTER); |
|
299 |
getAccessoryPanel().setPreferredSize(accessory.getPreferredSize()); |
|
300 |
getAccessoryPanel().setMaximumSize(MAX_SIZE); |
|
301 |
} else { |
|
302 |
getAccessoryPanel().setPreferredSize(ZERO_ACC_SIZE); |
|
303 |
getAccessoryPanel().setMaximumSize(ZERO_ACC_SIZE); |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
protected void doFileSelectionModeChanged(PropertyChangeEvent e) { |
|
309 |
directoryList.clearSelection(); |
|
310 |
rightPanel.setVisible(((Integer)e.getNewValue()).intValue() != JFileChooser.DIRECTORIES_ONLY); |
|
311 |
||
312 |
super.doFileSelectionModeChanged(e); |
|
313 |
} |
|
314 |
||
315 |
protected void doMultiSelectionChanged(PropertyChangeEvent e) { |
|
316 |
if (getFileChooser().isMultiSelectionEnabled()) { |
|
317 |
fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
|
318 |
} else { |
|
319 |
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
320 |
fileList.clearSelection(); |
|
321 |
} |
|
322 |
||
323 |
super.doMultiSelectionChanged(e); |
|
324 |
} |
|
325 |
||
326 |
protected void doControlButtonsChanged(PropertyChangeEvent e) { |
|
327 |
super.doControlButtonsChanged(e); |
|
328 |
||
329 |
JFileChooser filechooser = getFileChooser(); |
|
330 |
if (filechooser.getControlButtonsAreShown()) { |
|
331 |
filechooser.add(bottomButtonPanel, BorderLayout.SOUTH); |
|
332 |
} else { |
|
333 |
filechooser.remove(bottomButtonPanel); |
|
334 |
} |
|
335 |
updateDefaultButton(); |
|
336 |
} |
|
337 |
||
338 |
protected void doAncestorChanged(PropertyChangeEvent e) { |
|
339 |
if (e.getOldValue() == null && e.getNewValue() != null) { |
|
340 |
// Ancestor was added, set initial focus |
|
341 |
fileNameTextField.selectAll(); |
|
342 |
fileNameTextField.requestFocus(); |
|
343 |
updateDefaultButton(); |
|
344 |
} |
|
345 |
||
346 |
super.doAncestorChanged(e); |
|
347 |
} |
|
348 |
||
349 |
||
350 |
||
351 |
// ******************************************** |
|
352 |
// ************ Create Listeners ************** |
|
353 |
// ******************************************** |
|
354 |
||
355 |
public ListSelectionListener createListSelectionListener(JFileChooser fc) { |
|
356 |
return new SelectionListener(); |
|
357 |
} |
|
358 |
||
359 |
class DoubleClickListener extends MouseAdapter { |
|
360 |
JList list; |
|
361 |
public DoubleClickListener(JList list) { |
|
362 |
this.list = list; |
|
363 |
} |
|
364 |
||
365 |
public void mouseClicked(MouseEvent e) { |
|
366 |
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) { |
|
367 |
int index = list.locationToIndex(e.getPoint()); |
|
368 |
if (index >= 0) { |
|
369 |
File f = (File) list.getModel().getElementAt(index); |
|
370 |
try { |
|
371 |
// Strip trailing ".." |
|
372 |
f = ShellFolder.getNormalizedFile(f); |
|
373 |
} catch (IOException ex) { |
|
374 |
// That's ok, we'll use f as is |
|
375 |
} |
|
376 |
if (getFileChooser().isTraversable(f)) { |
|
377 |
list.clearSelection(); |
|
378 |
if (getFileChooser().getCurrentDirectory().equals(f)){ |
|
379 |
rescanCurrentDirectory(getFileChooser()); |
|
380 |
} else { |
|
381 |
getFileChooser().setCurrentDirectory(f); |
|
382 |
} |
|
383 |
} else { |
|
384 |
getFileChooser().approveSelection(); |
|
385 |
} |
|
386 |
} |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
public void mouseEntered(MouseEvent evt) { |
|
391 |
if (list != null) { |
|
392 |
TransferHandler th1 = getFileChooser().getTransferHandler(); |
|
393 |
TransferHandler th2 = list.getTransferHandler(); |
|
394 |
if (th1 != th2) { |
|
395 |
list.setTransferHandler(th1); |
|
396 |
} |
|
397 |
if (getFileChooser().getDragEnabled() != list.getDragEnabled()) { |
|
398 |
list.setDragEnabled(getFileChooser().getDragEnabled()); |
|
399 |
} |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
protected MouseListener createDoubleClickListener(JFileChooser fc, JList list) { |
|
405 |
return new DoubleClickListener(list); |
|
406 |
} |
|
407 |
||
408 |
||
409 |
||
410 |
protected class SelectionListener implements ListSelectionListener { |
|
411 |
public void valueChanged(ListSelectionEvent e) { |
|
412 |
if (!e.getValueIsAdjusting()) { |
|
413 |
JFileChooser chooser = getFileChooser(); |
|
414 |
JList list = (JList) e.getSource(); |
|
415 |
||
416 |
if (chooser.isMultiSelectionEnabled()) { |
|
417 |
File[] files = null; |
|
418 |
Object[] objects = list.getSelectedValues(); |
|
419 |
if (objects != null) { |
|
420 |
if (objects.length == 1 |
|
421 |
&& ((File)objects[0]).isDirectory() |
|
422 |
&& chooser.isTraversable(((File)objects[0])) |
|
423 |
&& (chooser.getFileSelectionMode() != chooser.DIRECTORIES_ONLY |
|
424 |
|| !chooser.getFileSystemView().isFileSystem(((File)objects[0])))) { |
|
425 |
setDirectorySelected(true); |
|
426 |
setDirectory(((File)objects[0])); |
|
427 |
} else { |
|
428 |
ArrayList fList = new ArrayList(objects.length); |
|
429 |
for (int i = 0; i < objects.length; i++) { |
|
430 |
File f = (File)objects[i]; |
|
431 |
if ((chooser.isFileSelectionEnabled() && f.isFile()) |
|
432 |
|| (chooser.isDirectorySelectionEnabled() && f.isDirectory())) { |
|
433 |
fList.add(f); |
|
434 |
} |
|
435 |
} |
|
436 |
if (fList.size() > 0) { |
|
437 |
files = (File[])fList.toArray(new File[fList.size()]); |
|
438 |
} |
|
439 |
setDirectorySelected(false); |
|
440 |
} |
|
441 |
} |
|
442 |
chooser.setSelectedFiles(files); |
|
443 |
} else { |
|
444 |
File file = (File)list.getSelectedValue(); |
|
445 |
if (file != null |
|
446 |
&& file.isDirectory() |
|
447 |
&& chooser.isTraversable(file) |
|
448 |
&& (chooser.getFileSelectionMode() == chooser.FILES_ONLY |
|
449 |
|| !chooser.getFileSystemView().isFileSystem(file))) { |
|
450 |
||
451 |
setDirectorySelected(true); |
|
452 |
setDirectory(file); |
|
453 |
} else { |
|
454 |
setDirectorySelected(false); |
|
455 |
if (file != null) { |
|
456 |
chooser.setSelectedFile(file); |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
} |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
||
465 |
// |
|
466 |
// ComponentUI Interface Implementation methods |
|
467 |
// |
|
468 |
public static ComponentUI createUI(JComponent c) { |
|
469 |
return new GTKFileChooserUI((JFileChooser)c); |
|
470 |
} |
|
471 |
||
472 |
public void installUI(JComponent c) { |
|
473 |
accessoryPanel = new JPanel(new BorderLayout(10, 10)); |
|
474 |
accessoryPanel.setName("GTKFileChooser.accessoryPanel"); |
|
475 |
||
476 |
super.installUI(c); |
|
477 |
} |
|
478 |
||
479 |
public void uninstallUI(JComponent c) { |
|
480 |
c.removePropertyChangeListener(filterComboBoxModel); |
|
481 |
super.uninstallUI(c); |
|
482 |
||
483 |
if (accessoryPanel != null) { |
|
484 |
accessoryPanel.removeAll(); |
|
485 |
} |
|
486 |
accessoryPanel = null; |
|
487 |
getFileChooser().removeAll(); |
|
488 |
} |
|
489 |
||
490 |
public void installComponents(JFileChooser fc) { |
|
491 |
super.installComponents(fc); |
|
492 |
||
493 |
boolean leftToRight = fc.getComponentOrientation().isLeftToRight(); |
|
494 |
||
495 |
fc.setLayout(new BorderLayout()); |
|
496 |
fc.setAlignmentX(JComponent.CENTER_ALIGNMENT); |
|
497 |
||
498 |
// Top row of buttons |
|
499 |
JPanel topButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0)); |
|
500 |
topButtonPanel.setBorder(new EmptyBorder(10, 10, 0, 10)); |
|
501 |
topButtonPanel.setName("GTKFileChooser.topButtonPanel"); |
|
502 |
||
503 |
if (!UIManager.getBoolean("FileChooser.readOnly")) { |
|
504 |
JButton newFolderButton = new JButton(getNewFolderAction()); |
|
505 |
newFolderButton.setName("GTKFileChooser.newFolderButton"); |
|
506 |
newFolderButton.setMnemonic(newFolderButtonMnemonic); |
|
507 |
newFolderButton.setToolTipText(newFolderButtonToolTipText); |
|
508 |
newFolderButton.setText(newFolderButtonText); |
|
509 |
topButtonPanel.add(newFolderButton); |
|
510 |
} |
|
511 |
JButton deleteFileButton = new JButton(deleteFileButtonText); |
|
512 |
deleteFileButton.setName("GTKFileChooser.deleteFileButton"); |
|
513 |
deleteFileButton.setMnemonic(deleteFileButtonMnemonic); |
|
514 |
deleteFileButton.setToolTipText(deleteFileButtonToolTipText); |
|
515 |
deleteFileButton.setEnabled(false); |
|
516 |
topButtonPanel.add(deleteFileButton); |
|
517 |
||
518 |
RenameFileAction rfa = new RenameFileAction(); |
|
519 |
JButton renameFileButton = new JButton(rfa); |
|
520 |
if (readOnly) { |
|
521 |
rfa.setEnabled(false); |
|
522 |
} |
|
523 |
renameFileButton.setText(renameFileButtonText); |
|
524 |
renameFileButton.setName("GTKFileChooser.renameFileButton"); |
|
525 |
renameFileButton.setMnemonic(renameFileButtonMnemonic); |
|
526 |
renameFileButton.setToolTipText(renameFileButtonToolTipText); |
|
527 |
topButtonPanel.add(renameFileButton); |
|
528 |
||
529 |
fc.add(topButtonPanel, BorderLayout.NORTH); |
|
530 |
||
531 |
||
532 |
JPanel interior = new JPanel(); |
|
533 |
interior.setBorder(new EmptyBorder(0, 10, 10, 10)); |
|
534 |
interior.setName("GTKFileChooser.interiorPanel"); |
|
535 |
align(interior); |
|
536 |
interior.setLayout(new BoxLayout(interior, BoxLayout.PAGE_AXIS)); |
|
537 |
||
538 |
fc.add(interior, BorderLayout.CENTER); |
|
539 |
||
540 |
JPanel comboBoxPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, |
|
541 |
0, 0) { |
|
542 |
public void layoutContainer(Container target) { |
|
543 |
super.layoutContainer(target); |
|
544 |
JComboBox comboBox = directoryComboBox; |
|
545 |
if (comboBox.getWidth() > target.getWidth()) { |
|
546 |
comboBox.setBounds(0, comboBox.getY(), target.getWidth(), |
|
547 |
comboBox.getHeight()); |
|
548 |
} |
|
549 |
} |
|
550 |
}); |
|
551 |
comboBoxPanel.setBorder(new EmptyBorder(0, 0, 4, 0)); |
|
552 |
comboBoxPanel.setName("GTKFileChooser.directoryComboBoxPanel"); |
|
553 |
// CurrentDir ComboBox |
|
554 |
directoryComboBoxModel = createDirectoryComboBoxModel(fc); |
|
555 |
directoryComboBox = new JComboBox(directoryComboBoxModel); |
|
556 |
directoryComboBox.setName("GTKFileChooser.directoryComboBox"); |
|
557 |
directoryComboBox.putClientProperty( "JComboBox.lightweightKeyboardNavigation", "Lightweight" ); |
|
558 |
directoryComboBox.addActionListener(directoryComboBoxAction); |
|
559 |
directoryComboBox.setMaximumRowCount(8); |
|
560 |
comboBoxPanel.add(directoryComboBox); |
|
561 |
interior.add(comboBoxPanel); |
|
562 |
||
563 |
||
564 |
// CENTER: left, right, accessory |
|
565 |
JPanel centerPanel = new JPanel(new BorderLayout()); |
|
566 |
centerPanel.setName("GTKFileChooser.centerPanel"); |
|
567 |
||
568 |
// SPLIT PANEL: left, right |
|
569 |
JSplitPane splitPanel = new JSplitPane(); |
|
570 |
splitPanel.setName("GTKFileChooser.splitPanel"); |
|
571 |
splitPanel.setDividerLocation((PREF_SIZE.width-8)/2); |
|
572 |
||
573 |
// left panel - Filter & directoryList |
|
574 |
JPanel leftPanel = new JPanel(new GridBagLayout()); |
|
575 |
leftPanel.setName("GTKFileChooser.directoryListPanel"); |
|
576 |
||
577 |
// Add the Directory List |
|
578 |
// Create a label that looks like button (should be a table header) |
|
579 |
TableCellRenderer headerRenderer = new JTableHeader().getDefaultRenderer(); |
|
580 |
JLabel directoryListLabel = |
|
581 |
(JLabel)headerRenderer.getTableCellRendererComponent(null, foldersLabelText, |
|
582 |
false, false, 0, 0); |
|
583 |
directoryListLabel.setName("GTKFileChooser.directoryListLabel"); |
|
584 |
leftPanel.add(directoryListLabel, new GridBagConstraints( |
|
585 |
0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, |
|
586 |
GridBagConstraints.HORIZONTAL, |
|
587 |
new Insets(0, 0, 0, 0), 0, 0)); |
|
588 |
leftPanel.add(createDirectoryList(), new GridBagConstraints( |
|
589 |
0, 1, 1, 1, 1, 1, GridBagConstraints.EAST, |
|
590 |
GridBagConstraints.BOTH, |
|
591 |
new Insets(0, 0, 0, 0), 0, 0)); |
|
592 |
directoryListLabel.setDisplayedMnemonic(foldersLabelMnemonic); |
|
593 |
directoryListLabel.setLabelFor(directoryList); |
|
594 |
||
595 |
// create files list |
|
596 |
rightPanel = new JPanel(new GridBagLayout()); |
|
597 |
rightPanel.setName("GTKFileChooser.fileListPanel"); |
|
598 |
||
599 |
headerRenderer = new JTableHeader().getDefaultRenderer(); |
|
600 |
JLabel fileListLabel = |
|
601 |
(JLabel)headerRenderer.getTableCellRendererComponent(null, filesLabelText, |
|
602 |
false, false, 0, 0); |
|
603 |
fileListLabel.setName("GTKFileChooser.fileListLabel"); |
|
604 |
rightPanel.add(fileListLabel, new GridBagConstraints( |
|
605 |
0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, |
|
606 |
GridBagConstraints.HORIZONTAL, |
|
607 |
new Insets(0, 0, 0, 0), 0, 0)); |
|
608 |
rightPanel.add(createFilesList(), new GridBagConstraints( |
|
609 |
0, 1, 1, 1, 1, 1, GridBagConstraints.EAST, |
|
610 |
GridBagConstraints.BOTH, |
|
611 |
new Insets(0, 0, 0, 0), 0, 0)); |
|
612 |
fileListLabel.setDisplayedMnemonic(filesLabelMnemonic); |
|
613 |
fileListLabel.setLabelFor(fileList); |
|
614 |
||
615 |
splitPanel.add(leftPanel, leftToRight ? JSplitPane.LEFT : JSplitPane.RIGHT); |
|
616 |
splitPanel.add(rightPanel, leftToRight ? JSplitPane.RIGHT : JSplitPane.LEFT); |
|
617 |
centerPanel.add(splitPanel, BorderLayout.CENTER); |
|
618 |
||
619 |
JComponent accessoryPanel = getAccessoryPanel(); |
|
620 |
JComponent accessory = fc.getAccessory(); |
|
621 |
if (accessoryPanel != null) { |
|
622 |
if (accessory == null) { |
|
623 |
accessoryPanel.setPreferredSize(ZERO_ACC_SIZE); |
|
624 |
accessoryPanel.setMaximumSize(ZERO_ACC_SIZE); |
|
625 |
} else { |
|
626 |
getAccessoryPanel().add(accessory, BorderLayout.CENTER); |
|
627 |
accessoryPanel.setPreferredSize(accessory.getPreferredSize()); |
|
628 |
accessoryPanel.setMaximumSize(MAX_SIZE); |
|
629 |
} |
|
630 |
align(accessoryPanel); |
|
631 |
centerPanel.add(accessoryPanel, BorderLayout.AFTER_LINE_ENDS); |
|
632 |
} |
|
633 |
interior.add(centerPanel); |
|
634 |
interior.add(Box.createRigidArea(vstrut10)); |
|
635 |
||
636 |
JPanel pathFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, |
|
637 |
0, 0)); |
|
638 |
pathFieldPanel.setBorder(new EmptyBorder(0, 0, 4, 0)); |
|
639 |
JLabel pathFieldLabel = new JLabel(pathLabelText); |
|
640 |
pathFieldLabel.setName("GTKFileChooser.pathFieldLabel"); |
|
641 |
pathFieldLabel.setDisplayedMnemonic(pathLabelMnemonic); |
|
642 |
align(pathFieldLabel); |
|
643 |
pathFieldPanel.add(pathFieldLabel); |
|
644 |
pathFieldPanel.add(Box.createRigidArea(hstrut3)); |
|
645 |
||
646 |
File currentDirectory = fc.getCurrentDirectory(); |
|
647 |
String curDirName = null; |
|
648 |
if (currentDirectory != null) { |
|
649 |
curDirName = currentDirectory.getPath(); |
|
650 |
} |
|
651 |
pathField = new JLabel(curDirName) { |
|
652 |
public Dimension getMaximumSize() { |
|
653 |
Dimension d = super.getMaximumSize(); |
|
654 |
d.height = getPreferredSize().height; |
|
655 |
return d; |
|
656 |
} |
|
657 |
}; |
|
658 |
pathField.setName("GTKFileChooser.pathField"); |
|
659 |
align(pathField); |
|
660 |
pathFieldPanel.add(pathField); |
|
661 |
interior.add(pathFieldPanel); |
|
662 |
||
663 |
// add the fileName field |
|
664 |
fileNameTextField = new JTextField() { |
|
665 |
public Dimension getMaximumSize() { |
|
666 |
Dimension d = super.getMaximumSize(); |
|
667 |
d.height = getPreferredSize().height; |
|
668 |
return d; |
|
669 |
} |
|
670 |
}; |
|
671 |
||
672 |
pathFieldLabel.setLabelFor(fileNameTextField); |
|
673 |
||
674 |
Set forwardTraversalKeys = fileNameTextField.getFocusTraversalKeys( |
|
675 |
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); |
|
676 |
forwardTraversalKeys = new HashSet(forwardTraversalKeys); |
|
677 |
forwardTraversalKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); |
|
678 |
fileNameTextField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardTraversalKeys); |
|
679 |
||
680 |
fileNameTextField.setName("GTKFileChooser.fileNameTextField"); |
|
681 |
fileNameTextField.getActionMap().put("fileNameCompletionAction", getFileNameCompletionAction()); |
|
682 |
fileNameTextField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "fileNameCompletionAction"); |
|
683 |
interior.add(fileNameTextField); |
|
684 |
||
685 |
// Add the filter combo box |
|
686 |
JPanel panel = new JPanel(); |
|
687 |
panel.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0)); |
|
688 |
panel.setBorder(new EmptyBorder(0, 0, 4, 0)); |
|
689 |
JLabel filterLabel = new JLabel(filterLabelText); |
|
690 |
filterLabel.setName("GTKFileChooser.filterLabel"); |
|
691 |
filterLabel.setDisplayedMnemonic(filterLabelMnemonic); |
|
692 |
panel.add(filterLabel); |
|
693 |
||
694 |
filterComboBoxModel = createFilterComboBoxModel(); |
|
695 |
fc.addPropertyChangeListener(filterComboBoxModel); |
|
696 |
filterComboBox = new JComboBox(filterComboBoxModel); |
|
697 |
filterComboBox.setRenderer(createFilterComboBoxRenderer()); |
|
698 |
filterLabel.setLabelFor(filterComboBox); |
|
699 |
||
700 |
interior.add(Box.createRigidArea(vstrut10)); |
|
701 |
interior.add(panel); |
|
702 |
interior.add(filterComboBox); |
|
703 |
||
704 |
// Add buttons |
|
705 |
bottomButtonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING)); |
|
706 |
bottomButtonPanel.setName("GTKFileChooser.bottomButtonPanel"); |
|
707 |
align(bottomButtonPanel); |
|
708 |
||
458
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
709 |
JPanel pnButtons = new JPanel(new GridLayout(1, 2, 5, 0)); |
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
710 |
|
2 | 711 |
JButton cancelButton = getCancelButton(fc); |
712 |
align(cancelButton); |
|
713 |
cancelButton.setMargin(buttonMargin); |
|
458
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
714 |
pnButtons.add(cancelButton); |
2 | 715 |
|
458
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
716 |
JButton approveButton = getApproveButton(fc); |
2 | 717 |
align(approveButton); |
718 |
approveButton.setMargin(buttonMargin); |
|
458
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
719 |
pnButtons.add(approveButton); |
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
720 |
|
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
721 |
bottomButtonPanel.add(pnButtons); |
2 | 722 |
|
723 |
if (fc.getControlButtonsAreShown()) { |
|
724 |
fc.add(bottomButtonPanel, BorderLayout.SOUTH); |
|
725 |
} |
|
726 |
} |
|
727 |
||
728 |
protected void installListeners(JFileChooser fc) { |
|
729 |
super.installListeners(fc); |
|
730 |
||
731 |
gtkFCPropertyChangeListener = new GTKFCPropertyChangeListener(); |
|
732 |
fc.addPropertyChangeListener(gtkFCPropertyChangeListener); |
|
733 |
} |
|
734 |
||
735 |
private int getMnemonic(String key, Locale l) { |
|
736 |
return SwingUtilities2.getUIDefaultsInt(key, l); |
|
737 |
} |
|
738 |
||
739 |
protected void uninstallListeners(JFileChooser fc) { |
|
740 |
super.uninstallListeners(fc); |
|
741 |
||
742 |
if (gtkFCPropertyChangeListener != null) { |
|
743 |
fc.removePropertyChangeListener(gtkFCPropertyChangeListener); |
|
744 |
} |
|
745 |
} |
|
746 |
||
747 |
private class GTKFCPropertyChangeListener implements PropertyChangeListener { |
|
748 |
public void propertyChange(PropertyChangeEvent e) { |
|
749 |
String prop = e.getPropertyName(); |
|
750 |
if (prop.equals("GTKFileChooser.showDirectoryIcons")) { |
|
751 |
showDirectoryIcons = Boolean.TRUE.equals(e.getNewValue()); |
|
752 |
} else if (prop.equals("GTKFileChooser.showFileIcons")) { |
|
753 |
showFileIcons = Boolean.TRUE.equals(e.getNewValue()); |
|
754 |
} |
|
755 |
} |
|
756 |
} |
|
757 |
||
758 |
protected void installDefaults(JFileChooser fc) { |
|
759 |
super.installDefaults(fc); |
|
760 |
readOnly = UIManager.getBoolean("FileChooser.readOnly"); |
|
761 |
showDirectoryIcons = |
|
762 |
Boolean.TRUE.equals(fc.getClientProperty("GTKFileChooser.showDirectoryIcons")); |
|
763 |
showFileIcons = |
|
764 |
Boolean.TRUE.equals(fc.getClientProperty("GTKFileChooser.showFileIcons")); |
|
765 |
} |
|
766 |
||
767 |
protected void installIcons(JFileChooser fc) { |
|
768 |
directoryIcon = UIManager.getIcon("FileView.directoryIcon"); |
|
769 |
fileIcon = UIManager.getIcon("FileView.fileIcon"); |
|
770 |
} |
|
771 |
||
772 |
protected void installStrings(JFileChooser fc) { |
|
773 |
super.installStrings(fc); |
|
774 |
||
775 |
Locale l = fc.getLocale(); |
|
776 |
||
777 |
newFolderDialogText = UIManager.getString("FileChooser.newFolderDialogText", l); |
|
778 |
newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l); |
|
779 |
newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l); |
|
780 |
newFolderButtonText = UIManager.getString("FileChooser.newFolderButtonText", l); |
|
781 |
newFolderNoDirectoryErrorTitleText = UIManager.getString("FileChooser.newFolderNoDirectoryErrorTitleText", l); |
|
782 |
newFolderNoDirectoryErrorText = UIManager.getString("FileChooser.newFolderNoDirectoryErrorText", l); |
|
783 |
deleteFileButtonText = UIManager.getString("FileChooser.deleteFileButtonText", l); |
|
784 |
renameFileButtonText = UIManager.getString("FileChooser.renameFileButtonText", l); |
|
785 |
||
786 |
newFolderButtonMnemonic = getMnemonic("FileChooser.newFolderButtonMnemonic", l); |
|
787 |
deleteFileButtonMnemonic = getMnemonic("FileChooser.deleteFileButtonMnemonic", l); |
|
788 |
renameFileButtonMnemonic = getMnemonic("FileChooser.renameFileButtonMnemonic", l); |
|
789 |
||
790 |
newFolderButtonToolTipText = UIManager.getString("FileChooser.newFolderButtonToolTipText", l); |
|
791 |
deleteFileButtonToolTipText = UIManager.getString("FileChooser.deleteFileButtonToolTipText", l); |
|
792 |
renameFileButtonToolTipText = UIManager.getString("FileChooser.renameFileButtonToolTipText", l); |
|
793 |
||
794 |
renameFileDialogText = UIManager.getString("FileChooser.renameFileDialogText", l); |
|
795 |
renameFileErrorTitle = UIManager.getString("FileChooser.renameFileErrorTitle", l); |
|
796 |
renameFileErrorText = UIManager.getString("FileChooser.renameFileErrorText", l); |
|
797 |
||
798 |
foldersLabelText = UIManager.getString("FileChooser.foldersLabelText",l); |
|
799 |
foldersLabelMnemonic = getMnemonic("FileChooser.foldersLabelMnemonic", l); |
|
800 |
||
801 |
filesLabelText = UIManager.getString("FileChooser.filesLabelText",l); |
|
802 |
filesLabelMnemonic = getMnemonic("FileChooser.filesLabelMnemonic", l); |
|
803 |
||
804 |
pathLabelText = UIManager.getString("FileChooser.pathLabelText",l); |
|
805 |
pathLabelMnemonic = getMnemonic("FileChooser.pathLabelMnemonic", l); |
|
806 |
||
807 |
filterLabelText = UIManager.getString("FileChooser.filterLabelText", l); |
|
808 |
filterLabelMnemonic = UIManager.getInt("FileChooser.filterLabelMnemonic"); |
|
809 |
} |
|
810 |
||
811 |
protected void uninstallStrings(JFileChooser fc) { |
|
812 |
super.uninstallStrings(fc); |
|
813 |
||
814 |
newFolderButtonText = null; |
|
815 |
deleteFileButtonText = null; |
|
816 |
renameFileButtonText = null; |
|
817 |
||
818 |
newFolderButtonToolTipText = null; |
|
819 |
deleteFileButtonToolTipText = null; |
|
820 |
renameFileButtonToolTipText = null; |
|
821 |
||
822 |
renameFileDialogText = null; |
|
823 |
renameFileErrorTitle = null; |
|
824 |
renameFileErrorText = null; |
|
825 |
||
826 |
foldersLabelText = null; |
|
827 |
filesLabelText = null; |
|
828 |
||
829 |
pathLabelText = null; |
|
830 |
||
831 |
newFolderDialogText = null; |
|
832 |
newFolderErrorText = null; |
|
833 |
newFolderErrorSeparator = null; |
|
834 |
} |
|
835 |
||
836 |
protected JScrollPane createFilesList() { |
|
837 |
fileList = new JList(); |
|
838 |
fileList.setName("GTKFileChooser.fileList"); |
|
839 |
fileList.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, filesLabelText); |
|
840 |
||
841 |
if (getFileChooser().isMultiSelectionEnabled()) { |
|
842 |
fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
|
843 |
} else { |
|
844 |
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
845 |
} |
|
846 |
||
847 |
fileList.setModel(new GTKFileListModel()); |
|
848 |
fileList.getSelectionModel().removeSelectionInterval(0, 0); |
|
849 |
fileList.setCellRenderer(new FileCellRenderer()); |
|
850 |
fileList.addListSelectionListener(createListSelectionListener(getFileChooser())); |
|
851 |
fileList.addMouseListener(createDoubleClickListener(getFileChooser(), fileList)); |
|
852 |
align(fileList); |
|
853 |
JScrollPane scrollpane = new JScrollPane(fileList); |
|
854 |
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
|
855 |
scrollpane.setName("GTKFileChooser.fileListScrollPane"); |
|
856 |
scrollpane.setPreferredSize(prefListSize); |
|
857 |
scrollpane.setMaximumSize(MAX_SIZE); |
|
858 |
align(scrollpane); |
|
859 |
return scrollpane; |
|
860 |
} |
|
861 |
||
862 |
protected JScrollPane createDirectoryList() { |
|
863 |
directoryList = new JList(); |
|
864 |
directoryList.setName("GTKFileChooser.directoryList"); |
|
865 |
directoryList.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, foldersLabelText); |
|
866 |
align(directoryList); |
|
867 |
||
868 |
directoryList.setCellRenderer(new DirectoryCellRenderer()); |
|
869 |
directoryListModel = new GTKDirectoryListModel(); |
|
870 |
directoryList.getSelectionModel().removeSelectionInterval(0, 0); |
|
871 |
directoryList.setModel(directoryListModel); |
|
872 |
directoryList.addMouseListener(createDoubleClickListener(getFileChooser(), directoryList)); |
|
873 |
directoryList.addListSelectionListener(createListSelectionListener(getFileChooser())); |
|
874 |
||
875 |
JScrollPane scrollpane = new JScrollPane(directoryList); |
|
876 |
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
|
877 |
scrollpane.setName("GTKFileChooser.directoryListScrollPane"); |
|
878 |
scrollpane.setMaximumSize(MAX_SIZE); |
|
879 |
scrollpane.setPreferredSize(prefListSize); |
|
880 |
align(scrollpane); |
|
881 |
return scrollpane; |
|
882 |
} |
|
883 |
||
884 |
protected void createModel() { |
|
885 |
model = new GTKDirectoryModel(); |
|
886 |
} |
|
887 |
||
888 |
public BasicDirectoryModel getModel() { |
|
889 |
return model; |
|
890 |
} |
|
891 |
||
892 |
public Action getApproveSelectionAction() { |
|
893 |
return approveSelectionAction; |
|
894 |
} |
|
895 |
||
896 |
private class GTKDirectoryModel extends BasicDirectoryModel { |
|
897 |
FileSystemView fsv; |
|
898 |
private Comparator fileComparator = new Comparator() { |
|
899 |
public int compare(Object o, Object o1) { |
|
900 |
return fsv.getSystemDisplayName((File) o).compareTo |
|
901 |
(fsv.getSystemDisplayName((File) o1)); |
|
902 |
} |
|
903 |
}; |
|
904 |
||
905 |
public GTKDirectoryModel() { |
|
906 |
super(getFileChooser()); |
|
907 |
} |
|
908 |
||
909 |
protected void sort(Vector<? extends File> v) { |
|
910 |
fsv = getFileChooser().getFileSystemView(); |
|
911 |
Collections.sort(v, fileComparator); |
|
912 |
} |
|
913 |
} |
|
914 |
||
915 |
protected class GTKDirectoryListModel extends AbstractListModel implements ListDataListener { |
|
916 |
File curDir; |
|
917 |
public GTKDirectoryListModel() { |
|
918 |
getModel().addListDataListener(this); |
|
919 |
directoryChanged(); |
|
920 |
} |
|
921 |
||
922 |
public int getSize() { |
|
923 |
return getModel().getDirectories().size() + 1; |
|
924 |
} |
|
925 |
||
926 |
public Object getElementAt(int index) { |
|
927 |
return index > 0 ? getModel().getDirectories().elementAt(index - 1): |
|
928 |
curDir; |
|
929 |
} |
|
930 |
||
931 |
public void intervalAdded(ListDataEvent e) { |
|
932 |
fireIntervalAdded(this, e.getIndex0(), e.getIndex1()); |
|
933 |
} |
|
934 |
||
935 |
public void intervalRemoved(ListDataEvent e) { |
|
936 |
fireIntervalRemoved(this, e.getIndex0(), e.getIndex1()); |
|
937 |
} |
|
938 |
||
939 |
// PENDING - this is inefficient - should sent out |
|
940 |
// incremental adjustment values instead of saying that the |
|
941 |
// whole list has changed. |
|
942 |
public void fireContentsChanged() { |
|
943 |
fireContentsChanged(this, 0, getModel().getDirectories().size()-1); |
|
944 |
} |
|
945 |
||
946 |
// PENDING - fire the correct interval changed - currently sending |
|
947 |
// out that everything has changed |
|
948 |
public void contentsChanged(ListDataEvent e) { |
|
949 |
fireContentsChanged(); |
|
950 |
} |
|
951 |
||
952 |
private void directoryChanged() { |
|
953 |
curDir = getFileChooser().getFileSystemView().createFileObject( |
|
954 |
getFileChooser().getCurrentDirectory(), "."); |
|
955 |
} |
|
956 |
} |
|
957 |
||
958 |
protected class GTKFileListModel extends AbstractListModel implements ListDataListener { |
|
959 |
public GTKFileListModel() { |
|
960 |
getModel().addListDataListener(this); |
|
961 |
} |
|
962 |
||
963 |
public int getSize() { |
|
964 |
return getModel().getFiles().size(); |
|
965 |
} |
|
966 |
||
967 |
public boolean contains(Object o) { |
|
968 |
return getModel().getFiles().contains(o); |
|
969 |
} |
|
970 |
||
971 |
public int indexOf(Object o) { |
|
972 |
return getModel().getFiles().indexOf(o); |
|
973 |
} |
|
974 |
||
975 |
public Object getElementAt(int index) { |
|
976 |
return getModel().getFiles().elementAt(index); |
|
977 |
} |
|
978 |
||
979 |
public void intervalAdded(ListDataEvent e) { |
|
980 |
fireIntervalAdded(this, e.getIndex0(), e.getIndex1()); |
|
981 |
} |
|
982 |
||
983 |
public void intervalRemoved(ListDataEvent e) { |
|
984 |
fireIntervalRemoved(this, e.getIndex0(), e.getIndex1()); |
|
985 |
} |
|
986 |
||
987 |
// PENDING - this is inefficient - should sent out |
|
988 |
// incremental adjustment values instead of saying that the |
|
989 |
// whole list has changed. |
|
990 |
public void fireContentsChanged() { |
|
991 |
fireContentsChanged(this, 0, getModel().getFiles().size()-1); |
|
992 |
} |
|
993 |
||
994 |
// PENDING - fire the interval changed |
|
995 |
public void contentsChanged(ListDataEvent e) { |
|
996 |
fireContentsChanged(); |
|
997 |
} |
|
998 |
} |
|
999 |
||
1000 |
||
1001 |
protected class FileCellRenderer extends DefaultListCellRenderer { |
|
1002 |
public Component getListCellRendererComponent(JList list, Object value, int index, |
|
1003 |
boolean isSelected, boolean cellHasFocus) { |
|
1004 |
||
1005 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
1006 |
setText(getFileChooser().getName((File) value)); |
|
1007 |
if (showFileIcons) { |
|
1008 |
setIcon(getFileChooser().getIcon((File)value)); |
|
1009 |
} |
|
1010 |
return this; |
|
1011 |
} |
|
1012 |
} |
|
1013 |
||
1014 |
protected class DirectoryCellRenderer extends DefaultListCellRenderer { |
|
1015 |
public Component getListCellRendererComponent(JList list, Object value, int index, |
|
1016 |
boolean isSelected, boolean cellHasFocus) { |
|
1017 |
||
1018 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
1019 |
||
1020 |
if (showDirectoryIcons) { |
|
1021 |
setIcon(getFileChooser().getIcon((File)value)); |
|
1022 |
setText(getFileChooser().getName((File)value)); |
|
1023 |
} else { |
|
1024 |
setText(getFileChooser().getName((File)value) + "/"); |
|
1025 |
} |
|
1026 |
return this; |
|
1027 |
} |
|
1028 |
} |
|
1029 |
||
1030 |
public Dimension getPreferredSize(JComponent c) { |
|
1031 |
Dimension prefSize = new Dimension(PREF_SIZE); |
|
1032 |
JComponent accessory = getFileChooser().getAccessory(); |
|
1033 |
if (accessory != null) { |
|
1034 |
prefSize.width += accessory.getPreferredSize().width + 20; |
|
1035 |
} |
|
1036 |
Dimension d = c.getLayout().preferredLayoutSize(c); |
|
1037 |
if (d != null) { |
|
1038 |
return new Dimension(d.width < prefSize.width ? prefSize.width : d.width, |
|
1039 |
d.height < prefSize.height ? prefSize.height : d.height); |
|
1040 |
} else { |
|
1041 |
return prefSize; |
|
1042 |
} |
|
1043 |
} |
|
1044 |
||
1045 |
public Dimension getMinimumSize(JComponent x) { |
|
1046 |
return new Dimension(MIN_SIZE); |
|
1047 |
} |
|
1048 |
||
1049 |
public Dimension getMaximumSize(JComponent x) { |
|
1050 |
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); |
|
1051 |
} |
|
1052 |
||
1053 |
protected void align(JComponent c) { |
|
1054 |
c.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
1055 |
c.setAlignmentY(JComponent.TOP_ALIGNMENT); |
|
1056 |
} |
|
1057 |
||
1058 |
public Action getNewFolderAction() { |
|
1059 |
if (newFolderAction == null) { |
|
1060 |
newFolderAction = new NewFolderAction(); |
|
1061 |
newFolderAction.setEnabled(!readOnly); |
|
1062 |
} |
|
1063 |
return newFolderAction; |
|
1064 |
} |
|
1065 |
||
1066 |
// |
|
1067 |
// DataModel for DirectoryComboxbox |
|
1068 |
// |
|
1069 |
protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) { |
|
1070 |
return new DirectoryComboBoxModel(); |
|
1071 |
} |
|
1072 |
||
1073 |
/** |
|
1074 |
* Data model for a type-face selection combo-box. |
|
1075 |
*/ |
|
1076 |
protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel { |
|
1077 |
Vector directories = new Vector(); |
|
1078 |
File selectedDirectory = null; |
|
1079 |
JFileChooser chooser = getFileChooser(); |
|
1080 |
FileSystemView fsv = chooser.getFileSystemView(); |
|
1081 |
||
1082 |
public DirectoryComboBoxModel() { |
|
1083 |
// Add the current directory to the model, and make it the |
|
1084 |
// selectedDirectory |
|
1085 |
File dir = getFileChooser().getCurrentDirectory(); |
|
1086 |
if (dir != null) { |
|
1087 |
addItem(dir); |
|
1088 |
} |
|
1089 |
} |
|
1090 |
||
1091 |
/** |
|
1092 |
* Adds the directory to the model and sets it to be selected, |
|
1093 |
* additionally clears out the previous selected directory and |
|
1094 |
* the paths leading up to it, if any. |
|
1095 |
*/ |
|
1096 |
private void addItem(File directory) { |
|
1097 |
||
1098 |
if (directory == null) { |
|
1099 |
return; |
|
1100 |
} |
|
1101 |
||
1102 |
int oldSize = directories.size(); |
|
1103 |
directories.clear(); |
|
1104 |
if (oldSize > 0) { |
|
1105 |
fireIntervalRemoved(this, 0, oldSize); |
|
1106 |
} |
|
1107 |
||
1108 |
// Get the canonical (full) path. This has the side |
|
1109 |
// benefit of removing extraneous chars from the path, |
|
1110 |
// for example /foo/bar/ becomes /foo/bar |
|
458
54413b296bdf
6642612: JFileChooser approve buttons should use Open and Save text (GTK)
rupashka
parents:
2
diff
changeset
|
1111 |
File canonical; |
2 | 1112 |
try { |
1113 |
canonical = fsv.createFileObject(ShellFolder.getNormalizedFile(directory).getPath()); |
|
1114 |
} catch (IOException e) { |
|
1115 |
// Maybe drive is not ready. Can't abort here. |
|
1116 |
canonical = directory; |
|
1117 |
} |
|
1118 |
||
1119 |
// create File instances of each directory leading up to the top |
|
1120 |
File f = canonical; |
|
1121 |
do { |
|
1122 |
directories.add(f); |
|
1123 |
} while ((f = f.getParentFile()) != null); |
|
1124 |
int newSize = directories.size(); |
|
1125 |
if (newSize > 0) { |
|
1126 |
fireIntervalAdded(this, 0, newSize); |
|
1127 |
} |
|
1128 |
setSelectedItem(canonical); |
|
1129 |
} |
|
1130 |
||
1131 |
public void setSelectedItem(Object selectedDirectory) { |
|
1132 |
this.selectedDirectory = (File)selectedDirectory; |
|
1133 |
fireContentsChanged(this, -1, -1); |
|
1134 |
} |
|
1135 |
||
1136 |
public Object getSelectedItem() { |
|
1137 |
return selectedDirectory; |
|
1138 |
} |
|
1139 |
||
1140 |
public int getSize() { |
|
1141 |
return directories.size(); |
|
1142 |
} |
|
1143 |
||
1144 |
public Object getElementAt(int index) { |
|
1145 |
return directories.elementAt(index); |
|
1146 |
} |
|
1147 |
} |
|
1148 |
||
1149 |
/** |
|
1150 |
* Acts when DirectoryComboBox has changed the selected item. |
|
1151 |
*/ |
|
1152 |
protected class DirectoryComboBoxAction extends AbstractAction { |
|
1153 |
protected DirectoryComboBoxAction() { |
|
1154 |
super("DirectoryComboBoxAction"); |
|
1155 |
} |
|
1156 |
||
1157 |
public void actionPerformed(ActionEvent e) { |
|
1158 |
File f = (File)directoryComboBox.getSelectedItem(); |
|
1159 |
getFileChooser().setCurrentDirectory(f); |
|
1160 |
} |
|
1161 |
} |
|
1162 |
||
1163 |
/** |
|
1164 |
* Creates a new folder. |
|
1165 |
*/ |
|
1166 |
private class NewFolderAction extends AbstractAction { |
|
1167 |
protected NewFolderAction() { |
|
1168 |
super(FilePane.ACTION_NEW_FOLDER); |
|
1169 |
} |
|
1170 |
public void actionPerformed(ActionEvent e) { |
|
1171 |
if (readOnly) { |
|
1172 |
return; |
|
1173 |
} |
|
1174 |
JFileChooser fc = getFileChooser(); |
|
1175 |
File currentDirectory = fc.getCurrentDirectory(); |
|
1176 |
String dirName = JOptionPane.showInputDialog(fc, |
|
1177 |
newFolderDialogText, newFolderButtonText, |
|
1178 |
JOptionPane.PLAIN_MESSAGE); |
|
1179 |
||
1180 |
if (dirName != null) { |
|
1181 |
if (!currentDirectory.exists()) { |
|
1182 |
JOptionPane.showMessageDialog(fc, |
|
1183 |
MessageFormat.format(newFolderNoDirectoryErrorText, dirName), |
|
1184 |
newFolderNoDirectoryErrorTitleText, JOptionPane.ERROR_MESSAGE); |
|
1185 |
return; |
|
1186 |
} |
|
1187 |
||
1188 |
File newDir = fc.getFileSystemView().createFileObject |
|
1189 |
(currentDirectory, dirName); |
|
1190 |
if (newDir == null || !newDir.mkdir()) { |
|
1191 |
JOptionPane.showMessageDialog(fc, |
|
1192 |
newFolderErrorText + newFolderErrorSeparator + " \"" + |
|
1193 |
dirName + "\"", |
|
1194 |
newFolderErrorText, JOptionPane.ERROR_MESSAGE); |
|
1195 |
} |
|
1196 |
fc.rescanCurrentDirectory(); |
|
1197 |
} |
|
1198 |
} |
|
1199 |
} |
|
1200 |
||
1201 |
private class GTKApproveSelectionAction extends ApproveSelectionAction { |
|
1202 |
public void actionPerformed(ActionEvent e) { |
|
1203 |
if (isDirectorySelected()) { |
|
1204 |
File dir = getDirectory(); |
|
1205 |
try { |
|
1206 |
// Strip trailing ".." |
|
1207 |
if (dir != null) { |
|
1208 |
dir = ShellFolder.getNormalizedFile(dir); |
|
1209 |
} |
|
1210 |
} catch (IOException ex) { |
|
1211 |
// Ok, use f as is |
|
1212 |
} |
|
1213 |
if (getFileChooser().getCurrentDirectory().equals(dir)) { |
|
1214 |
directoryList.clearSelection(); |
|
1215 |
fileList.clearSelection(); |
|
1216 |
ListSelectionModel sm = fileList.getSelectionModel(); |
|
1217 |
if (sm instanceof DefaultListSelectionModel) { |
|
1218 |
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0); |
|
1219 |
((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0); |
|
1220 |
} |
|
1221 |
rescanCurrentDirectory(getFileChooser()); |
|
1222 |
return; |
|
1223 |
} |
|
1224 |
} |
|
1225 |
super.actionPerformed(e); |
|
1226 |
} |
|
1227 |
} |
|
1228 |
||
1229 |
/** |
|
1230 |
* Renames file |
|
1231 |
*/ |
|
1232 |
private class RenameFileAction extends AbstractAction { |
|
1233 |
protected RenameFileAction() { |
|
1234 |
super(FilePane.ACTION_EDIT_FILE_NAME); |
|
1235 |
} |
|
1236 |
public void actionPerformed(ActionEvent e) { |
|
1237 |
if (getFileName().equals("")) { |
|
1238 |
return; |
|
1239 |
} |
|
1240 |
JFileChooser fc = getFileChooser(); |
|
1241 |
File currentDirectory = fc.getCurrentDirectory(); |
|
1242 |
String newFileName = (String) JOptionPane.showInputDialog |
|
1243 |
(fc, new MessageFormat(renameFileDialogText).format |
|
1244 |
(new Object[] { getFileName() }), |
|
1245 |
renameFileButtonText, JOptionPane.PLAIN_MESSAGE, null, null, |
|
1246 |
getFileName()); |
|
1247 |
||
1248 |
if (newFileName != null) { |
|
1249 |
File oldFile = fc.getFileSystemView().createFileObject |
|
1250 |
(currentDirectory, getFileName()); |
|
1251 |
File newFile = fc.getFileSystemView().createFileObject |
|
1252 |
(currentDirectory, newFileName); |
|
1253 |
if (oldFile == null || newFile == null || |
|
1254 |
!getModel().renameFile(oldFile, newFile)) { |
|
1255 |
JOptionPane.showMessageDialog(fc, |
|
1256 |
new MessageFormat(renameFileErrorText). |
|
1257 |
format(new Object[] { getFileName(), newFileName}), |
|
1258 |
renameFileErrorTitle, JOptionPane.ERROR_MESSAGE); |
|
1259 |
} else { |
|
1260 |
setFileName(getFileChooser().getName(newFile)); |
|
1261 |
fc.rescanCurrentDirectory(); |
|
1262 |
} |
|
1263 |
} |
|
1264 |
} |
|
1265 |
} |
|
1266 |
||
1267 |
// |
|
1268 |
// Renderer for Filter ComboBox |
|
1269 |
// |
|
1270 |
protected FilterComboBoxRenderer createFilterComboBoxRenderer() { |
|
1271 |
return new FilterComboBoxRenderer(); |
|
1272 |
} |
|
1273 |
||
1274 |
/** |
|
1275 |
* Render different filters |
|
1276 |
*/ |
|
1277 |
public class FilterComboBoxRenderer extends DefaultListCellRenderer implements UIResource { |
|
1278 |
public String getName() { |
|
1279 |
// As SynthComboBoxRenderer's are asked for a size BEFORE they |
|
1280 |
// are parented getName is overriden to force the name to be |
|
1281 |
// ComboBox.renderer if it isn't set. If we didn't do this the |
|
1282 |
// wrong style could be used for size calculations. |
|
1283 |
String name = super.getName(); |
|
1284 |
if (name == null) { |
|
1285 |
return "ComboBox.renderer"; |
|
1286 |
} |
|
1287 |
return name; |
|
1288 |
} |
|
1289 |
||
1290 |
public Component getListCellRendererComponent(JList list, Object value, |
|
1291 |
int index, boolean isSelected, |
|
1292 |
boolean cellHasFocus) { |
|
1293 |
||
1294 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
1295 |
||
1296 |
setName("ComboBox.listRenderer"); |
|
1297 |
||
1298 |
if (value != null) { |
|
1299 |
if (value instanceof FileFilter) { |
|
1300 |
setText(((FileFilter) value).getDescription()); |
|
1301 |
} |
|
1302 |
} else { |
|
1303 |
setText(""); |
|
1304 |
} |
|
1305 |
||
1306 |
return this; |
|
1307 |
} |
|
1308 |
} |
|
1309 |
||
1310 |
// |
|
1311 |
// DataModel for Filter Combobox |
|
1312 |
// |
|
1313 |
protected FilterComboBoxModel createFilterComboBoxModel() { |
|
1314 |
return new FilterComboBoxModel(); |
|
1315 |
} |
|
1316 |
||
1317 |
/** |
|
1318 |
* Data model for filter combo-box. |
|
1319 |
*/ |
|
1320 |
protected class FilterComboBoxModel extends AbstractListModel |
|
1321 |
implements ComboBoxModel, PropertyChangeListener { |
|
1322 |
protected FileFilter[] filters; |
|
1323 |
||
1324 |
protected FilterComboBoxModel() { |
|
1325 |
super(); |
|
1326 |
filters = getFileChooser().getChoosableFileFilters(); |
|
1327 |
} |
|
1328 |
||
1329 |
public void propertyChange(PropertyChangeEvent e) { |
|
1330 |
String prop = e.getPropertyName(); |
|
1331 |
if (prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) { |
|
1332 |
filters = (FileFilter[]) e.getNewValue(); |
|
1333 |
fireContentsChanged(this, -1, -1); |
|
1334 |
} else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) { |
|
1335 |
fireContentsChanged(this, -1, -1); |
|
1336 |
} |
|
1337 |
} |
|
1338 |
||
1339 |
public void setSelectedItem(Object filter) { |
|
1340 |
if (filter != null) { |
|
1341 |
getFileChooser().setFileFilter((FileFilter) filter); |
|
1342 |
fireContentsChanged(this, -1, -1); |
|
1343 |
} |
|
1344 |
} |
|
1345 |
||
1346 |
public Object getSelectedItem() { |
|
1347 |
// Ensure that the current filter is in the list. |
|
1348 |
// NOTE: we shouldnt' have to do this, since JFileChooser adds |
|
1349 |
// the filter to the choosable filters list when the filter |
|
1350 |
// is set. Lets be paranoid just in case someone overrides |
|
1351 |
// setFileFilter in JFileChooser. |
|
1352 |
FileFilter currentFilter = getFileChooser().getFileFilter(); |
|
1353 |
boolean found = false; |
|
1354 |
if (currentFilter != null) { |
|
1355 |
for (int i = 0; i < filters.length; i++) { |
|
1356 |
if (filters[i] == currentFilter) { |
|
1357 |
found = true; |
|
1358 |
} |
|
1359 |
} |
|
1360 |
if (found == false) { |
|
1361 |
getFileChooser().addChoosableFileFilter(currentFilter); |
|
1362 |
} |
|
1363 |
} |
|
1364 |
return getFileChooser().getFileFilter(); |
|
1365 |
} |
|
1366 |
||
1367 |
public int getSize() { |
|
1368 |
if (filters != null) { |
|
1369 |
return filters.length; |
|
1370 |
} else { |
|
1371 |
return 0; |
|
1372 |
} |
|
1373 |
} |
|
1374 |
||
1375 |
public Object getElementAt(int index) { |
|
1376 |
if (index > getSize() - 1) { |
|
1377 |
// This shouldn't happen. Try to recover gracefully. |
|
1378 |
return getFileChooser().getFileFilter(); |
|
1379 |
} |
|
1380 |
if (filters != null) { |
|
1381 |
return filters[index]; |
|
1382 |
} else { |
|
1383 |
return null; |
|
1384 |
} |
|
1385 |
} |
|
1386 |
} |
|
1387 |
} |