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.acacia.client.css.I_CmsWidgetsLayoutBundle; 031import org.opencms.gwt.client.ui.input.CmsComboBox; 032import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder; 033import org.opencms.gwt.client.util.CmsDomUtil; 034 035import java.util.Map.Entry; 036 037import com.google.gwt.dom.client.Element; 038import com.google.gwt.event.dom.client.FocusEvent; 039import com.google.gwt.event.dom.client.FocusHandler; 040import com.google.gwt.event.dom.client.KeyUpEvent; 041import com.google.gwt.event.dom.client.KeyUpHandler; 042import com.google.gwt.event.logical.shared.ValueChangeEvent; 043import com.google.gwt.event.logical.shared.ValueChangeHandler; 044import com.google.gwt.event.shared.HandlerRegistration; 045import com.google.gwt.user.client.ui.Composite; 046 047/** 048 * A combo box widget.<p> 049 * 050 * Regarding widget configuration, see <code>{@link org.opencms.acacia.client.widgets.CmsSelectConfigurationParser}</code>.<p> 051 */ 052public class CmsComboWidget extends Composite 053implements I_CmsEditWidget, I_CmsHasDisplayDirection, I_CmsHasPlaceholder { 054 055 /** Value of the activation. */ 056 private boolean m_active = true; 057 058 /** The combo box. */ 059 private CmsComboBox m_comboBox = new CmsComboBox(); 060 061 /** String to control new key press. */ 062 private String m_oldtext = ""; 063 064 /** 065 * Constructs an CmsComboWidget with the in XSD schema declared configuration.<p> 066 * 067 * @param config The configuration string given from OpenCms XSD. 068 */ 069 public CmsComboWidget(String config) { 070 071 parseConfiguration(config); 072 073 m_comboBox.addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxPanel()); 074 // add some styles to parts of the combobox. 075 m_comboBox.getOpener().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxSelected()); 076 m_comboBox.getSelectorPopup().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxPopup()); 077 m_comboBox.getTextBox().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().comboBoxInput()); 078 m_comboBox.getTextBox().addKeyUpHandler(new KeyUpHandler() { 079 080 public void onKeyUp(KeyUpEvent arg0) { 081 082 onkeyupevent(); 083 } 084 }); 085 086 m_comboBox.addValueChangeHandler(new ValueChangeHandler<String>() { 087 088 public void onValueChange(ValueChangeEvent<String> event) { 089 090 fireChangeEvent(); 091 092 } 093 094 }); 095 initWidget(m_comboBox); 096 m_comboBox.getTextBox().addFocusHandler(new FocusHandler() { 097 098 public void onFocus(FocusEvent event) { 099 100 CmsDomUtil.fireFocusEvent(CmsComboWidget.this); 101 } 102 }); 103 } 104 105 /** 106 * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler) 107 */ 108 public HandlerRegistration addFocusHandler(FocusHandler handler) { 109 110 return addDomHandler(handler, FocusEvent.getType()); 111 } 112 113 /** 114 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) 115 */ 116 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { 117 118 return addHandler(handler, ValueChangeEvent.getType()); 119 } 120 121 /** 122 * Represents a value change event.<p> 123 * 124 */ 125 public void fireChangeEvent() { 126 127 ValueChangeEvent.fire(this, m_comboBox.getFormValueAsString()); 128 129 } 130 131 /** 132 * @see org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection#getDisplayingDirection() 133 */ 134 public Direction getDisplayingDirection() { 135 136 return m_comboBox.displayingAbove() ? Direction.above : Direction.below; 137 } 138 139 /** 140 * @see com.google.gwt.user.client.ui.HasValue#getValue() 141 */ 142 public String getValue() { 143 144 return m_comboBox.getFormValueAsString(); 145 } 146 147 /** 148 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive() 149 */ 150 public boolean isActive() { 151 152 return m_active; 153 } 154 155 /** 156 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget() 157 */ 158 public void onAttachWidget() { 159 160 super.onAttach(); 161 } 162 163 /** 164 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element) 165 */ 166 public boolean owns(Element element) { 167 168 return getElement().isOrHasChild(element); 169 } 170 171 /** 172 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean) 173 */ 174 public void setActive(boolean active) { 175 176 if (active == m_active) { 177 return; 178 } 179 m_active = active; 180 m_comboBox.setEnabled(active); 181 if (active) { 182 fireChangeEvent(); 183 } 184 185 } 186 187 /** 188 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String) 189 */ 190 public void setName(String name) { 191 192 // no input field so nothing to do 193 194 } 195 196 /** 197 * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String) 198 */ 199 @Override 200 public void setPlaceholder(String placeholder) { 201 202 m_comboBox.setPlaceholder(placeholder); 203 } 204 205 /** 206 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object) 207 */ 208 public void setValue(String value) { 209 210 setValue(value, false); 211 212 } 213 214 /** 215 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean) 216 */ 217 public void setValue(String value, boolean fireEvents) { 218 219 //m_selectBox.selectValue(value); 220 m_comboBox.setFormValueAsString(value); 221 if (fireEvents) { 222 fireChangeEvent(); 223 } 224 225 } 226 227 /** 228 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#shouldSetDefaultWhenDisabled() 229 */ 230 public boolean shouldSetDefaultWhenDisabled() { 231 232 return true; 233 } 234 235 /** 236 * Helper function to handle the popup of the combobox. <p> 237 */ 238 protected void onkeyupevent() { 239 240 String newText = m_comboBox.getFormValueAsString(); 241 242 if (!newText.equals(m_oldtext)) { 243 fireChangeEvent(); 244 if (!newText.equals("")) { 245 m_comboBox.closeSelector(); 246 } else { 247 m_comboBox.openSelector(); 248 } 249 } 250 251 m_oldtext = newText; 252 } 253 254 /** 255 * Helper function for parsing the configuration of the combo-box.<p> 256 * 257 * @param config the configuration string. 258 */ 259 private void parseConfiguration(String config) { 260 261 CmsSelectConfigurationParser parser = new CmsSelectConfigurationParser(config); 262 // set the help info first!! 263 for (Entry<String, String> helpEntry : parser.getHelpTexts().entrySet()) { 264 m_comboBox.setTitle(helpEntry.getKey(), helpEntry.getValue()); 265 } 266 //set value and option to the combo box. 267 m_comboBox.setItems(parser.getOptions()); 268 //if one entrance is declared for default. 269 if (parser.getDefaultValue() != null) { 270 //set the declared value selected. 271 m_comboBox.selectValue(parser.getDefaultValue()); 272 } 273 fireChangeEvent(); 274 } 275}