KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > CmsSelectWidget


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsSelectWidget.java,v $
3  * Date : $Date: 2006/10/18 10:18:31 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.widgets;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.util.CmsMacroResolver;
36
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * Provides a widget for a standard HTML form select box.<p>
42  *
43  * Please see the documentation of <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code> for a description
44  * about the configuration String syntax for the select options.<p>
45  *
46  * The select widget does use the following select options:<ul>
47  * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getValue()}</code> for the <code>value</code> of the HTML select box
48  * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#isDefault()}</code> for pre-selecting a specific value
49  * <li><code>{@link org.opencms.widgets.CmsSelectWidgetOption#getOption()}</code> for the <code>option</code> of the HTML select box
50  * </ul>
51  * <p>
52  *
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.12 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsSelectWidget extends A_CmsSelectWidget {
60
61     /**
62      * Creates a new select widget.<p>
63      */

64     public CmsSelectWidget() {
65
66         // empty constructor is required for class registration
67
super();
68     }
69
70     /**
71      * Creates a select widget with the select options specified in the given configuration List.<p>
72      *
73      * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
74      *
75      * @param configuration the configuration (possible options) for the select widget
76      *
77      * @see CmsSelectWidgetOption
78      */

79     public CmsSelectWidget(List JavaDoc configuration) {
80
81         super(configuration);
82     }
83
84     /**
85      * Creates a select widget with the specified select options.<p>
86      *
87      * @param configuration the configuration (possible options) for the select box
88      */

89     public CmsSelectWidget(String JavaDoc configuration) {
90
91         super(configuration);
92     }
93
94     /**
95      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
96      */

97     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
98
99         String JavaDoc id = param.getId();
100         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
101
102         result.append("<td class=\"xmlTd\" style=\"height: 25px;\"><select class=\"xmlInput");
103         if (param.hasError()) {
104             result.append(" xmlInputError");
105         }
106         result.append("\" name=\"");
107         result.append(id);
108         result.append("\" id=\"");
109         result.append(id);
110         result.append("\">");
111
112         // get select box options from default value String
113
List JavaDoc options = parseSelectOptions(cms, widgetDialog, param);
114         String JavaDoc selected = getSelectedValue(cms, param);
115         Iterator JavaDoc i = options.iterator();
116         while (i.hasNext()) {
117             CmsSelectWidgetOption option = (CmsSelectWidgetOption)i.next();
118             // create the option
119
result.append("<option value=\"");
120             result.append(option.getValue());
121             result.append("\"");
122             if ((selected != null) && selected.equals(option.getValue())) {
123                 result.append(" selected=\"selected\"");
124             }
125             result.append(">");
126             result.append(option.getOption());
127             result.append("</option>");
128         }
129
130         result.append("</select>");
131         result.append("</td>");
132
133         return result.toString();
134     }
135
136     /**
137      * @see org.opencms.widgets.A_CmsWidget#getWidgetStringValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
138      */

139     public String JavaDoc getWidgetStringValue(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
140
141         String JavaDoc result = super.getWidgetStringValue(cms, widgetDialog, param);
142         String JavaDoc configuration = CmsMacroResolver.resolveMacros(getConfiguration(), cms, widgetDialog.getMessages());
143         if (configuration == null) {
144             configuration = param.getDefault(cms);
145         }
146         List JavaDoc options = CmsSelectWidgetOption.parseOptions(configuration);
147         for (int m = 0; m < options.size(); m++) {
148             CmsSelectWidgetOption option = (CmsSelectWidgetOption)options.get(m);
149             if (result.equals(option.getValue())) {
150                 result = option.getOption();
151                 break;
152             }
153         }
154         return result;
155     }
156
157     /**
158      * @see org.opencms.widgets.I_CmsWidget#newInstance()
159      */

160     public I_CmsWidget newInstance() {
161
162         return new CmsSelectWidget(getConfiguration());
163     }
164 }
Popular Tags