001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.acacia.client.widgets; 029 030import org.opencms.gwt.client.ui.input.CmsCheckBox; 031import org.opencms.gwt.client.ui.input.CmsMultiCheckBox; 032 033import java.util.HashMap; 034import java.util.Iterator; 035import java.util.List; 036import java.util.Map; 037 038import com.google.gwt.dom.client.Element; 039import com.google.gwt.event.dom.client.FocusEvent; 040import com.google.gwt.event.dom.client.FocusHandler; 041import com.google.gwt.event.logical.shared.ValueChangeEvent; 042import com.google.gwt.event.logical.shared.ValueChangeHandler; 043import com.google.gwt.event.shared.HandlerRegistration; 044import com.google.gwt.user.client.ui.Composite; 045 046/** 047 * Provides a standard HTML form checkbox widget, for use on a widget dialog.<p> 048 * 049 * 050 * @since 8.5.0 051 * 052 */ 053public class CmsMultiCheckboxWidget extends Composite implements I_CmsEditWidget { 054 055 /** Value of the activation. */ 056 private boolean m_active = true; 057 /** The multi check box panel. */ 058 private CmsMultiCheckBox m_multicheckbox; 059 /** The selected checkboxes. */ 060 private String m_selected; 061 062 /** 063 * Constructs an OptionalTextBox with the given caption on the check.<p> 064 * 065 * @param config configuration string 066 * 067 */ 068 public CmsMultiCheckboxWidget(String config) { 069 070 m_multicheckbox = new CmsMultiCheckBox(parseconfig(config)); 071 setValue(m_selected); 072 // All composites must call initWidget() in their constructors. 073 initWidget(m_multicheckbox); 074 //panel.add(m_multicheckbox); 075 076 List<CmsCheckBox> checkboxes = m_multicheckbox.getCheckboxes(); 077 Iterator<CmsCheckBox> it = checkboxes.iterator(); 078 while (it.hasNext()) { 079 it.next().addValueChangeHandler(new ValueChangeHandler<Boolean>() { 080 081 public void onValueChange(ValueChangeEvent<Boolean> arg0) { 082 083 fireChangeEvent(); 084 085 } 086 087 }); 088 } 089 090 } 091 092 /** 093 * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler) 094 */ 095 public HandlerRegistration addFocusHandler(FocusHandler handler) { 096 097 return addDomHandler(handler, FocusEvent.getType()); 098 } 099 100 /** 101 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) 102 */ 103 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { 104 105 return addHandler(handler, ValueChangeEvent.getType()); 106 } 107 108 /** 109 * Represents a value change event.<p> 110 */ 111 public void fireChangeEvent() { 112 113 ValueChangeEvent.fire(this, m_multicheckbox.getFormValueAsString()); 114 } 115 116 /** 117 * @see com.google.gwt.user.client.ui.HasValue#getValue() 118 */ 119 public String getValue() { 120 121 return m_multicheckbox.getFormValueAsString(); 122 } 123 124 /** 125 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive() 126 */ 127 public boolean isActive() { 128 129 return m_active; 130 } 131 132 /** 133 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget() 134 */ 135 public void onAttachWidget() { 136 137 super.onAttach(); 138 } 139 140 /** 141 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element) 142 */ 143 public boolean owns(Element element) { 144 145 // TODO implement this in case we want the delete behavior for optional fields 146 return false; 147 148 } 149 150 /** 151 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean) 152 */ 153 public void setActive(boolean active) { 154 155 if (active == m_active) { 156 return; 157 } 158 m_active = active; 159 m_multicheckbox.setEnabled(active); 160 if (active) { 161 fireChangeEvent(); 162 } 163 164 } 165 166 /** 167 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String) 168 */ 169 public void setName(String name) { 170 171 // no input field so nothing to do 172 173 } 174 175 /** 176 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object) 177 */ 178 public void setValue(String value) { 179 180 setValue(value, false); 181 182 } 183 184 /** 185 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean) 186 */ 187 public void setValue(String value, boolean fireEvents) { 188 189 m_selected = value; 190 m_multicheckbox.setFormValueAsString(value); 191 if (fireEvents) { 192 fireChangeEvent(); 193 } 194 195 } 196 197 /** 198 * Helper class for parsing the configuration in to a list of Radiobuttons. <p> 199 * 200 * @param config the configuration string 201 * @return Map of option and value of the checkbox 202 */ 203 private Map<String, String> parseconfig(String config) { 204 205 Map<String, String> result = new HashMap<String, String>(); 206 m_selected = ""; 207 String[] labels = config.split("\\|"); 208 for (int i = 0; i < labels.length; i++) { 209 210 if (labels[i].indexOf("*") >= 0) { 211 labels[i] = labels[i].replace("*", ""); 212 m_selected += labels[i] + "|"; 213 } 214 result.put(labels[i], labels[i]); 215 } 216 m_selected = m_selected.substring(0, m_selected.lastIndexOf("|")); 217 return result; 218 } 219 220}