001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.acacia.client.widgets;
029
030import org.opencms.acacia.client.css.I_CmsLayoutBundle;
031import org.opencms.gwt.client.I_CmsHasResizeOnShow;
032import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder;
033
034import com.google.gwt.dom.client.Element;
035import com.google.gwt.event.dom.client.FocusHandler;
036import com.google.gwt.event.logical.shared.HasResizeHandlers;
037import com.google.gwt.event.logical.shared.ResizeHandler;
038import com.google.gwt.event.logical.shared.ValueChangeHandler;
039import com.google.gwt.event.shared.HandlerRegistration;
040import com.google.gwt.user.client.ui.Composite;
041import com.google.gwt.user.client.ui.FlowPanel;
042import com.google.gwt.user.client.ui.HTML;
043import com.google.gwt.user.client.ui.Label;
044
045/**
046 * Wraps an edit widget to supply a widget label.<p>
047 */
048public class CmsFormWidgetWrapper extends Composite
049implements I_CmsFormEditWidget, HasResizeHandlers, I_CmsHasPlaceholder, I_CmsHasResizeOnShow, I_CmsHasDisplayDirection {
050
051    /** The edit widget. */
052    private I_CmsEditWidget m_editWidget;
053
054    /** The label. */
055    private HTML m_label;
056
057    /** The main panel. */
058    private FlowPanel m_mainPanel;
059
060    /**
061     * Constructor.<p>
062     */
063    public CmsFormWidgetWrapper() {
064
065        m_mainPanel = new FlowPanel();
066        m_label = new HTML();
067        m_label.setStyleName(I_CmsLayoutBundle.INSTANCE.form().label());
068        m_mainPanel.add(m_label);
069        initWidget(m_mainPanel);
070    }
071
072    /**
073     * Constructor.<p>
074     *
075     * @param editWidget the edit widget to wrap
076     */
077    public CmsFormWidgetWrapper(I_CmsEditWidget editWidget) {
078
079        this();
080        setEditWidget(editWidget);
081    }
082
083    /**
084     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
085     */
086    public HandlerRegistration addFocusHandler(FocusHandler handler) {
087
088        // make sure the widget has been initialized
089        assert m_editWidget != null;
090        return m_editWidget.addFocusHandler(handler);
091    }
092
093    /**
094     * @see com.google.gwt.event.logical.shared.HasResizeHandlers#addResizeHandler(com.google.gwt.event.logical.shared.ResizeHandler)
095     */
096    public HandlerRegistration addResizeHandler(ResizeHandler handler) {
097
098        // make sure the widget has been initialized
099        assert m_editWidget != null;
100        if (m_editWidget instanceof HasResizeHandlers) {
101            return ((HasResizeHandlers)m_editWidget).addResizeHandler(handler);
102        }
103        return null;
104    }
105
106    /**
107     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
108     */
109    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
110
111        // make sure the widget has been initialized
112        assert m_editWidget != null;
113        return m_editWidget.addValueChangeHandler(handler);
114    }
115
116    /**
117     * @see org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection#getDisplayingDirection()
118     */
119    public Direction getDisplayingDirection() {
120
121        return (m_editWidget instanceof I_CmsHasDisplayDirection)
122        ? ((I_CmsHasDisplayDirection)m_editWidget).getDisplayingDirection()
123        : Direction.none;
124    }
125
126    /**
127     * Gets the wrapped widget.<p>
128     *
129     * @return the wrapped widget
130     */
131    public I_CmsEditWidget getEditWidget() {
132
133        // make sure the widget has been initialized
134        assert m_editWidget != null;
135        return m_editWidget;
136    }
137
138    /**
139     * Returns the label widget.<p>
140     *
141     * @return the label widget
142     */
143    public Label getLabel() {
144
145        return m_label;
146    }
147
148    /**
149     * @see com.google.gwt.user.client.ui.HasValue#getValue()
150     */
151    public String getValue() {
152
153        // make sure the widget has been initialized
154        assert m_editWidget != null;
155        return m_editWidget.getValue();
156    }
157
158    /**
159     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
160     */
161    public boolean isActive() {
162
163        // make sure the widget has been initialized
164        assert m_editWidget != null;
165        return m_editWidget.isActive();
166    }
167
168    /**
169     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
170     */
171    public void onAttachWidget() {
172
173        super.onAttach();
174    }
175
176    /**
177     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
178     */
179    public boolean owns(Element element) {
180
181        // make sure the widget has been initialized
182        assert m_editWidget != null;
183        return (m_editWidget != null) && m_editWidget.owns(element);
184    }
185
186    /**
187     * @see org.opencms.gwt.client.I_CmsHasResizeOnShow#resizeOnShow()
188     */
189    public void resizeOnShow() {
190
191        // make sure the widget has been initialized
192        assert m_editWidget != null;
193        if (m_editWidget instanceof I_CmsHasResizeOnShow) {
194            ((I_CmsHasResizeOnShow)m_editWidget).resizeOnShow();
195        }
196    }
197
198    /**
199     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
200     */
201    public void setActive(boolean active) {
202
203        // make sure the widget has been initialized
204        assert m_editWidget != null;
205        m_editWidget.setActive(active);
206    }
207
208    /**
209     * The edit widget needs to set, before the widget may be used.<p>
210     *
211     * @param editWidget the edit widget to wrap
212     */
213    public void setEditWidget(I_CmsEditWidget editWidget) {
214
215        // the edit widget may be set only once
216        assert m_editWidget == null;
217        m_editWidget = editWidget;
218        m_mainPanel.add(m_editWidget);
219        m_editWidget.asWidget().addStyleName(I_CmsLayoutBundle.INSTANCE.form().widget());
220    }
221
222    /**
223     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
224     */
225    public void setName(String name) {
226
227        // make sure the widget has been initialized
228        assert m_editWidget != null;
229        m_editWidget.setName(name);
230
231    }
232
233    /**
234     * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String)
235     */
236    @Override
237    public void setPlaceholder(String placeholder) {
238
239        if (m_editWidget instanceof I_CmsHasPlaceholder) {
240            ((I_CmsHasPlaceholder)m_editWidget).setPlaceholder(placeholder);
241        }
242    }
243
244    /**
245     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
246     */
247    public void setValue(String value) {
248
249        // make sure the widget has been initialized
250        assert m_editWidget != null;
251        m_editWidget.setValue(value);
252    }
253
254    /**
255     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setValue(java.lang.String, boolean)
256     */
257    public void setValue(String value, boolean fireEvent) {
258
259        // make sure the widget has been initialized
260        assert m_editWidget != null;
261        m_editWidget.setValue(value, fireEvent);
262    }
263
264    /**
265     * @see org.opencms.acacia.client.widgets.I_CmsFormEditWidget#setWidgetInfo(java.lang.String, java.lang.String)
266     */
267    public void setWidgetInfo(String label, String help) {
268
269        m_label.setHTML(label);
270    }
271
272    /**
273     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#shouldSetDefaultWhenDisabled()
274     */
275    public boolean shouldSetDefaultWhenDisabled() {
276
277        assert m_editWidget != null;
278        return m_editWidget.shouldSetDefaultWhenDisabled();
279    }
280}