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 GmbH & Co. KG, 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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.util.CmsMacroResolver;
032
033import java.util.Iterator;
034import java.util.List;
035
036/**
037 * Provides a widget for a standard HTML form select box.<p>
038 *
039 * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description
040 * about the configuration String syntax for the select options.<p>
041 *
042 * The select widget does use the following select options:<ul>
043 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the <code>value</code> of the HTML select box
044 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value
045 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the <code>option</code> of the HTML select box
046 * </ul>
047 * <p>
048 *
049 * @since 6.0.0
050 */
051public class CmsSelectWidget extends A_CmsSelectWidget {
052
053    /**
054     * Creates a new select widget.<p>
055     */
056    public CmsSelectWidget() {
057
058        // empty constructor is required for class registration
059        super();
060    }
061
062    /**
063     * Creates a select widget with the select options specified in the given configuration List.<p>
064     *
065     * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
066     *
067     * @param configuration the configuration (possible options) for the select widget
068     *
069     * @see CmsSelectWidgetOption
070     */
071    public CmsSelectWidget(List<CmsSelectWidgetOption> configuration) {
072
073        super(configuration);
074    }
075
076    /**
077     * Creates a select widget with the specified select options.<p>
078     *
079     * @param configuration the configuration (possible options) for the select box
080     */
081    public CmsSelectWidget(String configuration) {
082
083        super(configuration);
084    }
085
086    /**
087     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
088     */
089    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
090
091        String id = param.getId();
092        StringBuffer result = new StringBuffer(16);
093
094        result.append("<td class=\"xmlTd\" style=\"height: 25px;\"><select class=\"xmlInput");
095        if (param.hasError()) {
096            result.append(" xmlInputError");
097        }
098        result.append("\" name=\"");
099        result.append(id);
100        result.append("\" id=\"");
101        result.append(id);
102        result.append("\">");
103
104        // get select box options from default value String
105        List<CmsSelectWidgetOption> options = parseSelectOptions(cms, widgetDialog, param);
106        String selected = getSelectedValue(cms, param);
107        Iterator<CmsSelectWidgetOption> i = options.iterator();
108        while (i.hasNext()) {
109            CmsSelectWidgetOption option = i.next();
110            // create the option
111            result.append("<option value=\"");
112            result.append(option.getValue());
113            result.append("\"");
114            if ((selected != null) && selected.equals(option.getValue())) {
115                result.append(" selected=\"selected\"");
116            }
117            result.append(">");
118            result.append(option.getOption());
119            result.append("</option>");
120        }
121
122        result.append("</select>");
123        result.append("</td>");
124
125        return result.toString();
126    }
127
128    /**
129     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
130     */
131    @Override
132    public String getWidgetName() {
133
134        return CmsSelectWidget.class.getName();
135    }
136
137    /**
138     * @see org.opencms.widgets.A_CmsWidget#getWidgetStringValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
139     */
140    @Override
141    public String getWidgetStringValue(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
142
143        String result = super.getWidgetStringValue(cms, widgetDialog, param);
144        String configuration = CmsMacroResolver.resolveMacros(getConfiguration(), cms, widgetDialog.getMessages());
145        if (configuration == null) {
146            configuration = param.getDefault(cms);
147        }
148        List<CmsSelectWidgetOption> options = CmsSelectWidgetOption.parseOptions(configuration);
149        for (int m = 0; m < options.size(); m++) {
150            CmsSelectWidgetOption option = options.get(m);
151            if (result.equals(option.getValue())) {
152                result = option.getOption();
153                break;
154            }
155        }
156        return result;
157    }
158
159    /**
160     * @see org.opencms.widgets.I_CmsWidget#newInstance()
161     */
162    public I_CmsWidget newInstance() {
163
164        return new CmsSelectWidget(getConfiguration());
165    }
166}