1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyEditorSupport ; 26 import java.io.File ; 27 import java.io.FilenameFilter ; 28 import java.util.logging.Logger ; 29 import javax.swing.JFileChooser ; 30 31 import org.openide.nodes.Node; 32 import org.openide.explorer.propertysheet.ExPropertyEditor; 33 import org.openide.explorer.propertysheet.PropertyEnv; 34 import org.openide.util.HelpCtx; 35 import org.openide.util.NbBundle; 36 37 42 public class FileArrayEditor extends PropertyEditorSupport implements ExPropertyEditor, PropertyChangeListener { 43 44 45 private int mode = JFileChooser.FILES_AND_DIRECTORIES; 46 47 48 private boolean directories = true; 49 50 private boolean files = true; 51 52 private boolean fileHiding = false; 53 54 private javax.swing.filechooser.FileFilter fileFilter; 55 56 private File currentDirectory; 57 58 private File baseDirectory; 59 60 66 private JFileChooser chooser; 67 68 69 private boolean editable = true; 70 71 76 public void attachEnv(PropertyEnv env) { 77 directories = true; 79 files = true; 80 fileFilter = null; 81 fileHiding = false; 82 83 Object dirs = env.getFeatureDescriptor().getValue(FileEditor.PROPERTY_SHOW_DIRECTORIES); 84 if (dirs instanceof Boolean ) { 85 directories = ((Boolean )dirs).booleanValue(); 86 } Object fil = env.getFeatureDescriptor().getValue(FileEditor.PROPERTY_SHOW_FILES); 88 if (fil instanceof Boolean ) { 89 files = ((Boolean )fil).booleanValue(); 90 } Object filter = env.getFeatureDescriptor().getValue(FileEditor.PROPERTY_FILTER); 92 if (filter instanceof FilenameFilter ) { 93 fileFilter = new FileEditor.DelegatingFilenameFilter((FilenameFilter )filter); 94 } else if (filter instanceof javax.swing.filechooser.FileFilter ) { 95 fileFilter = (javax.swing.filechooser.FileFilter )filter; 96 } else if (filter instanceof java.io.FileFilter ) { 97 fileFilter = new FileEditor.DelegatingFileFilter((java.io.FileFilter )filter); 98 } 100 Object curDir = env.getFeatureDescriptor().getValue(FileEditor.PROPERTY_CURRENT_DIR); 101 if (curDir instanceof File ) { 102 currentDirectory = (File )curDir; 103 if(! currentDirectory.isDirectory()) { 104 Logger.getAnonymousLogger().warning("java.io.File will not accept currentDir=" + baseDirectory); currentDirectory = null; 106 } 107 } 109 Object baseDir = env.getFeatureDescriptor().getValue(FileEditor.PROPERTY_BASE_DIR); 110 if(baseDir instanceof File ) { 111 baseDirectory = (File )baseDir; 112 if(!baseDirectory.isDirectory() || !baseDirectory.isAbsolute()) { 114 Logger.getAnonymousLogger().warning("java.io.File will not accept baseDir=" + baseDirectory); baseDirectory = null; 116 } 117 } if (files) { 119 mode = directories ? JFileChooser.FILES_AND_DIRECTORIES : 120 JFileChooser.FILES_ONLY; 121 } else { 122 mode = directories ? JFileChooser.DIRECTORIES_ONLY : 123 JFileChooser.FILES_AND_DIRECTORIES; } 125 126 Object fileHide = env.getFeatureDescriptor().getValue(FileEditor.PROPERTY_FILE_HIDING); 127 if (fileHide instanceof Boolean ) { 128 fileHiding = ((Boolean )fileHide).booleanValue(); 129 } 130 131 if (env.getFeatureDescriptor() instanceof Node.Property){ 132 Node.Property prop = (Node.Property)env.getFeatureDescriptor(); 133 editable = prop.canWrite(); 134 } 135 } 136 137 140 public String getAsText() { 141 File [] file = (File [])getValue(); 142 if (file == null) { 143 return ""; } 145 StringBuilder path = new StringBuilder ("["); 146 for (int i = 0; i < file.length; i++) { 147 path.append(file[i].getPath()); 148 } 149 if (file.length == 0) path.append('.'); return path.append(']').toString(); } 153 154 159 public void setAsText(String str) throws IllegalArgumentException { 160 if (str == null) { 161 throw new IllegalArgumentException ("null"); } 163 if ("".equals(str)) { setValue(null); 165 return; 166 } 167 169 } 171 172 175 public Component getCustomEditor() { 176 if (!editable) { 177 return new StringCustomEditor(getAsText(), false); 178 } 179 if (chooser == null) { 180 chooser = FileEditor.createHackedFileChooser(); 181 chooser.setMultiSelectionEnabled(true); 182 183 Object vv = getValue (); 184 File originalFile = null; 185 186 if (vv instanceof File []) { 187 File [] ofile = (File []) vv; 188 if (ofile.length > 0) { 189 originalFile = ofile[0]; 190 if (originalFile != null && ! originalFile.isAbsolute() && baseDirectory != null) { 191 originalFile = new File (baseDirectory, originalFile.getPath()); 192 } 193 } 194 } 195 if (currentDirectory != null) { 196 chooser.setCurrentDirectory (currentDirectory); 197 } else if (originalFile != null && originalFile.getParentFile() != null) { 198 chooser.setCurrentDirectory (originalFile.getParentFile()); 199 chooser.setSelectedFile (originalFile); 200 } else if (FileEditor.lastCurrentDir != null) { 201 chooser.setCurrentDirectory(FileEditor.lastCurrentDir); 202 } 203 chooser.setFileSelectionMode(mode); 204 if (fileFilter != null) { 205 chooser.setFileFilter(fileFilter); 206 } 207 switch (mode) { 208 case JFileChooser.FILES_AND_DIRECTORIES: 209 chooser.setDialogTitle (getString ("CTL_DialogTitleFilesAndDirs")); 210 break; 211 case JFileChooser.FILES_ONLY: 212 chooser.setDialogTitle (getString ("CTL_DialogTitleFiles")); 213 break; 214 case JFileChooser.DIRECTORIES_ONLY: 215 chooser.setDialogTitle (getString ("CTL_DialogTitleDirs")); 216 break; 217 } 218 chooser.setFileHidingEnabled(fileHiding); 219 220 chooser.setControlButtonsAreShown(false); 221 222 chooser.addPropertyChangeListener(this); 223 224 HelpCtx.setHelpIDString (chooser, getHelpCtx ().getHelpID ()); 225 } 226 return chooser; 227 } 228 229 232 public boolean supportsCustomEditor() { 233 return true; 234 } 235 236 239 public String getJavaInitializationString() { 240 File [] value = (File []) getValue (); 241 if (value == null) { 242 return "null"; } else { 244 StringBuilder retVal = new StringBuilder ("new java.io.File[] { "); for (int i = 0; i < value.length; i++) { 248 if (baseDirectory != null && !value[i].isAbsolute()) { 249 retVal.append("new java.io.File(") .append(FileEditor.stringify(baseDirectory.getPath())) 251 .append( ", " ) .append(FileEditor.stringify(value[i].getPath())) 253 .append( "), " ); } else { 255 retVal.append("new java.io.File(") .append(FileEditor.stringify(value[i].getAbsolutePath())) 257 .append("), "); } 259 } 260 return retVal.append(" }").toString(); 261 } 262 } 263 264 265 private HelpCtx getHelpCtx () { 266 return new HelpCtx (FileEditor.class); 267 } 268 269 270 private static String getString(String key) { 271 return NbBundle.getBundle(FileArrayEditor.class).getString(key); 272 } 273 274 275 public void propertyChange(PropertyChangeEvent e) { 276 if (e.getSource() instanceof JFileChooser ) { 277 JFileChooser jfc = (JFileChooser ) e.getSource(); 278 if (mode == jfc.DIRECTORIES_ONLY && jfc.DIRECTORY_CHANGED_PROPERTY.equals(e.getPropertyName())) { 279 if (jfc.getSelectedFile() == null) { 280 File dir = jfc.getCurrentDirectory(); 281 if (dir != null) { 282 setValue (new File [] {new File (dir.getAbsolutePath())}); 283 return; 284 } 285 } 286 } 287 } 288 289 if (( ! JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(e.getPropertyName())) && 290 ( ! JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName()))) { 291 return; 292 } 293 if (! (e.getSource() instanceof JFileChooser )) { 294 return; 295 } 296 JFileChooser chooser = (JFileChooser )e.getSource(); 297 File [] f = (File [])chooser.getSelectedFiles(); 298 if (f == null) { 299 return; 300 } 301 302 if ((f.length == 0) && (chooser.getSelectedFile() != null)) { 303 f = new File [] { chooser.getSelectedFile() }; 304 } 305 306 for (int i = 0; i < f.length; i++) { 307 if (!files && f[i].isFile ()) return; 308 if (!directories && f[i].isDirectory ()) return; 309 } 310 311 if (baseDirectory != null) { 312 for (int i = 0; i < f.length; i++) { 313 String rel = FileEditor.getChildRelativePath(baseDirectory, f[i]); 314 if (rel != null) { 315 f[i] = new File (rel); 316 } 317 } 318 } 319 320 File [] nf = new File [f.length]; 321 for (int i = 0; i < f.length; i++) { 322 nf[i] = new File (f[i].getAbsolutePath()); 327 } 328 setValue(nf); 329 330 FileEditor.lastCurrentDir = chooser.getCurrentDirectory(); 331 } 332 } 333 | Popular Tags |