KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > Utilities


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.wizards;
21
22 import org.openide.filesystems.FileObject;
23 import org.openide.util.NbBundle;
24 import org.openide.WizardDescriptor;
25 /**
26  *
27  * @author mkuchtiak
28  */

29 public class Utilities {
30     /** Checks if the given file name can be created in the target folder.
31      *
32      * @param dir target directory
33      * @param newObjectName name of created file
34      * @param extension extension of created file
35      * @return localized error message or null if all right
36      */

37     final public static String JavaDoc canUseFileName (java.io.File JavaDoc dir, String JavaDoc relativePath, String JavaDoc objectName, String JavaDoc extension) {
38         String JavaDoc newObjectName=objectName;
39         if (extension != null && extension.length () > 0) {
40             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
41             sb.append (objectName);
42             sb.append ('.'); // NOI18N
43
sb.append (extension);
44             newObjectName = sb.toString ();
45         }
46         
47         // check file name
48

49         if (!checkFileName(objectName)) {
50             return NbBundle.getMessage (Utilities.class, "MSG_invalid_filename", newObjectName); // NOI18N
51
}
52         // test if the directory is correctly specified
53
FileObject folder = null;
54         if (dir!=null) {
55             try {
56                  folder = org.openide.filesystems.FileUtil.toFileObject(dir);
57             } catch(java.lang.IllegalArgumentException JavaDoc ex) {
58                  return NbBundle.getMessage (Utilities.class, "MSG_invalid_path", relativePath); // NOI18N
59
}
60         }
61             
62         // test whether the selected folder on selected filesystem is read-only or exists
63
if (folder!= null) {
64             // target filesystem should be writable
65
if (!folder.canWrite ()) {
66                 return NbBundle.getMessage (Utilities.class, "MSG_fs_is_readonly"); // NOI18N
67
}
68
69             if (folder.getFileObject (newObjectName) != null) {
70                 return NbBundle.getMessage (Utilities.class, "MSG_file_already_exist", newObjectName); // NOI18N
71
}
72
73             if (org.openide.util.Utilities.isWindows ()) {
74                 if (checkCaseInsensitiveName (folder, newObjectName)) {
75                     return NbBundle.getMessage (Utilities.class, "MSG_file_already_exist", newObjectName); // NOI18N
76
}
77             }
78         }
79
80         // all ok
81
return null;
82     }
83     
84     // helper check for windows, its filesystem is case insensitive (workaround the bug #33612)
85
/** Check existence of file on case insensitive filesystem.
86      * Returns true if folder contains file with given name and extension.
87      * @param folder folder for search
88      * @param name name of file
89      * @param extension extension of file
90      * @return true if file with name and extension exists, false otherwise.
91      */

92     private static boolean checkCaseInsensitiveName (FileObject folder, String JavaDoc name) {
93         // bugfix #41277, check only direct children
94
java.util.Enumeration JavaDoc children = folder.getChildren (false);
95         FileObject fo;
96         while (children.hasMoreElements ()) {
97             fo = (FileObject) children.nextElement ();
98             if (name.equalsIgnoreCase (fo.getName ())) {
99                 return true;
100             }
101         }
102         return false;
103     }
104     
105     private static boolean checkFileName(String JavaDoc str) {
106         char c[] = str.toCharArray();
107         for (int i=0;i<c.length;i++) {
108             if (c[i]=='\\') return false;
109             if (c[i]=='/') return false;
110         }
111         return true;
112     }
113     
114     public static String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
115         //assert panels != null;
116
// hack to use the steps set before this panel processed
117
int diff = 0;
118         if (before == null) {
119             before = new String JavaDoc[0];
120         } else if (before.length > 0) {
121             diff = ("...".equals (before[before.length - 1])) ? 1 : 0; // NOI18N
122
}
123         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
124         for (int i = 0; i < res.length; i++) {
125             if (i < (before.length - diff)) {
126                 res[i] = before[i];
127             } else {
128                 res[i] = panels[i - before.length + diff].getComponent ().getName ();
129             }
130         }
131         return res;
132     }
133 }
134
Popular Tags