KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.riotfamily.common.beans.PropertyUtils;
38 import org.riotfamily.common.markup.DocumentWriter;
39 import org.riotfamily.common.markup.Html;
40 import org.riotfamily.forms.AbstractEditorBase;
41 import org.riotfamily.forms.ContentElement;
42 import org.riotfamily.forms.DHTMLElement;
43 import org.riotfamily.forms.Editor;
44 import org.riotfamily.forms.ErrorUtils;
45 import org.riotfamily.forms.TemplateUtils;
46 import org.riotfamily.forms.event.ChangeEvent;
47 import org.riotfamily.forms.event.ChangeListener;
48 import org.riotfamily.forms.event.JavaScriptEvent;
49 import org.riotfamily.forms.event.JavaScriptEventAdapter;
50 import org.riotfamily.forms.request.FormRequest;
51 import org.riotfamily.forms.resource.FormResource;
52 import org.riotfamily.forms.resource.ResourceElement;
53 import org.riotfamily.forms.resource.ScriptResource;
54 import org.springframework.util.StringUtils;
55
56 /**
57  * Abstract base class for elements that let the user choose a reference to
58  * to another object.
59  *
60  * @author Felix Gnass [fgnass at neteye dot de]
61  */

62 public abstract class AbstractChooser extends AbstractEditorBase
63         implements Editor, DHTMLElement, JavaScriptEventAdapter,
64         ResourceElement, ContentElement {
65     
66     private String JavaDoc displayNameProperty;
67     
68     private Object JavaDoc object;
69
70     private String JavaDoc displayName;
71
72     private List JavaDoc listeners;
73     
74     private static FormResource RESOURCE =
75             new ScriptResource("form/chooser.js", "Chooser");
76     
77     
78     public void setDisplayNameProperty(String JavaDoc displayNameProperty) {
79         this.displayNameProperty = displayNameProperty;
80     }
81
82     protected void renderInternal(PrintWriter JavaDoc writer) {
83         DocumentWriter doc = new DocumentWriter(writer);
84         doc.start(Html.DIV);
85         doc.attribute(Html.COMMON_ID, getId());
86         doc.attribute(Html.COMMON_CLASS, "chooser");
87         if (displayName != null) {
88             doc.start(Html.SPAN);
89             doc.body(displayName);
90             doc.end();
91         }
92         doc.start(Html.BUTTON).attribute(Html.COMMON_CLASS, "choose");
93         doc.body("Choose");
94         doc.end();
95         if (!isRequired() && getValue() != null) {
96             doc.start(Html.BUTTON);
97             doc.attribute(Html.COMMON_CLASS, "unset");
98             doc.body("Unset");
99             doc.end();
100         }
101         doc.end();
102     }
103
104     public int getEventTypes() {
105         return 0;
106     }
107     
108     public void processRequest(FormRequest request) {
109         validate();
110     }
111     
112     protected void validate() {
113         if (isRequired() && object == null) {
114             ErrorUtils.reject(this, "required");
115         }
116     }
117
118     public void handleJavaScriptEvent(JavaScriptEvent event) {
119         setObjectId(event.getValue());
120         getFormListener().elementChanged(this);
121     }
122     
123     public FormResource getResource() {
124         return RESOURCE;
125     }
126     
127     public String JavaDoc getInitScript() {
128         return "new Chooser('" + getId() + "');";
129     }
130     
131     protected abstract Object JavaDoc loadBean(String JavaDoc objectId);
132     
133     protected String JavaDoc getDisplayName(Object JavaDoc object) {
134         if (object == null) {
135             return null;
136         }
137         if (displayNameProperty != null) {
138             return PropertyUtils.getPropertyAsString(
139                     object, displayNameProperty);
140         }
141         return object.toString();
142     }
143     
144     protected void setObjectId(String JavaDoc objectId) {
145         log.debug("Setting objectId to: " + objectId);
146         Object JavaDoc oldObject = object;
147         if (StringUtils.hasLength(objectId)) {
148             object = loadBean(objectId);
149         }
150         else {
151             object = null;
152         }
153         fireChangeEvent(object, oldObject);
154         displayName = getDisplayName(object);
155     }
156     
157     public void setValue(Object JavaDoc value) {
158         this.object = value;
159         displayName = getDisplayName(value);
160     }
161
162     public Object JavaDoc getValue() {
163         return object;
164     }
165     
166     public void addChangeListener(ChangeListener listener) {
167         if (listeners == null) {
168             listeners = new ArrayList JavaDoc();
169         }
170         listeners.add(listener);
171     }
172
173     protected void fireChangeEvent(Object JavaDoc newValue, Object JavaDoc oldValue) {
174         if (listeners != null) {
175             ChangeEvent event = new ChangeEvent(this, newValue, oldValue);
176             Iterator JavaDoc it = listeners.iterator();
177             while (it.hasNext()) {
178                 ChangeListener listener = (ChangeListener) it.next();
179                 listener.valueChanged(event);
180             }
181         }
182     }
183     
184     public void handleContentRequest(HttpServletRequest JavaDoc request,
185             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
186
187         response.setContentType("text/html");
188         Map JavaDoc model = new HashMap JavaDoc();
189         model.put("chooserUrl", request.getContextPath() + getChooserUrl());
190         String JavaDoc template = TemplateUtils.getTemplatePath(AbstractChooser.class,
191                 "_content");
192         
193         getFormContext().getTemplateRenderer().render(template,
194                 model, response.getWriter());
195     }
196     
197     protected abstract String JavaDoc getChooserUrl();
198     
199 }
200
Popular Tags