001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.apache.jmeter.module.wizards;
043:
044: import org.openide.filesystems.FileObject;
045: import org.openide.util.NbBundle;
046: import org.openide.WizardDescriptor;
047:
048: /**
049: *
050: * @author mkuchtiak
051: */
052: public class Utilities {
053: /** Checks if the given file name can be created in the target folder.
054: *
055: * @param dir target directory
056: * @param newObjectName name of created file
057: * @param extension extension of created file
058: * @return localized error message or null if all right
059: */
060: final public static String canUseFileName(java.io.File dir,
061: String relativePath, String objectName, String extension) {
062: String newObjectName = objectName;
063: if (extension != null && extension.length() > 0) {
064: StringBuffer sb = new StringBuffer();
065: sb.append(objectName);
066: sb.append('.'); // NOI18N
067: sb.append(extension);
068: newObjectName = sb.toString();
069: }
070:
071: // check file name
072:
073: if (!checkFileName(objectName)) {
074: return NbBundle.getMessage(Utilities.class,
075: "MSG_invalid_filename", newObjectName); // NOI18N
076: }
077: // test if the directory is correctly specified
078: FileObject folder = null;
079: if (dir != null) {
080: try {
081: folder = org.openide.filesystems.FileUtil
082: .toFileObject(dir);
083: } catch (java.lang.IllegalArgumentException ex) {
084: return NbBundle.getMessage(Utilities.class,
085: "MSG_invalid_path", relativePath); // NOI18N
086: }
087: }
088:
089: // test whether the selected folder on selected filesystem is read-only or exists
090: if (folder != null) {
091: // target filesystem should be writable
092: if (!folder.canWrite()) {
093: return NbBundle.getMessage(Utilities.class,
094: "MSG_fs_is_readonly"); // NOI18N
095: }
096:
097: if (folder.getFileObject(newObjectName) != null) {
098: return NbBundle.getMessage(Utilities.class,
099: "MSG_file_already_exist", newObjectName); // NOI18N
100: }
101:
102: if (org.openide.util.Utilities.isWindows()) {
103: if (checkCaseInsensitiveName(folder, newObjectName)) {
104: return NbBundle.getMessage(Utilities.class,
105: "MSG_file_already_exist", newObjectName); // NOI18N
106: }
107: }
108: }
109:
110: // all ok
111: return null;
112: }
113:
114: // helper check for windows, its filesystem is case insensitive (workaround the bug #33612)
115: /** Check existence of file on case insensitive filesystem.
116: * Returns true if folder contains file with given name and extension.
117: * @param folder folder for search
118: * @param name name of file
119: * @param extension extension of file
120: * @return true if file with name and extension exists, false otherwise.
121: */
122: private static boolean checkCaseInsensitiveName(FileObject folder,
123: String name) {
124: // bugfix #41277, check only direct children
125: java.util.Enumeration children = folder.getChildren(false);
126: FileObject fo;
127: while (children.hasMoreElements()) {
128: fo = (FileObject) children.nextElement();
129: if (name.equalsIgnoreCase(fo.getName())) {
130: return true;
131: }
132: }
133: return false;
134: }
135:
136: private static boolean checkFileName(String str) {
137: char c[] = str.toCharArray();
138: for (int i = 0; i < c.length; i++) {
139: if (c[i] == '\\')
140: return false;
141: if (c[i] == '/')
142: return false;
143: }
144: return true;
145: }
146:
147: public static String[] createSteps(String[] before,
148: WizardDescriptor.Panel[] panels) {
149: //assert panels != null;
150: // hack to use the steps set before this panel processed
151: int diff = 0;
152: if (before == null) {
153: before = new String[0];
154: } else if (before.length > 0) {
155: diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
156: }
157: String[] res = new String[(before.length - diff)
158: + panels.length];
159: for (int i = 0; i < res.length; i++) {
160: if (i < (before.length - diff)) {
161: res[i] = before[i];
162: } else {
163: res[i] = panels[i - before.length + diff]
164: .getComponent().getName();
165: }
166: }
167: return res;
168: }
169: }
|