|
8 | 8 | import java.util.TreeMap;
|
9 | 9 |
|
10 | 10 | /**
|
11 |
| - * Represents a single node in a tree and simplifies adding children and serializing the final |
12 |
| - * tree into JSON. |
| 11 | + * Represents a single node in a tree and simplifies adding children and |
| 12 | + * serializing the final tree into JSON. |
| 13 | + * |
13 | 14 | * @author elandau
|
14 | 15 | */
|
15 | 16 | public class DynaTreeNode {
|
16 |
| - private String title; |
17 |
| - private String key; |
18 |
| - private String mode; |
19 |
| - private Map<String, DynaTreeNode> children; |
20 |
| - private boolean noLink = true; |
21 |
| - |
22 |
| - public DynaTreeNode() { |
23 |
| - |
24 |
| - } |
25 |
| - |
26 |
| - public DynaTreeNode setTitle(String title) { |
27 |
| - this.title = title; |
28 |
| - return this; |
29 |
| - } |
30 |
| - |
31 |
| - public String getTitle() { |
32 |
| - return this.title; |
33 |
| - } |
34 |
| - |
35 |
| - public DynaTreeNode setNoLink(boolean noLink) { |
36 |
| - this.noLink = noLink; |
37 |
| - return this; |
38 |
| - } |
39 |
| - |
40 |
| - public boolean getNoLink() { |
41 |
| - return this.noLink; |
42 |
| - } |
43 |
| - |
44 |
| - public DynaTreeNode setKey(String key) { |
45 |
| - this.key = key; |
46 |
| - return this; |
47 |
| - } |
48 |
| - |
49 |
| - public String getKey() { |
50 |
| - return this.key; |
51 |
| - } |
52 |
| - |
53 |
| - public DynaTreeNode setMode(String mode) { |
54 |
| - this.mode = mode; |
55 |
| - return this; |
56 |
| - } |
57 |
| - |
58 |
| - public String getMode() { |
59 |
| - return this.mode; |
60 |
| - } |
61 |
| - |
62 |
| - public Map<String, DynaTreeNode> getChildren() { |
63 |
| - if (children == null) { |
64 |
| - children = new TreeMap<String, DynaTreeNode>(); |
65 |
| - } |
66 |
| - return children; |
67 |
| - } |
68 |
| - |
69 |
| - public DynaTreeNode getChild(String title) { |
70 |
| - return getChildren().get(title); |
71 |
| - } |
72 |
| - |
73 |
| - public void putChild(DynaTreeNode child) { |
74 |
| - getChildren().put(child.title, child); |
75 |
| - } |
76 |
| - |
77 |
| - @SuppressWarnings("unchecked") |
| 17 | + private String title; |
| 18 | + private String key; |
| 19 | + private String mode; |
| 20 | + private Map<String, DynaTreeNode> children; |
| 21 | + private boolean noLink = true; |
| 22 | + |
| 23 | + public DynaTreeNode() { |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + public DynaTreeNode setTitle(String title) { |
| 28 | + this.title = title; |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + public String getTitle() { |
| 33 | + return this.title; |
| 34 | + } |
| 35 | + |
| 36 | + public DynaTreeNode setNoLink(boolean noLink) { |
| 37 | + this.noLink = noLink; |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + public boolean getNoLink() { |
| 42 | + return this.noLink; |
| 43 | + } |
| 44 | + |
| 45 | + public DynaTreeNode setKey(String key) { |
| 46 | + this.key = key; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public String getKey() { |
| 51 | + return this.key; |
| 52 | + } |
| 53 | + |
| 54 | + public DynaTreeNode setMode(String mode) { |
| 55 | + this.mode = mode; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public String getMode() { |
| 60 | + return this.mode; |
| 61 | + } |
| 62 | + |
| 63 | + public Map<String, DynaTreeNode> getChildren() { |
| 64 | + if (children == null) { |
| 65 | + children = new TreeMap<String, DynaTreeNode>(); |
| 66 | + } |
| 67 | + return children; |
| 68 | + } |
| 69 | + |
| 70 | + public DynaTreeNode getChild(String title) { |
| 71 | + return getChildren().get(title); |
| 72 | + } |
| 73 | + |
| 74 | + public void putChild(DynaTreeNode child) { |
| 75 | + getChildren().put(child.title, child); |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressWarnings("unchecked") |
78 | 79 | public JSONObject toJSONObject() throws JSONException {
|
79 |
| - return new JSONObject() |
80 |
| - .put("title", title) |
81 |
| - .put("key", key) |
82 |
| - .put("noLink", noLink) |
83 |
| - .put("mode", mode) |
84 |
| - .put("expand", true) |
85 |
| - .put("children", getChildrenJSONArray()); |
86 |
| - } |
87 |
| - |
88 |
| - @SuppressWarnings("unchecked") |
| 80 | + return new JSONObject().put("title", title).put("key", key).put("noLink", noLink).put("mode", mode) |
| 81 | + .put("expand", true).put("children", getChildrenJSONArray()); |
| 82 | + } |
| 83 | + |
| 84 | + @SuppressWarnings("unchecked") |
89 | 85 | public JSONArray getChildrenJSONArray() {
|
90 |
| - JSONArray ar = null; |
91 |
| - if (children != null) { |
92 |
| - ar = new JSONArray(); |
93 |
| - for (DynaTreeNode a : children.values()) { |
94 |
| - try { |
95 |
| - ar.put(a.toJSONObject()); |
| 86 | + JSONArray ar = null; |
| 87 | + if (children != null) { |
| 88 | + ar = new JSONArray(); |
| 89 | + for (DynaTreeNode a : children.values()) { |
| 90 | + try { |
| 91 | + ar.put(a.toJSONObject()); |
96 | 92 | } catch (JSONException e) {
|
97 |
| - // TODO Auto-generated catch block |
98 |
| - e.printStackTrace(); |
| 93 | + // TODO Auto-generated catch block |
| 94 | + e.printStackTrace(); |
99 | 95 | }
|
100 |
| - } |
101 |
| - } |
102 |
| - return ar; |
103 |
| - } |
| 96 | + } |
| 97 | + } |
| 98 | + return ar; |
| 99 | + } |
104 | 100 | }
|
0 commit comments