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.ade.contenteditor.client.css.I_CmsLayoutBundle; 032import org.opencms.gwt.client.ui.input.CmsFilterSelectBox; 033 034import java.util.Map; 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.logical.shared.ValueChangeEvent; 041import com.google.gwt.event.logical.shared.ValueChangeHandler; 042import com.google.gwt.event.shared.HandlerRegistration; 043import com.google.gwt.user.client.ui.Composite; 044 045/** 046 * An option of a select type widget.<p> 047 * 048 * Regarding widget configuration, see <code>{@link org.opencms.acacia.client.widgets.CmsSelectConfigurationParser}</code>.<p> 049 */ 050public class CmsFilterSelectWidget extends Composite implements I_CmsEditWidget, I_CmsHasDisplayDirection { 051 052 /** The global select box. */ 053 protected CmsFilterSelectBox m_selectBox = new CmsFilterSelectBox(); 054 055 /** Value of the activation. */ 056 private boolean m_active = true; 057 058 /** THe default value. */ 059 private String m_defaultValue; 060 061 /** The last value set through the setValue method. This is not necessarily the current widget value. */ 062 private String m_externalValue; 063 064 /** 065 * Constructs an CmsComboWidget with the in XSD schema declared configuration.<p> 066 * @param config The configuration string given from OpenCms XSD. 067 */ 068 public CmsFilterSelectWidget(String config) { 069 070 parseConfiguration(config); 071 072 // Place the check above the box using a vertical panel. 073 m_selectBox.addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxPanel()); 074 m_selectBox.setPopupResize(false); 075 // add some styles to parts of the selectbox. 076 m_selectBox.getOpener().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxSelected()); 077 m_selectBox.getSelectorPopup().addStyleName(I_CmsLayoutBundle.INSTANCE.globalWidgetCss().selectBoxPopup()); 078 m_selectBox.addValueChangeHandler(new ValueChangeHandler<String>() { 079 080 public void onValueChange(ValueChangeEvent<String> event) { 081 082 fireChangeEvent(); 083 084 } 085 086 }); 087 088 // All composites must call initWidget() in their constructors. 089 initWidget(m_selectBox); 090 091 } 092 093 /** 094 * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler) 095 */ 096 public HandlerRegistration addFocusHandler(FocusHandler handler) { 097 098 return addDomHandler(handler, FocusEvent.getType()); 099 } 100 101 /** 102 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) 103 */ 104 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { 105 106 return addHandler(handler, ValueChangeEvent.getType()); 107 } 108 109 /** 110 * Represents a value change event.<p> 111 * 112 */ 113 public void fireChangeEvent() { 114 115 ValueChangeEvent.fire(this, m_selectBox.getFormValueAsString()); 116 117 } 118 119 /** 120 * @see org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection#getDisplayingDirection() 121 */ 122 public Direction getDisplayingDirection() { 123 124 return Direction.below; 125 } 126 127 /** 128 * @see com.google.gwt.user.client.ui.HasValue#getValue() 129 */ 130 public String getValue() { 131 132 return m_selectBox.getFormValueAsString(); 133 } 134 135 /** 136 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive() 137 */ 138 public boolean isActive() { 139 140 return m_active; 141 } 142 143 /** 144 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget() 145 */ 146 public void onAttachWidget() { 147 148 super.onAttach(); 149 } 150 151 /** 152 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element) 153 */ 154 public boolean owns(Element element) { 155 156 // TODO implement this in case we want the delete behavior for optional fields 157 return false; 158 159 } 160 161 /** 162 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean) 163 */ 164 public void setActive(boolean active) { 165 166 // check if value change. If not do nothing. 167 if (m_active == active) { 168 if (active && !m_selectBox.getFormValueAsString().equals(m_externalValue)) { 169 // in case the previously set external value does not match the internal select box value fire change event 170 // this is needed in case the external value didn't match any select option, 171 // in this case the select box will display the first available value so fire the change to reflect that 172 fireChangeEvent(); 173 } 174 return; 175 } 176 // set new value. 177 m_active = active; 178 // set the new value to the selectbox. 179 m_selectBox.setEnabled(active); 180 // fire change event if necessary. 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 com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object) 198 */ 199 public void setValue(String value) { 200 201 setValue(value, false); 202 203 } 204 205 /** 206 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean) 207 */ 208 public void setValue(String value, boolean fireEvents) { 209 210 Map<String, String> items = m_selectBox.getItems(); 211 m_selectBox.setFormValue(value, false); 212 m_externalValue = value; 213 if (fireEvents) { 214 fireChangeEvent(); 215 } 216 217 } 218 219 /** 220 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#shouldSetDefaultWhenDisabled() 221 */ 222 public boolean shouldSetDefaultWhenDisabled() { 223 224 return true; 225 } 226 227 /** 228 * Helper class for parsing the configuration of the select-box. <p> 229 * 230 * @param config the configuration string 231 * */ 232 private void parseConfiguration(String config) { 233 234 CmsSelectConfigurationParser parser = new CmsSelectConfigurationParser(config); 235 // set the help info first!! 236 for (Entry<String, String> helpEntry : parser.getHelpTexts().entrySet()) { 237 m_selectBox.setTitle(helpEntry.getKey(), helpEntry.getValue()); 238 } 239 //set value and option to the combo box. 240 m_selectBox.setItems(parser.getOptions()); 241 //if one entrance is declared for default. 242 if (parser.getDefaultValue() != null) { 243 //set the declared value selected. 244 m_selectBox.selectValue(parser.getDefaultValue()); 245 m_defaultValue = parser.getDefaultValue(); 246 } 247 fireChangeEvent(); 248 } 249 250}