1 19 20 package org.netbeans.modules.openfile; 21 22 import java.awt.GridLayout ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.JFileChooser ; 27 import javax.swing.JLabel ; 28 import javax.swing.JPanel ; 29 import javax.swing.filechooser.FileFilter ; 30 import org.openide.DialogDisplayer; 31 import org.openide.NotifyDescriptor; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.util.HelpCtx; 34 import org.openide.util.NbBundle; 35 36 41 class FileChooser extends JFileChooser { 42 43 44 FileChooser() { 45 setFileSelectionMode(JFileChooser.FILES_ONLY); 46 setMultiSelectionEnabled(true); 47 48 49 FileFilter currentFilter = getFileFilter(); 50 addChoosableFileFilter(new Filter ( 51 new String [] {DefaultOpenFileImpl.JAVA_EXT}, 52 NbBundle.getMessage(FileChooser.class, "TXT_JavaFilter"))); addChoosableFileFilter(new Filter ( 54 new String [] {DefaultOpenFileImpl.TXT_EXT}, 55 NbBundle.getMessage(FileChooser.class, "TXT_TxtFilter"))); setFileFilter(currentFilter); 57 } 58 59 public void approveSelection() { 60 final File [] selectedFiles = getSelectedFiles(); 61 62 63 List errorMsgs = null; 64 for (int i = 0; i < selectedFiles.length; i++) { 65 String msgPatternRef = null; 66 boolean isUNCPath = false; 67 File file = selectedFiles[i]; 68 69 if (!file.exists()) { 70 msgPatternRef = "MSG_FileDoesNotExist"; } else if (OpenFile.isSpecifiedByUNCPath(file)) { 72 msgPatternRef = "MSG_FileSpecifiedByUNC"; isUNCPath = true; 74 } else if (file.isDirectory()) { 75 msgPatternRef = "MSG_FileIsADirectory"; } else if (!file.isFile()) { 77 msgPatternRef = "MSG_FileIsNotPlainFile"; } 79 if (msgPatternRef == null) { 80 continue; 81 } 82 83 if (errorMsgs == null) { 84 errorMsgs = new ArrayList (selectedFiles.length - i); 85 } 86 errorMsgs.add(NbBundle.getMessage(FileChooser.class, 87 msgPatternRef, 88 isUNCPath 89 ? getUNCPathToDisplay(file) 90 : file.getName())); 91 } 92 if (errorMsgs == null) { 93 super.approveSelection(); 94 } else { 95 JPanel panel = new JPanel (new GridLayout (errorMsgs.size(), 0, 96 0, 2)); for (java.util.Iterator i = errorMsgs.iterator(); i.hasNext(); ) { 98 panel.add(new JLabel ((String ) i.next())); 99 } 100 DialogDisplayer.getDefault().notify( 101 new NotifyDescriptor.Message( 102 panel, NotifyDescriptor.WARNING_MESSAGE)); 103 } 104 } 105 106 114 private static String getUNCPathToDisplay(File file) { 115 String name = file.getName(); 116 String path = file.getPath(); 117 String pathToCheck = path.substring(2, 118 path.length() - name.length() - 1); 119 120 final char sep = File.separatorChar; 121 int slashIndex = pathToCheck.indexOf(sep); 122 if (slashIndex != -1) { 123 slashIndex = pathToCheck.indexOf(sep, slashIndex + 1); 124 if ((slashIndex != -1) && (pathToCheck.indexOf(sep, slashIndex + 1) 125 != -1)) { 126 127 128 return new StringBuffer (slashIndex + name.length() + 7) 129 .append(path.substring(0, 2 + slashIndex)) 130 .append(sep) 131 .append("...") .append(sep) 133 .append(name) 134 .toString(); 135 } 136 } 137 return path; 138 } 139 140 141 private static class Filter extends FileFilter { 142 143 144 private String [] extensions; 145 146 147 private String description; 148 149 150 162 public Filter(String [] extensions, String description) { 163 164 this.extensions = new String [extensions.length]; 165 for (int i = 0; i < extensions.length; i++) { 166 this.extensions[i] = extensions[i].toUpperCase(); 167 } 168 this.description = description; 169 } 170 171 172 177 public boolean accept(File file) { 178 if (file.isDirectory()) { 179 return true; 180 } 181 for (int i = 0; i < extensions.length; i++) { 182 if (file.getName().toUpperCase().endsWith(extensions[i])) { 183 return true; 184 } 185 } 186 187 return false; 188 } 189 190 191 public String getDescription() { 192 return description; 193 } 194 } 196 } 197 | Popular Tags |