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