| 1 |
/* |
| 2 |
* @(#)BasicFolderChooserUI.java 4/12/2006 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2006 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.plaf.basic; |
| 7 |
|
| 8 |
import com.jidesoft.dialog.ButtonPanel; |
| 9 |
import com.jidesoft.hints.FileIntelliHints; |
| 10 |
import com.jidesoft.plaf.FolderChooserUI; |
| 11 |
import com.jidesoft.swing.FolderChooser; |
| 12 |
import com.jidesoft.swing.JideBoxLayout; |
| 13 |
import com.jidesoft.swing.SelectAllUtils; |
| 14 |
import com.jidesoft.utils.SystemInfo; |
| 15 |
import sun.awt.shell.ShellFolder; |
| 16 |
|
| 17 |
import javax.swing.*; |
| 18 |
import javax.swing.event.TreeSelectionEvent; |
| 19 |
import javax.swing.event.TreeSelectionListener; |
| 20 |
import javax.swing.filechooser.FileSystemView; |
| 21 |
import javax.swing.plaf.ComponentUI; |
| 22 |
import javax.swing.plaf.basic.BasicFileChooserUI; |
| 23 |
import javax.swing.tree.MutableTreeNode; |
| 24 |
import javax.swing.tree.TreePath; |
| 25 |
import javax.swing.tree.TreeSelectionModel; |
| 26 |
import java.awt.*; |
| 27 |
import java.awt.event.*; |
| 28 |
import java.beans.PropertyChangeEvent; |
| 29 |
import java.beans.PropertyChangeListener; |
| 30 |
import java.io.File; |
| 31 |
import java.text.MessageFormat; |
| 32 |
import java.util.*; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
public class BasicFolderChooserUI extends BasicFileChooserUI implements FolderChooserUI { |
| 36 |
private FolderChooser _folderChooser; |
| 37 |
|
| 38 |
protected FolderToolBar _toolbar; |
| 39 |
protected JTree _fileSystemTree; |
| 40 |
protected JScrollPane _treeScrollPane; |
| 41 |
|
| 42 |
protected JButton _approveButton; |
| 43 |
protected JButton _cancelButton; |
| 44 |
protected JTextField _navigationTextField; |
| 45 |
protected JPanel _buttonPanel; |
| 46 |
protected JPanel _navigationPanel; |
| 47 |
|
| 48 |
private Action _approveSelectionAction = new ApproveSelectionAction(); |
| 49 |
public BasicFolderChooserUI.FolderChooserSelectionListener _selectionListener; |
| 50 |
private FolderToolBarListener _folderToolbarListener; |
| 51 |
|
| 52 |
public BasicFolderChooserUI(FolderChooser chooser) { |
| 53 |
super(chooser); |
| 54 |
BasicFileSystemTreeNode.clearCache(); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
public static ComponentUI createUI(JComponent c) { |
| 59 |
return new BasicFolderChooserUI((FolderChooser) c); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void installComponents(JFileChooser chooser) { |
| 64 |
_folderChooser = (FolderChooser) chooser; |
| 65 |
|
| 66 |
JPanel toolBarPanel = new JPanel(new BorderLayout(6, 6)); |
| 67 |
toolBarPanel.add(createToolbar(), BorderLayout.BEFORE_FIRST_LINE); |
| 68 |
toolBarPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
| 69 |
|
| 70 |
JPanel holdingPanel = new JPanel(); |
| 71 |
BorderLayout borderLayout = new BorderLayout(); |
| 72 |
borderLayout.setHgap(7); |
| 73 |
holdingPanel.setLayout(borderLayout); |
| 74 |
holdingPanel.setBorder(BorderFactory.createEmptyBorder(0, 6, 6, 6)); |
| 75 |
holdingPanel.add(_navigationPanel = createNavigationPanel(), BorderLayout.NORTH); |
| 76 |
holdingPanel.add(createFileSystemTreePanel(), BorderLayout.CENTER); |
| 77 |
holdingPanel.add(_buttonPanel = createButtonPanel(), BorderLayout.SOUTH); |
| 78 |
|
| 79 |
Component accessory = chooser.getAccessory(); |
| 80 |
if (accessory != null) { |
| 81 |
chooser.add(chooser.getAccessory(), BorderLayout.BEFORE_FIRST_LINE); |
| 82 |
} |
| 83 |
|
| 84 |
chooser.setLayout(new JideBoxLayout(chooser, JideBoxLayout.Y_AXIS)); |
| 85 |
chooser.add(toolBarPanel, JideBoxLayout.FIX); |
| 86 |
chooser.add(holdingPanel, JideBoxLayout.VARY); |
| 87 |
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| 88 |
|
| 89 |
if (_folderChooser.isNavigationFieldVisible()) { |
| 90 |
setNavigationFieldVisible(true); |
| 91 |
} |
| 92 |
else { |
| 93 |
setNavigationFieldVisible(false); |
| 94 |
} |
| 95 |
|
| 96 |
updateView(chooser); |
| 97 |
|
| 98 |
Runnable runnable = new Runnable() { |
| 99 |
public void run() { |
| 100 |
_fileSystemTree.requestFocusInWindow(); |
| 101 |
} |
| 102 |
}; |
| 103 |
SwingUtilities.invokeLater(runnable); |
| 104 |
|
| 105 |
/* |
| 106 |
* _folderChooser ultimately extends JComponent (and not JDialog) and thus has no root pane. |
| 107 |
* As such, we need to do the following to set the default button. |
| 108 |
*/ |
| 109 |
_folderChooser.addHierarchyListener(new HierarchyListener() { |
| 110 |
public void hierarchyChanged(HierarchyEvent e) { |
| 111 |
if (_folderChooser.getRootPane() != null) { |
| 112 |
_folderChooser.getRootPane().setDefaultButton(_approveButton); |
| 113 |
} |
| 114 |
} |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return the default focus component inside the FolderChooser. |
| 120 |
* <p/> |
| 121 |
* By default, it is the file system tree. |
| 122 |
* |
| 123 |
* @return the default focus component. |
| 124 |
*/ |
| 125 |
public Component getDefaultFocusComponent() { |
| 126 |
return _fileSystemTree; |
| 127 |
} |
| 128 |
|
| 129 |
protected JPanel createButtonPanel() { |
| 130 |
_approveButton = new JButton(); |
| 131 |
_approveButton.setAction(getApproveSelectionAction()); |
| 132 |
|
| 133 |
_cancelButton = new JButton(); |
| 134 |
_cancelButton.addActionListener(getCancelSelectionAction()); |
| 135 |
|
| 136 |
ButtonPanel buttonPanel = new ButtonPanel(); |
| 137 |
buttonPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 0, 0)); |
| 138 |
buttonPanel.addButton(_approveButton, ButtonPanel.AFFIRMATIVE_BUTTON); |
| 139 |
buttonPanel.addButton(_cancelButton, ButtonPanel.CANCEL_BUTTON); |
| 140 |
return buttonPanel; |
| 141 |
} |
| 142 |
|
| 143 |
protected JPanel createNavigationPanel() { |
| 144 |
NavigationTextFieldListener navigationTextFieldListener = new NavigationTextFieldListener(); |
| 145 |
_navigationTextField = new JTextField(24); |
| 146 |
SelectAllUtils.install(_navigationTextField); |
| 147 |
FileIntelliHints fileIntelliHints = new FileIntelliHints(_navigationTextField); |
| 148 |
fileIntelliHints.setFolderOnly(true); |
| 149 |
fileIntelliHints.setShowFullPath(false); |
| 150 |
fileIntelliHints.setFollowCaret(true); |
| 151 |
_navigationTextField.addActionListener(navigationTextFieldListener); |
| 152 |
|
| 153 |
JPanel panel = new JPanel(); |
| 154 |
BorderLayout borderLayout = new BorderLayout(); |
| 155 |
panel.setLayout(borderLayout); |
| 156 |
panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 6, 0)); |
| 157 |
panel.add(_navigationTextField, BorderLayout.CENTER); |
| 158 |
|
| 159 |
return panel; |
| 160 |
} |
| 161 |
|
| 162 |
public void setNavigationFieldVisible(boolean navigationFieldVisible) { |
| 163 |
_navigationPanel.setVisible(navigationFieldVisible); |
| 164 |
} |
| 165 |
|
| 166 |
public class NavigationTextFieldListener implements ActionListener { |
| 167 |
public void actionPerformed(ActionEvent e) { |
| 168 |
|
| 169 |
String text = _navigationTextField.getText(); |
| 170 |
if (text == null || text.equals("")) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* If the node is already selected, we trigger the "open" button. That is... |
| 176 |
* |
| 177 |
* When a user enters a folder into the text field and hits the ENTER key, the folder will become |
| 178 |
* selected in the folder tree. The focus remains within the text field, allowing the user to press |
| 179 |
* the ENTER key one more time. This will allow the user to select the specified folder. |
| 180 |
* |
| 181 |
* Put briefly: The first ENTER will select the node. The second ENTER key, if the tree node is |
| 182 |
* already selected, we trigger the "open" button. |
| 183 |
*/ |
| 184 |
TreePath treePath = _fileSystemTree.getSelectionPath(); |
| 185 |
if (treePath != null) { |
| 186 |
if (text.equals("" + treePath.getLastPathComponent())) { |
| 187 |
_approveButton.doClick(200); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
File file = new File(text); |
| 192 |
if (file.exists()) { |
| 193 |
ensureFileIsVisible(file, true); |
| 194 |
_folderChooser.setSelectedFolder(file); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public void rescanCurrentDirectory(JFileChooser fc) { |
| 201 |
super.rescanCurrentDirectory(fc); |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public void ensureFileIsVisible(JFileChooser fc, File f) { |
| 206 |
super.ensureFileIsVisible(fc, f); |
| 207 |
ensureFileIsVisible(f, true); |
| 208 |
} |
| 209 |
|
| 210 |
protected JComponent createToolbar() { |
| 211 |
_toolbar = new FolderToolBar(true, _folderChooser.getRecentList()); |
| 212 |
_folderToolbarListener = new FolderToolBarListener() { |
| 213 |
// ------------------------------------------------------------------------------ |
| 214 |
// Implementation of FolderToolBarListener |
| 215 |
// ------------------------------------------------------------------------------ |
| 216 |
|
| 217 |
public void deleteFolderButtonClicked() { |
| 218 |
// make sure user really wants to do this |
| 219 |
String text; |
| 220 |
TreePath path = _fileSystemTree.getSelectionPaths()[0]; |
| 221 |
List selection = getSelectedFolders(new TreePath[]{path}); |
| 222 |
|
| 223 |
final ResourceBundle resourceBundle = FolderChooserResource.getResourceBundle(Locale.getDefault()); |
| 224 |
if (selection.size() > 1) { |
| 225 |
text = MessageFormat.format( |
| 226 |
resourceBundle.getString("FolderChooser.delete.message2"), selection.size()); |
| 227 |
} |
| 228 |
else { |
| 229 |
text = resourceBundle.getString("FolderChooser.delete.message1"); |
| 230 |
} |
| 231 |
final String title = resourceBundle.getString("FolderChooser.delete.title"); |
| 232 |
|
| 233 |
int result = JOptionPane.showConfirmDialog(_folderChooser, text, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); |
| 234 |
if (result == JOptionPane.OK_OPTION) { |
| 235 |
TreePath parentPath = path.getParentPath(); |
| 236 |
Object parentObject = parentPath.getLastPathComponent(); |
| 237 |
Object deletedObject = path.getLastPathComponent(); |
| 238 |
int index = _fileSystemTree.getModel().getIndexOfChild(parentObject, deletedObject); |
| 239 |
for (Object s : selection) { |
| 240 |
File f = (File) s; |
| 241 |
recursiveDelete(f); |
| 242 |
} |
| 243 |
((BasicFileSystemTreeModel) _fileSystemTree.getModel()).removePath(path, index, deletedObject); |
| 244 |
TreePath pathToSelect = parentPath; |
| 245 |
if (index >= ((MutableTreeNode) parentObject).getChildCount()) { |
| 246 |
index = ((MutableTreeNode) parentObject).getChildCount() - 1; |
| 247 |
} |
| 248 |
if (index > 0) { |
| 249 |
pathToSelect = parentPath.pathByAddingChild(((MutableTreeNode) parentObject).getChildAt(index)); |
| 250 |
} |
| 251 |
_fileSystemTree.setSelectionPath(pathToSelect); |
| 252 |
_fileSystemTree.scrollPathToVisible(pathToSelect); |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Recursively deletes a file/directory. |
| 259 |
* |
| 260 |
* @param file The file/folder to delete |
| 261 |
* @return <code>true</code> only if the file and all children were successfully deleted. |
| 262 |
*/ |
| 263 |
public final boolean recursiveDelete(File file) { |
| 264 |
if (isFileSystem(file)) { |
| 265 |
if (file.isDirectory()) { |
| 266 |
// delete all children first |
| 267 |
File[] children = FileSystemView.getFileSystemView().getFiles(file, false); |
| 268 |
for (File f : children) { |
| 269 |
if (!recursiveDelete(f)) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
// delete this file. |
| 274 |
return file.delete(); |
| 275 |
} |
| 276 |
else { |
| 277 |
return file.delete(); |
| 278 |
} |
| 279 |
} |
| 280 |
else { |
| 281 |
return false; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public void newFolderButtonClicked() { |
| 286 |
// get the selected folder |
| 287 |
TreePath[] paths = _fileSystemTree.getSelectionPaths(); |
| 288 |
List selection = getSelectedFolders(paths); |
| 289 |
if (selection.size() > 1 || selection.size() == 0) |
| 290 |
return; // should never happen |
| 291 |
|
| 292 |
File parent = (File) selection.get(0); |
| 293 |
|
| 294 |
final ResourceBundle resourceBundle = FolderChooserResource.getResourceBundle(Locale.getDefault()); |
| 295 |
String folderName = JOptionPane.showInputDialog(_folderChooser, resourceBundle.getString("FolderChooser.new.folderName"), |
| 296 |
resourceBundle.getString("FolderChooser.new.title"), JOptionPane.OK_CANCEL_OPTION | JOptionPane.QUESTION_MESSAGE); |
| 297 |
|
| 298 |
if (folderName != null && folderName.trim().length() > 0) { |
| 299 |
folderName = eraseBlankInTheEnd(folderName); |
| 300 |
File newFolder = new File(parent, folderName); |
| 301 |
boolean success = newFolder.mkdir(); |
| 302 |
|
| 303 |
TreePath parentPath = paths[0]; |
| 304 |
boolean isExpanded = _fileSystemTree.isExpanded(parentPath); |
| 305 |
if (!isExpanded) { // expand it first |
| 306 |
_fileSystemTree.expandPath(parentPath); |
| 307 |
} |
| 308 |
|
| 309 |
LazyMutableTreeNode parentTreeNode = (LazyMutableTreeNode) parentPath.getLastPathComponent(); |
| 310 |
BasicFileSystemTreeNode child = BasicFileSystemTreeNode.createFileSystemTreeNode(newFolder, _folderChooser); |
| 311 |
// child.setParent(parentTreeNode); |
| 312 |
if (success) { |
| 313 |
parentTreeNode.clear(); |
| 314 |
int insertIndex = _fileSystemTree.getModel().getIndexOfChild(parentTreeNode, child); |
| 315 |
if (insertIndex != -1) { |
| 316 |
// ((BasicFileSystemTreeModel) _fileSystemTree.getModel()).insertNodeInto(child, parentTreeNode, insertIndex); |
| 317 |
((BasicFileSystemTreeModel) _fileSystemTree.getModel()).nodeStructureChanged(parentTreeNode); |
| 318 |
// ((BasicFileSystemTreeModel) _fileSystemTree.getModel()).addPath(parentPath, insertIndex, child); |
| 319 |
} |
| 320 |
} |
| 321 |
TreePath newPath = parentPath.pathByAddingChild(child); |
| 322 |
_fileSystemTree.setSelectionPath(newPath); |
| 323 |
_fileSystemTree.scrollPathToVisible(newPath); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
private String eraseBlankInTheEnd(String folderName) { |
| 328 |
int i = folderName.length() - 1; |
| 329 |
for (; i >= 0; i--) { |
| 330 |
char c = folderName.charAt(i); |
| 331 |
if (c != ' ' && c != '\t') { |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
if (i < 0) { |
| 336 |
return null; |
| 337 |
} |
| 338 |
return folderName.substring(0, i + 1); |
| 339 |
} |
| 340 |
|
| 341 |
public void myDocumentsButtonClicked() { |
| 342 |
File myDocuments = FileSystemView.getFileSystemView().getDefaultDirectory(); |
| 343 |
ensureFileIsVisible(myDocuments, true); |
| 344 |
} |
| 345 |
|
| 346 |
public void desktopButtonClicked() { |
| 347 |
File desktop = FileSystemView.getFileSystemView().getHomeDirectory(); |
| 348 |
ensureFileIsVisible(desktop, true); |
| 349 |
} |
| 350 |
|
| 351 |
public void refreshButtonClicked() { |
| 352 |
File folder = _folderChooser.getSelectedFolder(); |
| 353 |
_folderChooser.updateUI(); |
| 354 |
while (folder != null) { |
| 355 |
if (folder.exists()) { |
| 356 |
_folderChooser.getUI().ensureFileIsVisible(_folderChooser, folder); |
| 357 |
break; |
| 358 |
} |
| 359 |
else { |
| 360 |
folder = folder.getParentFile(); |
| 361 |
if (folder == null) { |
| 362 |
break; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
public void recentFolderSelected(final File file) { |
| 369 |
new Thread(new Runnable() { |
| 370 |
public void run() { |
| 371 |
setWaitCursor(true); |
| 372 |
try { |
| 373 |
ensureFileIsVisible(file, true); |
| 374 |
} |
| 375 |
finally { |
| 376 |
setWaitCursor(false); |
| 377 |
} |
| 378 |
} |
| 379 |
}).start(); |
| 380 |
} |
| 381 |
|
| 382 |
private Cursor m_oldCursor; |
| 383 |
|
| 384 |
private void setWaitCursor(boolean isWait) { |
| 385 |
Window parentWindow = SwingUtilities.getWindowAncestor(_folderChooser); |
| 386 |
if (isWait) { |
| 387 |
Cursor hourglassCursor = new Cursor(Cursor.WAIT_CURSOR); |
| 388 |
m_oldCursor = parentWindow.getCursor(); |
| 389 |
parentWindow.setCursor(hourglassCursor); |
| 390 |
} |
| 391 |
else { |
| 392 |
if (m_oldCursor != null) { |
| 393 |
parentWindow.setCursor(m_oldCursor); |
| 394 |
m_oldCursor = null; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
public List getSelectedFolders() { |
| 401 |
TreePath[] paths = _fileSystemTree.getSelectionPaths(); |
| 402 |
return getSelectedFolders(paths); |
| 403 |
} |
| 404 |
|
| 405 |
public List getSelectedFolders(TreePath[] paths) { |
| 406 |
if (paths == null || paths.length == 0) |
| 407 |
return new ArrayList(); |
| 408 |
|
| 409 |
List<File> folders = new ArrayList<File>(paths.length); |
| 410 |
for (TreePath path : paths) { |
| 411 |
BasicFileSystemTreeNode f = (BasicFileSystemTreeNode) path.getLastPathComponent(); |
| 412 |
folders.add(f.getFile()); |
| 413 |
} |
| 414 |
return folders; |
| 415 |
} |
| 416 |
|
| 417 |
}; |
| 418 |
_toolbar.addListener(_folderToolbarListener); |
| 419 |
updateToolbarButtons(); |
| 420 |
return _toolbar; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Updates toolbar button status depending on current selection status |
| 425 |
*/ |
| 426 |
protected void updateToolbarButtons() { |
| 427 |
// delete folder button |
| 428 |
TreePath[] selectedFiles = _fileSystemTree == null ? new TreePath[0] : _fileSystemTree.getSelectionPaths(); |
| 429 |
// System.out.println("selectedFiles.length = " + selectedFiles == null ? 0 : selectedFiles.length); |
| 430 |
if (selectedFiles != null && selectedFiles.length > 0) { |
| 431 |
_toolbar.enableDelete(); |
| 432 |
} |
| 433 |
else { |
| 434 |
_toolbar.disableDelete(); |
| 435 |
} |
| 436 |
|
| 437 |
// new folder button (only enable if exactly one folder selected |
| 438 |
if (selectedFiles != null && selectedFiles.length == 1) { |
| 439 |
_toolbar.enableNewFolder(); |
| 440 |
} |
| 441 |
else { |
| 442 |
_toolbar.disableNewFolder(); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
private JComponent createFileSystemTreePanel() { |
| 447 |
JPanel panel = new JPanel(new BorderLayout()); |
| 448 |
_fileSystemTree = new BasicFileSystemTree(_folderChooser); |
| 449 |
updateMultiSelectionEnabled(); |
| 450 |
_treeScrollPane = new JScrollPane(_fileSystemTree); |
| 451 |
panel.add(_treeScrollPane); |
| 452 |
return panel; |
| 453 |
} |
| 454 |
|
| 455 |
private void updateMultiSelectionEnabled() { |
| 456 |
if (_folderChooser.isMultiSelectionEnabled()) { |
| 457 |
_fileSystemTree.getSelectionModel().setSelectionMode( |
| 458 |
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); |
| 459 |
} |
| 460 |
else { |
| 461 |
_fileSystemTree.getSelectionModel().setSelectionMode( |
| 462 |
TreeSelectionModel.SINGLE_TREE_SELECTION); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
@Override |
| 467 |
public void uninstallComponents(JFileChooser chooser) { |
| 468 |
chooser.remove(_treeScrollPane); |
| 469 |
chooser.remove(_buttonPanel); |
| 470 |
} |
| 471 |
|
| 472 |
@Override |
| 473 |
protected void installListeners(JFileChooser fc) { |
| 474 |
super.installListeners(fc); |
| 475 |
_selectionListener = new FolderChooserSelectionListener(); |
| 476 |
_fileSystemTree.addTreeSelectionListener(_selectionListener); |
| 477 |
_fileSystemTree.registerKeyboardAction(new AbstractAction() { |
| 478 |
private static final long serialVersionUID = -2758050378982771174L; |
| 479 |
|
| 480 |
public void actionPerformed(ActionEvent e) { |
| 481 |
if (_folderToolbarListener != null) { |
| 482 |
_folderToolbarListener.refreshButtonClicked(); |
| 483 |
} |
| 484 |
} |
| 485 |
}, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), JComponent.WHEN_FOCUSED); |
| 486 |
} |
| 487 |
|
| 488 |
@Override |
| 489 |
protected void uninstallListeners(JFileChooser fc) { |
| 490 |
super.uninstallListeners(fc); |
| 491 |
_fileSystemTree.removeTreeSelectionListener(_selectionListener); |
| 492 |
} |
| 493 |
|
| 494 |
@Override |
| 495 |
public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) { |
| 496 |
return new FolderChooserPropertyChangeListener(); |
| 497 |
} |
| 498 |
|
| 499 |
private void updateView(JFileChooser chooser) { |
| 500 |
if (chooser.getApproveButtonText() != null) { |
| 501 |
_approveButton.setText(chooser.getApproveButtonText()); |
| 502 |
_approveButton.setMnemonic(chooser.getApproveButtonMnemonic()); |
| 503 |
} |
| 504 |
else { |
| 505 |
if (JFileChooser.OPEN_DIALOG == chooser.getDialogType()) { |
| 506 |
_approveButton.setText(openButtonText); |
| 507 |
_approveButton.setToolTipText(openButtonToolTipText); |
| 508 |
_approveButton.setMnemonic(openButtonMnemonic); |
| 509 |
} |
| 510 |
else { |
| 511 |
_approveButton.setText(saveButtonText); |
| 512 |
_approveButton.setToolTipText(saveButtonToolTipText); |
| 513 |
_approveButton.setMnemonic(saveButtonMnemonic); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
_cancelButton.setText(cancelButtonText); |
| 518 |
_cancelButton.setMnemonic(cancelButtonMnemonic); |
| 519 |
|
| 520 |
_buttonPanel.setVisible(chooser.getControlButtonsAreShown()); |
| 521 |
if (_toolbar != null) { |
| 522 |
Component[] components = _toolbar.getComponents(); |
| 523 |
for (Component component : components) { |
| 524 |
if (component instanceof JButton) { |
| 525 |
String name = component.getName(); |
| 526 |
int buttons = _folderChooser.getAvailableButtons(); |
| 527 |
boolean visible = _toolbar.isButtonVisible(name, buttons); |
| 528 |
component.setVisible(visible); |
| 529 |
} |
| 530 |
else if (component instanceof JComboBox || component instanceof JLabel) { |
| 531 |
component.setVisible(_folderChooser.isRecentListVisible()); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Checks if <code>f</code> represents a real directory or file as opposed to a special folder such as |
| 539 |
* <code>"Desktop"</code>. Used by UI classes to decide if a folder is selectable when doing directory choosing. |
| 540 |
* |
| 541 |
* @param f a <code>File</code> object |
| 542 |
* @return <code>true</code> if <code>f</code> is a real file or directory. |
| 543 |
*/ |
| 544 |
public static boolean isFileSystem(File f) { |
| 545 |
if (f instanceof ShellFolder) { |
| 546 |
ShellFolder sf = (ShellFolder) f; |
| 547 |
// Shortcuts to directories are treated as not being file system objects, |
| 548 |
// so that they are never returned by JFileChooser. |
| 549 |
return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory()); |
| 550 |
} |
| 551 |
else { |
| 552 |
return true; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
private TreePath getTreePathForFile(File file) { |
| 557 |
if (!file.isDirectory()) { |
| 558 |
return null; |
| 559 |
} |
| 560 |
Stack<File> stack = new Stack<File>(); |
| 561 |
List<Object> list = new ArrayList<Object>(); |
| 562 |
list.add(_fileSystemTree.getModel().getRoot()); |
| 563 |
FileSystemView view = _folderChooser.getFileSystemView(); |
| 564 |
File[] alternativeRoots = null; |
| 565 |
File root = null; |
| 566 |
if (SystemInfo.isWindows()) { |
| 567 |
File[] roots = view.getRoots(); |
| 568 |
root = roots[0]; |
| 569 |
if (isFileSystem(root) && root.isDirectory()) { |
| 570 |
alternativeRoots = root.listFiles(); |
| 571 |
} |
| 572 |
} |
| 573 |
File parent = file; |
| 574 |
outloop: |
| 575 |
do { |
| 576 |
stack.push(parent); |
| 577 |
if (alternativeRoots != null) { |
| 578 |
for (File r : alternativeRoots) { |
| 579 |
if (r.equals(parent)) { |
| 580 |
stack.push(root); |
| 581 |
break outloop; |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
parent = _folderChooser.getFileSystemView().getParentDirectory(parent); |
| 586 |
} |
| 587 |
while (parent != null); |
| 588 |
|
| 589 |
while (!stack.empty()) { |
| 590 |
list.add(BasicFileSystemTreeNode.createFileSystemTreeNode(stack.pop(), _folderChooser)); |
| 591 |
} |
| 592 |
return new TreePath(list.toArray()); |
| 593 |
} |
| 594 |
|
| 595 |
private void ensureFileIsVisible(File file, boolean scroll) { |
| 596 |
final TreePath path = file == null ? new TreePath(_fileSystemTree.getModel().getRoot()) : getTreePathForFile(file); |
| 597 |
if (path != null) { |
| 598 |
_fileSystemTree.setSelectionPath(path); |
| 599 |
_fileSystemTree.expandPath(path); |
| 600 |
if (scroll) { |
| 601 |
Runnable runnable = new Runnable() { |
| 602 |
public void run() { |
| 603 |
_fileSystemTree.scrollPathToVisible(path); |
| 604 |
} |
| 605 |
}; |
| 606 |
SwingUtilities.invokeLater(runnable); |
| 607 |
} |
| 608 |
} |
| 609 |
// getApproveSelectionAction().setEnabled(_fileSystemTree.getSelectionCount() > 0); |
| 610 |
} |
| 611 |
|
| 612 |
private class FolderChooserPropertyChangeListener implements PropertyChangeListener { |
| 613 |
public void propertyChange(PropertyChangeEvent evt) { |
| 614 |
if (FolderChooser.PROPERTY_RECENTLIST.equals(evt.getPropertyName())) { |
| 615 |
_toolbar.setRecentList((List) evt.getNewValue()); |
| 616 |
} |
| 617 |
else if (JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
| 618 |
updateView(_folderChooser); |
| 619 |
} |
| 620 |
else if (JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
| 621 |
updateView(_folderChooser); |
| 622 |
} |
| 623 |
else if (JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
| 624 |
updateMultiSelectionEnabled(); |
| 625 |
} |
| 626 |
else if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
| 627 |
ensureFileIsVisible(_folderChooser.getCurrentDirectory(), true); |
| 628 |
} |
| 629 |
else if (JFileChooser.ACCESSORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
| 630 |
Component oldValue = (Component) evt.getOldValue(); |
| 631 |
Component newValue = (Component) evt.getNewValue(); |
| 632 |
if (oldValue != null) { |
| 633 |
_folderChooser.remove(oldValue); |
| 634 |
} |
| 635 |
if (newValue != null) { |
| 636 |
_folderChooser.add(newValue, BorderLayout.BEFORE_FIRST_LINE); |
| 637 |
} |
| 638 |
_folderChooser.revalidate(); |
| 639 |
_folderChooser.repaint(); |
| 640 |
} |
| 641 |
else if (JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
| 642 |
updateView(_folderChooser); |
| 643 |
} |
| 644 |
else if (FolderChooser.PROPERTY_NAVIGATION_FIELD_VISIBLE.equals(evt.getPropertyName())) { |
| 645 |
if (_folderChooser.isNavigationFieldVisible()) { |
| 646 |
setNavigationFieldVisible(true); |
| 647 |
} |
| 648 |
else { |
| 649 |
setNavigationFieldVisible(false); |
| 650 |
} |
| 651 |
} |
| 652 |
else if (FolderChooser.PROPERTY_AVAILABLE_BUTTONS.equals(evt.getPropertyName())) { |
| 653 |
Component[] components = _toolbar.getComponents(); |
| 654 |
for (Component component : components) { |
| 655 |
if (component instanceof JButton) { |
| 656 |
String name = component.getName(); |
| 657 |
int buttons = _folderChooser.getAvailableButtons(); |
| 658 |
boolean visible = _toolbar.isButtonVisible(name, buttons); |
| 659 |
component.setVisible(visible); |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
else if (FolderChooser.PROPERTY_RECENTLIST_VISIBLE.equals(evt.getPropertyName())) { |
| 664 |
Component[] components = _toolbar.getComponents(); |
| 665 |
for (Component component : components) { |
| 666 |
if (component instanceof JComboBox || component instanceof JLabel) { |
| 667 |
component.setVisible(_folderChooser.isRecentListVisible()); |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
private class FolderChooserSelectionListener implements TreeSelectionListener { |
| 675 |
public void valueChanged(TreeSelectionEvent e) { |
| 676 |
getApproveSelectionAction().setEnabled(_fileSystemTree.getSelectionCount() > 0); |
| 677 |
if (_toolbar != null) { |
| 678 |
updateToolbarButtons(); |
| 679 |
} |
| 680 |
|
| 681 |
/* |
| 682 |
* Added on 05/11/2008 in response to http://www.jidesoft.com/forum/viewtopic.php?p=26932#26932 |
| 683 |
* |
| 684 |
* The addition below ensures Component#firePropertyChange is called, and thus fires the |
| 685 |
* appropriate 'bound property event' on all folder selection changes. |
| 686 |
* |
| 687 |
* @see FolderChooser#setSelectedFolder(folder) |
| 688 |
*/ |
| 689 |
|
| 690 |
if (_fileSystemTree.getSelectionCount() > 0) { |
| 691 |
TreePath path = e.getNewLeadSelectionPath(); |
| 692 |
if (path != null) { |
| 693 |
String folderPath = (path.getLastPathComponent()).toString(); |
| 694 |
File folder = new File(folderPath); |
| 695 |
_folderChooser.setSelectedFolder(folder); |
| 696 |
|
| 697 |
/* |
| 698 |
* Ensure the _navigationTextField is in sync with the folder tree. That is, each time a folder is |
| 699 |
* selected in the tree, update the text field to reflect this. |
| 700 |
*/ |
| 701 |
TreePath treePath = _fileSystemTree.getSelectionPath(); |
| 702 |
if (treePath != null) { |
| 703 |
_navigationTextField.setText("" + treePath.getLastPathComponent()); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
* End of addition. |
| 710 |
* |
| 711 |
* Added on 05/11/2008 in response to http://www.jidesoft.com/forum/viewtopic.php?p=26932#26932 |
| 712 |
*/ |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
private void setSelectedFiles() { |
| 717 |
TreePath[] selectedPaths = _fileSystemTree.getSelectionPaths(); |
| 718 |
if (selectedPaths == null || selectedPaths.length == 0) { |
| 719 |
_folderChooser.setSelectedFile(null); |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
List<File> files = new ArrayList<File>(); |
| 724 |
for (int i = 0, c = selectedPaths.length; i < c; i++) { |
| 725 |
File f = ((BasicFileSystemTreeNode) selectedPaths[i].getLastPathComponent()).getFile(); |
| 726 |
files.add(f); |
| 727 |
} |
| 728 |
|
| 729 |
_folderChooser.setSelectedFiles(files.toArray(new File[files.size()])); |
| 730 |
} |
| 731 |
|
| 732 |
@Override |
| 733 |
public Action getApproveSelectionAction() { |
| 734 |
return _approveSelectionAction; |
| 735 |
} |
| 736 |
|
| 737 |
private class ApproveSelectionAction extends AbstractAction { |
| 738 |
private static final long serialVersionUID = -3465282473768757260L; |
| 739 |
|
| 740 |
public ApproveSelectionAction() { |
| 741 |
setEnabled(false); |
| 742 |
} |
| 743 |
|
| 744 |
public void actionPerformed(ActionEvent e) { |
| 745 |
setSelectedFiles(); |
| 746 |
_folderChooser.approveSelection(); |
| 747 |
} |
| 748 |
} |
| 749 |
} |