KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > criteria > MultipleListCriterion


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util.struts.criteria;
24
25 import java.util.Locale JavaDoc;
26
27 import org.mdarad.framework.expr.Criterion;
28 import org.mdarad.framework.expr.OperatorTypes;
29 import org.mdarad.framework.util.ClassUtils;
30 import org.mdarad.framework.util.ClassUtilsException;
31
32 /**
33  * This class represents a search criterion that presents a list of values to the user.
34  * To use this criterion, you need to set a {@link List List} of {@link CriterionListElement CriterionListElement}
35  * to the criterion. This list will be displayed as the selection of values and can have a multiple
36  * selection.
37  * @author Philippe Brouillette
38  * @version 1.0
39  */

40 public class MultipleListCriterion extends ListCriterion {
41
42     /**
43      * Constructor that takes all the properties to initialize a search
44      * criterion. By default, the criterion is dynamic.
45      * @param name name of the criterion. This name must be unique
46      * as it is a key in a map.
47      * @param associatedEntity class type associated to this criterion
48      * @param property property used for the query criterion
49      * @param bundleName bundle name
50      * @param locale locale information
51      * @see FormCriterion#FormCriterion(String, CriterionProperty, String, Locale) FormCriterion
52      */

53     public MultipleListCriterion(String JavaDoc name, Class JavaDoc associatedEntity, CriterionProperty property, String JavaDoc bundleName, Locale JavaDoc locale, Class JavaDoc elementType) {
54         super(name, associatedEntity, property, bundleName, locale);
55         this.elementType = elementType;
56     }
57
58     /**
59      * Constructor that clones a query criterion. This constructor is used
60      * to instanciate a criterion in a search form when the form
61      * contains dynamic criteria.
62      * @param criterion query criterion that must be of
63      * type <code>MultipleListCriterion</code>
64      * @see FormCriterion#FormCriterion(FormCriterion) FormCriterion
65      */

66     public MultipleListCriterion(MultipleListCriterion criterion) {
67         super(criterion);
68         setList(criterion.getList());
69         this.elementType = criterion.getElementType();
70     }
71     
72     /**
73      * Method that assigns the value of the criterion as a String. This method
74      * is needed for the user interface which uses only string values.
75      * @param value value to be assigned
76      * @see FormCriterion#setPropertyValue(java.lang.String)
77      */

78     public void setPropertyValues(String JavaDoc[] values) {
79
80         if (values != null) {
81             Object JavaDoc[] objValues = new Object JavaDoc[values.length];
82             // construct the object type with the string constructor
83
for (int i = 0; i < objValues.length; i++) {
84                 Class JavaDoc[] argTypes = new Class JavaDoc[] { String JavaDoc.class };
85                 try {
86                 objValues[i] = ClassUtils.getInstance(
87                                     getElementType(),
88                                     argTypes,
89                                     new Object JavaDoc[] { values[i] }
90                                     );
91                 } catch (ClassUtilsException cue) {
92                     throw new CriterionException(cue);
93                 }
94             }
95             setValues(objValues);
96         }
97     }
98
99     /**
100      * Method that returns the values selected by the user
101      * in string format.
102      * @return array of the string values selected by the
103      * user.
104      */

105     public String JavaDoc[] getPropertyValues() {
106         return (String JavaDoc[]) getValue();
107     }
108
109     /**
110      * Method used to keep the list of elements selected by the user
111      * @param values selected values.
112      */

113     public void setValues(Object JavaDoc[] values) {
114         // vérifier que les valeurs existents
115
if (values != null) {
116             setValue(values);
117         }
118     }
119     
120     /**
121      * Property that keeps the object type that is kept in
122      * the list of objects.
123      * By default, the type is <code>Object[]</code>
124      */

125     private Class JavaDoc objectType = Object JavaDoc[].class;
126     
127     /**
128      * Method that sets the object type
129      * @param type object class type
130      */

131     private void setObjectType(Class JavaDoc type) {
132         this.objectType = type;
133     }
134
135     /**
136      * Returns the object class type used by the list of possible values.
137      *
138      * @see FormCriterion#getObjectType()
139      */

140     public Class JavaDoc getObjectType() {
141         return objectType;
142     }
143
144     /**
145      * This method is not implemented because it should not be used.
146      * @exception UnsupportedOperationException
147      * @see @see FormCriterion#setPropertyValue(java.lang.String)
148      */

149     public void setPropertyValue(String JavaDoc object) {
150         throw new UnsupportedOperationException JavaDoc("The setPropertyValue with String argument is not supported");
151     }
152
153     /**
154      * Method that returns the criterion from the expression
155      * framework. ({@link Criterion})
156      * <p><b>NOTE:</b> The implementation must take care of the type
157      * of operator that is used to determine the criterion to use.
158      * @return the criterion
159      */

160     public Criterion getExprCriterion() {
161         Criterion crit =
162             new Criterion(getAssociatedEntity(),
163                           getProperty().getName(),
164                           OperatorTypes.IN,
165                           (Object JavaDoc[]) getValue());
166         return crit;
167     }
168
169     /**
170      * Method that returns the displaying pattern.
171      *
172      * @see FormCriterion#getFormPattern()
173      */

174     public CriterionFormPattern getFormPattern() {
175         return CriterionFormPatterns.NO_OPERATOR_MULTIPLE_LIST;
176     }
177
178     /**
179      * Property that keeps the type of each element of the list
180      */

181     private Class JavaDoc elementType = String JavaDoc.class;
182     
183     public Class JavaDoc getElementType() {
184         return elementType;
185     }
186
187     public void setElementType(Class JavaDoc type) {
188         elementType = type;
189     }
190 }
Popular Tags