KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > externaltools > ExternalToolsManager


1 package org.columba.core.gui.externaltools;
2
3 import java.io.File JavaDoc;
4
5 import org.columba.core.config.Config;
6 import org.columba.core.xml.XmlElement;
7
8 /**
9  * Provides an easy way to integrate external apps in Columba.
10  * <p>
11  * This includes a first-time assistant for the user. And a configuration file
12  * "external_tools.xml" to store the options of the external tools.
13  * <p>
14  * When using external commandline (already used examples are aspell and GnuPG)
15  * tools, you should just use this handler to get the location of the
16  * executable.
17  * <p>
18  * If the executable wasn't configured, yet a wizard will assist the user in
19  * configuring the external tool. If everything is correctly configured, it will
20  * just return the path of the commandline tool as <code>File</code>.
21  * <p>
22  * <verbatim> File file = getLocationOfExternalTool("gpg"); </verbatim>
23  *
24  * <p>
25  *
26  * @see org.columba.api.plugin.external_tools.xml
27  *
28  * @author fdietz
29  */

30 public class ExternalToolsManager {
31
32     private static ExternalToolsManager instance = new ExternalToolsManager();
33
34     private ExternalToolsManager() {
35     }
36
37     public static ExternalToolsManager getInstance() {
38         return instance;
39     }
40
41     /**
42      * Gets the location of an external commandline tool.
43      * <p>
44      * TODO: test this method
45      *
46      * @param toolID
47      * id of tool
48      * @return location of tool
49      */

50     public File JavaDoc getLocationOfExternalTool(String JavaDoc toolID) {
51
52         // check configuration
53
XmlElement root = getConfiguration(toolID);
54
55         if (root == null) {
56             // create xml node
57
XmlElement parent = Config.getInstance().get("external_tools")
58                     .getElement("tools");
59             XmlElement child = new XmlElement("tool");
60             child.addAttribute("first_time", "true");
61             child.addAttribute("name", toolID);
62             parent.addElement(child);
63
64             root = child;
65         }
66
67         boolean firsttime = false;
68
69         if (root.getAttribute("first_time").equals("true")) {
70             firsttime = true;
71         }
72
73         if (firsttime) {
74             // start the configuration wizard
75
ExternalToolsWizardLauncher launcher = new ExternalToolsWizardLauncher();
76             launcher.launchWizard(toolID, true);
77
78             if (launcher.isFinished()) {
79                 // ok, now the tool is initialized correctly
80
XmlElement r = getConfiguration(toolID);
81                 File JavaDoc file = new File JavaDoc(r.getAttribute("location"));
82
83                 return file;
84             }
85         } else {
86             String JavaDoc location = root.getAttribute("location");
87
88             File JavaDoc file = new File JavaDoc(location);
89
90             return file;
91         }
92
93         return null;
94     }
95
96     /**
97      * Gets xml configuration of tool with id.
98      *
99      * @param id
100      * id of tool
101      * @return xml treenode
102      */

103     public XmlElement getConfiguration(String JavaDoc id) {
104         XmlElement root = Config.getInstance().get("external_tools")
105                 .getElement("tools");
106         for (int i = 0; i < root.count(); i++) {
107             XmlElement child = root.getElement(i);
108
109             if (child.getAttribute("name").equals(id)) {
110                 return child;
111             }
112         }
113
114         return null;
115     }
116 }
117
Popular Tags