Java Code Examples for org.openide.filesystems.FileUtil#findBrother()

The following examples show how to use org.openide.filesystems.FileUtil#findBrother() . 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: AssetData.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void saveProperties() throws FileAlreadyLockedException, IOException {
    OutputStream out = null;
    FileLock lock = null;
    try {
        FileObject pFile = file.getPrimaryFile();
        FileObject myFile = FileUtil.findBrother(pFile, extension);
        if (myFile == null) {
            myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension);
        }
        lock = myFile.lock();
        out = myFile.getOutputStream(lock);
        store(out, "");
    } finally {
        if (out != null) {
            out.close();
        }
        if (lock != null) {
            lock.releaseLock();
        }
    }
}
 
Example 2
Source File: BinaryModelFileLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** For a given file find the primary file. 
 * @param fo the file to find the primary file for
 * @return the primary file for this file or null
 * if this file is not recognized by this loader.
 */
protected FileObject findPrimaryFile(FileObject fo) {
    // never recognize folders.
    if (fo.isFolder()) {
        return null;
    }
    String ext = fo.getExt();
    if (ext.equalsIgnoreCase(INFO_EXTENSION)) {
        FileObject info = FileUtil.findBrother(fo, FILE_EXTENSION);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
    if (getExtensions().isRegistered(fo)) {
        return fo;
    }
    return null;
}
 
Example 3
Source File: CreatedModifiedFiles.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws IOException {
    FileObject target = FileUtil.createData(getProject().getProjectDirectory(), path);
    if (tokens == null) {
        copyByteAfterByte(content, target);
    } else {
        copyAndSubstituteTokens(content, target, tokens);
    }
    // #129446: form editor doesn't work sanely unless you do this:
    if (target.hasExt("form")) { // NOI18N
        FileObject java = FileUtil.findBrother(target, "java"); // NOI18N
        if (java != null) {
            java.setAttribute("justCreatedByNewWizard", true); // NOI18N
        }
    } else if (target.hasExt("java") && FileUtil.findBrother(target, "form") != null) { // NOI18N
        target.setAttribute("justCreatedByNewWizard", true); // NOI18N
    }
}
 
Example 4
Source File: FightWithWrongOrderInPathsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected FileObject findPrimaryFile(FileObject fo)
{
    LOG.info("FormKitDataLoader.findPrimaryFile(): " + fo.getNameExt());
    cnt++;
    
    String ext = fo.getExt();
    if (ext.equals(FORM_EXTENSION))
    {
        return FileUtil.findBrother(fo, JAVA_EXTENSION);
    }
    if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FORM_EXTENSION) != null)
    {
        return fo;
    }
    return null;
}
 
Example 5
Source File: MultiDataObjectDeleteSecondaryEntryTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected FileObject findPrimaryFile(FileObject fo)
{
    LOG.info("FXKitDataLoader.findPrimaryFile(): " + fo.getNameExt());
    cnt++;

    String ext = fo.getExt();
    if (ext.equals(FX_EXT))
    {
        return FileUtil.findBrother(fo, JAVA_EXTENSION);
    }
    if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FX_EXT) != null)
    {
        return fo;
    }
    return null;
}
 
Example 6
Source File: FightWithWrongOrderOfLoadersTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo)
{
    LOG.info("FormKitDataLoader.findPrimaryFile(): " + fo.getNameExt());
    cnt++;

    String ext = fo.getExt();
    if (ext.equals(FORM_EXTENSION))
    {
        return FileUtil.findBrother(fo, JAVA_EXTENSION);
    }
    if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FORM_EXTENSION) != null)
    {
        return fo;
    }
    return null;
}
 
Example 7
Source File: FightWithWrongOrderOfLoadersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, java.io.IOException
{
    LOG.info("FormKitDataLoader.createMultiObject(): " + primaryFile.getNameExt());

    return new FormKitDataObject(FileUtil.findBrother(primaryFile, FORM_EXTENSION),
            primaryFile,
            this);
}
 
Example 8
Source File: DataObjectSaveAsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 9
Source File: FightWithWrongOrderInPathsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, java.io.IOException
{
    LOG.info("FormKitDataLoader.createMultiObject(): " + primaryFile.getNameExt());

    return new FormKitDataObject(FileUtil.findBrother(primaryFile, FORM_EXTENSION),
            primaryFile,
            this);
}
 
Example 10
Source File: MultiDataObjectDeleteSecondaryEntryTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, java.io.IOException
{
    LOG.info("FXKitDataLoader.createMultiObject(): " + primaryFile.getNameExt());

    return new FXKitDataObject(FileUtil.findBrother(primaryFile, FX_EXT),
            primaryFile,
            this);
}
 
Example 11
Source File: FormDataLoader.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** For a given file finds a primary file.
    * @param fo the file to find primary file for
    *
    * @return the primary file for the file or null if the file is not
    *   recognized by this loader
    */
   @Override
   protected FileObject findPrimaryFile(FileObject fo) {
// never recognize folders.
       if (fo.isFolder()) return null;
       String ext = fo.getExt();
       if (ext.equals(FORM_EXTENSION))
           return FileUtil.findBrother(fo, JAVA_EXTENSION);

       FileObject javaFile = findJavaPrimaryFile(fo);
       return javaFile != null
                   && FileUtil.findBrother(javaFile, FORM_EXTENSION) != null ?
           javaFile : null;
   }
 
Example 12
Source File: DocumentTitlePropertyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 13
Source File: DataEditorSupportSaveAsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 14
Source File: PropertyChangeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 15
Source File: DataEditorSupportMoveTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 16
Source File: LayersBridge.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns instance of GlobalAction encapsulating action, or null.
 */
private GlobalAction createAction (DataObject dataObject, String prefix, String name, boolean ignoreUserRemoves) {
    InstanceCookie ic = dataObject.getCookie(InstanceCookie.class);
    // handle any non-IC file as instruction to remove the action
    FileObject pf = dataObject.getPrimaryFile();
    if (ignoreUserRemoves && pf.canRevert()) {
        return null;
    }
    if (ic == null) {
        if (!EXT_REMOVED.equals(pf.getExt())) {
            LOG.log(Level.WARNING, "Invalid shortcut: {0}", dataObject);
            return null;
        }
        // ignore the 'remove' file, if there's a shadow (= real action) present
        if (FileUtil.findBrother(pf, "shadow") != null) {
            // handle redefinition + removal: ignore the removal.
            return null;
        }
        return REMOVED;
    }
    try {
        Object action = ic.instanceCreate ();
        if (action == null) return null;
        if (!(action instanceof Action)) return null;
        return createAction((Action) action, prefix, name);
    } catch (Exception ex) {
        ex.printStackTrace ();
        return null;
    }
}
 
Example 17
Source File: FormDataLoader.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates the right data object for given primary file.
 * It is guaranteed that the provided file is realy primary file
 * returned from the method findPrimaryFile.
 *
 * @param primaryFile the primary file
 * @return the data object for this file
 * @exception DataObjectExistsException if the primary file already has data object
 */
@Override
protected MultiDataObject createMultiObject(FileObject primaryFile)
    throws DataObjectExistsException
{
    return new FormDataObject(FileUtil.findBrother(primaryFile, FORM_EXTENSION),
                              primaryFile,
                              this);
}
 
Example 18
Source File: LayersBridge.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** 
 * Reads original keymap entries, which are now replaced or deleted by the user's
 * writable layer.
 * 
 * @param root
 * @return 
 */
private Map<ShortcutAction, Set<String>> readOriginalKeymap(DataFolder root) {
    Map<ShortcutAction, Set<String>> keymap = 
            new HashMap<ShortcutAction, Set<String>> ();
    if (root == null) return keymap;

    FileObject fo = root.getPrimaryFile();
    Collection<FileObject> entries = (Collection<FileObject>)fo.getAttribute("revealEntries");
    Set<String> names = new HashSet<String>();
    for (FileObject f : entries) {
        try {
            GlobalAction action;
            
            names.add(f.getName());
            if (EXT_REMOVED.equals(f.getExt()) && FileUtil.findBrother(f, "shadow") == null) {
                action = REMOVED;
            } else {
                FileObject orig = DataShadow.findOriginal(f);
                if (orig == null) {
                    continue;
                }
                DataObject dataObject = DataObject.find(orig);

                if (dataObject instanceof DataFolder) continue;
                action = createActionWithLookup (dataObject, null, f.getName(), false);
            }
            if (action == null) continue;
            String shortcut = f.getName();

            LOG.log(Level.FINEST, "Overriden action {0}: {1}, by {2}", new Object[] {
                action.getId(),
                shortcut,
                f.getPath()
            });
            Set<String> s = keymap.get (action);
            if (s == null) {
                s = new HashSet<String> ();
                keymap.put (action, s);
            }
            s.add (shortcut);
        } catch (IOException ex) {
            // handle somehow
        }
    }
    return keymap;
}
 
Example 19
Source File: SCFTHandlerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testCreateWithNameAndExtForForm() throws Exception {
    LocalFileSystem lfs = new LocalFileSystem();
    lfs.setRootDirectory(getWorkDir());
    FileObject root = lfs.getRoot();
    FileObject fo = FileUtil.createData(root, "j.java");
    OutputStream os = fo.getOutputStream();
    String txt = "<html><h1>${nameAndExt}</h1></html>";
    os.write(txt.getBytes());
    os.close();
    fo.setAttribute("javax.script.ScriptEngine", "freemarker");
    
    FileObject fo2 = FileUtil.createData(root, "j.form");
    OutputStream os2 = fo2.getOutputStream();
    String txt2 = "<html><h2>${nameAndExt}</h2></html>";
    os2.write(txt2.getBytes());
    os2.close();
    fo2.setAttribute("javax.script.ScriptEngine", "freemarker");
    
    DataObject obj = DataObject.find(fo);
    assertEquals("Both files", 2, obj.files().size());
    
    DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
    
    Map<String,String> parameters = Collections.emptyMap();
    DataObject n = obj.createFromTemplate(folder, "complex", parameters);
    
    assertEquals("Two files", 2, n.files().size());
    
    
    FileObject newForm = FileUtil.findBrother(n.getPrimaryFile(), "form");
    
    assertEquals("Primary file is java", "java", n.getPrimaryFile().getExt());
    
    assertNotNull("Form copied", newForm);
    DataObject frm = DataObject.find(newForm);
    assertSame("Form belongs to java", n, frm);
    
    assertEquals("Created in right place", folder, n.getFolder());
    assertEquals("Created with right name", "complex", n.getName());
    
    String exp = "<html><h1>complex.java</h1></html>";
    assertEquals("Primary file" + n.getPrimaryFile(), exp, readFile(n.getPrimaryFile()));
    
    String exp2 = "<html><h2>complex.form</h2></html>";
    assertEquals(exp2, readFile(newForm));
}
 
Example 20
Source File: SCFTHandlerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testCreateWithNameAndExtForForm() throws Exception {
    LocalFileSystem lfs = new LocalFileSystem();
    lfs.setRootDirectory(getWorkDir());
    FileObject root = lfs.getRoot();
    FileObject fo = FileUtil.createData(root, "j.java");
    OutputStream os = fo.getOutputStream();
    String txt = "print('<html><h1>', nameAndExt, '</h1></html>')";
    os.write(txt.getBytes());
    os.close();
    fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js");
    
    FileObject fo2 = FileUtil.createData(root, "j.form");
    OutputStream os2 = fo2.getOutputStream();
    String txt2 = "print('<html><h2>', nameAndExt, '</h2></html>')";
    os2.write(txt2.getBytes());
    os2.close();
    fo2.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js");
    
    DataObject obj = DataObject.find(fo);
    assertEquals("Both files", 2, obj.files().size());
    
    DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
    
    Map<String,String> parameters = Collections.emptyMap();
    DataObject n = obj.createFromTemplate(folder, "complex", parameters);
    
    assertEquals("Two files", 2, n.files().size());
    
    
    FileObject newForm = FileUtil.findBrother(n.getPrimaryFile(), "form");
    
    assertEquals("Primary file is java", "java", n.getPrimaryFile().getExt());
    
    assertNotNull("Form copied", newForm);
    DataObject frm = DataObject.find(newForm);
    assertSame("Form belongs to java", n, frm);
    
    assertEquals("Created in right place", folder, n.getFolder());
    assertEquals("Created with right name", "complex", n.getName());
    
    String exp = "<html><h1> complex.java </h1></html>\n";
    assertEquals("Primary file" + n.getPrimaryFile(), exp, readFile(n.getPrimaryFile()));
    
    String exp2 = "<html><h2> complex.form </h2></html>\n";
    assertEquals(exp2, readFile(newForm));
}