I believe that knowing how to do this is going to be extremely useful for a lot of developers, especially those providing some kind of support for XML files, such as if you're creating web framework support, for example, or anything else involving XML files. Here are the steps, with all the code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE MIME-resolver PUBLIC "-//NetBeans//DTD MIME Resolver 1.0//EN"
"http://www.netbeans.org/dtds/mime-resolver-1_0.dtd">
<MIME-resolver><file>
<ext name="xml"/>
<resolver mime="text/x-project+xml">
<xml-rule>
<element ns="http://www.netbeans.org/ns/project/1"/>
</xml-rule>
</resolver>
</file>
</MIME-resolver>
private Map<String, List<String>> entries;
//We will call this method from our DataNode.
//When we do so, we parse the project.xml file
//and return org.w3c.dom.Node names to the DataNode:
synchronized Map<String, List<String>> getEntries() {
if (entries == null) {
parse();
}
return entries;
}
private void parse() {
try {
entries = new LinkedHashMap<String, List<String>>();
ListsectionEntries = null;
BufferedReader br = null;//Use the FileObject retrieved from the DataObject,
//via DataObject.getPrimaryFile(), to get the input stream:
br = new BufferedReader(new InputStreamReader(getPrimaryFile().getInputStream()));
InputSource source = new InputSource(br);//You could use any kind of parser, depending on your file type,
//though for XML files you can use the NetBeans IDE org.openide.xml.XMLUtil class
//to convert your input source to a org.w3c.dom.Document object:
org.w3c.dom.Document doc = XMLUtil.parse(source, false, false, null, null);
org.w3c.dom.NodeList list = doc.getElementsByTagName("\*");
int length = list.getLength();
for (int i = 0; i < length; i++) {
org.w3c.dom.Node mainNode = list.item(i);
String value = mainNode.getNodeName();//For purposes of this example, we simply put
//the name of the node in our linked hashmap:
entries.put(value, sectionEntries);
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
}
}
static class SectionChildren extends Children.Keys{
private ProjectDataObject obj;
private Lookup lookup;
private SectionChildren(ProjectDataObject obj, Lookup lookup) {
this.obj = obj;
this.lookup = lookup;
}//Called the first time that a list of nodes is needed.
//An example of this is when the node is expanded.
@Override
protected void addNotify() {
setKeys(obj.getEntries().keySet());
}//Called when the user collapses a node and starts working on
//something else. The NetBeans Platform will notice that the list
//of nodes is no longer needed, and it will free up the memory that
//is no longer being used.
@Override
protected void removeNotify() {
setKeys(Collections.emptyList());
}
//Called whenever a child node needs to be constructed.
@Override
protected Node[] createNodes(String key) {
return new Node[]{new SectionNode(key, obj.getEntries().get(key), lookup)};
}
}
static class SectionNode extends AbstractNode {
SectionNode(String name, java.util.Listentries, Lookup lookup) {
super(Children.LEAF);
setName(name);
setIconBaseWithExtension(ENTRY_IMAGE_ICON_BASE);
}
}
For further info on the above code, see NetBeans System Properties Module Tutorial.
public ProjectDataNode(ProjectDataObject obj) {
super(obj, new SectionChildren(obj, null));
setIconBaseWithExtension(SECTION_IMAGE_ICON_BASE);
}
ProjectDataNode(ProjectDataObject obj, Lookup lookup) {
super(obj, new SectionChildren(obj, lookup), lookup);
setIconBaseWithExtension(SECTION_IMAGE_ICON_BASE);
}
The bits in bold above are what I added to trigger the creating of the child nodes.
And that's all. Now you can install your module and expand the node of the file type that you're interested in. Using the parse method above, you can parse the content in any way you like, so that when the DataNode creates new section nodes, the data that you've parsed will be used to set the display name (or something else) on the child nodes. Here, for example, I've parsed the project.xml file in such a way that all the module dependencies appear as the display names of the child nodes:
You can also add child nodes below the existing child nodes, and so on.
thanks
Hi Geertjan,
thanks for your block entry. I just needed exactly just feature and it works perfectly for me.
best regards
Oliver