KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > repopulation > SelectFormElement


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: SelectFormElement.java,v 1.6 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation;
21
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import org.enhydra.barracuda.core.forms.DefaultFormElement;
25 import org.enhydra.barracuda.core.comp.ListModel;
26 import org.enhydra.barracuda.core.comp.DefaultListModel;
27 import org.apache.log4j.Logger;
28 import org.enhydra.barracuda.core.comp.ListSelectionModel;
29 import org.enhydra.barracuda.core.forms.FormType;
30 import org.enhydra.barracuda.core.forms.FormValidator;
31 import org.enhydra.barracuda.core.comp.ItemMap;
32 import org.enhydra.barracuda.core.comp.ViewContext;
33 import org.enhydra.barracuda.core.comp.BSelect;
34
35 /**
36  * Use this FormElement to repopulate the request value
37  * into a select box created by the <select>-tag.
38  *
39  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
40  * @version 1.0
41  */

42 public class SelectFormElement extends DefaultFormElement implements RepopulationElement {
43     private static final Logger logger = Logger.getLogger(SelectFormElement.class.getName());
44     ListModel lm;
45     ListSelectionModel lsm;
46     int selectionType = -1;
47
48     /**
49      * Set the list of allowed values.
50      *
51      * @param lm a <code>ListModel</code> value
52      */

53     public void setListModel(ListModel lm) {
54         this.lm = lm;
55     }
56
57     /**
58      * Get the list of allowed values.
59      *
60      * @return a <code>ListModel</code> value
61      */

62     public ListModel getListModel() {
63         return lm;
64     }
65
66     /**
67      * Set the ListSelectionModel that determines which entries
68      * of the select box are rendered.
69      *
70      * @param lsm a <code>ListSelectionModel</code> value
71      */

72     public void setListSelectionModel(ListSelectionModel lsm) {
73         this.lsm = lsm;
74     }
75
76
77     /**
78      * Get the ListSelectionModel.
79      *
80      * @return a <code>ListSelectionModel</code> value
81      */

82     public ListSelectionModel getListSelectionModel() {
83         if(logger.isDebugEnabled()) {
84             logger.debug("Name: " + getName());
85             logger.debug("allowMultiples: " + (
86                                                allowMultiples() ? "yes" : "no"));
87             if(getVals() != null) {
88                 logger.debug("type(Vals): " + getVals().getClass().getName());
89                 logger.debug("Vals: " + getVals());
90             }
91         }
92         if(lsm == null) {
93             int selType = selectionType;
94             if(selType == -1) {
95                 if(allowMultiples()) {
96                     selType = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
97                 } else {
98                     selType = ListSelectionModel.SINGLE_SELECTION;
99                 }
100             }
101             List JavaDoc selected;
102             if(getOrigVal() != null) {
103                 if(getOrigVal() instanceof List JavaDoc) {
104                     selected = (List JavaDoc)getOrigVal();
105                 } else {
106                     selected = new ArrayList JavaDoc();
107                     selected.add(getOrigVal());
108                 }
109             } else {
110                 // If we have Vals (set be the
111
selected = new ArrayList JavaDoc();
112             }
113             if(lm instanceof DefaultListModel) {
114                 return calcListSelectionModel(((DefaultListModel)lm).subList(0, lm.getSize()), selected, selType);
115             }
116             else {
117                 List JavaDoc values = new ArrayList JavaDoc(lm.getSize());
118                 for(int i = 0; i < lm.getSize(); i++) {
119                     values.add(lm.getItemAt(i));
120                 }
121                 return calcListSelectionModel(values, selected, selType);
122             }
123         }
124         return lsm;
125     }
126
127
128     /**
129      * This is a helper method that takes a list of permitted values, and a subset
130      * of selected values out of the former list. It will then compute the mode that
131      * fits this selection. See {@link org.enhydra.barracuda.core.comp.ListSelectionModel ListSelectionModel}.
132      *
133      *
134      * @param vals a <code>List</code> of permitted values
135      * @param sel a <code>List</code> which specifies a subset of <b>vals</b>
136      * @return the mode, as specified in {@link org.enhydra.barracuda.core.comp.ListSelectionModel ListSelectionModel}
137      */

138     public static int calcSelectionMode(List JavaDoc vals, List JavaDoc sel) {
139         ListSelectionModel lsm = calcListSelectionModel(vals, sel, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
140         return calcSelectionMode(lsm);
141     }
142
143     /**
144      * This method computes the actual mode for the given ListSelectionModel.
145      * See {@link org.enhydra.barracuda.core.comp.ListSelectionModel ListSelectionModel} for the range
146      * of permitted values.
147      *
148      * @param lsm a <code>ListSelectionModel</code> value
149      * @return an <code>int</code> value
150      */

151     public static int calcSelectionMode(ListSelectionModel lsm) {
152         int selMode = -1;
153         int min = lsm.getMinSelectionIndex();
154         if(min != -1) {
155             int max = lsm.getMaxSelectionIndex();
156             if(min != max) {
157                 boolean singleInterval = true;
158                 for(int i = min; i <= max; i++) {
159                     singleInterval = singleInterval && lsm.isSelectedIndex(i);
160                 }
161                 if(singleInterval) {
162                     selMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
163                 } else {
164                     selMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
165                 }
166             } else {
167                 selMode = ListSelectionModel.SINGLE_SELECTION;
168             }
169         }
170         return selMode;
171     }
172
173     /**
174      * A convenience method that takes two lists, the latter beeing a subset of the first, and
175      * computes the ListSelectionModel for them.
176      *
177      * @param vals a <code>List</code> value
178      * @param sel a <code>List</code> value
179      * @param selType see {@link org.enhydra.barracuda.core.comp.ListSelectionModel ListSelectionModel} for the range
180      * of permitted values
181      * @return a <code>ListSelectionModel</code> value
182      */

183     public static ListSelectionModel calcListSelectionModel(List JavaDoc vals, List JavaDoc sel, int selType) {
184         if(logger.isDebugEnabled()) {
185             logger.debug("Calculating");
186         }
187
188         ListSelectionModel lm = new org.enhydra.barracuda.core.comp.DefaultListSelectionModel();
189         lm.setSelectionMode(selType);
190         if(vals == null || sel == null || sel.size() == 0) {
191             logger.debug("No selection");
192             return lm;
193         }
194         for(int i = 0;i < vals.size(); i++) {
195             Object JavaDoc key;
196             Object JavaDoc rawKey, item = vals.get(i);
197             if (logger.isDebugEnabled() && item != null) {
198                 logger.debug("Itemtype:" + item.getClass());
199             } // end of if ((logger.isDebugEnabled())
200

201             if(item instanceof ItemMap) {
202                 key = ((ItemMap)item).getKey();
203             } else {
204                 key = item;
205             }
206             if (logger.isDebugEnabled() && key != null) {
207                 logger.debug("ItemKey:" + key);
208                 logger.debug("ItemKeyType:" + key.getClass());
209             } // end of if ((logger.isDebugEnabled())
210

211             if(sel.contains(key) || sel.contains(key.toString())) {
212                 logger.debug("Added interval " + i + " to " + i);
213                 lm.addSelectionInterval(i, i);
214             }
215         }
216         if(logger.isDebugEnabled()) {
217             for(int i = 0;i < vals.size(); i++) {
218                 if(lm.isSelectedIndex(i))
219                     logger.debug("Index " + i + " is selected");
220             }
221         }
222         return lm;
223     }
224
225     /**
226      * Creates a new <code>SelectFormElement</code> instance.
227      *
228      * @param ikey a <code>java.lang.String</code> value
229      * @param iname a <code>java.lang.String</code> value
230      * @param itype a <code>FormType</code> value
231      * @param idefaultVal a <code>java.lang.Object</code> value
232      * @param ivalidator a <code>FormValidator</code> value
233      * @param iallowMultiples a <code>boolean</code> value
234      */

235     public SelectFormElement(java.lang.String JavaDoc ikey,
236                              java.lang.String JavaDoc iname,
237                              FormType itype,
238                              java.lang.Object JavaDoc idefaultVal,
239                              FormValidator ivalidator,
240                              boolean iallowMultiples) {
241         super(ikey, iname, itype, idefaultVal, ivalidator, iallowMultiples);
242
243     }
244
245     /**
246      * Creates a new <code>SelectFormElement</code> instance.
247      *
248      * @param lm a <code>ListModel</code> value
249      * @param ikey a <code>java.lang.String</code> value
250      * @param iname a <code>java.lang.String</code> value
251      * @param itype a <code>FormType</code> value
252      * @param idefaultVal a <code>java.lang.Object</code> value
253      * @param ivalidator a <code>FormValidator</code> value
254      * @param iallowMultiples a <code>boolean</code> value
255      */

256     public SelectFormElement(ListModel lm, java.lang.String JavaDoc ikey,
257                              java.lang.String JavaDoc iname,
258                              FormType itype,
259                              java.lang.Object JavaDoc idefaultVal,
260                              FormValidator ivalidator,
261                              boolean iallowMultiples) {
262         super(ikey, iname, itype, idefaultVal, ivalidator, iallowMultiples);
263         this.lm = lm;
264     }
265
266     /**
267      * Creates a new <code>SelectFormElement</code> instance.
268      *
269      * @param lm a <code>ListModel</code> value
270      * @param ikey a <code>java.lang.String</code> value
271      * @param itype a <code>FormType</code> value
272      * @param idefaultVal a <code>java.lang.Object</code> value
273      * @param ivalidator a <code>FormValidator</code> value
274      * @param iallowMultiples a <code>boolean</code> value
275      */

276     public SelectFormElement(ListModel lm,
277                              java.lang.String JavaDoc ikey,
278                              FormType itype,
279                              java.lang.Object JavaDoc idefaultVal,
280                              FormValidator ivalidator,
281                              boolean iallowMultiples) {
282         super(ikey, itype, idefaultVal, ivalidator, iallowMultiples);
283         this.lm = lm;
284     }
285
286     /**
287      * Creates a new <code>SelectFormElement</code> instance.
288      *
289      * @param lm a <code>ListModel</code> value
290      * @param lsm a <code>ListSelectionModel</code> value
291      * @param ikey a <code>java.lang.String</code> value
292      * @param iname a <code>java.lang.String</code> value
293      * @param itype a <code>FormType</code> value
294      * @param idefaultVal a <code>java.lang.Object</code> value
295      * @param ivalidator a <code>FormValidator</code> value
296      * @param iallowMultiples a <code>boolean</code> value
297      */

298     public SelectFormElement(ListModel lm, ListSelectionModel lsm, java.lang.String JavaDoc ikey,
299                              java.lang.String JavaDoc iname,
300                              FormType itype,
301                              java.lang.Object JavaDoc idefaultVal,
302                              FormValidator ivalidator,
303                              boolean iallowMultiples) {
304         super(ikey, iname, itype, idefaultVal, ivalidator, iallowMultiples);
305         this.lm = lm;
306         this.lsm = lsm;
307     }
308     /**
309      * Creates a new <code>SelectFormElement</code> instance.
310      *
311      * @param ikey a <code>java.lang.String</code> value
312      * @param itype a <code>FormType</code> value
313      * @param idefaultVal a <code>java.lang.Object</code> value
314      * @param ivalidator a <code>FormValidator</code> value
315      */

316     public SelectFormElement(java.lang.String JavaDoc ikey,
317                              FormType itype,
318                              java.lang.Object JavaDoc idefaultVal,
319                              FormValidator ivalidator) {
320         super(ikey, itype, idefaultVal, ivalidator);
321     }
322
323     /**
324      * Creates a new <code>SelectFormElement</code> instance.
325      *
326      * @param lm a <code>ListModel</code> value
327      * @param lsm a <code>ListSelectionModel</code> value
328      * @param ikey a <code>java.lang.String</code> value
329      * @param itype a <code>FormType</code> value
330      * @param idefaultVal a <code>java.lang.Object</code> value
331      * @param ivalidator a <code>FormValidator</code> value
332      */

333     public SelectFormElement(ListModel lm, ListSelectionModel lsm, java.lang.String JavaDoc ikey,
334                              FormType itype,
335                              java.lang.Object JavaDoc idefaultVal,
336                              FormValidator ivalidator) {
337         super(ikey, itype, idefaultVal, ivalidator);
338         this.lm = lm;
339         this.lsm = lsm;
340     }
341
342     /**
343      * Describe <code>render</code> method here.
344      *
345      * @param context a <code>ViewContext</code> value
346      * @return an <code>Object</code> value
347      */

348     public Object JavaDoc render(ViewContext context) {
349         BSelect bs = new BSelect(getListModel());
350         bs.setName(getName());
351         bs.setSelectionModel(getListSelectionModel());
352         return bs;
353     }
354 }
355
Popular Tags