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.xml.content.I_CmsXmlContentHandler.DisplayType;
032
033import java.util.Iterator;
034import java.util.List;
035
036/**
037 * Provides a widget for a standard HTML form for a group of radio buttons.<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 multi select widget does use the following select options:<ul>
043 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the value of the option
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 display name of the option
046 * </ul>
047 * <p>
048 *
049 * @since 6.9.1
050 */
051public class CmsRadioSelectWidget extends A_CmsSelectWidget {
052
053    /**
054     * Creates a new select widget.<p>
055     */
056    public CmsRadioSelectWidget() {
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 CmsRadioSelectWidget(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 CmsRadioSelectWidget(String configuration) {
082
083        super(configuration);
084    }
085
086    /**
087     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
088     */
089    @Override
090    public DisplayType getDefaultDisplayType() {
091
092        return DisplayType.wide;
093    }
094
095    /**
096     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
097     */
098    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
099
100        String id = param.getId();
101        StringBuffer result = new StringBuffer(16);
102
103        result.append("<td class=\"xmlTd\">");
104
105        // get select box options from default value String
106        List<CmsSelectWidgetOption> options = parseSelectOptions(cms, widgetDialog, param);
107        String selected = getSelectedValue(cms, param);
108        Iterator<CmsSelectWidgetOption> i = options.iterator();
109        while (i.hasNext()) {
110            CmsSelectWidgetOption option = i.next();
111
112            // create the option
113            result.append("<input type='radio' name='");
114            result.append(id);
115            result.append("' value='");
116            result.append(option.getValue());
117            result.append("'");
118            if (selected.indexOf(option.getValue()) >= 0) {
119                result.append(" checked");
120            }
121            result.append(">");
122            result.append(option.getOption());
123            result.append("<br>");
124        }
125        result.append("</td>");
126
127        return result.toString();
128    }
129
130    /**
131     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
132     */
133    @Override
134    public String getWidgetName() {
135
136        return CmsRadioSelectWidget.class.getName();
137    }
138
139    /**
140     * @see org.opencms.widgets.I_CmsWidget#newInstance()
141     */
142    public I_CmsWidget newInstance() {
143
144        return new CmsRadioSelectWidget(getConfiguration());
145    }
146}