KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > visual > ui > BackgroundImageUrlDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * BackgroundImageUrlDialog.java
22  * Created on November 5, 2004, 5:08 PM
23  */

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 JavaDoc;
30 import java.io.File JavaDoc;
31 import javax.swing.JFileChooser JavaDoc;
32 import javax.swing.filechooser.FileFilter JavaDoc;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.NbBundle;
36
37 /**
38  * Dialog to select a Image URL
39  * @author Winston Prakash
40  * @version 1.0
41  */

42 public class BackgroundImageUrlDialog { //extends URLPanel{
43

44     private String JavaDoc imageUrl = null;
45     private FileFilter JavaDoc imgFilter = new ImageFilter() ;
46     
47     // XXX Get URL also not just local file
48
public boolean show(Component JavaDoc parent){
49         boolean retValue = false;
50         JFileChooser JavaDoc fileChooser = Utilities.getJFileChooser();
51         File JavaDoc currDir = null;
52         try{
53             FileObject fo = CssMetaModel.getDataObject().getPrimaryFile();
54             currDir = FileUtil.toFile(fo).getParentFile();
55             if (currDir == null) currDir = new File JavaDoc(System.getProperty("user.home")); //NOI18N
56
if (currDir != null ) fileChooser.setCurrentDirectory(currDir);
57             fileChooser.addChoosableFileFilter(new ImageFilter()) ;
58             if ( fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
59                 File JavaDoc imageFile = fileChooser.getSelectedFile();
60                 File JavaDoc newImageFile = new File JavaDoc(currDir, imageFile.getName());
61                 Utilities.copyFile(imageFile, newImageFile);
62                 imageUrl = imageFile.getName();
63                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc exc){
77             exc.printStackTrace();
78         }
79         return retValue;
80     }
81     
82     public String JavaDoc getImageUrl(){
83         return imageUrl;
84     }
85     
86     public static class ImageFilter extends FileFilter JavaDoc {
87         //Accept all directories and image files.
88
public boolean accept(File JavaDoc f) {
89             if (f.isDirectory()) {
90                 return true;
91             }
92             String JavaDoc extension = null;
93             String JavaDoc 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") || //NOI18N
102
extension.toLowerCase().equals("jpg") || //NOI18N
103
extension.toLowerCase().equals("png") ) { //NOI18N
104
return true;
105                 } else {
106                     return false;
107                 }
108             }
109             return false;
110         }
111         
112         //The description of this filter
113
public String JavaDoc getDescription() {
114             return NbBundle.getMessage(BackgroundImageUrlDialog.class, "IMAGE_FILTER"); //NOI18N
115
}
116     }
117     
118 }
Popular Tags