Java Code Examples for org.openide.filesystems.FileObject.getAttributes()

The following are Jave code examples for showing how to use getAttributes() of the org.openide.filesystems.FileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: SaveAsTemplateAction.java   Source Code and License Vote up 6 votes
/** Returns map of attributes for given FileObject. */ // XXX: copied from org.netbeans.modules.favorites.templates
private static Map<String, Object> getAttributes (FileObject fo) {
    HashMap<String, Object> attributes = new HashMap<String, Object> ();
    Enumeration<String> attributeNames = fo.getAttributes ();
    while (attributeNames.hasMoreElements ()) {
        String attrName = attributeNames.nextElement ();
        if (attrName == null) {
            continue;
        }
        Object attrValue = fo.getAttribute (attrName);
        if (attrValue != null) {
            attributes.put (attrName, attrValue);
        }
    }
    return attributes;
}
 
Example 2
Project: incubator-netbeans   File: SystemFileSystemTest.java   Source Code and License Vote up 5 votes
public void testUserHasPreferenceOverFSs() throws Exception {
    FileObject global = FileUtil.createData(FileUtil.getConfigRoot(), "dir/file.txt");
    global.setAttribute("global", 3);
    write(global, "global");
    
    FileObject fo1 = FileUtil.createData(fs1.getRoot(), "dir/file.txt");
    fo1.setAttribute("one", 1);
    write(fo1, "fileone");
    
    FileObject fo2 = FileUtil.createData(fs2.getRoot(), "dir/file.txt");
    fo2.setAttribute("two", 2);
    write(fo2, "two");

    events.clear();
    
    MainLookup.register(fs1, this);
    MainLookup.register(fs2, this);
    
    Enumeration<String> en = global.getAttributes();
    TreeSet<String> t = new TreeSet<String>();
    while (en.hasMoreElements()) {
        t.add(en.nextElement());
    }
    
    assertEquals("three elements: " + t, 3, t.size());
    assertTrue(t.contains("two"));
    assertTrue(t.contains("one"));
    assertTrue(t.contains("global"));
    
    assertEquals(1, global.getAttribute("one"));
    assertEquals(2, global.getAttribute("two"));
    assertEquals(3, global.getAttribute("global"));

    assertEquals("contains global", 6, global.getSize());
    assertEquals("global", read(global));
    
    
    assertTrue("no events: " + events, events.isEmpty());
}
 
Example 3
Project: incubator-netbeans   File: MenuBar.java   Source Code and License Vote up 5 votes
/** Constructor. */
public LazyMenu(final DataFolder df, boolean icon) {
    this.master = df;
    this.icon = icon;
    this.dynaModel = new DynaMenuModel();
    this.slave = new MenuFolder();
    
    setName(df.getName());
    final FileObject pf = df.getPrimaryFile();
    Object prefix = pf.getAttribute("property-prefix"); // NOI18N
    if (prefix instanceof String) {
        Enumeration<String> en = pf.getAttributes();
        while (en.hasMoreElements()) {
            String attrName = en.nextElement();
            if (attrName.startsWith((String)prefix)) {
                putClientProperty(
                    attrName.substring(((String)prefix).length()), 
                    pf.getAttribute(attrName)
                );
            }
        }
    }

    // Listen for changes in Node's DisplayName/Icon
    Node n = master.getNodeDelegate ();
    n.addNodeListener (org.openide.nodes.NodeOp.weakNodeListener (this, n));
    Mutex.EVENT.readAccess(this);
    getModel().addChangeListener(this);
}
 
Example 4
Project: incubator-netbeans   File: QuickSearchProvider.java   Source Code and License Vote up 5 votes
private void readKeywordsFromNewAnnotation() {
FileObject keywordsFOs = FileUtil.getConfigRoot().getFileObject(CategoryModel.OD_LAYER_KEYWORDS_FOLDER_NAME);

       for(FileObject keywordsFO : keywordsFOs.getChildren()) {
    String location = keywordsFO.getAttribute("location").toString(); //NOI18N
    String tabTitle = keywordsFO.getAttribute("tabTitle").toString(); //NOI18N
    Set<String> keywords = new HashSet<String>();
    Enumeration<String> attributes = keywordsFO.getAttributes();
    while (attributes.hasMoreElements()) {
	String attribute = attributes.nextElement();
	if (attribute.startsWith("keywords")) {
	    String word = keywordsFO.getAttribute(attribute).toString();
	    for (String s : word.split(",")) {
		keywords.add(s.trim());
	    }
	}
    }
    String path = location.concat("/").concat(tabTitle);
    Set<String> keywordsFromPath = path2keywords.get(path);
    if(keywordsFromPath != null) {
	for (String keyword : keywordsFromPath) {
	    if (!keywords.contains(keyword)) {
		keywords.add(keyword);
	    }
	}
    }
    path2keywords.put(path, keywords);
       }
   }
 
Example 5
Project: incubator-netbeans   File: OutputKeymapManager.java   Source Code and License Vote up 5 votes
private void storeShortCuts(String profileName) {
    FileObject root = FileUtil.getConfigRoot();
    try {
        FileObject actionsDir = FileUtil.createFolder(
                root, STORAGE_DIR + profileName);
        for (OutWinShortCutAction a : allActions) {
            FileObject data = actionsDir.getFileObject(a.getId());
            if (data == null) {
                data = actionsDir.createData(a.getId());
            } else if (data.isFolder()) {
                throw new IOException(data + " is a folder.");      //NOI18N
            }
            Enumeration<String> atts = data.getAttributes();
            while (atts.hasMoreElements()) {
                String attName = atts.nextElement();
                data.setAttribute(attName, null);
            }
            int index = 1;
            if (keymaps.get(profileName).get(a) == null) {
                continue;
            }
            for (String shortCut : keymaps.get(profileName).get(a)) {
                data.setAttribute(SHORTCUT_PREFIX + index++, shortCut);
            }
        }
    } catch (IOException e) {
        LOG.log(Level.WARNING, "Cannot create folder", e);          //NOI18N
    }
}
 
Example 6
Project: incubator-netbeans   File: OutputKeymapManager.java   Source Code and License Vote up 5 votes
private void loadShortCuts() {
    FileObject root = FileUtil.getConfigRoot();
    FileObject actionsDir = root.getFileObject(STORAGE_DIR);
    if (actionsDir == null) {
        return;
    }
    for (FileObject profileDir : actionsDir.getChildren()) {
        if (!profileDir.isFolder()) {
            continue;
        }
        Map<ShortcutAction, Set<String>> keymap =
                new HashMap<ShortcutAction, Set<String>>();
        keymaps.put(profileDir.getName(), keymap);
        for (OutWinShortCutAction a : allActions) {
            FileObject actionFile = profileDir.getFileObject(a.getId());
            if (actionFile == null || !actionFile.isData()) {
                keymap.put(a, Collections.<String>emptySet());
                continue;
            }
            Enumeration<String> atts = actionFile.getAttributes();
            Set<String> strokes = new HashSet<String>();
            while (atts.hasMoreElements()) {
                String att = atts.nextElement();
                if (att.startsWith(SHORTCUT_PREFIX)) {
                    strokes.add((String) actionFile.getAttribute(att));
                }
            }
            keymap.put(a, strokes);
        }
    }
}
 
Example 7
Project: incubator-netbeans   File: SystemFileSystemTest.java   Source Code and License Vote up 4 votes
public void testUserHasPreferenceOverFSsButGeneratesAnEvent() throws Exception {
    FileObject fo1 = FileUtil.createData(fs1.getRoot(), "dir/file.txt");
    fo1.setAttribute("one", 1);
    write(fo1, "fileone");
    
    FileObject fo2 = FileUtil.createData(fs2.getRoot(), "dir/file.txt");
    fo2.setAttribute("two", 2);
    write(fo2, "two");

    events.clear();
    
    MainLookup.register(fs1, this);
    MainLookup.register(fs2, this);

    assertFalse("not empty", events.isEmpty());
    events.clear();
    
    FileObject global = FileUtil.createData(FileUtil.getConfigRoot(), "dir/file.txt");
    global.setAttribute("global", 3);
    write(global, "global");
    
    assertFalse("yet another set", events.isEmpty());
    
    Enumeration<String> en = global.getAttributes();
    TreeSet<String> t = new TreeSet<String>();
    while (en.hasMoreElements()) {
        t.add(en.nextElement());
    }

    
    
    assertEquals("three elements: " + t, 3, t.size());
    assertTrue(t.contains("two"));
    assertTrue(t.contains("one"));
    assertTrue(t.contains("global"));
    
    assertEquals(1, global.getAttribute("one"));
    assertEquals(2, global.getAttribute("two"));
    assertEquals(3, global.getAttribute("global"));

    assertEquals("contains global", 6, global.getSize());
    assertEquals("global", read(global));
}
 
Example 8
Project: incubator-netbeans   File: FolderPathLookup.java   Source Code and License Vote up 4 votes
/** get class name from specified file object*/
private static String getClassName(FileObject fo) {
    // first of all try "instanceClass" property of the primary file
    Object attr = fo.getAttribute ("instanceClass");
    if (attr instanceof String) {
        return BaseUtilities.translate((String) attr);
    } else if (attr != null) {
        LOG.warning(
            "instanceClass was a " + attr.getClass().getName()); // NOI18N
    }

    attr = fo.getAttribute("instanceCreate");
    if (attr != null) {
        return attr.getClass().getName();
    } else {
        Enumeration<String> attributes = fo.getAttributes();
        while (attributes.hasMoreElements()) {
            if (attributes.nextElement().equals("instanceCreate")) {
                // It was specified, just unloadable (usually a methodvalue).
                return null;
            }
        }
    }

    // otherwise extract the name from the filename
    String name = fo.getName ();

    int first = name.indexOf('[') + 1;
    if (first != 0) {
        LOG.log(Level.WARNING, "Cannot understand {0}", fo);
    }

    int last = name.indexOf (']');
    if (last < 0) {
        last = name.length ();
    }

    // take only a part of the string
    if (first < last) {
        name = name.substring (first, last);
    }

    name = name.replace ('-', '.');
    name = BaseUtilities.translate(name);

    return name;
}
 
Example 9
Project: incubator-netbeans   File: ValidateLayerConsistencyTest.java   Source Code and License Vote up 4 votes
public void testAreAttributesFine () {
    List<String> errors = new ArrayList<String>();
    
    FileObject root = FileUtil.getConfigRoot();
    Enumeration<? extends FileObject> files = Enumerations.concat(Enumerations.singleton(root), root.getChildren(true));
    while (files.hasMoreElements()) {
        FileObject fo = files.nextElement();
        
        if (
            "Keymaps/NetBeans/D-BACK_QUOTE.shadow".equals(fo.getPath()) ||
            "Keymaps/NetBeans55/D-BACK_QUOTE.shadow".equals(fo.getPath()) ||
            "Keymaps/Emacs/D-BACK_QUOTE.shadow".equals(fo.getPath())
        ) {
            // #46753
            continue;
        }
        if (
            "Services/Browsers/FirefoxBrowser.settings".equals(fo.getPath()) ||
            "Services/Browsers/MozillaBrowser.settings".equals(fo.getPath()) ||
            "Services/Browsers/NetscapeBrowser.settings".equals(fo.getPath())
        ) {
            // #161784
            continue;
        }
        
        Enumeration<String> attrs = fo.getAttributes();
        while (attrs.hasMoreElements()) {
            String name = attrs.nextElement();

            if (isInstanceAttribute(name)) {
                continue;
            }
            
            if (name.indexOf('\\') != -1) {
                errors.add("File: " + fo.getPath() + " attribute name must not contain backslashes: " + name);
            }
            
            Object attr = fo.getAttribute(name);
            if (attr == null) {
                CharSequence warning = Log.enable("", Level.WARNING);
                if (
                    fo.getAttribute("class:" + name) != null &&
                    fo.getAttribute(name) == null &&
                    warning.length() == 0
                ) {
                    // ok, factory method returned null
                    continue;
                }

                errors.add("File: " + fo.getPath() + " attribute name: " + name);
            }

            if (attr instanceof URL) {
                URL u = (URL) attr;
                int read = -1;
                try {
                    read = u.openStream().read(new byte[4096]);
                } catch (IOException ex) {
                    errors.add(fo.getPath() + ": " + ex.getMessage());
                }
                if (read <= 0) {
                    errors.add("URL resource does not exist: " + fo.getPath() + " attr: " + name + " value: " + attr);
                }
            }

        }
    }
    
    assertNoErrors("Some attributes in files are unreadable", errors);
}
 
Example 10
Project: incubator-netbeans   File: DefaultSettingsContext.java   Source Code and License Vote up 4 votes
public BindingEnumeration(FileObject fo) {
    this.fo = fo;
    this.en = fo.getAttributes();
}
 
Example 11
Project: incubator-netbeans   File: RootNode.java   Source Code and License Vote up 4 votes
static void enableActionsOnExpand(ServerRegistry registry) {
    FileObject fo = FileUtil.getConfigFile(registry.getPath()+"/Actions"); // NOI18N
    Enumeration<String> en;
    if (fo != null) {
        for (FileObject o : fo.getChildren()) {
            en = o.getAttributes();
            while (en.hasMoreElements()) {
                String attr = en.nextElement();
                boolean enable = false;
                final String prefix = "property-"; // NOI18N
                if (attr.startsWith(prefix)) {
                    attr = attr.substring(prefix.length());
                    if (System.getProperty(attr) != null) {
                        enable = true;
                    }
                } else {
                    final String config = "config-"; // NOI18N
                    if (attr.startsWith(config)) {
                        attr = attr.substring(config.length());
                        FileObject configFile = FileUtil.getConfigFile(attr);
                        if (configFile != null) {
                            if (!configFile.isFolder() || configFile.getChildren().length > 0) {
                                enable = true;
                            }
                        }
                    }
                }

                if (enable) {
                    Lookup l = Lookups.forPath(registry.getPath()+"/Actions"); // NOI18N
                    for (Lookup.Item<Action> item : l.lookupResult(Action.class).allItems()) {
                        if (item.getId().contains(o.getName())) {
                            Action a = item.getInstance();
                            a.actionPerformed(new ActionEvent(getInstance(), 0, "noui")); // NOI18N
                        }
                    }
                }
            }
        }
    }
}
 
Example 12
Project: incubator-netbeans   File: RecognizeInstanceFiles.java   Source Code and License Vote up 4 votes
/** get class name from specified file object*/
private static String getClassName(FileObject fo) {
    // first of all try "instanceClass" property of the primary file
    Object attr = fo.getAttribute ("instanceClass");
    if (attr instanceof String) {
        return BaseUtilities.translate((String) attr);
    } else if (attr != null) {
        LOG.warning(
            "instanceClass was a " + attr.getClass().getName()); // NOI18N
    }

    attr = fo.getAttribute("instanceCreate");
    if (attr != null) {
        return attr.getClass().getName();
    } else {
        Enumeration<String> attributes = fo.getAttributes();
        while (attributes.hasMoreElements()) {
            if (attributes.nextElement().equals("instanceCreate")) {
                // It was specified, just unloadable (usually a methodvalue).
                return null;
            }
        }
    }

    // otherwise extract the name from the filename
    String name = fo.getName ();

    int first = name.indexOf('[') + 1;
    if (first != 0) {
        LOG.log(Level.WARNING, "Cannot understand {0}", fo);
    }

    int last = name.indexOf (']');
    if (last < 0) {
        last = name.length ();
    }

    // take only a part of the string
    if (first < last) {
        name = name.substring (first, last);
    }

    name = name.replace ('-', '.');
    name = BaseUtilities.translate(name);

    //System.out.println ("Original: " + getPrimaryFile ().getName () + " new one: " + name); // NOI18N
    return name;
}
 
Example 13
Project: incubator-netbeans   File: OptionsPanel.java   Source Code and License Vote up 4 votes
private void handlePanel(FileObject keywordsFO) {
       String location = keywordsFO.getAttribute("location").toString(); //NOI18N
       String tabTitle = keywordsFO.getAttribute("tabTitle").toString(); //NOI18N
       JTabbedPane pane = categoryid2tabbedpane.get(location);
       int tabIndex = pane == null ? -1 : pane.indexOfTab(tabTitle);

       Set<String> keywords = new HashSet<String>();
keywords.add(location.toUpperCase());
       keywords.add(tabTitle.toUpperCase());
Enumeration<String> attributes = keywordsFO.getAttributes();
while(attributes.hasMoreElements()) {
    String attribute = attributes.nextElement();
    if(attribute.startsWith("keywords")) {
	String word = keywordsFO.getAttribute(attribute).toString();
	keywords.add(word.toUpperCase());
    }
}

       ArrayList<String> words = categoryid2words.get(location);
       if (words == null) {
           words = new ArrayList<String>();
       }

       Set<String> newWords = new HashSet<String>();
       for (String keyword : keywords) {
           if (!words.contains(keyword)) {
               newWords.add(keyword);
            }
        }

       words.addAll(newWords);
       categoryid2words.put(location, words);

       if (!categoryid2tabs.containsKey(location)) {
           categoryid2tabs.put(location, new HashMap<Integer, TabInfo>());
       }
       HashMap<Integer, TabInfo> categoryTabs = categoryid2tabs.get(location);
       TabInfo tabInfo;
       if (!categoryTabs.containsKey(tabIndex)) {
           tabInfo = new TabInfo();
       } else {
           tabInfo = categoryTabs.get(tabIndex);
       }
       tabInfo.addWords(keywords);
       categoryTabs.put(tabIndex, tabInfo);
       categoryid2tabs.put(location, categoryTabs);
    }