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