001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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, 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;
032import org.opencms.workplace.search.CmsSearchDialog;
033
034import java.util.Iterator;
035import java.util.List;
036
037/**
038 * Provides a widget for a standard HTML form select box with. The jsp where this widget is used is 
039 * reloaded, when the select box value is changed.<p>
040 * 
041 * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description 
042 * about the configuration String syntax for the select options.<p>
043 *
044 * The select widget does use the following select options:<ul>
045 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the <code>value</code> of the HTML select box
046 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value 
047 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the <code>option</code> of the HTML select box
048 * </ul>
049 * <p>
050 * 
051 * @since 7.5.3
052 */
053public class CmsSelectOnChangeReloadWidget extends CmsSelectWidget {
054
055    /**
056     * Creates a new select widget.<p>
057     */
058    public CmsSelectOnChangeReloadWidget() {
059
060        // empty constructor is required for class registration
061        super();
062    }
063
064    /**
065     * Creates a select widget with the select options specified in the given configuration List.<p>
066     * 
067     * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
068     * 
069     * @param configuration the configuration (possible options) for the select widget
070     * 
071     * @see CmsSelectWidgetOption
072     */
073    public CmsSelectOnChangeReloadWidget(List<CmsSelectWidgetOption> configuration) {
074
075        super(configuration);
076    }
077
078    /**
079     * Creates a select widget with the specified select options.<p>
080     * 
081     * @param configuration the configuration (possible options) for the select box
082     */
083    public CmsSelectOnChangeReloadWidget(String configuration) {
084
085        super(configuration);
086    }
087
088    /**
089     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
090     */
091    @Override
092    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
093
094        String id = param.getId();
095        StringBuffer result = new StringBuffer(16);
096
097        result.append("<td class=\"xmlTd\" style=\"height: 25px;\"><select class=\"xmlInput");
098        if (param.hasError()) {
099            result.append(" xmlInputError");
100        }
101        result.append("\" name=\"");
102        result.append(id);
103        result.append("\" id=\"");
104        result.append(id);
105        result.append("\"");
106        result.append(" onChange=\"document.getElementsByName('"
107            + CmsDialog.PARAM_ACTION
108            + "')[0].value='"
109            + CmsSearchDialog.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}