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: /*
043: * Utilities.java
044: *
045: * Created on September 24, 2002, 11:11 AM
046: */
047:
048: package gui;
049:
050: import java.beans.PropertyVetoException;
051: import java.io.File;
052: import java.io.IOException;
053: import java.util.Enumeration;
054: import javax.swing.text.StyledDocument;
055: import org.netbeans.jemmy.JemmyException;
056: import org.netbeans.junit.AssertionFailedErrorException;
057: import org.openide.cookies.EditorCookie;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileSystem;
060: import org.openide.filesystems.LocalFileSystem;
061: import org.openide.filesystems.Repository;
062: import org.openide.loaders.DataObject;
063:
064: /**
065: *
066: * @author jb105785
067: */
068: public class Utilities {
069:
070: /** Creates a new instance of Utilities */
071: public Utilities() {
072: }
073:
074: /** Mounts <userdir>/sampledir through API
075: * @return absolute path of mounted dir
076: */
077: public static boolean mountSampledir() {
078: String userdir = System.getProperty("netbeans.user"); // NOI18N
079: String mountPoint = userdir + File.separator + "sampledir"; // NOI18N
080: mountPoint = mountPoint.replace('\\', '/');
081: FileSystem fs = Repository.getDefault().findFileSystem(
082: mountPoint);
083: if (fs == null) {
084: try {
085: LocalFileSystem lfs = new LocalFileSystem();
086: lfs.setRootDirectory(new File(mountPoint));
087: Repository.getDefault().addFileSystem(lfs);
088: return true;
089: } catch (IOException ioe) {
090: throw new JemmyException("Mounting FS: " + mountPoint
091: + " failed.", ioe);
092: } catch (PropertyVetoException pve) {
093: throw new JemmyException("Mounting FS: " + mountPoint
094: + " failed.", pve);
095: }
096: }
097: return true;
098: }
099:
100: public static void delete(String file) {
101: FileObject fileObject = Repository.getDefault().findResource(
102: file);
103: if (fileObject == null)
104: return;
105: try {
106: DataObject.find(fileObject).delete();
107: } catch (java.io.IOException e) {
108: }
109: }
110:
111: /** Removes time and author's name
112: * @param result
113: * @return
114: *
115: */
116: public static String unify(String result) {
117: int left = result.indexOf("* Created on");
118: int right;
119: if (left >= 0) {
120: right = result.indexOf('\n', left);
121: result = result
122: .substring(0, left + "* Created on".length())
123: + result.substring(right);
124: }
125:
126: if (left >= 0) {
127: left = result.indexOf("@author");
128: right = result.indexOf('\n', left);
129: result = result.substring(0, left + "@author".length())
130: + result.substring(right);
131: }
132: return result;
133: }
134:
135: public static String getAsString(String file) {
136: String result;
137: try {
138: FileObject testFile = Repository.getDefault().findResource(
139: file);
140: DataObject DO = DataObject.find(testFile);
141:
142: EditorCookie ec = (EditorCookie) (DO
143: .getCookie(EditorCookie.class));
144: StyledDocument doc = ec.openDocument();
145: result = doc.getText(0, doc.getLength());
146: // result=Common.unify(result);
147: } catch (Exception e) {
148: throw new AssertionFailedErrorException(e);
149: }
150: return result;
151: }
152:
153: public static FileSystem findFileSystem(String pattern) {
154: Enumeration fs = Repository.getDefault().getFileSystems();
155: FileSystem result;
156: while (fs.hasMoreElements()) {
157: if ((result = (FileSystem) fs.nextElement())
158: .getDisplayName().indexOf(pattern) > 0) {
159: return result;
160: }
161: }
162: return null;
163: }
164:
165: }
|