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