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.gwt.client.ui.input.form;
029
030import org.opencms.gwt.client.ui.I_CmsButton;
031import org.opencms.gwt.client.ui.css.I_CmsInputCss;
032import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034import org.opencms.gwt.client.ui.input.form.CmsFieldTooltip.Data;
035import org.opencms.util.CmsStringUtil;
036
037import java.util.Arrays;
038import java.util.List;
039
040import com.google.common.base.Supplier;
041import com.google.common.base.Suppliers;
042import com.google.gwt.core.client.GWT;
043import com.google.gwt.event.dom.client.ClickEvent;
044import com.google.gwt.event.dom.client.ClickHandler;
045import com.google.gwt.event.dom.client.MouseOutEvent;
046import com.google.gwt.event.dom.client.MouseOutHandler;
047import com.google.gwt.event.dom.client.MouseOverEvent;
048import com.google.gwt.event.dom.client.MouseOverHandler;
049import com.google.gwt.uibinder.client.UiBinder;
050import com.google.gwt.uibinder.client.UiField;
051import com.google.gwt.user.client.ui.Composite;
052import com.google.gwt.user.client.ui.Label;
053import com.google.gwt.user.client.ui.Panel;
054import com.google.gwt.user.client.ui.Widget;
055
056/**
057 * A row in a properties form.<p>
058 *
059 * This widget contains both a label and a panel into which an input widget for the form field can be placed.
060 * These widgets are next to each other horizontally.
061 *
062 * @since 8.0.0
063 */
064public class CmsFormRow extends Composite {
065
066    /** The ui binder interface for this widget. */
067    protected interface I_CmsFormRowUiBinder extends UiBinder<Widget, CmsFormRow> {
068        // uibinder
069    }
070
071    /** The width of the label. */
072    public static final int LABEL_WIDTH = 160;
073
074    /** The width of the opener. */
075    public static final int OPENER_WIDTH = 16;
076
077    /** The default widget container width. */
078    public static final int WIDGET_CONTAINER_WIDTH = 370;
079
080    /** The required right margin. */
081    public static final int WIDGET_MARGIN_RIGHT = 15;
082
083    /** The CSS bundle used for this widget. */
084    protected static I_CmsInputCss CSS = I_CmsInputLayoutBundle.INSTANCE.inputCss();
085
086    /** The ui binder instance for this form row. */
087    private static I_CmsFormRowUiBinder uiBinder = GWT.create(I_CmsFormRowUiBinder.class);
088
089    /** List of style names for the help icon. */
090    public static List<String> ICON_STYLES = Arrays.asList(
091        I_CmsButton.ICON_FONT,
092        I_CmsButton.ICON_CIRCLE_HELP,
093        I_CmsLayoutBundle.INSTANCE.buttonCss().cmsFontIconButton(),
094        I_CmsLayoutBundle.INSTANCE.buttonCss().hoverBlack(),
095        I_CmsLayoutBundle.INSTANCE.buttonCss().helpIcon());
096
097    /** The label used for displaying the information icon. */
098    @UiField
099    protected Panel m_icon;
100
101    /** The label for the form row. */
102    @UiField
103    protected Label m_label;
104
105    /** Tag to show next to the field label. */
106    @UiField
107    protected Label m_tag;
108
109    /** The widget container for the form row. */
110    @UiField
111    protected Panel m_widgetContainer;
112
113    /**
114     * The default constructor.
115     */
116    public CmsFormRow() {
117
118        Widget main = uiBinder.createAndBindUi(this);
119        initWidget(main);
120        main.addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().highTextBoxes());
121    }
122
123    /**
124     * Returns the width of the label as a string.<p>
125     *
126     * @return the width of the label as a string
127     */
128    public static String getLabelWidth() {
129
130        return LABEL_WIDTH + "px";
131    }
132
133    /**
134     * Returns the width of the opener as a string.<p>
135     *
136     * @return the width of the opener as a string
137     */
138    public static String getOpenerWidth() {
139
140        return OPENER_WIDTH + "px";
141    }
142
143    /**
144     * Returns the left margin of the widget container as a string.<p>
145     *
146     * @return the left margin of the widget container as a string
147     */
148    public static String getWidgetContainerLeftMargin() {
149
150        return OPENER_WIDTH + LABEL_WIDTH + "px";
151    }
152
153    /**
154     * Returns the left margin of the widget container as a string.<p>
155     *
156     * @return the left margin of the widget container as a string
157     */
158    public static String getWidgetContainerWidth() {
159
160        return WIDGET_CONTAINER_WIDTH + "px";
161    }
162
163    /**
164     * Installs the DOM event handlers for displaying tooltips on a help icon.<p>
165     *
166     * The supplier passed in should not create a new tooltip data instance each time,
167     * but cache the different possible tooltip data instances.
168     *
169     * @param icon the help icon
170     * @param dataSupplier provides the tooltip data at the time the DOM events occur
171     */
172    public static void installTooltipEventHandlers(final Panel icon, final Supplier<Data> dataSupplier) {
173
174        icon.addDomHandler(new MouseOverHandler() {
175
176            public void onMouseOver(MouseOverEvent event) {
177
178                CmsFieldTooltip.getHandler().buttonHover(dataSupplier.get());
179            }
180        }, MouseOverEvent.getType());
181
182        icon.addDomHandler(new MouseOutHandler() {
183
184            public void onMouseOut(MouseOutEvent event) {
185
186                CmsFieldTooltip.getHandler().buttonOut(dataSupplier.get());
187            }
188
189        }, MouseOutEvent.getType());
190
191        icon.addDomHandler(new ClickHandler() {
192
193            public void onClick(ClickEvent event) {
194
195                CmsFieldTooltip.getHandler().buttonClick(dataSupplier.get());
196
197            }
198        }, ClickEvent.getType());
199    }
200
201    /**
202     * Gets the icon.<p>
203     *
204     * @return the icon
205     */
206    public Panel getIcon() {
207
208        return m_icon;
209    }
210
211    /**
212     * Returns the label for the form row.<p>
213     *
214     * @return the label for the form row
215     */
216    public Label getLabel() {
217
218        return m_label;
219    }
220
221    /**
222     * Returns the widget container for the form row.<p>
223     *
224     * @return the widget container for the form row
225     */
226    public Panel getWidgetContainer() {
227
228        return m_widgetContainer;
229    }
230
231    /**
232     * Initializes the style for the info button.<p>
233     */
234    public void initInfoStyle() {
235
236        m_icon.addStyleName(I_CmsButton.ICON_FONT);
237        m_icon.addStyleName(I_CmsButton.ICON_CIRCLE_HELP);
238        m_icon.addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().cmsFontIconButton());
239        m_icon.addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().hoverBlack());
240        m_icon.addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().helpIcon());
241    }
242
243    /**
244     * Shows the info icon and sets the information text as its title.<p>
245     *
246     * @param info the info
247     * @param isHtml true if info should be interpreted as HTML rather than plain text
248     */
249    public void setInfo(final String info, final boolean isHtml) {
250
251        if (info != null) {
252            if (!CmsStringUtil.isEmptyOrWhitespaceOnly(info)) {
253                initInfoStyle();
254                final Data tooltipData = new CmsFieldTooltip.Data(m_icon, info, isHtml);
255                final Panel icon = m_icon;
256                final Supplier<Data> dataSupplier = Suppliers.ofInstance(tooltipData);
257                installTooltipEventHandlers(icon, dataSupplier);
258            }
259        }
260    }
261
262    /**
263     * Sets the tag to show next to the field label.
264     *
265     * <p>To hide the tag, set it to null.
266     *
267     * @param tag the tag to display
268     */
269    public void setTag(String tag) {
270
271        if (tag != null) {
272            m_tag.setText(tag);
273            m_tag.setVisible(true);
274        } else {
275            m_tag.setVisible(false);
276        }
277
278    }
279
280}