KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > controller > DefaultFormContext


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  * flx
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.controller;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import org.riotfamily.common.i18n.MessageResolver;
30 import org.riotfamily.forms.ContentElement;
31 import org.riotfamily.forms.FormContext;
32 import org.riotfamily.forms.TemplateRenderer;
33 import org.springframework.beans.PropertyEditorRegistrar;
34
35 /**
36  * @author Felix Gnass [fgnass at neteye dot de]
37  * @since 6.4
38  */

39 public class DefaultFormContext implements FormContext {
40
41     private String JavaDoc contextPath;
42     
43     private String JavaDoc resourcePath;
44     
45     /** Renderer for templates */
46     private TemplateRenderer templateRenderer;
47
48     private MessageResolver messageResolver;
49     
50     /** Current writer used for rendering */
51     private PrintWriter JavaDoc writer;
52
53     private String JavaDoc formUrl;
54     
55     private PropertyEditorRegistrar[] propertyEditorRegistrars;
56     
57     public DefaultFormContext() {
58     }
59     
60     public DefaultFormContext(MessageResolver messageResolver,
61             TemplateRenderer templateRenderer,
62             String JavaDoc contextPath, String JavaDoc resourcePath, String JavaDoc formUrl) {
63
64         this.messageResolver = messageResolver;
65         this.contextPath = contextPath;
66         setResourcePath(resourcePath);
67         this.templateRenderer = templateRenderer;
68         this.formUrl = formUrl;
69     }
70
71     private void setResourcePath(String JavaDoc path) {
72         if (path != null && path.length() > 0) {
73             if (path.charAt(path.length() - 1) != '/') {
74                 path += '/';
75             }
76         }
77         else {
78             path = "/";
79         }
80         this.resourcePath = path;
81     }
82     
83     public Locale JavaDoc getLocale() {
84         return messageResolver.getLocale();
85     }
86     
87     public MessageResolver getMessageResolver() {
88         return messageResolver;
89     }
90     
91     public String JavaDoc getContextPath() {
92         return contextPath;
93     }
94     
95     public String JavaDoc getResourcePath() {
96         return resourcePath;
97     }
98
99     public TemplateRenderer getTemplateRenderer() {
100         return templateRenderer;
101     }
102
103     public PrintWriter JavaDoc getWriter() {
104         return writer;
105     }
106
107     public void setWriter(PrintWriter JavaDoc writer) {
108         this.writer = writer;
109     }
110     
111     public String JavaDoc getFormUrl() {
112         return formUrl;
113     }
114     
115     private StringBuffer JavaDoc createFormUrlBuffer() {
116         StringBuffer JavaDoc url = new StringBuffer JavaDoc(formUrl);
117         if (url.indexOf("?") != -1) {
118             url.append('&');
119         }
120         else {
121             url.append('?');
122         }
123         return url;
124     }
125     
126     public String JavaDoc getContentUrl(ContentElement el) {
127         return createFormUrlBuffer()
128             .append("_content=" + el.getId())
129             .toString();
130     }
131     
132     public String JavaDoc getUploadUrl(String JavaDoc uploadId) {
133         return createFormUrlBuffer().append("uploadId=" + uploadId).toString();
134     }
135
136     public PropertyEditorRegistrar[] getPropertyEditorRegistrars() {
137         return this.propertyEditorRegistrars;
138     }
139
140     public void setPropertyEditorRegistrars(
141             PropertyEditorRegistrar[] registrars) {
142         
143         this.propertyEditorRegistrars = registrars;
144     }
145
146 }
147
Popular Tags