001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (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, 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.util.CmsStringUtil; 032import org.opencms.workplace.CmsWorkplace; 033 034import java.util.Iterator; 035import java.util.List; 036import java.util.Map; 037 038/** 039 * Provides a widget for a standard HTML form multi select list or a group of check boxes.<p> 040 * 041 * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description 042 * about the configuration String syntax for the select options.<p> 043 * 044 * The multi select widget does use the following select options:<ul> 045 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the value of the option 046 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value 047 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the display name of the option 048 * </ul> 049 * <p> 050 * 051 * @since 6.0.0 052 */ 053public class CmsMultiSelectWidget extends A_CmsSelectWidget { 054 055 /** Configuration parameter to set the height from the select widget in pixel. */ 056 public static final String CONFIGURATION_ASCHECKBOXES = "ascheckboxes"; 057 058 /** Configuration parameter to indicate the multi-select needs to be activated by a check box. */ 059 public static final String CONFIGURATION_REQUIRES_ACTIVATION = "requiresactivation"; 060 061 /** Indicates if used html code is a multi selection list or a list of checkboxes. */ 062 private boolean m_asCheckBoxes; 063 064 /** Flag to indicate if the multi-select needs to be activated by a check box. */ 065 private boolean m_requiresActivation; 066 067 /** 068 * Creates a new select widget.<p> 069 */ 070 public CmsMultiSelectWidget() { 071 072 // empty constructor is required for class registration 073 super(); 074 } 075 076 /** 077 * Creates a select widget with the select options specified in the given configuration List.<p> 078 * 079 * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p> 080 * 081 * @param configuration the configuration (possible options) for the select widget 082 * 083 * @see CmsSelectWidgetOption 084 */ 085 public CmsMultiSelectWidget(List<CmsSelectWidgetOption> configuration) { 086 087 this(configuration, false); 088 } 089 090 /** 091 * Creates a select widget with the select options specified in the given configuration List.<p> 092 * 093 * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p> 094 * 095 * @param configuration the configuration (possible options) for the select widget 096 * @param asCheckboxes indicates if used html code is a multi selection list or a list of checkboxes 097 * 098 * @see CmsSelectWidgetOption 099 */ 100 public CmsMultiSelectWidget(List<CmsSelectWidgetOption> configuration, boolean asCheckboxes) { 101 102 super(configuration); 103 m_asCheckBoxes = asCheckboxes; 104 } 105 106 /** 107 * Creates a select widget with the specified select options.<p> 108 * 109 * @param configuration the configuration (possible options) for the select box 110 */ 111 public CmsMultiSelectWidget(String configuration) { 112 113 super(configuration); 114 } 115 116 /** 117 * @param cms 118 * @param formParameters 119 * @param widgetDialog 120 * @param param 121 */ 122 public static void setMultiSelectEditorValue( 123 CmsObject cms, 124 Map<String, String[]> formParameters, 125 I_CmsWidgetDialog widgetDialog, 126 I_CmsWidgetParameter param) { 127 128 String[] values = formParameters.get(param.getId()); 129 if ((values != null) && (values.length > 0)) { 130 StringBuffer value = new StringBuffer(128); 131 for (int i = 0; i < values.length; i++) { 132 if (i > 0) { 133 value.append(','); 134 } 135 value.append(values[i]); 136 } 137 // set the value 138 param.setStringValue(cms, value.toString()); 139 } else { 140 // erase: 141 param.setStringValue(cms, ""); 142 } 143 } 144 145 /** 146 * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog) 147 */ 148 @Override 149 public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 150 151 return getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/multiselector.js"); 152 } 153 154 /** 155 * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 156 */ 157 public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 158 159 String id = param.getId(); 160 StringBuffer result = new StringBuffer(16); 161 162 List<CmsSelectWidgetOption> options = parseSelectOptions(cms, widgetDialog, param); 163 result.append("<td class=\"xmlTd\">"); 164 // the configured select widget height start element 165 if (!m_asCheckBoxes) { 166 if (m_requiresActivation) { 167 result.append("<input style=\"vertical-align:middle;\" type=\"checkbox\" id=\"check" 168 + id 169 + "\" name=\"check" 170 + id 171 + "\"" 172 + "onclick=toggleMultiSelectWidget(this);" 173 + " />"); 174 result.append(" <label style=\"vertical-align:middle;\" for=\"check" + id + "\">"); 175 result.append(widgetDialog.getMessages().key(Messages.GUI_MULTISELECT_ACTIVATE_0)); 176 result.append("</label> "); 177 // adding hidden input with the current value, because disabled select box value won't be submitted 178 result.append("<input type='hidden' name='").append(id).append("' id='").append(id).append("' value='"); 179 List<String> values = getSelectedValues(cms, param); 180 if (values.size() > 0) { 181 result.append(values.get(0)); 182 for (int i = 1; i < values.size(); i++) { 183 result.append(",").append(values.get(i)); 184 } 185 } 186 result.append("' />"); 187 id = "display" + id; 188 } 189 result.append("<select multiple size='"); 190 result.append(options.size()); 191 result.append("' style=\"vertical-align:middle;\" class=\"xmlInput"); 192 if (param.hasError()) { 193 result.append(" xmlInputError"); 194 } 195 result.append("\" "); 196 if (m_requiresActivation) { 197 result.append("disabled=\"true\" "); 198 } 199 result.append("name=\""); 200 result.append(id); 201 result.append("\" id=\""); 202 result.append(id); 203 result.append("\">"); 204 } 205 206 // get select box options from default value String 207 List<String> selected = getSelectedValues(cms, param); 208 Iterator<CmsSelectWidgetOption> i = options.iterator(); 209 while (i.hasNext()) { 210 CmsSelectWidgetOption option = i.next(); 211 // create the option 212 if (!m_asCheckBoxes) { 213 result.append("<option value=\""); 214 result.append(option.getValue()); 215 result.append("\""); 216 if (selected.contains(option.getValue())) { 217 result.append(" selected=\"selected\""); 218 } 219 result.append(">"); 220 result.append(option.getOption()); 221 result.append("</option>"); 222 } else { 223 result.append("<input type='checkbox' name='"); 224 result.append(id); 225 result.append("' value='"); 226 result.append(option.getValue()); 227 result.append("'"); 228 if (selected.contains(option.getValue())) { 229 result.append(" checked"); 230 } 231 result.append(">"); 232 result.append(option.getOption()); 233 result.append("<br>"); 234 } 235 } 236 if (!m_asCheckBoxes) { 237 result.append("</select>"); 238 } 239 result.append("</td>"); 240 return result.toString(); 241 } 242 243 /** 244 * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName() 245 */ 246 @Override 247 public String getWidgetName() { 248 249 return CmsMultiSelectWidget.class.getName(); 250 } 251 252 /** 253 * @see org.opencms.widgets.I_CmsWidget#newInstance() 254 */ 255 public I_CmsWidget newInstance() { 256 257 return new CmsMultiSelectWidget(getConfiguration()); 258 } 259 260 /** 261 * @see org.opencms.widgets.A_CmsWidget#setConfiguration(java.lang.String) 262 */ 263 @Override 264 public void setConfiguration(String configuration) { 265 266 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration)) { 267 int asCheckBoxesIndex = configuration.indexOf(CONFIGURATION_ASCHECKBOXES); 268 if (asCheckBoxesIndex != -1) { 269 // the height is set 270 String asCheckBoxes = configuration.substring(asCheckBoxesIndex 271 + CONFIGURATION_ASCHECKBOXES.length() 272 + 1); 273 if (asCheckBoxes.indexOf('|') != -1) { 274 // cut eventual following configuration values 275 asCheckBoxes = asCheckBoxes.substring(0, asCheckBoxes.indexOf('|')); 276 } 277 m_asCheckBoxes = Boolean.parseBoolean(asCheckBoxes); 278 } 279 int reqiresActivationIndex = configuration.indexOf(CONFIGURATION_REQUIRES_ACTIVATION); 280 if (reqiresActivationIndex != -1) { 281 // the height is set 282 String requiresActivation = configuration.substring(reqiresActivationIndex 283 + CONFIGURATION_REQUIRES_ACTIVATION.length() 284 + 1); 285 if (requiresActivation.indexOf('|') != -1) { 286 // cut eventual following configuration values 287 requiresActivation = requiresActivation.substring(0, requiresActivation.indexOf('|')); 288 } 289 m_requiresActivation = Boolean.parseBoolean(requiresActivation); 290 } 291 } 292 super.setConfiguration(configuration); 293 } 294 295 /** 296 * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 297 */ 298 @Override 299 public void setEditorValue( 300 CmsObject cms, 301 Map<String, String[]> formParameters, 302 I_CmsWidgetDialog widgetDialog, 303 I_CmsWidgetParameter param) { 304 305 setMultiSelectEditorValue(cms, formParameters, widgetDialog, param); 306 } 307}