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.CmsListItemWidget;
031import org.opencms.gwt.client.ui.input.I_CmsFormField;
032import org.opencms.util.CmsStringUtil;
033
034import java.util.Collection;
035
036import com.google.gwt.dom.client.Style.Unit;
037import com.google.gwt.user.client.ui.Composite;
038import com.google.gwt.user.client.ui.Label;
039import com.google.gwt.user.client.ui.Widget;
040
041/**
042 * The abstract class for form field container widgets.<p>
043 *
044 *  @since 8.0.0
045 */
046public abstract class A_CmsFormFieldPanel extends Composite {
047
048    public static final String NO_DESCRIPTION = "!nodescription";
049
050    /** The info widget. */
051    protected CmsListItemWidget m_infoWidget;
052
053    /**
054     * Returns the default group name.<p>
055     *
056     * @return the default group name
057     */
058    public abstract String getDefaultGroup();
059
060    /**
061     * Returns the info widget.<p>
062     *
063     * @return the info widget
064     */
065    public CmsListItemWidget getInfoWidget() {
066
067        return m_infoWidget;
068    }
069
070    /**
071     * Renders a collection of fields.<p>
072     *
073     * This should only be called once, when the form is being built.<p>
074     *
075     * @param fields the fields to render
076     */
077    public abstract void renderFields(Collection<I_CmsFormField> fields);
078
079    /**
080     * Re-renders the fields of a group.<p>
081     *
082     * Not supported by all subclasses.<p>
083     *
084     * @param group the group whose fields to re-render
085     *
086     * @param fieldsInGroup the  fields to re-render
087     */
088    public void rerenderFields(String group, Collection<I_CmsFormField> fieldsInGroup) {
089
090        throw new UnsupportedOperationException();
091    }
092
093    /**
094     * Helper method for creating a form row widget.<p>
095     *
096     * @param field the field for which to create a form row
097     *
098     * @return the newly created form row
099     */
100    protected CmsFormRow createRow(I_CmsFormField field) {
101
102        return createRow(
103            field.getLabel(),
104            field.getDescription(),
105            (Widget)field.getWidget(),
106            field.getLayoutData().get("info"),
107            Boolean.parseBoolean(field.getLayoutData().get("htmlinfo")));
108
109    }
110
111    /**
112     * Creates a form row.<p>
113     *
114     * @param labelText the label text
115     * @param description the description
116     * @param widget the widget to use
117     *
118     * @return the new form row
119     */
120    protected CmsFormRow createRow(String labelText, String description, Widget widget) {
121
122        return createRow(labelText, description, widget, null, false);
123    }
124
125    /**
126     * Adds a new row with a given label and input widget to the form.<p>
127     *
128     * @param labelText the label text for the form field
129     * @param description the description of the form field
130     * @param widget the widget for the form field
131     * @param infoText the text to display on the info icon (may be null)
132     * @param infoIsHtml true if the info text should be interpreted as HTML rather than plain text
133     *
134     * @return the newly added form row
135     */
136    protected CmsFormRow createRow(
137        String labelText,
138        String description,
139        Widget widget,
140        final String infoText,
141        final boolean infoIsHtml) {
142
143        CmsFormRow row = new CmsFormRow();
144        Label label = row.getLabel();
145        label.setText(labelText);
146        if (description != NO_DESCRIPTION) {
147            label.setTitle(CmsStringUtil.isNotEmptyOrWhitespaceOnly(description) ? description : labelText);
148        }
149        row.setInfo(infoText, infoIsHtml);
150        row.getWidgetContainer().add(widget);
151
152        return row;
153    }
154
155    /**
156     * Helper method for adding a border to a widget.<p>
157     *
158     * @param widget the widget which a border should be added to
159     */
160    protected void setBorder(Widget widget) {
161
162        String cornerAll = org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.generalCss().cornerAll();
163        String border = org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.generalCss().border();
164        widget.addStyleName(border);
165        widget.addStyleName(cornerAll);
166        widget.getElement().getStyle().setPadding(6, Unit.PX);
167    }
168
169}