1 19 20 package org.netbeans.modules.web.project.ui; 21 22 import org.openide.filesystems.FileUtil; 23 24 import javax.swing.*; 25 import javax.swing.filechooser.FileFilter ; 26 import java.io.File ; 27 import java.awt.*; 28 29 public class FileChooser extends JFileChooser { 30 31 private String key; 32 protected String initialPath; 33 34 public FileChooser(String key, String currentDirectoryPath) { 35 super(getInitialDirectory(key, currentDirectoryPath)); 36 this.key = key; 37 } 38 39 public FileChooser(String key) { 40 this(key, null); 41 this.key = key; 42 } 43 44 private static File getInitialDirectory(String key, String currentDirectoryPath) { 45 return getInitialDirectory(key, currentDirectoryPath == null ? null : new File (currentDirectoryPath)); 46 } 47 48 private static File getInitialDirectory(String key, File f) { 49 while (f != null) { 50 if (f.exists() && f.isDirectory()) { 51 return f; 52 } 53 f = f.getParentFile(); 54 } 55 File lastChooserLocation = getLastChooserLocation(key); 56 if (lastChooserLocation != null && lastChooserLocation.exists()) { 57 return lastChooserLocation; 58 } else { 59 String pathname = System.getProperty("user.home"); if(pathname != null) { 61 File file = new File (pathname).getAbsoluteFile(); 62 if(file.exists()) { 63 return file; 64 } 65 } 66 File file = new File ("").getAbsoluteFile(); assert file.exists() : "Default directory '" + file.getAbsolutePath() + "' does not exist"; return f; 69 } 70 } 71 72 public int showDialog(Component parent, String approveButtonText) throws HeadlessException { 73 FileUtil.preventFileChooserSymlinkTraversal(this, getInitialDirectory(key, getCurrentDirectory())); 74 return super.showDialog(parent, approveButtonText); 75 } 76 77 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 78 if (SELECTED_FILE_CHANGED_PROPERTY.equals(propertyName)) { 79 newValue = correctFile((File ) newValue); 80 } 81 super.firePropertyChange(propertyName, oldValue, newValue); 82 } 83 84 public void approveSelection() { 85 saveCurrentLocation(); 86 super.approveSelection(); 87 } 88 89 private static File correctFile(File f) { 90 while(f != null && ".".equals(f.getName())) { f = f.getParentFile(); 92 } 93 return f; 94 } 95 96 private void saveCurrentLocation() { 97 if (!isMultiSelectionEnabled() && isDirectorySelectionEnabled()) { 98 if (saveLocation(getSelectedFile())) { 100 return; 101 } 102 } 103 saveLocation(getCurrentDirectory()); 104 } 105 106 private boolean saveLocation(File f) { 107 if (f != null && f.isDirectory()) { 108 setLastChooserLocation(key, f); 109 return true; 110 } else { 111 return false; 112 } 113 } 114 115 public static File getLastChooserLocation(String key) { 116 String path = FoldersListSettings.getPreferences().get(FoldersListSettings.LAST_USED_CHOOSER_LOCATIONS+key, null); 117 return path != null ? new File (path) : null; 118 } 119 120 public static void setLastChooserLocation(String key, File folder) { 121 FoldersListSettings.getPreferences().put(FoldersListSettings.LAST_USED_CHOOSER_LOCATIONS+key, folder.getPath()); 122 } 123 124 public static FileChooser createDirectoryChooser(String key) { 125 return createDirectoryChooser(key, null); 126 } 127 128 public static FileChooser createDirectoryChooser(String key, String initialPath) { 129 FileChooser chooser = new FileChooser(key, initialPath); 130 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 131 chooser.setMultiSelectionEnabled(false); 132 chooser.setAcceptAllFileFilterUsed(false); 133 return chooser; 134 } 135 136 public static FileChooser createFileChooser(String key, String dialogTitle, FileFilter fileFilter) { 137 FileChooser chooser = new FileChooser(key); 138 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 139 chooser.setMultiSelectionEnabled(true); 140 chooser.setDialogTitle(dialogTitle); 141 chooser.setAcceptAllFileFilterUsed( false ); 143 chooser.setFileFilter(fileFilter); 144 return chooser; 145 } 146 } 147 | Popular Tags |