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