KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > CmsMultiSelectWidget


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsMultiSelectWidget.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.widgets;
33
34 import org.opencms.file.CmsObject;
35
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * Provides a widget for a standard HTML form multi select list or a group of check boxes.<p>
42  *
43  * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description
44  * about the configuration String syntax for the select options.<p>
45  *
46  * The multi select widget does use the following select options:<ul>
47  * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the value of the option
48  * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value
49  * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the display name of the option
50  * </ul>
51  * <p>
52  *
53  * @author Michael Moossen
54  *
55  * @version $Revision: 1.3 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsMultiSelectWidget extends A_CmsSelectWidget {
60
61     /** Indicates if used html code is a multi selection list or a list of checkboxes. */
62     private boolean m_asCheckBoxes;
63
64     /**
65      * Creates a new select widget.<p>
66      */

67     public CmsMultiSelectWidget() {
68
69         // empty constructor is required for class registration
70
super();
71     }
72
73     /**
74      * Creates a select widget with the select options specified in the given configuration List.<p>
75      *
76      * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
77      *
78      * @param configuration the configuration (possible options) for the select widget
79      *
80      * @see CmsSelectWidgetOption
81      */

82     public CmsMultiSelectWidget(List JavaDoc configuration) {
83
84         this(configuration, false);
85     }
86
87     /**
88      * Creates a select widget with the select options specified in the given configuration List.<p>
89      *
90      * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
91      *
92      * @param configuration the configuration (possible options) for the select widget
93      * @param asCheckboxes indicates if used html code is a multi selection list or a list of checkboxes
94      *
95      * @see CmsSelectWidgetOption
96      */

97     public CmsMultiSelectWidget(List JavaDoc configuration, boolean asCheckboxes) {
98
99         super(configuration);
100         m_asCheckBoxes = asCheckboxes;
101     }
102
103     /**
104      * Creates a select widget with the specified select options.<p>
105      *
106      * @param configuration the configuration (possible options) for the select box
107      */

108     public CmsMultiSelectWidget(String JavaDoc configuration) {
109
110         super(configuration);
111     }
112
113     /**
114      * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
115      */

116     public void setEditorValue(
117         CmsObject cms,
118         Map JavaDoc formParameters,
119         I_CmsWidgetDialog widgetDialog,
120         I_CmsWidgetParameter param) {
121
122         String JavaDoc[] values = (String JavaDoc[])formParameters.get(param.getId());
123         if ((values != null) && (values.length > 0)) {
124             StringBuffer JavaDoc value = new StringBuffer JavaDoc(128);
125             for (int i = 0; i < values.length; i++) {
126                 if (i > 0) {
127                     value.append(',');
128                 }
129                 value.append(values[i]);
130             }
131             // set the value
132
param.setStringValue(cms, value.toString());
133         }
134     }
135
136     /**
137      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
138      */

139     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
140
141         String JavaDoc id = param.getId();
142         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
143
144         List JavaDoc options = parseSelectOptions(cms, widgetDialog, param);
145         result.append("<td class=\"xmlTd\">");
146         if (!m_asCheckBoxes) {
147             result.append("<select multiple size='");
148             result.append(options.size());
149             result.append("' class=\"xmlInput");
150             if (param.hasError()) {
151                 result.append(" xmlInputError");
152             }
153             result.append("\" name=\"");
154             result.append(id);
155             result.append("\" id=\"");
156             result.append(id);
157             result.append("\">");
158         }
159
160         // get select box options from default value String
161
List JavaDoc selected = getSelectedValues(cms, param);
162         Iterator JavaDoc i = options.iterator();
163         while (i.hasNext()) {
164             CmsSelectWidgetOption option = (CmsSelectWidgetOption)i.next();
165             // create the option
166
if (!m_asCheckBoxes) {
167                 result.append("<option value=\"");
168                 result.append(option.getValue());
169                 result.append("\"");
170                 if (selected.contains(option.getValue())) {
171                     result.append(" selected=\"selected\"");
172                 }
173                 result.append(">");
174                 result.append(option.getOption());
175                 result.append("</option>");
176             } else {
177                 result.append("<input type='checkbox' name='");
178                 result.append(id);
179                 result.append("' value='");
180                 result.append(option.getValue());
181                 result.append("'");
182                 if (selected.contains(option.getValue())) {
183                     result.append(" checked");
184                 }
185                 result.append(">");
186                 result.append(option.getOption());
187                 result.append("<br>");
188             }
189         }
190         if (!m_asCheckBoxes) {
191             result.append("</select>");
192         }
193         result.append("</td>");
194
195         return result.toString();
196     }
197
198     /**
199      * @see org.opencms.widgets.I_CmsWidget#newInstance()
200      */

201     public I_CmsWidget newInstance() {
202
203         return new CmsMultiSelectWidget(getConfiguration());
204     }
205 }
Popular Tags