1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.render.bean;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23
24 import org.springframework.extensions.surf.RequestContext;
25 import org.springframework.extensions.surf.WebFrameworkConstants;
26 import org.springframework.extensions.surf.render.Processor;
27 import org.springframework.extensions.surf.render.ProcessorContext;
28 import org.springframework.extensions.surf.render.RenderContext;
29 import org.springframework.extensions.surf.render.RenderMode;
30 import org.springframework.extensions.surf.render.Renderable;
31 import org.springframework.extensions.surf.render.ProcessorContext.ProcessorDescriptor;
32 import org.springframework.extensions.surf.render.bean.ComponentRenderer;
33 import org.springframework.extensions.surf.studio.WebStudioService;
34 import org.springframework.extensions.surf.types.Component;
35 import org.springframework.extensions.surf.types.ComponentType;
36 import org.springframework.extensions.webscripts.DeclarativeRegistry;
37 import org.springframework.extensions.webscripts.WebScript;
38
39
40
41
42
43
44
45
46
47
48 public class WebStudioComponentRenderer extends ComponentRenderer
49 {
50 private WebStudioService webStudioService = null;
51 private DeclarativeRegistry registry = null;
52
53
54
55
56
57
58 public void setWebStudioService(WebStudioService webStudioService)
59 {
60 this.webStudioService = webStudioService;
61 }
62
63
64
65
66
67
68 public void setRegistry(DeclarativeRegistry registry)
69 {
70 this.registry = registry;
71 }
72
73
74
75
76
77
78 protected WebStudioService getWebStudioService()
79 {
80 return this.webStudioService;
81 }
82
83
84
85
86
87
88 public void postProcess(RenderContext context) throws IOException
89 {
90
91 boolean webStudioEnabled = true;
92 if (webStudioEnabled)
93 {
94
95 if (!context.isPassiveMode())
96 {
97
98 String htmlId = (String) context
99 .getValue(WebFrameworkConstants.RENDER_DATA_HTMLID);
100
101
102 String componentId = (String) context
103 .getValue(WebFrameworkConstants.RENDER_DATA_COMPONENT_ID);
104 String componentTypeId = (String) context
105 .getValue(WebFrameworkConstants.RENDER_DATA_COMPONENT_TYPE_ID);
106
107
108 PrintWriter writer = context.getResponse().getWriter();
109 writer.println("<script type='text/javascript'>");
110
111
112 if (componentId != null && componentTypeId != null)
113 {
114 String componentTitle = "";
115 String componentTypeTitle = "";
116
117 Component c = (Component) context.getModel().getComponent(componentId);
118 if (c != null)
119 {
120 componentTitle = getComponentTitle(context, c);
121 componentTypeTitle = getComponentTypeTitle(context, c);
122 }
123
124 writer.println("if(typeof WebStudio != \"undefined\"){");
125 writer.println("WebStudio.configureComponent('" + htmlId
126 + "', '" + componentId + "', '" + componentTypeId
127 + "', '" + componentTitle + "', '" + componentTypeTitle + "');");
128
129
130 boolean hasEditMode = false;
131 ComponentType ct = (ComponentType) c.getComponentType(context);
132 if (ct instanceof Renderable)
133 {
134 Renderable renderable = (Renderable) ct;
135
136
137 Processor processor = this.getRenderService().getProcessor((Renderable)ct, RenderMode.EDIT);
138 if (processor != null)
139 {
140
141 RenderContext renderContext = getRenderService().provideRenderContext(context, context.getRequest(), context.getResponse(), RenderMode.EDIT);
142
143
144 ProcessorContext processorContext = new ProcessorContext(renderContext);
145
146
147 processorContext.load((Renderable)ct);
148
149
150 if (c.getURL() != null)
151 {
152 ProcessorDescriptor viewDescriptor = processorContext.getDescriptor(RenderMode.VIEW);
153 viewDescriptor.put("uri", c.getURL());
154 }
155
156 hasEditMode = processor.exists(processorContext);
157 }
158 }
159
160 if (hasEditMode)
161 {
162 writer.println("WebStudio.configureComponentEditable('" + htmlId + "');");
163 }
164
165
166 writer.println("}");
167 }
168
169 writer.println("</script>");
170 writer.flush();
171 }
172 }
173 }
174
175 public String getComponentTitle(RequestContext context, Component c)
176 {
177 String title = c.getTitle();
178 if (title == null)
179 {
180 title = c.getId();
181 }
182
183 return title;
184 }
185
186 public String getComponentTypeTitle(RequestContext context,
187 Component c)
188 {
189 String title = null;
190
191 ComponentType ct = c.getComponentType(context);
192 if (ct != null)
193 {
194 title = ct.getTitle();
195 if (title == null)
196 {
197 title = ct.getId();
198 }
199
200 if ("webscript".equals(ct.getId()))
201 {
202 String url = c.getURL();
203
204 WebScript webScript = this.registry.getWebScript(url);
205 if (webScript != null)
206 {
207 title = webScript.getDescription().getShortName();
208 }
209 }
210 }
211 else
212 {
213
214 String url = c.getComponentTypeId();
215
216 for (WebScript webscript : this.registry.getWebScripts())
217 {
218 String[] uris = webscript.getDescription().getURIs();
219 for (int z = 0; z < uris.length; z++)
220 {
221 String uri = uris[z];
222 if (uri.equals(url))
223 {
224 title = webscript.getDescription().getShortName();
225 }
226 }
227 }
228 }
229
230 return title;
231 }
232 }