1 19 20 24 25 package org.netbeans.modules.css.visual.ui; 26 27 import org.netbeans.modules.css.Utilities; 28 import org.netbeans.modules.css.visual.model.CssMetaModel; 29 import java.awt.Component ; 30 import java.io.File ; 31 import javax.swing.JFileChooser ; 32 import javax.swing.filechooser.FileFilter ; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.NbBundle; 36 37 42 public class BackgroundImageUrlDialog { 44 private String imageUrl = null; 45 private FileFilter imgFilter = new ImageFilter() ; 46 47 public boolean show(Component parent){ 49 boolean retValue = false; 50 JFileChooser fileChooser = Utilities.getJFileChooser(); 51 File currDir = null; 52 try{ 53 FileObject fo = CssMetaModel.getDataObject().getPrimaryFile(); 54 currDir = FileUtil.toFile(fo).getParentFile(); 55 if (currDir == null) currDir = new File (System.getProperty("user.home")); if (currDir != null ) fileChooser.setCurrentDirectory(currDir); 57 fileChooser.addChoosableFileFilter(new ImageFilter()) ; 58 if ( fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { 59 File imageFile = fileChooser.getSelectedFile(); 60 File newImageFile = new File (currDir, imageFile.getName()); 61 Utilities.copyFile(imageFile, newImageFile); 62 imageUrl = imageFile.getName(); 63 StringBuffer sb = new StringBuffer (); 64 int len = imageUrl.length(); 65 for (int i = 0; i < len; i++) { 66 char chr = imageUrl.charAt(i); 67 if (chr == ' '){ 68 sb.append("%20"); 69 }else{ 70 sb.append(chr); 71 } 72 } 73 imageUrl = sb.toString(); 74 retValue = true; 75 } 76 } catch (Exception exc){ 77 exc.printStackTrace(); 78 } 79 return retValue; 80 } 81 82 public String getImageUrl(){ 83 return imageUrl; 84 } 85 86 public static class ImageFilter extends FileFilter { 87 public boolean accept(File f) { 89 if (f.isDirectory()) { 90 return true; 91 } 92 String extension = null; 93 String s = f.getName(); 94 int i = s.lastIndexOf('.'); 95 96 if (i > 0 && i < s.length() - 1) { 97 extension = s.substring(i+1).toLowerCase(); 98 } 99 100 if (extension != null) { 101 if (extension.toLowerCase().equals("gif") || extension.toLowerCase().equals("jpg") || extension.toLowerCase().equals("png") ) { return true; 105 } else { 106 return false; 107 } 108 } 109 return false; 110 } 111 112 public String getDescription() { 114 return NbBundle.getMessage(BackgroundImageUrlDialog.class, "IMAGE_FILTER"); } 116 } 117 118 } | Popular Tags |