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.i18n.CmsEncoder;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.workplace.CmsWorkplace;
034
035import java.util.Iterator;
036import java.util.List;
037
038/**
039 * Provides a HTML text input field with optional values to select in a combo box, for use on a widget dialog.<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 combo widget does use the following select options:<ul>
045 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the texts to be displayed in the combo selector
046 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> to fill the input with a preselected text
047 * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getHelp()}</code> to display an (optional) help text for the combo option
048 * </ul>
049 *
050 * @since 6.0.0
051 */
052public class CmsComboWidget extends A_CmsSelectWidget {
053
054    /**
055     * Creates a new combo widget.<p>
056     */
057    public CmsComboWidget() {
058
059        // empty constructor is required for class registration
060        super();
061    }
062
063    /**
064     * Creates a combo 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 CmsComboWidget(List<CmsSelectWidgetOption> configuration) {
073
074        super(configuration);
075    }
076
077    /**
078     * Creates a combo widget with the specified combo options.<p>
079     *
080     * @param configuration the configuration (possible options) for the combo box
081     */
082    public CmsComboWidget(String configuration) {
083
084        super(configuration);
085    }
086
087    /**
088     * @see org.opencms.widgets.A_CmsWidget#getDialogHtmlEnd(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
089     */
090    @Override
091    public String getDialogHtmlEnd(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
092
093        String id = param.getId();
094        StringBuffer result = new StringBuffer(256);
095
096        // get the select box options
097        List<CmsSelectWidgetOption> options = parseSelectOptions(cms, widgetDialog, param);
098
099        if (options.size() > 0) {
100            // create combo div
101            result.append("<div class=\"widgetcombo\" id=\"combo");
102            result.append(id);
103            result.append("\">\n");
104
105            int count = 0;
106            Iterator<CmsSelectWidgetOption> i = options.iterator();
107            while (i.hasNext()) {
108                CmsSelectWidgetOption option = i.next();
109                String itemId = new StringBuffer(64).append("ci").append(id).append('.').append(count).toString();
110                // create the link around value
111                result.append("\t<a href=\"javascript:setComboValue(\'");
112                result.append(id);
113                result.append("\', \'");
114                result.append(itemId);
115                result.append("\')\" name=\"");
116                result.append(itemId);
117                result.append("\" id=\"");
118                result.append(itemId);
119                result.append("\"");
120                if (option.getHelp() != null) {
121                    // create help text mousevent attributes
122                    // can't use method in CmsEncoder because we need to keep < > for HTML in help text
123                    String locValue = CmsStringUtil.substitute(option.getHelp(), "\"", "&quot;");
124                    result.append(
125                        getJsHelpMouseHandler(widgetDialog, itemId, CmsStringUtil.escapeJavaScript(locValue)));
126                }
127                result.append(">");
128                result.append(option.getValue());
129                result.append("</a>\n");
130                count++;
131            }
132
133            // close combo div
134            result.append("</div>\n");
135
136            if (widgetDialog.useNewStyle()) {
137                // create help texts for the values in admin view
138                count = 0;
139                i = options.iterator();
140                while (i.hasNext()) {
141                    CmsSelectWidgetOption option = i.next();
142                    if (option.getHelp() != null) {
143                        // help text is optional
144                        String itemId = new StringBuffer(64).append("ci").append(id).append('.').append(
145                            count).toString();
146                        result.append("<div class=\"help\" id=\"help");
147                        result.append(itemId);
148                        result.append("\"");
149                        result.append(getJsHelpMouseHandler(widgetDialog, itemId, itemId));
150                        result.append(">");
151                        result.append(option.getHelp());
152                        result.append("</div>\n");
153                        count++;
154                    }
155                }
156            }
157        }
158
159        // return the icon help text from super class
160        result.append(super.getDialogHtmlEnd(cms, widgetDialog, param));
161        return result.toString();
162    }
163
164    /**
165     * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
166     */
167    @Override
168    public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
169
170        StringBuffer result = new StringBuffer(16);
171        result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/combobox.js"));
172        return result.toString();
173    }
174
175    /**
176     * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
177     */
178    @Override
179    public String getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
180
181        return "\tinitComboBox();\n";
182    }
183
184    /**
185     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
186     */
187    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
188
189        String id = param.getId();
190        StringBuffer result = new StringBuffer(16);
191        result.append("<td class=\"xmlTd\">");
192
193        result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>");
194        // medium text input field
195        result.append("<input type=\"text\" class=\"xmlInputMedium");
196        if (param.hasError()) {
197            result.append(" xmlInputError");
198        }
199        result.append("\" name=\"");
200        result.append(id);
201        result.append("\" id=\"");
202        result.append(id);
203        result.append("\"");
204        parseSelectOptions(cms, widgetDialog, param);
205        String selected = getSelectedValue(cms, param);
206        if (selected != null) {
207            // append the selection
208            result.append(" value=\"");
209            result.append(CmsEncoder.escapeXml(selected));
210            result.append("\"");
211        }
212        result.append(">");
213        result.append("</td><td>");
214        // button to open combo box
215        result.append("<button name=\"test\" onclick=\"showCombo(\'").append(id).append("\', \'combo").append(id);
216        result.append("\');return false;\" class=\"widgetcombobutton\">");
217        result.append("<img src=\"");
218        result.append(CmsWorkplace.getSkinUri()).append("components/widgets/combo.png");
219        result.append("\" width=\"7\" height=\"12\" alt=\"\" border=\"0\">");
220        result.append("</button></td></tr></table>");
221
222        result.append("</td>");
223        return result.toString();
224    }
225
226    /**
227     * @see org.opencms.widgets.A_CmsSelectWidget#getWidgetName()
228     */
229    @Override
230    public String getWidgetName() {
231
232        return CmsComboWidget.class.getName();
233    }
234
235    /**
236     * @see org.opencms.widgets.I_CmsWidget#newInstance()
237     */
238    public I_CmsWidget newInstance() {
239
240        return new CmsComboWidget(getConfiguration());
241    }
242}