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.gwt.client.ui.input.form;
029
030import org.opencms.gwt.client.ui.CmsFieldSet;
031import org.opencms.gwt.client.ui.CmsListItemWidget;
032import org.opencms.gwt.client.ui.I_CmsTruncable;
033import org.opencms.gwt.client.ui.input.I_CmsFormField;
034
035import org.opencms.gwt.shared.CmsListInfoBean;
036import org.opencms.gwt.shared.CmsListInfoBean.StateIcon;
037
038import java.util.Collection;
039import java.util.HashMap;
040import java.util.List;
041import java.util.Map;
042
043import com.google.gwt.dom.client.Style;
044import com.google.gwt.user.client.ui.FlowPanel;
045
046/**
047 * Form field panel which puts its form fields into a field set, and also displays a resource info box.<p>
048 */
049public class CmsFieldsetFormFieldPanel extends A_CmsFormFieldPanel {
050
051    /** Text metrics key for the info box. */
052    public static String TM_INFOBOX = "infobox_";
053
054    /** The default group id. */
055    private static final String DEFAULT_GROUP = "";
056
057    /** The list of form fields. */
058    protected List<I_CmsFormField> m_fields;
059
060    /** Group specific field sets. */
061    private Map<String, CmsFieldSet> m_groupFieldSets;
062
063    /** The main panel .*/
064    private FlowPanel m_panel;
065
066    /**
067     * Creates a new instance.<p>
068     *
069     * @param info the bean used to display the info item
070     * @param legend the legend for the field set
071     */
072    public CmsFieldsetFormFieldPanel(CmsListInfoBean info, String legend) {
073
074        m_panel = new FlowPanel();
075        m_groupFieldSets = new HashMap<String, CmsFieldSet>();
076        if (info != null) {
077            m_infoWidget = new CmsListItemWidget(info);
078
079            m_infoWidget.truncate(TM_INFOBOX, CmsFormDialog.STANDARD_DIALOG_WIDTH - 50);
080            m_infoWidget.setStateIcon(StateIcon.standard);
081            m_panel.add(m_infoWidget);
082        }
083
084        CmsFieldSet fieldSet = new CmsFieldSet();
085        fieldSet.setLegend(legend);
086        fieldSet.getElement().getStyle().setMarginTop(10, Style.Unit.PX);
087        addGroupFieldSet(DEFAULT_GROUP, fieldSet);
088        initWidget(m_panel);
089    }
090
091    /**
092     * Adds a group specific field set.<p>
093     *
094     * @param group the group id
095     * @param fieldSet the field set
096     */
097    public void addGroupFieldSet(String group, CmsFieldSet fieldSet) {
098
099        // can't add a group field set twice
100        if (!m_groupFieldSets.containsKey(group)) {
101            m_groupFieldSets.put(group, fieldSet);
102            m_panel.add(fieldSet);
103        }
104    }
105
106    /**
107     * @see org.opencms.gwt.client.ui.input.form.A_CmsFormFieldPanel#getDefaultGroup()
108     */
109    @Override
110    public String getDefaultGroup() {
111
112        return DEFAULT_GROUP;
113    }
114
115    /**
116     * Returns the main field set.<p>
117     *
118     * @return the main field set
119     */
120    public CmsFieldSet getFieldSet() {
121
122        return m_groupFieldSets.get(DEFAULT_GROUP);
123    }
124
125    /**
126     * Gets the main panel.<p>
127     *
128     * @return the main panel
129     */
130    public FlowPanel getMainPanel() {
131
132        return m_panel;
133    }
134
135    /**
136     * @see org.opencms.gwt.client.ui.input.form.A_CmsFormFieldPanel#renderFields(java.util.Collection)
137     */
138    @Override
139    public void renderFields(Collection<I_CmsFormField> fields) {
140
141        for (CmsFieldSet fieldSet : m_groupFieldSets.values()) {
142            fieldSet.clear();
143        }
144        for (I_CmsFormField field : fields) {
145            CmsFormRow row = createRow(field);
146            String fieldGroup = field.getLayoutData().get("group");
147            CmsFieldSet fieldSet;
148            if (m_groupFieldSets.containsKey(fieldGroup)) {
149                fieldSet = m_groupFieldSets.get(fieldGroup);
150            } else {
151                fieldSet = getFieldSet();
152            }
153            fieldSet.add(row);
154        }
155        for (CmsFieldSet fieldSet : m_groupFieldSets.values()) {
156            fieldSet.setVisible(fieldSet.getWidgetCount() > 0);
157        }
158    }
159
160}