Java Code Examples for org.openide.filesystems.FileObject#isRoot()

The following examples show how to use org.openide.filesystems.FileObject#isRoot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SourceURLMapper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public @Override URL getURL(FileObject fo, int type) {
    if (type != URLMapper.INTERNAL) {
        return null;
    }
    try {
        FileSystem fs = fo.getFileSystem();
        if (fs instanceof SourceFS) {
            String path = fo.getPath();
            if (fo.isFolder() && !fo.isRoot()) {
                path += '/';
            }
            return url((SourceFS) fs, path);
        }
    } catch (FileStateInvalidException x) {
        // ignore
    }
    return null;
}
 
Example 2
Source File: SourcePathProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Returns source root for given ClassPath root as String, or <code>null</code>.
 */
public static String getRoot(FileObject fileObject) {
    File f = null;
    String path = "";
    try {
        if (fileObject.getFileSystem () instanceof JarFileSystem) {
            f = ((JarFileSystem) fileObject.getFileSystem ()).getJarFile ();
            if (!fileObject.isRoot()) {
                path = "!/"+fileObject.getPath();
            }
        } else {
            f = FileUtil.toFile (fileObject);
        }
    } catch (FileStateInvalidException ex) {
    }
    if (f != null) {
        return f.getAbsolutePath () + path;
    } else {
        return null;
    }
}
 
Example 3
Source File: SourceURLMapper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public @Override URL getURL(FileObject fo, int type) {
    if (type != URLMapper.INTERNAL) {
        return null;
    }
    try {
        FileSystem fs = fo.getFileSystem();
        if (fs instanceof SourceFS) {
            String path = fo.getPath();
            if (fo.isFolder() && !fo.isRoot()) {
                path += '/';
            }
            return url((SourceFS) fs, path);
        }
    } catch (FileStateInvalidException x) {
        // ignore
    }
    return null;
}
 
Example 4
Source File: TemplateManager.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
public static List<Template> findTemplates(String subFolder) {
    List<Template> tmp = new ArrayList<>();
    URL location = AndroidTemplateLocator.class.getProtectionDomain().getCodeSource().getLocation();
    FileObject fo = URLMapper.findFileObject(location);
    FileObject archiveFile = FileUtil.getArchiveRoot(fo);
    if (archiveFile == null && fo.isRoot()) {
        archiveFile = fo;
    }
    FileObject rootDir = archiveFile.getFileObject("/android/studio/imports/templates/" + subFolder);
    Enumeration<? extends FileObject> children = rootDir.getChildren(true);
    while (children.hasMoreElements()) {
        FileObject nextElement = children.nextElement();
        if ("template.xml".equalsIgnoreCase(nextElement.getNameExt())) {
            Template template = new Template(nextElement);
            TemplateMetadata metadata = template.getMetadata();
            tmp.add(template);
        }
    }
    return tmp;
}
 
Example 5
Source File: ActionProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isActionEnabled(String command, Lookup context) throws IllegalArgumentException {
    FileObject file = context.lookup(FileObject.class);

    if (file == null)
        return false;
    
    while (!file.isRoot()) {
        if (Utilities.isJDKRepository(file))
            return true;
        file = file.getParent();
    }

    return false;
}
 
Example 6
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testGetRoot() throws IOException {
    FileSystem fs = FileBasedFileSystem.getInstance();
    FileObject workDirFo = FileBasedFileSystem.getFileObject(getWorkDir());
    while (workDirFo != null && !workDirFo.isRoot()) {
        assertFalse(workDirFo.isRoot());
        workDirFo = workDirFo.getParent();
    }
    assertNotNull(workDirFo);
    assertTrue(workDirFo.isRoot());
    assertSame(workDirFo, fs.getRoot());
}
 
Example 7
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test of isFolder method, of class org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj.
 */
public void testIsFolder() {
    File f = testFile;
    
    while (f != null && f.exists()) {            
        FileObject fo = FileBasedFileSystem.getFileObject(f);
        assertNotNull(f.getAbsolutePath(),fo);
        if (fo.isFolder() && !fo.isRoot()) {
            assertTrue(fo instanceof FolderObj);
        }
       
        f = f.getParentFile();            
    }
}
 
Example 8
Source File: Actions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static boolean isAllowed(DataObject dataObject) {
    //Action is disabled for root folder eg:"/" on Linux or "C:" on Win
    if (dataObject == null) {
        return false;
    }
    FileObject fo = dataObject.getPrimaryFile();
    if (fo != null) {
        //#63459: Do not enable action on internal object/URL.
        if (URLMapper.findURL(fo, URLMapper.EXTERNAL) == null) {
            return false;
        }
        //Check if it is root.
        if (fo.isRoot()) {
            //It is root: disable.
            return false;
        }
    }

    // Fix #14740 disable action on SystemFileSystem.
    try {
        if (dataObject.getPrimaryFile().getFileSystem().isDefault()) {
            return false;
        }
    } catch (FileStateInvalidException fsie) {
        return false;
    }
    return true;
}
 
Example 9
Source File: Hinter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes an obsolete layer entry.
 * Also deletes empty parent directories.
 * @param entry a file to delete
 * @throws IOException in case of problem
 */
public void delete(FileObject entry) throws IOException {
    entry.delete();
    FileObject parent = entry.getParent();
    if (parent.getChildren().length == 0 && !parent.getAttributes().hasMoreElements()) {
        if (parent.isRoot()) {
            // XXX maybe delete the whole layer file! (and its reference in manifest.mf)
        } else {
            delete(parent);
        }
    }
}
 
Example 10
Source File: RepositoryUpdaterTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public ClassPath findClassPath(FileObject file, String type) {
    while (!file.isRoot()) {
        for (Entry<Entry<FileObject, String>, ClassPath> e : classPathSpec.entrySet()) {
            if (e.getKey().getKey() == file && e.getKey().getValue().equals(type)) {
                return e.getValue();
            }
        }
        file = file.getParent();
    }

    return null;
}
 
Example 11
Source File: TemplateManager.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static FileObject getRootFolder() {
    URL location = AndroidTemplateLocator.class.getProtectionDomain().getCodeSource().getLocation();
    FileObject fo = URLMapper.findFileObject(location);
    FileObject archiveFile = FileUtil.getArchiveRoot(fo);
    if (archiveFile == null && fo.isRoot()) {
        archiveFile = fo;
    }
    return archiveFile.getFileObject("/android/studio/imports/templates/");
}
 
Example 12
Source File: JavaDataLoader.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Modify the replacement map.
      * May be extended in subclasses to provide additional key/value
      * pairs sensitive to the details of instantiation.
      * @param map the map to add to
      * @param target the destination folder for instantiation
      * @param n the new file name
      * @param e the new file extension
      */
      protected void modifyMap(Map<String, String> map, FileObject target, String n, String e) {
          ClassPath cp = ClassPath.getClassPath(target, ClassPath.SOURCE);
          String resourcePath = "";
          if (cp != null) {
              resourcePath = cp.getResourceName(target);
              if (resourcePath == null) {
                  Logger.getLogger(JavaDataLoader.class.getName()).log(Level.WARNING, "{0} is not on its own source path", FileUtil.getFileDisplayName(target));
                  resourcePath = "";
              }
          } else {
              Logger.getLogger(JavaDataLoader.class.getName()).warning("No classpath was found for folder: "+target);
          }
          map.put("NAME", n); // NOI18N
          // Yes, this is package sans filename (target is a folder).
          map.put("PACKAGE", resourcePath.replace('/', '.')); // NOI18N
          map.put("PACKAGE_SLASHES", resourcePath); // NOI18N
   // Fully-qualified name:
   if (target.isRoot ()) {
map.put ("PACKAGE_AND_NAME", n); // NOI18N
map.put ("PACKAGE_AND_NAME_SLASHES", n); // NOI18N
   } else {
map.put ("PACKAGE_AND_NAME", resourcePath.replace('/', '.') + '.' + n); // NOI18N
map.put ("PACKAGE_AND_NAME_SLASHES", resourcePath + '/' + n); // NOI18N
   }
          // No longer needed due to #6025. (You can just put in quotes, they will not
          // prevent things from being escaped.) But leave the token here for
          // compatibility with old templates. --jglick 26/08/00
          map.put("QUOTES","\""); // NOI18N
          
          for (CreateFromTemplateAttributesProvider provider
                  : Lookup.getDefault().lookupAll(CreateFromTemplateAttributesProvider.class)) {
              Map<String, ?> attrs = provider.attributesFor(
                      getDataObject(),
                      DataFolder.findFolder(target),
                      n);
              if (attrs == null) //#123006
                  continue;
              Object aName = attrs.get("user"); // NOI18N
              if (aName instanceof String) {
                  map.put("USER", (String) aName); // NOI18N
                  break;
              }
          }
      }
 
Example 13
Source File: FavoritesNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Action[] getActions(boolean context) {
    Action[] arr;
    arr = super.getActions(context);
    
    //Find if given node is root
    boolean isRoot = false;
    FileObject fo = getOriginal().getLookup().lookup(FileObject.class);
    if (fo == null) {
        Logger.getLogger(FavoritesNode.class.getName()).log(Level.INFO, "No FO in node: {0}:{1}", //NOI18N
                new Object[] { getOriginal().getName(), getOriginal()});
    } else {
        //Check if it is root.
        isRoot = fo.isRoot();
    }
    
    if (isRoot) {
        return createActionsForRoot(arr, FavoritesNode.getNode().equals(this.getParentNode()));
    } else {
        if (FavoritesNode.getNode().equals(this.getParentNode())) {
            DataShadow ds = getShadow();
            if (ds != null) {
                if (ds.getOriginal().getPrimaryFile().isFolder()) {
                    return createActionsForFavoriteFolder(arr);
                } else {
                    return createActionsForFavoriteFile(arr);
                }
            }
        } else {
            if (fo != null) {
                if (fo.isFolder()) {
                    return createActionsForFolder(arr);
                } else {
                    return createActionsForFile(arr);
                }
            }
        }
    }
    //Unknown node - return unmodified actions.
    return arr;
}
 
Example 14
Source File: JFXProjectUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Finds the relative path to targetFO from sourceFO. 
 * Unlike FileUtil.getRelativePath() does not require targetFO to be within sourceFO sub-tree
 * Returns null if there is no shared parent directory except root.
 * 
 * @param sourceFO file/dir to which the relative path will be related
 * @param targetFO file whose location will be determined with respect to sourceFO
 * @return string relative path leading from sourceFO to targetFO
 */
public static String getRelativePath(@NonNull final FileObject sourceFO, @NonNull final FileObject targetFO) {
    String path = ""; //NOI18N
    FileObject src = sourceFO;
    FileObject tgt = targetFO;
    String targetName = null;
    if(!src.isFolder()) {
        src = src.getParent();
    }
    if(!tgt.isFolder()) {
        targetName = tgt.getNameExt();
        tgt = tgt.getParent();
    }
    LinkedList<String> srcSplit = new LinkedList<String>();
    LinkedList<String> tgtSplit = new LinkedList<String>();
    while(!src.isRoot()) {
        srcSplit.addFirst(src.getName());
        src = src.getParent();
    }
    while(!tgt.isRoot()) {
        tgtSplit.addFirst(tgt.getName());
        tgt = tgt.getParent();
    }
    boolean share = false;
    while(!srcSplit.isEmpty() && !tgtSplit.isEmpty()) {
        if(srcSplit.getFirst().equals(tgtSplit.getFirst())) {
            srcSplit.removeFirst();
            tgtSplit.removeFirst();
            share = true;
        } else {
            break;
        }
    }
    if(!share) {
        return null;
    }
    for(int left = 0; left < srcSplit.size(); left++) {
        if(left == 0) {
            path += ".."; //NOI18N
        } else {
            path += "/.."; //NOI18N
        }
    }
    while(!tgtSplit.isEmpty()) {
        if(path.isEmpty()) {
            path += tgtSplit.getFirst();
        } else {
            path += "/" + tgtSplit.getFirst(); //NOI18N
        }
        tgtSplit.removeFirst();
    }
    if(targetName != null) {
        if(!path.isEmpty()) {
            path += "/" + targetName; //NOI18N
        } else {
            path += targetName;
        }
    }
    return path;
}
 
Example 15
Source File: GsfDataLoader.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) {
    FileEntry.Format entry = new FileEntry.Format(obj, primaryFile) {
        @Override
        protected java.text.Format createFormat (FileObject target, String n, String e) {
            ClassPath cp = ClassPath.getClassPath(target, ClassPath.SOURCE);
            String resourcePath = "";
            if (cp != null) {
                resourcePath = cp.getResourceName(target);
                if (resourcePath == null) {
                    resourcePath = ""; // NOI18N
                }
            } else {
                ErrorManager.getDefault().log(ErrorManager.WARNING, "No classpath was found for folder: "+target);
            }
            Map<String,String> m = new HashMap<String,String>();
            m.put("NAME", n ); //NOI18N
            String capitalizedName;
            if (n.length() > 1) {
                capitalizedName = Character.toUpperCase(n.charAt(0))+n.substring(1);
            } else if (n.length() == 1) {
                capitalizedName = ""+Character.toUpperCase(n.charAt(0));
            } else {
                capitalizedName = "";
            }
            m.put("CAPITALIZEDNAME", capitalizedName); //NOI18N
            m.put("LOWERNAME", n.toLowerCase()); //NOI18N
            m.put("UPPERNAME", n.toUpperCase()); //NOI18N

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n.length(); i++) {
                char c = n.charAt(i);
                if (Character.isJavaIdentifierPart(c)) {
                    sb.append(c);
                }
            }
            String identifier = sb.toString();
            m.put("IDENTIFIER", identifier); // NOI18N
            sb.setCharAt(0, Character.toUpperCase(identifier.charAt(0)));
            m.put("CAPITALIZEDIDENTIFIER", sb.toString()); // NOI18N
            m.put("LOWERIDENTIFIER", identifier.toLowerCase()); //NOI18N
            
            // Yes, this is package sans filename (target is a folder).
            String packageName = resourcePath.replace('/', '.');
            m.put("PACKAGE", packageName); // NOI18N
            String capitalizedPkgName;
            if (packageName == null || packageName.length() == 0) {
                packageName = "";
                capitalizedPkgName = "";
            } else if (packageName.length() > 1) {
                capitalizedPkgName = Character.toUpperCase(packageName.charAt(0))+packageName.substring(1);
            } else {
                capitalizedPkgName = ""+Character.toUpperCase(packageName.charAt(0));
            }
            m.put("CAPITALIZEDPACKAGE", capitalizedPkgName); // NOI18N
            m.put("PACKAGE_SLASHES", resourcePath); // NOI18N
            // Fully-qualified name:
            if (target.isRoot ()) {
                m.put ("PACKAGE_AND_NAME", n); // NOI18N
                m.put ("PACKAGE_AND_NAME_SLASHES", n); // NOI18N
            } else {
                m.put ("PACKAGE_AND_NAME", resourcePath.replace('/', '.') + '.' + n); // NOI18N
                m.put ("PACKAGE_AND_NAME_SLASHES", resourcePath + '/' + n); // NOI18N
            }
            m.put("DATE", DateFormat.getDateInstance(DateFormat.LONG).format(new Date())); // NOI18N
            m.put("TIME", DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date())); // NOI18N
            MapFormat f = new MapFormat(m);
            f.setLeftBrace( "__" ); //NOI18N
            f.setRightBrace( "__" ); //NOI18N
            f.setExactMatch(false);

            return f;
        }
    };
    return entry;
}