01: package abbot.script.parsers;
02:
03: import javax.swing.tree.TreePath;
04:
05: import abbot.i18n.Strings;
06: import abbot.script.ArgumentParser;
07:
08: /** Convert a {@link String} into a {@link javax.swing.tree.TreePath}. */
09: public class TreePathParser implements Parser {
10:
11: /** The string representation of a TreePath is what is usually generated
12: by its toString method, e.g.
13: <p>
14: [root, parent, child]
15: <p>
16: Nodes which contain a comma need to have that comma preceded by a
17: backslash to avoid it being interpreted as two separate nodes.<p>
18: NOTE: The returned TreePath is only a TreePath constructed of Strings;
19: it requires further manipulation to be turned into a true TreePath as
20: it relates to a given Tree.
21: */
22: public Object parse(String input) throws IllegalArgumentException {
23: if (!(input.startsWith("[") && input.endsWith("]"))) {
24: String msg = Strings.get("parser.treepath.bad_format",
25: new Object[] { input });
26: throw new IllegalArgumentException(msg);
27: }
28: input = input.substring(1, input.length() - 1);
29: // Use our existing utility for parsing a comma-separated list
30: String[] nodeNames = ArgumentParser.parseArgumentList(input);
31: // Strip off leading space, if there is one
32: for (int i = 0; i < nodeNames.length; i++) {
33: if (nodeNames[i] != null && nodeNames[i].startsWith(" "))
34: nodeNames[i] = nodeNames[i].substring(1);
35: }
36: TreePath path = new TreePath(nodeNames);
37: return path;
38: }
39: }
|