| 1 7 8 package javax.swing; 9 10 import javax.swing.event.*; 11 import javax.swing.filechooser.*; 12 import javax.swing.plaf.FileChooserUI ; 13 14 import javax.accessibility.*; 15 16 import java.io.File ; 17 import java.io.ObjectOutputStream ; 18 import java.io.ObjectInputStream ; 19 import java.io.IOException ; 20 21 import java.util.Vector ; 22 import java.awt.AWTEvent ; 23 import java.awt.Component ; 24 import java.awt.Container ; 25 import java.awt.BorderLayout ; 26 import java.awt.Window ; 27 import java.awt.Dialog ; 28 import java.awt.Frame ; 29 import java.awt.GraphicsEnvironment ; 30 import java.awt.HeadlessException ; 31 import java.awt.EventQueue ; 32 import java.awt.Toolkit ; 33 import java.awt.event.*; 34 import java.beans.PropertyChangeListener ; 35 import java.beans.PropertyChangeEvent ; 36 import java.lang.ref.WeakReference ; 37 38 74 public class JFileChooser extends JComponent implements Accessible { 75 76 80 private static final String uiClassID = "FileChooserUI"; 81 82 86 90 public static final int OPEN_DIALOG = 0; 91 92 96 public static final int SAVE_DIALOG = 1; 97 98 102 public static final int CUSTOM_DIALOG = 2; 103 104 105 109 112 public static final int CANCEL_OPTION = 1; 113 114 117 public static final int APPROVE_OPTION = 0; 118 119 122 public static final int ERROR_OPTION = -1; 123 124 125 129 130 131 public static final int FILES_ONLY = 0; 132 133 134 public static final int DIRECTORIES_ONLY = 1; 135 136 137 public static final int FILES_AND_DIRECTORIES = 2; 138 139 140 public static final String CANCEL_SELECTION = "CancelSelection"; 141 142 146 public static final String APPROVE_SELECTION = "ApproveSelection"; 147 148 149 public static final String APPROVE_BUTTON_TEXT_CHANGED_PROPERTY = "ApproveButtonTextChangedProperty"; 150 151 155 public static final String APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY = "ApproveButtonToolTipTextChangedProperty"; 156 157 158 public static final String APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY = "ApproveButtonMnemonicChangedProperty"; 159 160 161 public static final String CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY = "ControlButtonsAreShownChangedProperty"; 162 163 164 public static final String DIRECTORY_CHANGED_PROPERTY = "directoryChanged"; 165 166 167 public static final String SELECTED_FILE_CHANGED_PROPERTY = "SelectedFileChangedProperty"; 168 169 170 public static final String SELECTED_FILES_CHANGED_PROPERTY = "SelectedFilesChangedProperty"; 171 172 173 public static final String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY = "MultiSelectionEnabledChangedProperty"; 174 175 179 public static final String FILE_SYSTEM_VIEW_CHANGED_PROPERTY = "FileSystemViewChanged"; 180 181 185 public static final String FILE_VIEW_CHANGED_PROPERTY = "fileViewChanged"; 186 187 188 public static final String FILE_HIDING_CHANGED_PROPERTY = "FileHidingChanged"; 189 190 191 public static final String FILE_FILTER_CHANGED_PROPERTY = "fileFilterChanged"; 192 193 197 public static final String FILE_SELECTION_MODE_CHANGED_PROPERTY = "fileSelectionChanged"; 198 199 203 public static final String ACCESSORY_CHANGED_PROPERTY = "AccessoryChangedProperty"; 204 205 208 public static final String ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY = "acceptAllFileFilterUsedChanged"; 209 210 211 public static final String DIALOG_TITLE_CHANGED_PROPERTY = "DialogTitleChangedProperty"; 212 213 217 public static final String DIALOG_TYPE_CHANGED_PROPERTY = "DialogTypeChangedProperty"; 218 219 223 public static final String CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY = "ChoosableFileFilterChangedProperty"; 224 225 229 private String dialogTitle = null; 230 private String approveButtonText = null; 231 private String approveButtonToolTipText = null; 232 private int approveButtonMnemonic = 0; 233 234 private ActionListener actionListener = null; 235 236 private Vector filters = new Vector (5); 237 private JDialog dialog = null; 238 private int dialogType = OPEN_DIALOG; 239 private int returnValue = ERROR_OPTION; 240 private JComponent accessory = null; 241 242 private FileView fileView = null; 243 244 private transient FileView uiFileView = null; 247 248 private boolean controlsShown = true; 249 250 private boolean useFileHiding = true; 251 private static final String SHOW_HIDDEN_PROP = "awt.file.showHiddenFiles"; 252 253 private PropertyChangeListener showFilesListener = null; 257 258 private int fileSelectionMode = FILES_ONLY; 259 260 private boolean multiSelectionEnabled = false; 261 262 private boolean useAcceptAllFileFilter = true; 263 264 private boolean dragEnabled = false; 265 266 private FileFilter fileFilter = null; 267 268 private FileSystemView fileSystemView = null; 269 270 private File currentDirectory = null; 271 private File selectedFile = null; 272 private File [] selectedFiles; 273 274 278 284 public JFileChooser() { 285 this((File ) null, (FileSystemView) null); 286 } 287 288 299 public JFileChooser(String currentDirectoryPath) { 300 this(currentDirectoryPath, (FileSystemView) null); 301 } 302 303 314 public JFileChooser(File currentDirectory) { 315 this(currentDirectory, (FileSystemView) null); 316 } 317 318 322 public JFileChooser(FileSystemView fsv) { 323 this((File ) null, fsv); 324 } 325 326 327 331 public JFileChooser(File currentDirectory, FileSystemView fsv) { 332 setup(fsv); 333 setCurrentDirectory(currentDirectory); 334 } 335 336 340 public JFileChooser(String currentDirectoryPath, FileSystemView fsv) { 341 setup(fsv); 342 if(currentDirectoryPath == null) { 343 setCurrentDirectory(null); 344 } else { 345 setCurrentDirectory(fileSystemView.createFileObject(currentDirectoryPath)); 346 } 347 } 348 349 352 protected void setup(FileSystemView view) { 353 Toolkit tk = Toolkit.getDefaultToolkit(); 355 Object showHiddenProperty = tk.getDesktopProperty(SHOW_HIDDEN_PROP); 356 if (showHiddenProperty instanceof Boolean ) { 357 useFileHiding = !((Boolean )showHiddenProperty).booleanValue(); 358 showFilesListener = new WeakPCL(this); 359 tk.addPropertyChangeListener(SHOW_HIDDEN_PROP, showFilesListener); 360 } 361 362 if(view == null) { 363 view = FileSystemView.getFileSystemView(); 364 } 365 setFileSystemView(view); 366 updateUI(); 367 if(isAcceptAllFileFilterUsed()) { 368 setFileFilter(getAcceptAllFileFilter()); 369 } 370 } 371 372 416 public void setDragEnabled(boolean b) { 417 if (b && GraphicsEnvironment.isHeadless()) { 418 throw new HeadlessException (); 419 } 420 dragEnabled = b; 421 } 422 423 430 public boolean getDragEnabled() { 431 return dragEnabled; 432 } 433 434 438 447 public File getSelectedFile() { 448 return selectedFile; 449 } 450 451 464 public void setSelectedFile(File file) { 465 File oldValue = selectedFile; 466 selectedFile = file; 467 if(selectedFile != null) { 468 if (file.isAbsolute() && !getFileSystemView().isParent(getCurrentDirectory(), selectedFile)) { 469 setCurrentDirectory(selectedFile.getParentFile()); 470 } 471 if (!isMultiSelectionEnabled() || selectedFiles == null || selectedFiles.length == 1) { 472 ensureFileIsVisible(selectedFile); 473 } 474 } 475 firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, oldValue, selectedFile); 476 } 477 478 482 public File [] getSelectedFiles() { 483 if(selectedFiles == null) { 484 return new File [0]; 485 } else { 486 return (File []) selectedFiles.clone(); 487 } 488 } 489 490 498 public void setSelectedFiles(File [] selectedFiles) { 499 File [] oldValue = this.selectedFiles; 500 if (selectedFiles != null && selectedFiles.length == 0) { 501 selectedFiles = null; 502 } 503 this.selectedFiles = selectedFiles; 504 setSelectedFile((selectedFiles != null) ? selectedFiles[0] : null); 505 firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, oldValue, this.selectedFiles); 506 } 507 508 514 public File getCurrentDirectory() { 515 return currentDirectory; 516 } 517 518 539 public void setCurrentDirectory(File dir) { 540 File oldValue = currentDirectory; 541 542 if (dir != null && !dir.exists()) { 543 dir = currentDirectory; 544 } 545 if (dir == null) { 546 dir = getFileSystemView().getDefaultDirectory(); 547 } 548 if (currentDirectory != null) { 549 550 if (this.currentDirectory.equals(dir)) { 551 return; 552 } 553 } 554 555 File prev = null; 556 while (!isTraversable(dir) && prev != dir) { 557 prev = dir; 558 dir = getFileSystemView().getParentDirectory(dir); 559 } 560 currentDirectory = dir; 561 562 firePropertyChange(DIRECTORY_CHANGED_PROPERTY, oldValue, currentDirectory); 563 } 564 565 571 public void changeToParentDirectory() { 572 selectedFile = null; 573 File oldValue = getCurrentDirectory(); 574 setCurrentDirectory(getFileSystemView().getParentDirectory(oldValue)); 575 } 576 577 580 public void rescanCurrentDirectory() { 581 getUI().rescanCurrentDirectory(this); 582 } 583 584 590 public void ensureFileIsVisible(File f) { 591 getUI().ensureFileIsVisible(this, f); 592 } 593 594 598 618 public int showOpenDialog(Component parent) throws HeadlessException { 619 setDialogType(OPEN_DIALOG); 620 return showDialog(parent, null); 621 } 622 623 643 public int showSaveDialog(Component parent) throws HeadlessException { 644 setDialogType(SAVE_DIALOG); 645 return showDialog(parent, null); 646 } 647 648 702 public int showDialog(Component parent, String approveButtonText) 703 throws HeadlessException { 704 if(approveButtonText != null) { 705 setApproveButtonText(approveButtonText); 706 setDialogType(CUSTOM_DIALOG); 707 } 708 dialog = createDialog(parent); 709 dialog.addWindowListener(new WindowAdapter() { 710 public void windowClosing(WindowEvent e) { 711 returnValue = CANCEL_OPTION; 712 } 713 }); 714 returnValue = ERROR_OPTION; 715 rescanCurrentDirectory(); 716 717 dialog.show(); 718 firePropertyChange("JFileChooserDialogIsClosingProperty", dialog, null); 719 dialog.removeAll(); 720 dialog.dispose(); 721 dialog = null; 722 return returnValue; 723 } 724 725 750 protected JDialog createDialog(Component parent) throws HeadlessException { 751 String title = getUI().getDialogTitle(this); 752 getAccessibleContext().setAccessibleDescription(title); 753 754 JDialog dialog; 755 Window window = JOptionPane.getWindowForComponent(parent); 756 if (window instanceof Frame ) { 757 dialog = new JDialog ((Frame )window, title, true); 758 } else { 759 dialog = new JDialog ((Dialog )window, title, true); 760 } 761 dialog.setComponentOrientation(this.getComponentOrientation()); 762 763 Container contentPane = dialog.getContentPane(); 764 contentPane.setLayout(new BorderLayout ()); 765 contentPane.add(this, BorderLayout.CENTER); 766 767 if (JDialog.isDefaultLookAndFeelDecorated()) { 768 boolean supportsWindowDecorations = 769 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 770 if (supportsWindowDecorations) { 771 dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG); 772 } 773 } 774 dialog.pack(); 775 dialog.setLocationRelativeTo(parent); 776 777 return dialog; 778 } 779 780 784 794 public boolean getControlButtonsAreShown() { 795 return controlsShown; 796 } 797 798 799 823 public void setControlButtonsAreShown(boolean b) { 824 if(controlsShown == b) { 825 return; 826 } 827 boolean oldValue = controlsShown; 828 controlsShown = b; 829 firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, oldValue, controlsShown); 830 } 831 832 845 public int getDialogType() { 846 return dialogType; 847 } 848 849 884 public void setDialogType(int dialogType) { 886 if(this.dialogType == dialogType) { 887 return; 888 } 889 if(!(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG || dialogType == CUSTOM_DIALOG)) { 890 throw new IllegalArgumentException ("Incorrect Dialog Type: " + dialogType); 891 } 892 int oldValue = this.dialogType; 893 this.dialogType = dialogType; 894 if(dialogType == OPEN_DIALOG || dialogType == SAVE_DIALOG) { 895 setApproveButtonText(null); 896 } 897 firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, oldValue, dialogType); 898 } 899 900 914 public void setDialogTitle(String dialogTitle) { 915 String oldValue = this.dialogTitle; 916 this.dialogTitle = dialogTitle; 917 if(dialog != null) { 918 dialog.setTitle(dialogTitle); 919 } 920 firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, oldValue, dialogTitle); 921 } 922 923 928 public String getDialogTitle() { 929 return dialogTitle; 930 } 931 932 936 937 938 953 public void setApproveButtonToolTipText(String toolTipText) { 954 if(approveButtonToolTipText == toolTipText) { 955 return; 956 } 957 String oldValue = approveButtonToolTipText; 958 approveButtonToolTipText = toolTipText; 959 firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldValue, approveButtonToolTipText); 960 } 961 962 963 973 public String getApproveButtonToolTipText() { 974 return approveButtonToolTipText; 975 } 976 977
|