1 33 34 35 package edu.rice.cs.util.swing; 36 37 import edu.rice.cs.util.FileOps; 38 import edu.rice.cs.util.swing.Utilities; 39 40 import java.io.File ; 41 42 import javax.swing.JFileChooser ; 43 import javax.swing.filechooser.FileFilter ; 44 import javax.swing.filechooser.FileView ; 45 46 import java.awt.Component ; 47 48 public class DirectoryChooser extends JFileChooser { 49 50 51 protected Component _owner; 52 53 54 protected File _root; 55 56 57 public DirectoryChooser() { this(null, null, false, false); } 58 59 60 public DirectoryChooser(Component owner) { this(owner, null, false, false); } 61 62 65 public DirectoryChooser(Component owner, boolean allowMultiple) { this(owner, null, allowMultiple, false); } 66 67 70 public DirectoryChooser(Component owner, File root) { this(owner, root, false, false); } 71 72 76 public DirectoryChooser(Component owner, File root, boolean allowMultiple, boolean showHidden) { 77 79 super(root); 80 _init(owner, root, allowMultiple, showHidden); 81 } 82 83 84 85 86 private void _init(Component owner, final File root, boolean allowMultiple, boolean showHidden) { 87 88 89 96 _owner = owner; 97 _root = root; if (root != null) { 99 if (! root.exists()) _root = null; 100 else if (! root.isDirectory()) _root = root.getParentFile(); 101 } 102 103 setMultiSelectionEnabled(allowMultiple); 104 setFileHidingEnabled(! showHidden); 105 setFileSelectionMode(DIRECTORIES_ONLY); 106 setDialogType(CUSTOM_DIALOG); 107 setApproveButtonText("Select"); 108 setFileFilter(new FileFilter () { 109 public boolean accept(File f) { return true; } 110 public String getDescription() { return "All Folders"; } 111 }); 112 } 113 114 public int showDialog(File initialSelection) { 115 setCurrentDirectory(initialSelection); 116 return showDialog(_owner, null); } 118 119 120 public void setOwner(Component owner) { _owner = owner; } 121 122 125 public int showDialog() { return showDialog(_owner, null); } 126 127 130 public File [] getSelectedDirectories() { return getSelectedFiles(); } 131 132 135 public File getSelectedDirectory() { return getSelectedFile(); } 136 137 } 143 | Popular Tags |