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.ade.contenteditor.widgetregistry.client;
029
030import org.opencms.acacia.client.widgets.I_CmsFormEditWidget;
031import org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection;
032import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder;
033
034import com.google.gwt.dom.client.Document;
035import com.google.gwt.dom.client.Element;
036import com.google.gwt.event.dom.client.DomEvent;
037import com.google.gwt.event.dom.client.FocusEvent;
038import com.google.gwt.event.dom.client.FocusHandler;
039import com.google.gwt.event.logical.shared.ValueChangeEvent;
040import com.google.gwt.event.logical.shared.ValueChangeHandler;
041import com.google.gwt.event.shared.HandlerRegistration;
042import com.google.gwt.user.client.ui.Widget;
043
044/**
045 * Wrapper for a native widget.<p>
046 */
047public final class WidgetWrapper extends Widget
048implements I_CmsFormEditWidget, I_CmsHasDisplayDirection, I_CmsHasPlaceholder {
049
050    /** The wrapped native widget. */
051    private NativeEditWidget m_nativeWidget;
052
053    /**
054     * Constructor.<p>
055     *
056     * @param nativeWidget the native widget
057     */
058    protected WidgetWrapper(NativeEditWidget nativeWidget) {
059
060        m_nativeWidget = nativeWidget;
061        setElement(m_nativeWidget.getElement());
062        initNativeWidget();
063    }
064
065    /**
066     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
067     */
068    public HandlerRegistration addFocusHandler(FocusHandler handler) {
069
070        return addDomHandler(handler, FocusEvent.getType());
071    }
072
073    /**
074     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
075     */
076    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
077
078        return addHandler(handler, ValueChangeEvent.getType());
079    }
080
081    /**
082     * @see org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection#getDisplayingDirection()
083     */
084    public Direction getDisplayingDirection() {
085
086        try {
087            return Direction.valueOf(m_nativeWidget.getDisplayingDirection());
088        } catch (Exception e) {
089            return Direction.none;
090        }
091    }
092
093    /**
094     * @see com.google.gwt.user.client.ui.HasValue#getValue()
095     */
096    public String getValue() {
097
098        return m_nativeWidget.getValue();
099    }
100
101    /**
102     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
103     */
104    public boolean isActive() {
105
106        return m_nativeWidget.isActive();
107    }
108
109    /**
110     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
111     */
112    public void onAttachWidget() {
113
114        onAttach();
115    }
116
117    /**
118     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
119     */
120    public boolean owns(Element element) {
121
122        // TODO implement this in case we want the delete behavior for optional fields
123        return false;
124
125    }
126
127    /**
128     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
129     */
130    public void setActive(boolean active) {
131
132        m_nativeWidget.setActive(active);
133    }
134
135    /**
136     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
137     */
138    public void setName(String name) {
139
140        // no input field so nothing to do
141
142    }
143
144    /**
145     * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String)
146     */
147    @Override
148    public void setPlaceholder(String placeholder) {
149
150        m_nativeWidget.setPlaceholder(placeholder);
151    }
152
153    /**
154     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
155     */
156    public void setValue(String value) {
157
158        setValue(value, true);
159    }
160
161    /**
162     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setValue(java.lang.String, boolean)
163     */
164    public void setValue(String value, boolean fireEvents) {
165
166        m_nativeWidget.setValue(value, fireEvents);
167    }
168
169    /**
170     * @see org.opencms.acacia.client.widgets.I_CmsFormEditWidget#setWidgetInfo(java.lang.String, java.lang.String)
171     */
172    public void setWidgetInfo(String label, String help) {
173
174        m_nativeWidget.setWidgetInfo(label, help);
175    }
176
177    /**
178     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#shouldSetDefaultWhenDisabled()
179     */
180    public boolean shouldSetDefaultWhenDisabled() {
181
182        return m_nativeWidget.shouldSetDefaultWhenDisabled();
183    }
184
185    /**
186     * Fires the value change event.<p>
187     */
188    protected void fireChangeEvent() {
189
190        ValueChangeEvent.fire(this, getValue());
191    }
192
193    /**
194     * Fires the focus event.<p>
195     */
196    protected void fireFocusEvent() {
197
198        DomEvent.fireNativeEvent(Document.get().createFocusEvent(), this);
199    }
200
201    /**
202     * @see com.google.gwt.user.client.ui.Widget#onAttach()
203     */
204    @Override
205    protected void onAttach() {
206
207        super.onAttach();
208        m_nativeWidget.onAttachWidget();
209    }
210
211    /**
212     * Initializes the native widget by setting the on change and on focus functions.<p>
213     */
214    private native void initNativeWidget()/*-{
215        var self = this;
216        var nativeWidget = this.@org.opencms.ade.contenteditor.widgetregistry.client.WidgetWrapper::m_nativeWidget;
217        nativeWidget.onChangeCommand = function() {
218            self.@org.opencms.ade.contenteditor.widgetregistry.client.WidgetWrapper::fireChangeEvent()();
219        }
220        nativeWidget.onFocusCommand = function() {
221            self.@org.opencms.ade.contenteditor.widgetregistry.client.WidgetWrapper::fireFocusEvent()();
222        }
223    }-*/;
224}