KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > select > AbstractSelectElement


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element.select;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.riotfamily.common.beans.PropertyUtils;
33 import org.riotfamily.forms.AbstractEditorBase;
34 import org.riotfamily.forms.Editor;
35 import org.riotfamily.forms.ErrorUtils;
36 import org.riotfamily.forms.MessageUtils;
37 import org.riotfamily.forms.event.ChangeEvent;
38 import org.riotfamily.forms.event.ChangeListener;
39 import org.riotfamily.forms.event.JavaScriptEvent;
40 import org.riotfamily.forms.event.JavaScriptEventAdapter;
41 import org.springframework.util.Assert;
42
43 /**
44  * Abstract superclass for elements that let the user choose from a set of
45  * options like selectboxes or radio button groups.
46  */

47 public abstract class AbstractSelectElement extends AbstractEditorBase implements
48         Editor, SelectElement, JavaScriptEventAdapter {
49
50     
51     private OptionsModel optionsModel;
52     
53     private String JavaDoc valueProperty;
54     
55     private String JavaDoc labelProperty;
56     
57     private String JavaDoc labelMessageKey;
58     
59     private boolean appendLabel;
60
61     private OptionRenderer optionRenderer;
62     
63     private List JavaDoc options;
64     
65     private List JavaDoc listeners;
66     
67
68     public void setOptionRenderer(OptionRenderer optionRenderer) {
69         this.optionRenderer = optionRenderer;
70     }
71
72     public void renderOption(Option option) {
73         optionRenderer.renderOption(option, getFormContext().getWriter());
74     }
75     
76     public void setValueProperty(String JavaDoc valueProperty) {
77         this.valueProperty = valueProperty;
78     }
79
80     public void setLabelProperty(String JavaDoc labelProperty) {
81         this.labelProperty = labelProperty;
82     }
83
84     public void setLabelMessageKey(String JavaDoc labelMessageKey) {
85         this.labelMessageKey = labelMessageKey;
86     }
87     
88     public void setAppendLabel(boolean appendLabelToMessageKey) {
89         this.appendLabel = appendLabelToMessageKey;
90     }
91
92     public void setOptionsModel(OptionsModel optionsModel) {
93         this.optionsModel = optionsModel;
94         resetOptions();
95         setValue(null);
96         if (getFormListener() != null) {
97             getFormListener().elementChanged(this);
98         }
99     }
100
101     public void setOptionValues(Collection JavaDoc optionValues) {
102         setOptionsModel(new StaticOptionsModel(optionValues));
103     }
104     
105     public OptionsModel getOptionsModel() {
106         return optionsModel;
107     }
108
109     public void render(PrintWriter JavaDoc writer) {
110         resetOptions();
111         super.render(writer);
112     }
113     
114     protected void resetOptions() {
115         options = null;
116     }
117     
118     protected final List JavaDoc getOptions() {
119         if (options == null) {
120             options = createOptions();
121         }
122         return options;
123     }
124     
125     protected List JavaDoc createOptions() {
126         List JavaDoc options = new ArrayList JavaDoc();
127         if (optionsModel != null) {
128             Iterator JavaDoc it = optionsModel.getOptionValues().iterator();
129             for (int i = 0; it.hasNext(); i++) {
130                 Object JavaDoc item = it.next();
131                 String JavaDoc label = getOptionLabel(item);
132                 Object JavaDoc value = getOptionValue(item);
133                 options.add(new Option(item, value, label, this));
134             }
135         }
136         return options;
137     }
138     
139     protected String JavaDoc getOptionLabel(Object JavaDoc item) {
140         Object JavaDoc obj = item;
141         if (labelProperty != null) {
142             obj = PropertyUtils.getProperty(item, labelProperty);
143         }
144         String JavaDoc label = obj != null ? obj.toString() : null;
145         if (labelMessageKey != null) {
146             if (appendLabel) {
147                 return MessageUtils.getMessage(this, labelMessageKey + label,
148                         null, label);
149             }
150             else {
151                 return MessageUtils.getMessage(this, labelMessageKey,
152                         new Object JavaDoc[] {obj}, label);
153             }
154         }
155         else {
156             return label;
157         }
158     }
159     
160     protected Object JavaDoc getOptionValue(Object JavaDoc item) {
161         if (item == null) {
162             return null;
163         }
164         if (valueProperty != null) {
165             return PropertyUtils.getProperty(item, valueProperty);
166         }
167         else {
168             return item;
169         }
170     }
171     
172     public int getOptionIndex(Option option) {
173         Assert.notNull(options);
174         return options.indexOf(option);
175     }
176     
177     protected abstract boolean hasSelection();
178             
179     protected void validate() {
180         if (isRequired() && !hasSelection()) {
181             ErrorUtils.rejectRequired(this);
182         }
183     }
184     
185     public int getEventTypes() {
186         if (listeners != null) {
187             return JavaScriptEvent.ON_CHANGE;
188         }
189         return 0;
190     }
191
192     public void addChangeListener(ChangeListener listener) {
193         if (listeners == null) {
194             listeners = new ArrayList JavaDoc();
195         }
196         listeners.add(listener);
197     }
198
199     protected void fireChangeEvent(Object JavaDoc newValue, Object JavaDoc oldValue) {
200         if (listeners != null) {
201             ChangeEvent event = new ChangeEvent(this, newValue, oldValue);
202             Iterator JavaDoc it = listeners.iterator();
203             while (it.hasNext()) {
204                 ChangeListener listener = (ChangeListener) it.next();
205                 listener.valueChanged(event);
206             }
207         }
208     }
209
210 }
Popular Tags