KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/A_CmsSelectWidget.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.7 $
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 import org.opencms.util.CmsStringUtil;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * Base class for select widgets.<p>
45  *
46  * @author Alexander Kandzior
47  * @author Andreas Zahner
48  *
49  * @version $Revision: 1.7 $
50  *
51  * @since 6.0.0
52  *
53  * @see org.opencms.widgets.CmsSelectWidgetOption
54  */

55 public abstract class A_CmsSelectWidget extends A_CmsWidget {
56
57     /** The possible options for the select box. */
58     private List JavaDoc m_selectOptions;
59
60     /**
61      * Creates a new select widget.<p>
62      */

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

78     public A_CmsSelectWidget(List JavaDoc configuration) {
79
80         super();
81         m_selectOptions = configuration;
82     }
83
84     /**
85      * Creates a select widget with the select options specified in the given configuration String.<p>
86      *
87      * Please see <code>{@link CmsSelectWidgetOption}</code> for a description of the syntax
88      * of the configuration String.<p>
89      *
90      * @param configuration the configuration (possible options) for the select widget
91      *
92      * @see CmsSelectWidgetOption
93      */

94     public A_CmsSelectWidget(String JavaDoc configuration) {
95
96         super(configuration);
97     }
98
99     /**
100      * Adds a new select option to this widget.<p>
101      *
102      * @param option the select option to add
103      */

104     public void addSelectOption(CmsSelectWidgetOption option) {
105
106         if (m_selectOptions == null) {
107             m_selectOptions = new ArrayList JavaDoc();
108         }
109         m_selectOptions.add(option);
110     }
111
112     /**
113      * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
114      */

115     public String JavaDoc getConfiguration() {
116
117         if (super.getConfiguration() != null) {
118             return super.getConfiguration();
119         }
120         return CmsSelectWidgetOption.createConfigurationString(m_selectOptions);
121     }
122
123     /**
124      * Returns the currently selected value of the select widget.<p>
125      *
126      * If a value is found in the given parameter, this is used. Otherwise
127      * the default value of the select options are used. If there is neither a parameter value
128      * nor a default value, <code>null</code> is returned.<p>
129      *
130      * @param cms the current users OpenCms context
131      * @param param the widget parameter of this dialog
132      *
133      * @return the currently selected value of the select widget
134      */

135     protected String JavaDoc getSelectedValue(CmsObject cms, I_CmsWidgetParameter param) {
136
137         String JavaDoc paramValue = param.getStringValue(cms);
138         if (CmsStringUtil.isEmpty(paramValue)) {
139             CmsSelectWidgetOption option = CmsSelectWidgetOption.getDefaultOption(m_selectOptions);
140             if (option != null) {
141                 paramValue = option.getValue();
142             }
143         }
144         return paramValue;
145     }
146
147     /**
148      * Returns the currently selected values of the select widget.<p>
149      *
150      * If a value is found in the given parameter, this is used. Otherwise
151      * the default value of the select options are used. If there is neither a parameter value
152      * nor a default value, <code>null</code> is used.<p>
153      *
154      * @param cms the current users OpenCms context
155      * @param param the widget parameter of this dialog
156      *
157      * @return a list of the currently selected values of the select widget
158      */

159     protected List JavaDoc getSelectedValues(CmsObject cms, I_CmsWidgetParameter param) {
160
161         List JavaDoc values = new ArrayList JavaDoc();
162         String JavaDoc paramValue = param.getStringValue(cms);
163         if (CmsStringUtil.isEmpty(paramValue)) {
164             Iterator JavaDoc itOptions = CmsSelectWidgetOption.getDefaultOptions(m_selectOptions).iterator();
165             while (itOptions.hasNext()) {
166                 CmsSelectWidgetOption option = (CmsSelectWidgetOption)itOptions.next();
167                 values.add(option.getValue());
168             }
169         } else {
170             values.addAll(CmsStringUtil.splitAsList(paramValue, ',', true));
171         }
172         return values;
173     }
174
175     /**
176      * Returns the list of configured select options.<p>
177      *
178      * The list elements are of type <code>{@link CmsSelectWidgetOption}</code>.<p>
179      *
180      * @return the list of select options
181      */

182     protected List JavaDoc getSelectOptions() {
183
184         return m_selectOptions;
185     }
186
187     /**
188      * Returns the list of configured select options, parsing the configuration String if required.<p>
189      *
190      * The list elements are of type <code>{@link CmsSelectWidgetOption}</code>.
191      * The configuration String is parsed only once and then stored internally.<p>
192      *
193      * @param cms the current users OpenCms context
194      * @param widgetDialog the dialog of this widget
195      * @param param the widget parameter of this dialog
196      *
197      * @return the list of select options
198      *
199      * @see CmsSelectWidgetOption
200      */

201     protected List JavaDoc parseSelectOptions(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
202
203         if (m_selectOptions == null) {
204             String JavaDoc configuration = getConfiguration();
205             if (configuration == null) {
206                 // workaround: use the default value to parse the options
207
configuration = param.getDefault(cms);
208             }
209             configuration = CmsMacroResolver.resolveMacros(configuration, cms, widgetDialog.getMessages());
210             m_selectOptions = CmsSelectWidgetOption.parseOptions(configuration);
211             if (m_selectOptions == Collections.EMPTY_LIST) {
212                 m_selectOptions = new ArrayList JavaDoc();
213             }
214         }
215         return m_selectOptions;
216     }
217
218     /**
219      * Sets the list of configured select options.<p>
220      *
221      * The list elements must be of type <code>{@link CmsSelectWidgetOption}</code>.<p>
222      *
223      * @param selectOptions the list of select options to set
224      */

225     protected void setSelectOptions(List JavaDoc selectOptions) {
226
227         m_selectOptions = new ArrayList JavaDoc();
228         m_selectOptions.addAll(selectOptions);
229     }
230 }
Popular Tags