001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsSelectWidget.java,v $
003: * Date : $Date: 2008-02-27 12:05:44 $
004: * Version: $Revision: 1.15 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.widgets;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.util.CmsMacroResolver;
036:
037: import java.util.Iterator;
038: import java.util.List;
039:
040: /**
041: * Provides a widget for a standard HTML form select box.<p>
042: *
043: * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description
044: * about the configuration String syntax for the select options.<p>
045: *
046: * The select widget does use the following select options:<ul>
047: * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the <code>value</code> of the HTML select box
048: * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value
049: * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the <code>option</code> of the HTML select box
050: * </ul>
051: * <p>
052: *
053: * @author Andreas Zahner
054: *
055: * @version $Revision: 1.15 $
056: *
057: * @since 6.0.0
058: */
059: public class CmsSelectWidget extends A_CmsSelectWidget {
060:
061: /**
062: * Creates a new select widget.<p>
063: */
064: public CmsSelectWidget() {
065:
066: // empty constructor is required for class registration
067: super ();
068: }
069:
070: /**
071: * Creates a select widget with the select options specified in the given configuration List.<p>
072: *
073: * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
074: *
075: * @param configuration the configuration (possible options) for the select widget
076: *
077: * @see CmsSelectWidgetOption
078: */
079: public CmsSelectWidget(List configuration) {
080:
081: super (configuration);
082: }
083:
084: /**
085: * Creates a select widget with the specified select options.<p>
086: *
087: * @param configuration the configuration (possible options) for the select box
088: */
089: public CmsSelectWidget(String configuration) {
090:
091: super (configuration);
092: }
093:
094: /**
095: * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
096: */
097: public String getDialogWidget(CmsObject cms,
098: I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
099:
100: String id = param.getId();
101: StringBuffer result = new StringBuffer(16);
102:
103: result
104: .append("<td class=\"xmlTd\" style=\"height: 25px;\"><select class=\"xmlInput");
105: if (param.hasError()) {
106: result.append(" xmlInputError");
107: }
108: result.append("\" name=\"");
109: result.append(id);
110: result.append("\" id=\"");
111: result.append(id);
112: result.append("\">");
113:
114: // get select box options from default value String
115: List options = parseSelectOptions(cms, widgetDialog, param);
116: String selected = getSelectedValue(cms, param);
117: Iterator i = options.iterator();
118: while (i.hasNext()) {
119: CmsSelectWidgetOption option = (CmsSelectWidgetOption) i
120: .next();
121: // create the option
122: result.append("<option value=\"");
123: result.append(option.getValue());
124: result.append("\"");
125: if ((selected != null)
126: && selected.equals(option.getValue())) {
127: result.append(" selected=\"selected\"");
128: }
129: result.append(">");
130: result.append(option.getOption());
131: result.append("</option>");
132: }
133:
134: result.append("</select>");
135: result.append("</td>");
136:
137: return result.toString();
138: }
139:
140: /**
141: * @see org.opencms.widgets.A_CmsWidget#getWidgetStringValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
142: */
143: public String getWidgetStringValue(CmsObject cms,
144: I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
145:
146: String result = super .getWidgetStringValue(cms, widgetDialog,
147: param);
148: String configuration = CmsMacroResolver.resolveMacros(
149: getConfiguration(), cms, widgetDialog.getMessages());
150: if (configuration == null) {
151: configuration = param.getDefault(cms);
152: }
153: List options = CmsSelectWidgetOption
154: .parseOptions(configuration);
155: for (int m = 0; m < options.size(); m++) {
156: CmsSelectWidgetOption option = (CmsSelectWidgetOption) options
157: .get(m);
158: if (result.equals(option.getValue())) {
159: result = option.getOption();
160: break;
161: }
162: }
163: return result;
164: }
165:
166: /**
167: * @see org.opencms.widgets.I_CmsWidget#newInstance()
168: */
169: public I_CmsWidget newInstance() {
170:
171: return new CmsSelectWidget(getConfiguration());
172: }
173: }
|