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.netbeans.modules.refactoring.spi.impl;
043:
044: import java.io.File;
045: import java.util.LinkedHashSet;
046: import java.util.Set;
047: import org.openide.filesystems.FileChangeListener;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileStateInvalidException;
050: import org.openide.filesystems.FileSystem;
051: import org.openide.filesystems.FileUtil;
052:
053: /**
054: *
055: * @author Jan Becicka
056: */
057:
058: public class Util {
059:
060: //copy - paste programming
061: //http://ant.netbeans.org/source/browse/ant/src-bridge/org/apache/tools/ant/module/bridge/impl/BridgeImpl.java.diff?r1=1.15&r2=1.16
062: //http:/java.netbeans.org/source/browse/java/javacore/src/org/netbeans/modules/javacore/Util.java
063: //http://core.netbeans.org/source/browse/core/ui/src/org/netbeans/core/ui/MenuWarmUpTask.java
064: //http://core.netbeans.org/source/browse/core/src/org/netbeans/core/actions/RefreshAllFilesystemsAction.java
065: //http://java.netbeans.org/source/browse/java/api/src/org/netbeans/api/java/classpath/ClassPath.java
066:
067: private static FileSystem[] fileSystems;
068:
069: private static FileSystem[] getFileSystems() {
070: if (fileSystems != null) {
071: return fileSystems;
072: }
073: File[] roots = File.listRoots();
074: Set allRoots = new LinkedHashSet();
075: assert roots != null && roots.length > 0 : "Could not list file roots"; // NOI18N
076:
077: for (int i = 0; i < roots.length; i++) {
078: File root = roots[i];
079: FileObject random = FileUtil.toFileObject(root);
080: if (random == null)
081: continue;
082:
083: FileSystem fs;
084: try {
085: fs = random.getFileSystem();
086: allRoots.add(fs);
087: } catch (FileStateInvalidException e) {
088: throw new AssertionError(e);
089: }
090: }
091: FileSystem[] retVal = new FileSystem[allRoots.size()];
092: allRoots.toArray(retVal);
093: assert retVal.length > 0 : "Could not get any filesystem"; // NOI18N
094:
095: return fileSystems = retVal;
096: }
097:
098: /**
099: * Adds given listener to all FileSystems
100: * @param l listener to add
101: */
102: public static void addFileSystemsListener(FileChangeListener l) {
103: FileSystem[] fileSystems = getFileSystems();
104: for (int i = 0; i < fileSystems.length; i++) {
105: fileSystems[i].addFileChangeListener(l);
106: }
107: }
108:
109: /**
110: * Removes given listener to all FileSystems
111: * @param l listener to remove
112: */
113: public static void removeFileSystemsListener(FileChangeListener l) {
114: FileSystem[] fileSystems = getFileSystems();
115: for (int i = 0; i < fileSystems.length; i++) {
116: fileSystems[i].removeFileChangeListener(l);
117: }
118: }
119: }
|