View Javadoc

1   /**
2    * Copyright 2005-2012 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.HashMap;
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  		CoreConstants.CommonElements.FUTURE_ELEMENTS })
48  public final class RemotableSelect extends RemotableAbstractControl implements RemotableSelectContract {
49  
50      @XmlElement(name = Elements.KEY_LABELS, required = false)
51      @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
52      private final Map<String, String> keyLabels;
53  
54      @XmlElementWrapper(name = Elements.GROUPS, required = true)
55      @XmlElement(name = Elements.GROUP, required = false)
56      private final List<RemotableSelectGroup> groups;
57  
58      @XmlElement(name = Elements.SIZE, required = false)
59      private final Integer size;
60  
61      @XmlElement(name = Elements.MULTIPLE, required = false)
62      private final boolean multiple;
63  
64      @SuppressWarnings("unused")
65      @XmlAnyElement
66      private final Collection<Element> _futureElements = null;
67  
68      /**
69       * Should only be invoked by JAXB.
70       */
71      @SuppressWarnings("unused")
72      private RemotableSelect() {
73          size = null;
74          keyLabels = null;
75          groups = null;
76          multiple = false;
77      }
78  
79      private RemotableSelect(Builder b) {
80          size = b.size;
81          keyLabels = b.keyLabels;
82  
83          final List<RemotableSelectGroup> temp = new ArrayList<RemotableSelectGroup>();
84          for (RemotableSelectGroup.Builder attr : b.groups) {
85              temp.add(attr.build());
86          }
87          this.groups = Collections.unmodifiableList(temp);
88  
89          multiple = b.multiple;
90      }
91  
92      @Override
93      public Map<String, String> getKeyLabels() {
94          return keyLabels;
95      }
96  
97      @Override
98      public Integer getSize() {
99          return size;
100     }
101 
102     @Override
103     public List<RemotableSelectGroup> getGroups() {
104         return groups;
105     }
106 
107     @Override
108     public boolean isMultiple() {
109         return multiple;
110     }
111 
112     public static final class Builder extends RemotableAbstractControl.Builder implements RemotableSelectContract {
113         private Integer size;
114         private Map<String, String> keyLabels = Collections.emptyMap();
115         private List<RemotableSelectGroup.Builder> groups = Collections.emptyList();
116         private boolean multiple;
117 
118         private Builder(Map<String, String> keyLabels) {
119             setKeyLabels(keyLabels);
120         }
121 
122         private Builder(List<RemotableSelectGroup.Builder> groups) {
123             setGroups(groups);
124         }
125 
126         public static Builder create(Map<String, String> keyLabels) {
127             return new Builder(keyLabels);
128         }
129 
130         public static Builder create(List<RemotableSelectGroup.Builder> groups) {
131             return new Builder(groups);
132         }
133 
134         @Override
135         public Integer getSize() {
136             return size;
137         }
138 
139         public void setSize(Integer size) {
140             if (size != null && size < 1) {
141                 throw new IllegalArgumentException("size was < 1");
142             }
143 
144             this.size = size;
145         }
146 
147         @Override
148         public Map<String, String> getKeyLabels() {
149             return keyLabels;
150         }
151 
152         public void setKeyLabels(Map<String, String> keyLabels) {
153             if (keyLabels == null) {
154                 throw new IllegalArgumentException("keyLabels was null");
155             }
156 
157             this.keyLabels = Collections.unmodifiableMap(new HashMap<String, String>(keyLabels));
158         }
159 
160         @Override
161         public List<RemotableSelectGroup.Builder> getGroups() {
162             return groups;
163         }
164 
165         public void setGroups(List<RemotableSelectGroup.Builder> groups) {
166             if (groups == null) {
167                 throw new IllegalArgumentException("groups was null");
168             }
169 
170             this.groups = Collections.unmodifiableList(new ArrayList<RemotableSelectGroup.Builder>(groups));
171         }
172 
173         @Override
174         public boolean isMultiple() {
175             return multiple;
176         }
177 
178         public void setMultiple(boolean multiple) {
179             this.multiple = multiple;
180         }
181 
182         @Override
183         public RemotableSelect build() {
184             if (keyLabels.isEmpty() && groups.isEmpty()) {
185                 throw new IllegalStateException("the keyLabels or groups must be set to a non-empty collection");
186             }
187 
188             return new RemotableSelect(this);
189         }
190     }
191 
192     /**
193      * Defines some internal constants used on this class.
194      */
195     static final class Constants {
196         static final String TYPE_NAME = "SelectType";
197         final static String ROOT_ELEMENT_NAME = "select";
198     }
199 
200     static final class Elements {
201         static final String SIZE = "size";
202         static final String KEY_LABELS = "keyLabels";
203         static final String GROUPS = "groups";
204         static final String GROUP = "group";
205         static final String MULTIPLE = "multiple";
206     }
207 }