View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
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  /**
27   * A special case of {@link Label labels} accepting other choice
28   * {@link ChoiceItem items} and lists as its children and hence serving as an
29   * inner node in a hierarchy of possible choices. Selecting the label of a
30   * choice list must make its children accessible.
31   * 
32   * @author mtazari
33   * @author Carsten Stockloew
34   * @navassoc "" children "*" Label
35   */
36  public class ChoiceList extends Label {
37      public static final String MY_URI = Form.uAAL_DIALOG_NAMESPACE
38  	    + "ChoiceList";
39  
40      /**
41       * Property for accessing the list of items and sublists contained in a
42       * choice list.
43       */
44      public static final String PROP_CHILDREN = Form.uAAL_DIALOG_NAMESPACE
45  	    + "subchoices";
46  
47      private List children;
48  
49      /**
50       * For use by de-serializers only.
51       */
52      public ChoiceList() {
53  	super();
54      }
55  
56      /**
57       * Constructs a new choice list.
58       * 
59       * @param labelText
60       *            see {@link Label#Label(String, String)}
61       * @param iconURL
62       *            see {@link Label#Label(String, String)}
63       */
64      public ChoiceList(String labelText, String iconURL) {
65  	super(labelText, iconURL);
66  	children = new ArrayList();
67  	props.put(PROP_CHILDREN, children);
68      }
69  
70      /**
71       * Adds an item to this choice list.
72       * 
73       * @param item
74       *            The item to be added to this choice list.
75       */
76      public void addChild(ChoiceItem item) {
77  	if (item != null)
78  	    children.add(item);
79      }
80  
81      /**
82       * Adds a sublist to this choice list.
83       * 
84       * @param sublist
85       *            The sublist to be added to this choice list.
86       */
87      public void addChild(ChoiceList sublist) {
88  	if (sublist != null)
89  	    children.add(sublist);
90      }
91  
92      /**
93       * Returns the items and sublists contained in this choice list in the order
94       * of their addition to the list.
95       */
96      public Label[] getChildren() {
97  	return (Label[]) children.toArray(new Label[children.size()]);
98      }
99  
100     ChoiceItem findItem(String label) {
101 	ChoiceItem result = null;
102 	if (children != null)
103 	    for (Iterator i = children.iterator(); result == null
104 		    && i.hasNext();) {
105 		Object child = i.next();
106 		if (child instanceof ChoiceList)
107 		    result = ((ChoiceList) child).findItem(label);
108 		else if (child instanceof ChoiceItem
109 			&& child.toString().equals(label))
110 		    return (ChoiceItem) child;
111 	    }
112 	return result;
113     }
114 
115     /**
116      * @see org.universAAL.middleware.ui.rdf.Label#getMaxLength()
117      */
118     int getMaxLength() {
119 	int res = -1;
120 	for (Iterator i = children.iterator(); i.hasNext();) {
121 	    Object o = i.next();
122 	    if (o instanceof Label) {
123 		int aux = ((Label) o).getMaxLength();
124 		if (aux > res)
125 		    res = aux;
126 	    }
127 	}
128 	return res;
129     }
130 
131     /**
132      * @see org.universAAL.middleware.rdf.Resource#setProperty(String, Object)
133      */
134     public boolean setProperty(String propURI, Object value) {
135 	if (PROP_CHILDREN.equals(propURI))
136 	    if (value instanceof List && children == null) {
137 		for (Iterator i = ((List) value).iterator(); i.hasNext();) {
138 		    Object o = i.next();
139 		    if (!(o instanceof ChoiceItem)
140 			    && !(o instanceof ChoiceList))
141 			return false;
142 		}
143 		children = (List) value;
144 		props.put(PROP_CHILDREN, children);
145 		return true;
146 	    } else
147 		return false;
148 	else
149 	    return super.setProperty(propURI, value);
150     }
151 }