KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsCheckboxWidget.java,v $
3  * Date : $Date: 2005/06/23 11:11:23 $
4  * Version: $Revision: 1.9 $
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 import org.opencms.util.CmsStringUtil;
36 import org.opencms.xml.types.CmsXmlBooleanValue;
37
38 import java.util.Map JavaDoc;
39
40 /**
41  * Provides a standard HTML form checkbox widget, for use on a widget dialog.<p>
42  *
43  * @author Andreas Zahner
44  *
45  * @version $Revision: 1.9 $
46  *
47  * @since 6.0.0
48  */

49 public class CmsCheckboxWidget extends A_CmsWidget {
50
51     /** Suffix for the hidden input that contains the original value. */
52     public static final String JavaDoc HIDDEN_SUFFIX = ".value";
53
54     /**
55      * Creates a new checkbox widget.<p>
56      */

57     public CmsCheckboxWidget() {
58
59         // empty constructor is required for class registration
60
this("");
61     }
62
63     /**
64      * Creates a new checkbox widget with the given configuration.<p>
65      *
66      * @param configuration the configuration to use
67      */

68     public CmsCheckboxWidget(String JavaDoc configuration) {
69
70         super(configuration);
71     }
72
73     /**
74      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
75      */

76     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
77
78         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
79
80         result.append("<td class=\"xmlTd\">");
81         result.append("<input type=\"checkbox\" name=\"");
82         result.append(param.getId());
83         result.append("\" value=\"true\"");
84         boolean booleanValue = CmsXmlBooleanValue.getBooleanValue(cms, param);
85         if (booleanValue) {
86             result.append(" checked=\"checked\"");
87         }
88         result.append(">");
89         result.append("<input type=\"hidden\" name=\"");
90         result.append(param.getId());
91         result.append(HIDDEN_SUFFIX);
92         result.append("\" value=\"");
93         result.append(booleanValue);
94         result.append("\">");
95         result.append("</td>");
96
97         return result.toString();
98     }
99
100     /**
101      * @see org.opencms.widgets.I_CmsWidget#newInstance()
102      */

103     public I_CmsWidget newInstance() {
104
105         return new CmsCheckboxWidget(getConfiguration());
106     }
107
108     /**
109      * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
110      */

111     public void setEditorValue(
112         CmsObject cms,
113         Map JavaDoc formParameters,
114         I_CmsWidgetDialog widgetDialog,
115         I_CmsWidgetParameter param) {
116
117         String JavaDoc[] values = (String JavaDoc[])formParameters.get(param.getId());
118         if ((values != null) && (values.length > 0)) {
119
120             // first get the current boolean value for the element
121
boolean booleanValue = CmsXmlBooleanValue.getBooleanValue(cms, param);
122
123             // now check if there's a new value in the form parameters
124
String JavaDoc formValue = values[0].trim();
125             if (CmsStringUtil.isNotEmpty(formValue)) {
126                 booleanValue = Boolean.valueOf(formValue).booleanValue();
127             }
128
129             // set the value
130
param.setStringValue(cms, String.valueOf(booleanValue));
131
132         } else {
133             String JavaDoc value;
134             values = (String JavaDoc[])formParameters.get(param.getId() + HIDDEN_SUFFIX);
135             if ((values != null) && (values.length > 0)) {
136                 // found hidden value, so checkbox was not checked
137
value = Boolean.FALSE.toString();
138             } else {
139                 // no hidden value found, use default value
140
value = param.getDefault(cms);
141             }
142             param.setStringValue(cms, value);
143         }
144     }
145 }
Popular Tags