KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > form > core > ProcessSetForm


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 com.blandware.atleap.webapp.form.core;
17
18 import com.blandware.atleap.webapp.form.BaseForm;
19 import org.apache.struts.action.ActionMapping;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * <p>Mab-backed form bean that handles set selections of checkboxes or simple properties
30  * </p>
31  * <p><a HREF="ProcessSetForm.java.htm"><i>View Source</i></a></p>
32  * <p/>
33  *
34  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
35  * @version $Revision: 1.5 $ $Date: 2005/08/04 17:25:16 $
36  * @struts.form name="processSetForm"
37  */

38 public class ProcessSetForm extends BaseForm {
39
40     /**
41      * Selected elements
42      */

43     protected List JavaDoc selectedItems = Collections.synchronizedList(new ArrayList JavaDoc());
44
45     /**
46      * Holds all values for checked boxes. Need to have different field of type <code>java.util.Map</code>
47      * because there is no another way to get some value from checkbox. We can only know, was it checked or not.
48      */

49     protected Map JavaDoc checkedBoxes = Collections.synchronizedMap(new HashMap JavaDoc());
50
51     /**
52      * Creates the new instance of ProcessSetForm
53      */

54     public ProcessSetForm() {
55     }
56
57     /**
58      * Returns element from specified position in list of selected items
59      *
60      * @param index Position of element to return
61      * @return Selected item with given position
62      */

63     public Object JavaDoc getSelectedItem(int index) {
64         if ( selectedItems != null && index > 0 && index < selectedItems.size() ) {
65             return selectedItems.get(index);
66         } else {
67             return null;
68         }
69     }
70
71     /**
72      * Replaces element on specified position in list, or adds it to the end of
73      * list of selected items
74      *
75      * @param index Position to set element to. If index greater than or equal to list size, element will be added,
76      * otherwise it will replace element, which is on specified position in list.
77      * @param value Value to set
78      */

79     public void setSelectedItem(int index, Object JavaDoc value) {
80         if ( log.isDebugEnabled() ) {
81             log.debug("Setting element: index=" + index);
82         }
83         if ( index >= selectedItems.size() ) {
84             selectedItems.add(value);
85         } else {
86             selectedItems.set(index, value);
87         }
88     }
89
90     /**
91      * Returns a list of selected elements
92      *
93      * @return list of selected elements
94      */

95     public List JavaDoc getSelectedItems() {
96         return selectedItems;
97     }
98
99     /**
100      * Sets a list of selected elements
101      *
102      * @param selectedItems list of selected elements to set
103      */

104     public void setSelectedItems(List JavaDoc selectedItems) {
105         this.selectedItems = selectedItems;
106     }
107
108     /**
109      * Returns a mapping from keys of checked boxes to some values (those values
110      * don't matter; only their existance in map does matter)
111      *
112      * @return mapping from keys of checked boxes to some values
113      */

114     public Map JavaDoc getCheckedBoxes() {
115         return checkedBoxes;
116     }
117
118     /**
119      * Returns a mapping from keys of checked boxes to some values (those values
120      * don't matter; only their existance in map does matter)
121      *
122      * @param checkedBoxes mapping from keys of checked boxes to some values
123      */

124     public void setCheckedBoxes(Map JavaDoc checkedBoxes) {
125         this.checkedBoxes = checkedBoxes;
126     }
127
128
129     /**
130      * Returns <code>true</code>, if checkbox was checked
131      *
132      * @param key Key to look for. If it is exists in map, <code>Boolean.TRUE</code> will be returned
133      * @return <code>Boolean.TRUE</code> if key exists in map, and <code>Boolean.FALSE</code> otherwise
134      */

135     public Object JavaDoc getCheckedBox(String JavaDoc key) {
136         if ( checkedBoxes.containsKey(key) ) {
137             return Boolean.TRUE;
138         } else {
139             return Boolean.FALSE;
140         }
141     }
142
143     /**
144      * Puts key in map
145      *
146      * @param key Key to associate value with
147      * @param value Value to put for specified key
148      */

149     public void setCheckedBox(String JavaDoc key, Object JavaDoc value) {
150         if ( log.isDebugEnabled() ) {
151             log.debug("Putting pair: key=" + key + "; value= " + value);
152         }
153         checkedBoxes.put(key, value);
154     }
155
156     /**
157      * Resets all properties to their default values
158      *
159      * @param mapping The ActionMapping used to select this instance
160      * @param request The non-http request we are proceeding
161      */

162     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
163         this.selectedItems = Collections.synchronizedList(new ArrayList JavaDoc());
164         this.checkedBoxes = Collections.synchronizedMap(new HashMap JavaDoc());
165     }
166
167 }
Popular Tags