1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.ui.dialogs.property;
19
20 import java.util.Collection;
21 import java.util.Comparator;
22 import java.util.Map;
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25
26 import pl.edu.agh.cast.data.model.property.MetaProperty;
27 import pl.edu.agh.cast.data.model.property.Property;
28
29
30
31
32
33
34
35
36
37
38
39 public final class PropertyTreeEntry {
40
41 private String id;
42
43 private PropertyEditType type;
44
45 private String label;
46
47 private boolean isMarkAsLabel;
48
49 private Property<? extends MetaProperty> property;
50
51 private PropertyTreeEntry parent;
52
53 private SortedMap<String, PropertyTreeEntry> children;
54
55
56
57
58
59
60 public enum PropertyEditType {
61 CUSTOM_EDITABLE, CUSTOM_NOT_EDITABLE, EDITABLE, NOT_EDITABLE, CATEGORY
62 }
63
64
65
66
67
68
69 class AlphabeticComparator implements Comparator<String> {
70 public int compare(String o1, String o2) {
71 return o1.toLowerCase().compareTo(o2.toLowerCase());
72 }
73
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public PropertyTreeEntry(String id, String label) {
87 this(id, PropertyEditType.CATEGORY);
88 if (label == null) {
89 throw new IllegalArgumentException();
90 }
91 this.label = label;
92 this.property = null;
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public PropertyTreeEntry(String id, Property<? extends MetaProperty> property, PropertyEditType type) {
106 this(id, type);
107 if (property == null) {
108 throw new IllegalArgumentException();
109 }
110 this.label = null;
111 this.property = property;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125 public PropertyTreeEntry(String id, String label, PropertyTreeEntry parent) {
126 this(id, label);
127 setParent(parent);
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public PropertyTreeEntry(String id, Property<? extends MetaProperty> property, PropertyEditType type,
144 PropertyTreeEntry parent) {
145 this(id, property, type);
146 setParent(parent);
147 }
148
149 private PropertyTreeEntry(String id, PropertyEditType type) {
150 if (id == null || type == null) {
151 throw new IllegalArgumentException();
152 }
153 this.id = id;
154 this.type = type;
155 children = new TreeMap<String, PropertyTreeEntry>(new AlphabeticComparator());
156 isMarkAsLabel = false;
157 }
158
159
160
161
162
163 public String getId() {
164 return id;
165 }
166
167 public PropertyEditType getType() {
168 return type;
169 }
170
171 public String getLabel() {
172 return label;
173 }
174
175 public boolean isMarkAsLabel() {
176 return isMarkAsLabel;
177 }
178
179 public void setIsMarkAsLabel(boolean isMarkAsLabel) {
180 this.isMarkAsLabel = isMarkAsLabel;
181 }
182
183 public Property<? extends MetaProperty> getProperty() {
184 return property;
185 }
186
187 public void setParent(PropertyTreeEntry parent) {
188 this.parent = parent;
189 }
190
191 public PropertyTreeEntry getParent() {
192 return parent;
193 }
194
195 public Collection<PropertyTreeEntry> getChildren() {
196 return children.values();
197 }
198
199
200
201
202
203
204
205
206
207
208
209 public void addChild(PropertyTreeEntry entry) {
210 if (entry == null) {
211 throw new IllegalArgumentException();
212 }
213 children.put(entry.getId(), entry);
214 entry.setParent(this);
215 }
216
217
218
219
220
221
222
223 public void addAllChildren(Map<String, PropertyTreeEntry> childrenToAdd) {
224 if (childrenToAdd == null) {
225 throw new IllegalArgumentException();
226 }
227 this.children.putAll(childrenToAdd);
228 for (PropertyTreeEntry entry : childrenToAdd.values()) {
229 entry.setParent(this);
230 }
231 }
232
233
234
235
236
237
238
239
240 public PropertyTreeEntry removeChild(PropertyTreeEntry entry) {
241 if (entry == null) {
242 throw new IllegalArgumentException();
243 }
244 return children.remove(entry.getId());
245 }
246
247
248
249
250
251
252 public boolean hasChildren() {
253 return !children.isEmpty();
254 }
255
256
257
258
259
260
261
262
263 public PropertyTreeEntry getChild(String childId) {
264 return children.get(childId);
265 }
266
267
268
269
270
271
272 public Map<String, PropertyTreeEntry> getChildrenMap() {
273 return children;
274 }
275
276
277
278 }