annotate gui/edu/unc/genomics/ToolsTreeModel.java @ 2:e16016635b2a

Uploaded
author timpalpant
date Mon, 13 Feb 2012 22:12:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
1 package edu.unc.genomics;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
3 import java.io.IOException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
4 import java.nio.file.Files;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5 import java.nio.file.Path;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import java.nio.file.Paths;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8 import javax.swing.tree.DefaultMutableTreeNode;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 import javax.swing.tree.DefaultTreeModel;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10 import javax.xml.parsers.DocumentBuilder;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 import javax.xml.parsers.DocumentBuilderFactory;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 import javax.xml.parsers.ParserConfigurationException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14 import org.apache.log4j.Logger;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 import org.w3c.dom.Document;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16 import org.w3c.dom.Node;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17 import org.w3c.dom.NodeList;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 import org.xml.sax.SAXException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21 * Model for the ToolsTree
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 * Essentially just a DefaultTreeModel, but provides methods for
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 * loading the available tools from a configuration file
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24 *
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25 * @author timpalpant
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26 *
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28 public class ToolsTreeModel extends DefaultTreeModel {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 public static final Path DEFAULT_CONFIGURATION_FILE = Paths.get("toolConf.xml");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 private static final Logger log = Logger.getLogger(ToolsTreeModel.class);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 private static final long serialVersionUID = -6587614270922489960L;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36 public ToolsTreeModel() {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 super(new DefaultMutableTreeNode("Tools"));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 public static ToolsTreeModel loadDefaultConfig() throws ClassNotFoundException, ParserConfigurationException, SAXException, IOException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 return loadConfig(ResourceManager.getResourceDirectory().resolve(DEFAULT_CONFIGURATION_FILE));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 public static ToolsTreeModel loadConfig(Path p) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 ToolsTreeModel model = new ToolsTreeModel();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 // Populate the TreeModel with the tools in the default configuration file
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 log.debug("Loading tools from: " + DEFAULT_CONFIGURATION_FILE.toAbsolutePath());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 Document doc = dBuilder.parse(p.toFile());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 // Iterate over the sections
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 NodeList sections = doc.getElementsByTagName("section");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 log.debug("Found "+sections.getLength()+" sections");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 for (int i = 0; i < sections.getLength(); i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 Node section = sections.item(i);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 String sectionName = section.getAttributes().getNamedItem("name").getNodeValue();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 log.debug("Loading section: " + sectionName);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 DefaultMutableTreeNode sectionNode = new DefaultMutableTreeNode(sectionName);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
62 root.add(sectionNode);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
63 NodeList tools = section.getChildNodes();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
64
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
65 // Iterate over the tools in each section
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66 for (int j = 0; j < tools.getLength(); j++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 Node tool = tools.item(j);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 if (tool.getNodeType() == Node.ELEMENT_NODE && tool.getNodeName().equalsIgnoreCase("tool")) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 String toolName = tool.getAttributes().getNamedItem("name").getNodeValue();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70 log.debug("Loading tool: " + toolName);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 String toolClassName = tool.getAttributes().getNamedItem("class").getNodeValue();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 Class<? extends CommandLineTool> toolClass = (Class<? extends CommandLineTool>) Class.forName(toolClassName);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73 ToolsTreeNode toolNode = new ToolsTreeNode(toolName, toolClass);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
74 sectionNode.add(toolNode);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
75 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
76 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
77 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
78
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
79 return model;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
80 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
81
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
82 }