001: package org.columba.core.gui.externaltools;
002:
003: import java.io.File;
004:
005: import org.columba.core.config.Config;
006: import org.columba.core.xml.XmlElement;
007:
008: /**
009: * Provides an easy way to integrate external apps in Columba.
010: * <p>
011: * This includes a first-time assistant for the user. And a configuration file
012: * "external_tools.xml" to store the options of the external tools.
013: * <p>
014: * When using external commandline (already used examples are aspell and GnuPG)
015: * tools, you should just use this handler to get the location of the
016: * executable.
017: * <p>
018: * If the executable wasn't configured, yet a wizard will assist the user in
019: * configuring the external tool. If everything is correctly configured, it will
020: * just return the path of the commandline tool as <code>File</code>.
021: * <p>
022: * <verbatim> File file = getLocationOfExternalTool("gpg"); </verbatim>
023: *
024: * <p>
025: *
026: * @see org.columba.api.plugin.external_tools.xml
027: *
028: * @author fdietz
029: */
030: public class ExternalToolsManager {
031:
032: private static ExternalToolsManager instance = new ExternalToolsManager();
033:
034: private ExternalToolsManager() {
035: }
036:
037: public static ExternalToolsManager getInstance() {
038: return instance;
039: }
040:
041: /**
042: * Gets the location of an external commandline tool.
043: * <p>
044: * TODO: test this method
045: *
046: * @param toolID
047: * id of tool
048: * @return location of tool
049: */
050: public File getLocationOfExternalTool(String toolID) {
051:
052: // check configuration
053: XmlElement root = getConfiguration(toolID);
054:
055: if (root == null) {
056: // create xml node
057: XmlElement parent = Config.getInstance().get(
058: "external_tools").getElement("tools");
059: XmlElement child = new XmlElement("tool");
060: child.addAttribute("first_time", "true");
061: child.addAttribute("name", toolID);
062: parent.addElement(child);
063:
064: root = child;
065: }
066:
067: boolean firsttime = false;
068:
069: if (root.getAttribute("first_time").equals("true")) {
070: firsttime = true;
071: }
072:
073: if (firsttime) {
074: // start the configuration wizard
075: ExternalToolsWizardLauncher launcher = new ExternalToolsWizardLauncher();
076: launcher.launchWizard(toolID, true);
077:
078: if (launcher.isFinished()) {
079: // ok, now the tool is initialized correctly
080: XmlElement r = getConfiguration(toolID);
081: File file = new File(r.getAttribute("location"));
082:
083: return file;
084: }
085: } else {
086: String location = root.getAttribute("location");
087:
088: File file = new File(location);
089:
090: return file;
091: }
092:
093: return null;
094: }
095:
096: /**
097: * Gets xml configuration of tool with id.
098: *
099: * @param id
100: * id of tool
101: * @return xml treenode
102: */
103: public XmlElement getConfiguration(String 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: }
|