001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.widgets; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.i18n.CmsMessages; 033import org.opencms.util.CmsStringUtil; 034import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType; 035import org.opencms.xml.types.A_CmsXmlContentValue; 036import org.opencms.xml.types.CmsXmlBooleanValue; 037 038import java.util.List; 039import java.util.Locale; 040import java.util.Map; 041 042/** 043 * Provides a standard HTML form checkbox widget, for use on a widget dialog.<p> 044 * 045 * @since 6.0.0 046 */ 047public class CmsCheckboxWidget extends A_CmsWidget implements I_CmsADEWidget { 048 049 /** Suffix for the hidden input that contains the original value. */ 050 public static final String HIDDEN_SUFFIX = ".value"; 051 052 /** 053 * Creates a new checkbox widget.<p> 054 */ 055 public CmsCheckboxWidget() { 056 057 // empty constructor is required for class registration 058 this(""); 059 } 060 061 /** 062 * Creates a new checkbox widget with the given configuration.<p> 063 * 064 * @param configuration the configuration to use 065 */ 066 public CmsCheckboxWidget(String configuration) { 067 068 super(configuration); 069 } 070 071 /** 072 * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale) 073 */ 074 public String getConfiguration( 075 CmsObject cms, 076 A_CmsXmlContentValue schemaType, 077 CmsMessages messages, 078 CmsResource resource, 079 Locale contentLocale) { 080 081 return getConfiguration(); 082 } 083 084 /** 085 * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject) 086 */ 087 public List<String> getCssResourceLinks(CmsObject cms) { 088 089 return null; 090 } 091 092 /** 093 * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType() 094 */ 095 public DisplayType getDefaultDisplayType() { 096 097 return DisplayType.wide; 098 } 099 100 /** 101 * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 102 */ 103 public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 104 105 StringBuffer result = new StringBuffer(16); 106 107 result.append("<td class=\"xmlTd\">"); 108 result.append("<input type=\"checkbox\" name=\""); 109 result.append(param.getId()); 110 result.append("\" value=\"true\""); 111 boolean booleanValue = CmsXmlBooleanValue.getBooleanValue(cms, param); 112 if (booleanValue) { 113 result.append(" checked=\"checked\""); 114 } 115 result.append(">"); 116 result.append("<input type=\"hidden\" name=\""); 117 result.append(param.getId()); 118 result.append(HIDDEN_SUFFIX); 119 result.append("\" value=\""); 120 result.append(booleanValue); 121 result.append("\">"); 122 result.append("</td>"); 123 124 return result.toString(); 125 } 126 127 /** 128 * @see org.opencms.widgets.I_CmsADEWidget#getInitCall() 129 */ 130 public String getInitCall() { 131 132 return null; 133 } 134 135 /** 136 * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject) 137 */ 138 public List<String> getJavaScriptResourceLinks(CmsObject cms) { 139 140 return null; 141 } 142 143 /** 144 * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName() 145 */ 146 public String getWidgetName() { 147 148 return CmsCheckboxWidget.class.getName(); 149 } 150 151 /** 152 * @see org.opencms.widgets.I_CmsADEWidget#isInternal() 153 */ 154 public boolean isInternal() { 155 156 return true; 157 } 158 159 /** 160 * @see org.opencms.widgets.I_CmsWidget#newInstance() 161 */ 162 public I_CmsWidget newInstance() { 163 164 return new CmsCheckboxWidget(getConfiguration()); 165 } 166 167 /** 168 * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 169 */ 170 @Override 171 public void setEditorValue( 172 CmsObject cms, 173 Map<String, String[]> formParameters, 174 I_CmsWidgetDialog widgetDialog, 175 I_CmsWidgetParameter param) { 176 177 String[] values = formParameters.get(param.getId()); 178 if ((values != null) && (values.length > 0)) { 179 180 // first get the current boolean value for the element 181 boolean booleanValue = CmsXmlBooleanValue.getBooleanValue(cms, param); 182 183 // now check if there's a new value in the form parameters 184 String formValue = values[0].trim(); 185 if (CmsStringUtil.isNotEmpty(formValue)) { 186 booleanValue = Boolean.valueOf(formValue).booleanValue(); 187 } 188 189 // set the value 190 param.setStringValue(cms, String.valueOf(booleanValue)); 191 192 } else { 193 String value; 194 values = formParameters.get(param.getId() + HIDDEN_SUFFIX); 195 if ((values != null) && (values.length > 0)) { 196 // found hidden value, so checkbox was not checked 197 value = Boolean.FALSE.toString(); 198 } else { 199 // no hidden value found, use default value 200 value = param.getDefault(cms); 201 } 202 param.setStringValue(cms, value); 203 } 204 } 205}