KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > select > AbstractMultiSelectElement


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element.select;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.riotfamily.forms.ErrorUtils;
33 import org.riotfamily.forms.event.JavaScriptEvent;
34 import org.riotfamily.forms.request.FormRequest;
35 import org.springframework.beans.BeanUtils;
36
37 /**
38  * Abstract superclass for elements that let the user choose from a set of
39  * options like selectboxes or radio button groups.
40  */

41 public abstract class AbstractMultiSelectElement
42         extends AbstractSelectElement {
43
44     private ArrayList JavaDoc selectedValues = new ArrayList JavaDoc();
45
46     private Class JavaDoc collectionClass;
47
48     private Integer JavaDoc maxSelection;
49     
50     /**
51      * Sets the class to use if a new collection instance needs to be created.
52      * If no class is set a suitable implementation will be selected according
53      * to the type of the property the element is bound to.
54      *
55      * @param collectionClass the class to use for new collections
56      */

57     public void setCollectionClass(Class JavaDoc collectionClass) {
58         this.collectionClass = collectionClass;
59     }
60     
61     public void setMaxSelection(Integer JavaDoc maxSelection) {
62         this.maxSelection = maxSelection;
63     }
64
65     protected void afterBindingSet() {
66         if (collectionClass == null) {
67             Class JavaDoc type = getEditorBinding().getPropertyType();
68             if (type.isInterface()) {
69                 if (Set JavaDoc.class.isAssignableFrom(type)) {
70                     collectionClass = HashSet JavaDoc.class;
71                 }
72                 else {
73                     collectionClass = ArrayList JavaDoc.class;
74                 }
75             }
76             else {
77                 collectionClass = type;
78             }
79         }
80     }
81     
82     public final void setValue(Object JavaDoc value) {
83         selectedValues = new ArrayList JavaDoc();
84         if (value != null) {
85             Collection JavaDoc collection = (Collection JavaDoc) value;
86             selectedValues.addAll(collection);
87         }
88     }
89     
90     public Object JavaDoc getValue() {
91         Collection JavaDoc collection = null;
92         if (getEditorBinding() != null) {
93             collection = (Collection JavaDoc) getEditorBinding().getValue();
94         }
95         if (collection == null) {
96             collection = (Collection JavaDoc) BeanUtils.instantiateClass(collectionClass);
97         }
98         else {
99             collection.clear();
100         }
101         collection.addAll(selectedValues);
102         return collection;
103     }
104
105     protected Collection JavaDoc getSelectedValues() {
106         return selectedValues;
107     }
108
109     protected boolean hasSelection() {
110         return !selectedValues.isEmpty();
111     }
112     
113     public boolean isSelected(Option option) {
114         return selectedValues != null &&
115                 selectedValues.contains(option.getValue());
116     }
117
118     /**
119      * @see org.riotfamily.forms.AbstractElement#processRequest
120      */

121     public void processRequest(FormRequest request) {
122         updateSelection(request.getParameterValues(getParamName()));
123     }
124     
125     private void updateSelection(String JavaDoc[] indexes) {
126         List JavaDoc options = getOptions();
127         selectedValues = new ArrayList JavaDoc();
128         if (indexes != null) {
129             for (int i = 0; i < indexes.length; i++) {
130                 int index = Integer.parseInt(indexes[i]);
131                 if (index != -1) {
132                     selectedValues.add(((Option) options.get(index)).getValue());
133                 }
134             }
135         }
136         validate();
137     }
138     
139     protected void validate() {
140         super.validate();
141         if (maxSelection != null && selectedValues.size() > maxSelection.intValue()) {
142             ErrorUtils.reject(this, "tooManyValuesSelected", maxSelection);
143         }
144     }
145     
146     public void handleJavaScriptEvent(JavaScriptEvent event) {
147         if (event.getType() == JavaScriptEvent.ON_CHANGE) {
148             Collection JavaDoc oldValue = selectedValues;
149             updateSelection(event.getValues());
150             fireChangeEvent(selectedValues, oldValue);
151         }
152     }
153
154 }
Popular Tags