1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.universAAL.middleware.ui.rdf;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.universAAL.middleware.container.utils.StringUtils;
27 import org.universAAL.middleware.owl.MergedRestriction;
28 import org.universAAL.middleware.rdf.PropertyPath;
29 import org.universAAL.middleware.rdf.Resource;
30
31
32
33
34
35
36
37
38
39
40
41 public class Select extends Input {
42 public static final String MY_URI = Form.uAAL_DIALOG_NAMESPACE + "Select";
43
44
45
46
47
48 public static final String PROP_CHOICES = Form.uAAL_DIALOG_NAMESPACE
49 + "choices";
50
51
52
53
54 public Select() {
55 super();
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public Select(Group parent, Label label, PropertyPath ref,
76 MergedRestriction valueRestriction, Object initialValue) {
77 super(MY_URI, parent, label, ref, valueRestriction, initialValue);
78 }
79
80 protected Select(String typeURI, Group parent, Label label,
81 PropertyPath ref, MergedRestriction valueRestriction,
82 Object initialValue) {
83 super(typeURI, parent, label, ref, valueRestriction, initialValue);
84 }
85
86
87
88
89 public void addChoiceItem(ChoiceItem item) {
90 if (item != null) {
91 List l = (List) props.get(PROP_CHOICES);
92 if (l == null) {
93 l = new ArrayList();
94 props.put(PROP_CHOICES, l);
95 }
96 l.add(item);
97 }
98 }
99
100
101
102
103
104 public void addChoiceList(ChoiceList list) {
105 if (list != null) {
106 List l = (List) props.get(PROP_CHOICES);
107 if (l == null) {
108 l = new ArrayList();
109 props.put(PROP_CHOICES, l);
110 }
111 l.add(list);
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124 public boolean generateChoices(Object[] elems) {
125 if (elems == null || elems.length == 0)
126 return false;
127
128 for (int i = 0; i < elems.length; i++) {
129 if (elems[i] == null)
130 return false;
131 String label = (elems[i] instanceof Resource) ? ((Resource) elems[i])
132 .getResourceLabel()
133 : null;
134 if (label == null)
135 label = elems[i].toString();
136 addChoiceItem(new ChoiceItem(StringUtils.deriveLabel(label), null,
137 elems[i]));
138 }
139
140 return true;
141 }
142
143
144
145
146
147
148
149 public boolean generateChoices() {
150 MergedRestriction r = getRestrictions();
151 return (r == null) ? false : generateChoices(r.getEnumeratedValues());
152 }
153
154
155
156
157
158
159 public Label[] getChoices() {
160 List l = (List) props.get(PROP_CHOICES);
161 if (l == null)
162 return new Label[0];
163
164 return (Label[]) l.toArray(new Label[l.size()]);
165 }
166
167
168
169
170
171
172 public int getMaxCardinality() {
173 MergedRestriction r = getRestrictions();
174 return (r == null) ? -1 : r.getMaxCardinality();
175 }
176
177
178
179
180
181 public int getMaxLength() {
182 List l = (List) props.get(PROP_CHOICES);
183 if (l == null)
184 return -1;
185
186 int res = -1;
187 for (Iterator i = l.iterator(); i.hasNext();) {
188 Object o = i.next();
189 if (o instanceof Label) {
190 int aux = ((Label) o).getMaxLength();
191 if (aux > res)
192 res = aux;
193 }
194 }
195 return res;
196 }
197
198
199
200
201
202
203 public int getMinCardinality() {
204 MergedRestriction r = getRestrictions();
205 return (r == null) ? -1 : r.getMinCardinality();
206 }
207
208
209
210
211
212 public boolean isMultilevel() {
213 List l = (List) props.get(PROP_CHOICES);
214 if (l == null)
215 return false;
216
217 for (Iterator i = l.iterator(); i.hasNext();)
218 if (i.next() instanceof ChoiceList)
219 return true;
220
221 return false;
222 }
223
224
225
226
227
228
229 public boolean setProperty(String propURI, Object value) {
230 if (PROP_CHOICES.equals(propURI)) {
231 if (props.containsKey(propURI))
232 return false;
233 else if (value instanceof List) {
234 for (Iterator i = ((List) value).iterator(); i.hasNext();) {
235 Object o = i.next();
236 if (!(o instanceof ChoiceItem)
237 && !(o instanceof ChoiceList))
238 return false;
239 }
240 props.put(propURI, value);
241 return true;
242 } else if (value instanceof ChoiceItem
243 || value instanceof ChoiceList) {
244 List l = new ArrayList(1);
245 l.add(value);
246 props.put(propURI, l);
247 return true;
248 }
249 } else
250 return super.setProperty(propURI, value);
251 return false;
252 }
253
254
255
256
257
258
259
260
261
262
263
264
265 public boolean storeUserInputByLabelString(String selectedLabelString) {
266 if (selectedLabelString != null) {
267 List l = (List) props.get(PROP_CHOICES);
268 if (l != null)
269 for (Iterator i = l.iterator(); i.hasNext();) {
270 Object label = i.next();
271 if (label instanceof ChoiceList)
272 label = ((ChoiceList) label)
273 .findItem(selectedLabelString);
274 if (label instanceof ChoiceItem
275 && selectedLabelString.equals(label.toString()))
276 return storeUserInput(((ChoiceItem) label).getValue());
277 }
278 }
279 return storeUserInput(selectedLabelString);
280 }
281 }