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    /** The widget container for the form row. */
106    @UiField
107    protected Panel m_widgetContainer;
108
109    /**
110     * The default constructor.
111     */
112    public CmsFormRow() {
113
114        Widget main = uiBinder.createAndBindUi(this);
115        initWidget(main);
116        main.addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().highTextBoxes());
117    }
118
119    /**
120     * Returns the width of the label as a string.<p>
121     *
122     * @return the width of the label as a string
123     */
124    public static String getLabelWidth() {
125
126        return LABEL_WIDTH + "px";
127    }
128
129    /**
130     * Returns the width of the opener as a string.<p>
131     *
132     * @return the width of the opener as a string
133     */
134    public static String getOpenerWidth() {
135
136        return OPENER_WIDTH + "px";
137    }
138
139    /**
140     * Returns the left margin of the widget container as a string.<p>
141     *
142     * @return the left margin of the widget container as a string
143     */
144    public static String getWidgetContainerLeftMargin() {
145
146        return OPENER_WIDTH + LABEL_WIDTH + "px";
147    }
148
149    /**
150     * Returns the left margin of the widget container as a string.<p>
151     *
152     * @return the left margin of the widget container as a string
153     */
154    public static String getWidgetContainerWidth() {
155
156        return WIDGET_CONTAINER_WIDTH + "px";
157    }
158
159    /**
160     * Installs the DOM event handlers for displaying tooltips on a help icon.<p>
161     *
162     * The supplier passed in should not create a new tooltip data instance each time,
163     * but cache the different possible tooltip data instances.
164     *
165     * @param icon the help icon
166     * @param dataSupplier provides the tooltip data at the time the DOM events occur
167     */
168    public static void installTooltipEventHandlers(final Panel icon, final Supplier<Data> dataSupplier) {
169
170        icon.addDomHandler(new MouseOverHandler() {
171
172            public void onMouseOver(MouseOverEvent event) {
173
174                CmsFieldTooltip.getHandler().buttonHover(dataSupplier.get());
175            }
176        }, MouseOverEvent.getType());
177
178        icon.addDomHandler(new MouseOutHandler() {
179
180            public void onMouseOut(MouseOutEvent event) {
181
182                CmsFieldTooltip.getHandler().buttonOut(dataSupplier.get());
183            }
184
185        }, MouseOutEvent.getType());
186
187        icon.addDomHandler(new ClickHandler() {
188
189            public void onClick(ClickEvent event) {
190
191                CmsFieldTooltip.getHandler().buttonClick(dataSupplier.get());
192
193            }
194        }, ClickEvent.getType());
195    }
196
197    /**
198     * Gets the icon.<p>
199     *
200     * @return the icon
201     */
202    public Panel getIcon() {
203
204        return m_icon;
205    }
206
207    /**
208     * Returns the label for the form row.<p>
209     *
210     * @return the label for the form row
211     */
212    public Label getLabel() {
213
214        return m_label;
215    }
216
217    /**
218     * Returns the widget container for the form row.<p>
219     *
220     * @return the widget container for the form row
221     */
222    public Panel getWidgetContainer() {
223
224        return m_widgetContainer;
225    }
226
227    /**
228     * Initializes the style for the info button.<p>
229     */
230    public void initInfoStyle() {
231
232        m_icon.addStyleName(I_CmsButton.ICON_FONT);
233        m_icon.addStyleName(I_CmsButton.ICON_CIRCLE_HELP);
234        m_icon.addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().cmsFontIconButton());
235        m_icon.addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().hoverBlack());
236        m_icon.addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().helpIcon());
237    }
238
239    /**
240     * Shows the info icon and sets the information text as its title.<p>
241     *
242     * @param info the info
243     * @param isHtml true if info should be interpreted as HTML rather than plain text
244     */
245    public void setInfo(final String info, final boolean isHtml) {
246
247        if (info != null) {
248            if (!CmsStringUtil.isEmptyOrWhitespaceOnly(info)) {
249                initInfoStyle();
250                final Data tooltipData = new CmsFieldTooltip.Data(m_icon, info, isHtml);
251                final Panel icon = m_icon;
252                final Supplier<Data> dataSupplier = Suppliers.ofInstance(tooltipData);
253                installTooltipEventHandlers(icon, dataSupplier);
254            }
255        }
256    }
257
258}