View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.api.uif;
17  
18  import org.kuali.rice.core.api.CoreConstants;
19  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
20  import org.w3c.dom.Element;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlAnyElement;
25  import javax.xml.bind.annotation.XmlElement;
26  import javax.xml.bind.annotation.XmlElementWrapper;
27  import javax.xml.bind.annotation.XmlRootElement;
28  import javax.xml.bind.annotation.XmlType;
29  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * A select control type.
39   */
40  @XmlRootElement(name = RemotableSelect.Constants.ROOT_ELEMENT_NAME)
41  @XmlAccessorType(XmlAccessType.NONE)
42  @XmlType(name = RemotableSelect.Constants.TYPE_NAME, propOrder = {
43          RemotableSelect.Elements.KEY_LABELS,
44          RemotableSelect.Elements.GROUPS,
45          RemotableSelect.Elements.SIZE,
46          RemotableSelect.Elements.MULTIPLE,
47          RemotableSelect.Elements.REFRESH_ON_CHANGE,
48  		CoreConstants.CommonElements.FUTURE_ELEMENTS })
49  public final class RemotableSelect extends RemotableAbstractControl implements RemotableSelectContract {
50  
51      @XmlElement(name = Elements.KEY_LABELS, required = false)
52      @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
53      private final Map<String, String> keyLabels;
54  
55      @XmlElementWrapper(name = Elements.GROUPS, required = true)
56      @XmlElement(name = Elements.GROUP, required = false)
57      private final List<RemotableSelectGroup> groups;
58  
59      @XmlElement(name = Elements.SIZE, required = false)
60      private final Integer size;
61  
62      @XmlElement(name = Elements.MULTIPLE, required = false)
63      private final boolean multiple;
64  
65      @XmlElement(name = Elements.REFRESH_ON_CHANGE, required = false)
66      private final boolean refreshOnChange;
67  
68      @SuppressWarnings("unused")
69      @XmlAnyElement
70      private final Collection<Element> _futureElements = null;
71  
72      /**
73       * Should only be invoked by JAXB.
74       */
75      @SuppressWarnings("unused")
76      private RemotableSelect() {
77          size = null;
78          keyLabels = null;
79          groups = null;
80          multiple = false;
81          refreshOnChange = false;
82      }
83  
84      private RemotableSelect(Builder b) {
85          size = b.size;
86          keyLabels = b.keyLabels;
87  
88          final List<RemotableSelectGroup> temp = new ArrayList<RemotableSelectGroup>();
89          for (RemotableSelectGroup.Builder attr : b.groups) {
90              temp.add(attr.build());
91          }
92          this.groups = Collections.unmodifiableList(temp);
93  
94          multiple = b.multiple;
95          refreshOnChange = b.refreshOnChange;
96      }
97  
98      @Override
99      public Map<String, String> getKeyLabels() {
100         return keyLabels;
101     }
102 
103     @Override
104     public Integer getSize() {
105         return size;
106     }
107 
108     @Override
109     public List<RemotableSelectGroup> getGroups() {
110         return groups;
111     }
112 
113     @Override
114     public boolean isMultiple() {
115         return multiple;
116     }
117 
118     @Override
119     public boolean isRefreshOnChange() {
120         return refreshOnChange;
121     }
122 
123     public static final class Builder extends RemotableAbstractControl.Builder implements RemotableSelectContract {
124         private Integer size;
125         private Map<String, String> keyLabels = Collections.emptyMap();
126         private List<RemotableSelectGroup.Builder> groups = Collections.emptyList();
127         private boolean multiple;
128         private boolean refreshOnChange;
129 
130         private Builder(Map<String, String> keyLabels) {
131             setKeyLabels(keyLabels);
132         }
133 
134         private Builder(List<RemotableSelectGroup.Builder> groups) {
135             setGroups(groups);
136         }
137 
138         public static Builder create(Map<String, String> keyLabels) {
139             return new Builder(keyLabels);
140         }
141 
142         public static Builder create(List<RemotableSelectGroup.Builder> groups) {
143             return new Builder(groups);
144         }
145 
146         @Override
147         public Integer getSize() {
148             return size;
149         }
150 
151         public void setSize(Integer size) {
152             if (size != null && size < 1) {
153                 throw new IllegalArgumentException("size was < 1");
154             }
155 
156             this.size = size;
157         }
158 
159         @Override
160         public Map<String, String> getKeyLabels() {
161             return keyLabels;
162         }
163 
164         public void setKeyLabels(Map<String, String> keyLabels) {
165             if (keyLabels == null) {
166                 throw new IllegalArgumentException("keyLabels was null");
167             }
168 
169             this.keyLabels = Collections.unmodifiableMap(new LinkedHashMap<String, String>(keyLabels));
170         }
171 
172         @Override
173         public List<RemotableSelectGroup.Builder> getGroups() {
174             return groups;
175         }
176 
177         public void setGroups(List<RemotableSelectGroup.Builder> groups) {
178             if (groups == null) {
179                 throw new IllegalArgumentException("groups was null");
180             }
181 
182             this.groups = Collections.unmodifiableList(new ArrayList<RemotableSelectGroup.Builder>(groups));
183         }
184 
185         @Override
186         public boolean isMultiple() {
187             return multiple;
188         }
189 
190         public void setMultiple(boolean multiple) {
191             this.multiple = multiple;
192         }
193 
194         @Override
195         public boolean isRefreshOnChange() {
196             return refreshOnChange;
197         }
198 
199         public void setRefreshOnChange(boolean refreshOnChange) {
200             this.refreshOnChange = refreshOnChange;
201         }
202 
203         @Override
204         public RemotableSelect build() {
205             if (keyLabels.isEmpty() && groups.isEmpty()) {
206                 throw new IllegalStateException("the keyLabels or groups must be set to a non-empty collection");
207             }
208 
209             return new RemotableSelect(this);
210         }
211     }
212 
213     /**
214      * Defines some internal constants used on this class.
215      */
216     static final class Constants {
217         static final String TYPE_NAME = "SelectType";
218         final static String ROOT_ELEMENT_NAME = "select";
219     }
220 
221     static final class Elements {
222         static final String SIZE = "size";
223         static final String KEY_LABELS = "keyLabels";
224         static final String GROUPS = "groups";
225         static final String GROUP = "group";
226         static final String MULTIPLE = "multiple";
227         static final String REFRESH_ON_CHANGE = "refreshOnChange";
228     }
229 }