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_CmsEditWidget;
031
032import com.google.gwt.core.client.JavaScriptObject;
033import com.google.gwt.dom.client.Element;
034
035/**
036 * Overlay object for native java script widgets.<p>
037 */
038public final class NativeEditWidget extends JavaScriptObject {
039
040    /**
041     * Constructor.<p>
042     */
043    protected NativeEditWidget() {
044
045    }
046
047    /**
048     * Wraps a GWT edit widget instance to be used from within another GWT module.<p>
049     *
050     * @param widget the widget to wrap
051     * @param isFormWidget in case of a form widget
052     *
053     * @return the wrapping native java script object
054     */
055    public static native NativeEditWidget wrapWidget(I_CmsEditWidget widget, boolean isFormWidget)/*-{
056        var nat = {
057            instance : widget
058        };
059        nat.getElement = function() {
060            return this.instance.@org.opencms.acacia.client.widgets.I_CmsEditWidget::asWidget()().@com.google.gwt.user.client.ui.Widget::getElement()();
061        }
062        nat.isActive = function() {
063            return this.instance.@org.opencms.acacia.client.widgets.I_CmsEditWidget::isActive()();
064        }
065        nat.setActive = function(active) {
066            return this.instance.@org.opencms.acacia.client.widgets.I_CmsEditWidget::setActive(Z)(active);
067        }
068        nat.getValue = function() {
069            return this.instance.@org.opencms.acacia.client.widgets.I_CmsEditWidget::getValue()();
070        }
071        nat.setValue = function(value, fireEvent) {
072            this.instance.@org.opencms.acacia.client.widgets.I_CmsEditWidget::setValue(Ljava/lang/String;Z)(value, fireEvent);
073        }
074        if (isFormWidget) {
075            nat.setWidgetInfo = function(label, help) {
076                this.instance.@org.opencms.acacia.client.widgets.I_CmsFormEditWidget::setWidgetInfo(Ljava/lang/String;Ljava/lang/String;)(label,help);
077            }
078        } else {
079            nat.setWidgetInfo = function() {
080            }
081        }
082        nat.onAttachWidget = function() {
083            this.instance.@org.opencms.acacia.client.widgets.I_CmsEditWidget::onAttachWidget()();
084        }
085        nat.onChange = function() {
086            if (this.onChangeCommand != null) {
087                this.onChangeCommand();
088            }
089        };
090        nat.onFocus = function() {
091            if (this.onFocusCommand != null) {
092                this.onFocusCommand();
093            }
094        };
095        var nativeHandler = @org.opencms.ade.contenteditor.widgetregistry.client.NativeEditWidget::getNativeHandler(Lorg/opencms/ade/contenteditor/widgetregistry/client/NativeEditWidget;)(nat);
096        widget.@org.opencms.acacia.client.widgets.I_CmsEditWidget::addValueChangeHandler(Lcom/google/gwt/event/logical/shared/ValueChangeHandler;)(nativeHandler);
097        widget.@org.opencms.acacia.client.widgets.I_CmsEditWidget::addFocusHandler(Lcom/google/gwt/event/dom/client/FocusHandler;)(nativeHandler);
098        return nat;
099    }-*/;
100
101    /**
102     * Returns an event handler that delegates to a native java script object.<p>
103     *
104     * @param connector the native java script object
105     *
106     * @return the event handler
107     */
108    private static NativeEventHandler getNativeHandler(NativeEditWidget connector) {
109
110        return new NativeEventHandler(connector);
111    }
112
113    public native void setPlaceholder(String placeholder) /*-{
114        if (this.setPlaceholder) {
115            if (placeholder === null) {
116                placeholder = "";
117            }
118            this.setPlaceholder(placeholder);
119        }
120    }-*/;
121
122    public native boolean shouldSetDefaultWhenDisabled() /*-{
123        return this.shouldSetDefaultWhenDisabled
124                && this.shouldSetDefaultWhenDisabled();
125    }-*/;
126
127    /**
128     * Gets the display direction for popups used by this widget using the 'getDisplayingDirection' JavaScript method (or returns "none" if that method isn't available).
129     *
130     * @return the direction in which this widget displays popups ('above', 'below' or 'none')
131     */
132    protected native String getDisplayingDirection() /*-{
133        var result = null;
134        if (this.getDisplayingDirection) {
135            result = this.getDisplayingDirection();
136        }
137        if (!result) {
138            result = "none";
139        }
140        return result;
141    }-*/;
142
143    /**
144     * Returns the widget element.<p>
145     *
146     * @return the widget element
147     */
148    protected native Element getElement() /*-{
149        return this.getElement();
150    }-*/;
151
152    /**
153     * Returns the widget value.<p>
154     *
155     * @return the widget value
156     */
157    protected native String getValue()/*-{
158        return this.getValue();
159    }-*/;
160
161    /**
162     * Returns if the widget is active.<p>
163     *
164     * @return <code>true</code> if the widget is active
165     */
166    protected native boolean isActive() /*-{
167        return this.isActive();
168    }-*/;
169
170    /**
171     * Call when the widget was added into the window document.<p>
172     */
173    protected native void onAttachWidget()/*-{
174        this.onAttachWidget();
175    }-*/;
176
177    /**
178     * Sets the widget active.<p>
179     *
180     * @param active <code>true</code> to activate the widget
181     */
182    protected native void setActive(boolean active)/*-{
183        this.setActive(active);
184    }-*/;
185
186    /**
187     * Sets the widget value.<p>
188     *
189     * @param value the value
190     */
191    protected native void setValue(String value)/*-{
192        this.setValue(value, false);
193    }-*/;
194
195    /**
196     * Sets the widget value.<p>
197     *
198     * @param value the value
199     * @param fireEvent <code>true</code> to fire the value change event
200     */
201    protected native void setValue(String value, boolean fireEvent)/*-{
202        this.setValue(value, fireEvent);
203    }-*/;
204
205    /**
206     * Sets the widget label and help text.<p>
207     *
208     * @param label the widget label text
209     * @param help the widget help text
210     */
211    protected native void setWidgetInfo(String label, String help)/*-{
212        this.setWidgetInfo(label, help);
213    }-*/;
214}