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.workplace.CmsDialog;
032
033import java.util.Iterator;
034import java.util.List;
035
036/**
037 * Provides a widget for a standard HTML form select box with. The jsp where this widget is used is
038 * reloaded, when the select box value is changed.<p>
039 *
040 * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description
041 * about the configuration String syntax for the select options.<p>
042 *
043 * The select widget does use the following select options:<ul>
044 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the <code>value</code> of the HTML select box
045 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value
046 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the <code>option</code> of the HTML select box
047 * </ul>
048 * <p>
049 *
050 * @since 7.5.3
051 */
052public class CmsSelectOnChangeReloadWidget extends CmsSelectWidget {
053
054    /**
055     * Creates a new select widget.<p>
056     */
057    public CmsSelectOnChangeReloadWidget() {
058
059        // empty constructor is required for class registration
060        super();
061    }
062
063    /**
064     * Creates a select widget with the select options specified in the given configuration List.<p>
065     *
066     * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
067     *
068     * @param configuration the configuration (possible options) for the select widget
069     *
070     * @see CmsSelectWidgetOption
071     */
072    public CmsSelectOnChangeReloadWidget(List<CmsSelectWidgetOption> configuration) {
073
074        super(configuration);
075    }
076
077    /**
078     * Creates a select widget with the specified select options.<p>
079     *
080     * @param configuration the configuration (possible options) for the select box
081     */
082    public CmsSelectOnChangeReloadWidget(String configuration) {
083
084        super(configuration);
085    }
086
087    /**
088     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
089     */
090    @Override
091    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
092
093        String id = param.getId();
094        StringBuffer result = new StringBuffer(16);
095
096        result.append("<td class=\"xmlTd\" style=\"height: 25px;\"><select class=\"xmlInput");
097        if (param.hasError()) {
098            result.append(" xmlInputError");
099        }
100        result.append("\" name=\"");
101        result.append(id);
102        result.append("\" id=\"");
103        result.append(id);
104        result.append("\"");
105        result.append(
106            " onChange=\"document.getElementsByName('"
107                + CmsDialog.PARAM_ACTION
108                + "')[0].value='"
109                + CmsDialog.PARAM_ACTION_VALUE_FOR_CHANGED_INDEX
110                + "';this.form.submit()\"");
111        result.append(">");
112
113        // get select box options from default value String
114        List<CmsSelectWidgetOption> options = parseSelectOptions(cms, widgetDialog, param);
115        String selected = getSelectedValue(cms, param);
116        Iterator<CmsSelectWidgetOption> i = options.iterator();
117        while (i.hasNext()) {
118            CmsSelectWidgetOption option = i.next();
119            // create the option
120            result.append("<option value=\"");
121            result.append(option.getValue());
122            result.append("\"");
123            if ((selected != null) && selected.equals(option.getValue())) {
124                result.append(" selected=\"selected\"");
125            }
126            result.append(">");
127            result.append(option.getOption());
128            result.append("</option>");
129        }
130
131        result.append("</select>");
132        result.append("</td>");
133
134        return result.toString();
135    }
136}