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.datebox.CmsDateBox;
032import org.opencms.gwt.client.util.CmsDomUtil;
033
034import java.util.Date;
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.KeyCodes;
040import com.google.gwt.event.dom.client.KeyPressEvent;
041import com.google.gwt.event.dom.client.KeyPressHandler;
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.json.client.JSONObject;
046import com.google.gwt.json.client.JSONParser;
047import com.google.gwt.json.client.JSONValue;
048import com.google.gwt.user.client.ui.Composite;
049
050/**
051 * Provides a DHTML calendar widget, for use on a widget dialog.<p>
052 *
053 * */
054public class CmsCalendarWidget extends Composite implements I_CmsEditWidget, I_CmsHasDisplayDirection {
055
056    /** Value of the activation. */
057    private boolean m_active = true;
058
059    /** The global select box. */
060    private CmsDateBox m_dateBox = new CmsDateBox();
061
062    /**
063     * Constructs an CmsComboWidget with the in XSD schema declared configuration.<p>
064     * @param config The configuration string given from OpenCms XSD.
065     */
066    public CmsCalendarWidget(String config) {
067
068        JSONObject jsonConfig = JSONParser.parseStrict(config).isObject();
069        JSONValue jFixedTime = jsonConfig.get("fixedTime");
070        if ((jFixedTime != null) && (jFixedTime.isString() != null)) {
071            String fixedTime = jFixedTime.isString().stringValue();
072            m_dateBox.setFixedTime(fixedTime);
073            m_dateBox.setDateOnly(true);
074        }
075        m_dateBox.setAllowInvalidValue(true);
076
077        // All composites must call initWidget() in their constructors.
078        initWidget(m_dateBox);
079
080        m_dateBox.getTextField().getTextBoxContainer().addStyleName(
081            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().calendarStyle());
082        ValueChangeHandler<Date> test = new ValueChangeHandler<Date>() {
083
084            public void onValueChange(ValueChangeEvent<Date> arg0) {
085
086                fireChangeEvent();
087
088            }
089
090        };
091
092        m_dateBox.addValueChangeHandler(test);
093        m_dateBox.addKeyPressHandler(new KeyPressHandler() {
094
095            public void onKeyPress(KeyPressEvent arg0) {
096
097                int keyCode = arg0.getUnicodeCharCode();
098                if (keyCode == 0) {
099                    // Probably Firefox
100                    keyCode = arg0.getNativeEvent().getKeyCode();
101                }
102                if (keyCode == KeyCodes.KEY_ENTER) {
103                    fireChangeEvent();
104                }
105
106            }
107        });
108        m_dateBox.getTextField().addFocusHandler(new FocusHandler() {
109
110            public void onFocus(FocusEvent event) {
111
112                CmsDomUtil.fireFocusEvent(CmsCalendarWidget.this);
113            }
114        });
115    }
116
117    /**
118     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
119     */
120    public HandlerRegistration addFocusHandler(FocusHandler handler) {
121
122        return addDomHandler(handler, FocusEvent.getType());
123    }
124
125    /**
126     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
127     */
128    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
129
130        return addHandler(handler, ValueChangeEvent.getType());
131    }
132
133    /**
134     * Represents a value change event.<p>
135     *
136     */
137    public void fireChangeEvent() {
138
139        ValueChangeEvent.fire(this, m_dateBox.getFormValueAsString());
140    }
141
142    /**
143     * @see org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection#getDisplayingDirection()
144     */
145    public Direction getDisplayingDirection() {
146
147        return Direction.below;
148    }
149
150    /**
151     * @see com.google.gwt.user.client.ui.HasValue#getValue()
152     */
153    public String getValue() {
154
155        String result = m_dateBox.getFormValueAsString();
156        return result;
157    }
158
159    /**
160     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
161     */
162    public boolean isActive() {
163
164        return m_active;
165    }
166
167    /**
168     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
169     */
170    public void onAttachWidget() {
171
172        super.onAttach();
173    }
174
175    /**
176     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
177     */
178    public boolean owns(Element element) {
179
180        return getElement().isOrHasChild(element);
181    }
182
183    /**
184     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
185     */
186    public void setActive(boolean active) {
187
188        if (active == m_active) {
189            return;
190        }
191        m_active = active;
192        if (m_active) {
193            getElement().removeClassName(org.opencms.acacia.client.css.I_CmsLayoutBundle.INSTANCE.form().inActive());
194            getElement().focus();
195        } else {
196            getElement().addClassName(org.opencms.acacia.client.css.I_CmsLayoutBundle.INSTANCE.form().inActive());
197        }
198        if (active) {
199            fireChangeEvent();
200        }
201    }
202
203    /**
204     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
205     */
206    public void setName(String name) {
207
208        m_dateBox.setName(name);
209    }
210
211    /**
212     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
213     */
214    public void setValue(String value) {
215
216        setValue(value, false);
217
218    }
219
220    /**
221     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
222     */
223    public void setValue(String value, boolean fireEvents) {
224
225        m_dateBox.setFormValueAsString(value);
226        if (fireEvents) {
227            fireChangeEvent();
228        }
229
230    }
231}