1 package mx.com.brained.taglib;
2
3 import java.io.*;
4
5 import javax.servlet.http.*;
6 import javax.servlet.jsp.*;
7 import javax.servlet.jsp.tagext.*;
8
9 import org.apache.struts.taglib.*;
10 import java.util.Map;
11 import java.net.MalformedURLException;
12 import org.springframework.web.context.WebApplicationContext;
13 import mx.com.brained.resource.*;
14 import org.apache.commons.lang.StringUtils;
15
16 /***
17 *
18 * <p>Title: AwTaglib</p>
19 *
20 * <p>Description: This tag generates an IFrame HTML tag that will contain a jsp with an ActiveWidgets grid. It accepts
21 * four different parameters for the internal information to be rendered, that are as follows:
22 * <ul>
23 * <li>forward - Use the value of this attribute as the name of a global ActionForward to be looked up, and use the module-relative or context-relative URI found there. If the forward is module-relative then it must point to an action and NOT to a page.</li>
24 * <li>action - Use the value of this attribute as the name of a Action to be looked up, and use the module-relative or context-relative URI found there.</li>
25 * <li>href - Use the value of this attribute unchanged.</li>
26 * <li>page - Use the value of this attribute as a module-relative URI, and generate a server-relative URI by including the context path and module prefix.</li>
27 * </ul></br>It works very similar to an html:link tag from the Struts framework</p>
28 *
29 * <p>Copyright: Copyright (c) 2005</p>
30 *
31 * <p>Company: Brain-ED</p>
32 *
33 * @author Isidoro Treviņo, Eliseo Partida
34 * @version 1.0
35 *
36 *
37 * @jsp.tag
38 * name="gridFrame"
39 * body-content="JSP"
40 * description="Tag that shows an iframe, containing an ActiveWidgets Grid"
41 */
42 public class GridFrameTag extends TagSupport {
43 private String forward;
44 private String action;
45 private String href;
46 private String page;
47 private String module;
48 private String anchor;
49 private String paramId;
50 private String paramName;
51 private String paramProperty;
52 private String paramMap;
53 private String paramMapProperty;
54 private String linkName;
55
56
57 private String styleClass;
58 private String name;
59
60 public GridFrameTag() {
61 }
62
63 /***
64 * Sets the CSS class to be used by the frame
65 * @param styleClass String
66 */
67 public void setStyleClass(String styleClass) {
68 this.styleClass = styleClass;
69 }
70
71 /***
72 * Set the name of the IFrame
73 * @param name String
74 */
75 public void setName(String name) {
76 this.name = name;
77 }
78
79 /***
80 *
81 * @param forward String
82 */
83 public void setForward(String forward) {
84 this.forward = forward;
85 }
86 /***
87 *
88 * @param action String
89 */
90 public void setAction(String action) {
91 this.action = action;
92 }
93 /***
94 *
95 * @param href String
96 */
97 public void setHref(String href) {
98 this.href = href;
99 }
100 /***
101 *
102 * @param page String
103 */
104 public void setPage(String page) {
105 this.page = page;
106 }
107 /***
108 * If inside a struts module, use this
109 * @param module String
110 */
111 public void setModule(String module) {
112 this.module = module;
113 }
114 /***
115 * If using an anchor, set this
116 * @param anchor String
117 */
118 public void setAnchor(String anchor) {
119 this.anchor = anchor;
120 }
121 /***
122 * The name of the request parameter that will be dynamically added to the
123 * generated hyperlink. The corresponding value is defined by the paramName
124 * and (optional) paramProperty attributes, optionally scoped by the paramScope attribute
125 * @param paramId String
126 */
127 public void setParamId(String paramId) {
128 this.paramId = paramId;
129 }
130 /***
131 * The name of a JSP bean that is a String containing the value for the
132 * request parameter named by paramId (if paramProperty is not specified),
133 * or a JSP bean whose property getter is called to return a String (if paramProperty
134 * is specified). The JSP bean is constrained to the bean scope specified by
135 * the paramScope property, if it is specified.
136 * @param paramName String
137 */
138 public void setParamName(String paramName) {
139 this.paramName = paramName;
140 }
141
142 /***
143 * The name of a property of the bean specified by the paramName attribute,
144 * whose return value must be a String containing the value of the request
145 * parameter (named by the paramId attribute) that will be dynamically added
146 * to this hyperlink.
147 * @param paramProperty String
148 */
149 public void setParamProperty(String paramProperty) {
150 this.paramProperty = paramProperty;
151 }
152 /***
153 * The name of a JSP bean that contains a Map representing the query parameters
154 * (if property is not specified), or a JSP bean whose property getter is called
155 * to return a Map (if property is specified).
156 * @param paramMap String
157 */
158 public void setParamMap(String paramMap) {
159 this.paramMap = paramMap;
160 }
161
162 /***
163 * The name of a property of the bean specified by the name attribute, whose
164 * return value must be a java.util.Map containing the query parameters to be
165 * added to the hyperlink. You must specify the name attribute if you specify this attribute.
166 * @param paramMapProperty String
167 */
168 public void setParamMapProperty(String paramMapProperty) {
169 this.paramMapProperty = paramMapProperty;
170 }
171
172 /***
173 * The anchor name to be defined within this page, so that you can reference
174 * it with intra-page hyperlinks. In other words, the value specified here will
175 * render a "name" element in the generated anchor tag.
176 * @param linkName String
177 */
178 public void setLinkName(String linkName) {
179 this.linkName = linkName;
180 }
181
182 /***
183 *
184 * @return Object
185 * @jsp.attribute
186 * required="false"
187 * rtexprvalue="true"
188 */
189 public String getStyleClass() {
190 return styleClass;
191 }
192
193 /***
194 *
195 * @return Object
196 * @jsp.attribute
197 * required="true"
198 * rtexprvalue="false"
199 */
200 public String getName() {
201 return name;
202 }
203 /***
204 *
205 * @return Object
206 * @jsp.attribute
207 * required="false"
208 * rtexprvalue="true"
209 */
210 public String getForward() {
211 return forward;
212 }
213 /***
214 *
215 * @return Object
216 * @jsp.attribute
217 * required="false"
218 * rtexprvalue="true"
219 */
220 public String getAction() {
221 return action;
222 }
223 /***
224 *
225 * @return Object
226 * @jsp.attribute
227 * required="false"
228 * rtexprvalue="true"
229 */
230 public String getHref() {
231 return href;
232 }
233 /***
234 *
235 * @return Object
236 * @jsp.attribute
237 * required="false"
238 * rtexprvalue="true"
239 */
240 public String getPage() {
241 return page;
242 }
243
244 public String getModule() {
245 return module;
246 }
247 /***
248 *
249 * @return Object
250 * @jsp.attribute
251 * required="false"
252 * rtexprvalue="true"
253 */
254 public String getAnchor() {
255 return anchor;
256 }
257 /***
258 *
259 * @return Object
260 * @jsp.attribute
261 * required="false"
262 * rtexprvalue="true"
263 */
264 public String getParamId() {
265 return paramId;
266 }
267 /***
268 *
269 * @return Object
270 * @jsp.attribute
271 * required="false"
272 * rtexprvalue="true"
273 */
274 public String getParamName() {
275 return paramName;
276 }
277 /***
278 *
279 * @return Object
280 * @jsp.attribute
281 * required="false"
282 * rtexprvalue="true"
283 */
284 public String getParamProperty() {
285 return paramProperty;
286 }
287 /***
288 *
289 * @return Object
290 * @jsp.attribute
291 * required="false"
292 * rtexprvalue="true"
293 */
294 public String getParamMap() {
295 return paramMap;
296 }
297 /***
298 *
299 * @return Object
300 * @jsp.attribute
301 * required="false"
302 * rtexprvalue="true"
303 */
304 public String getParamMapProperty() {
305 return paramMapProperty;
306 }
307 /***
308 *
309 * @return Object
310 * @jsp.attribute
311 * required="false"
312 * rtexprvalue="true"
313 */
314 public String getLinkName() {
315 return linkName;
316 }
317
318 public int doStartTag() throws JspException {
319 JspWriter writer = pageContext.getOut();
320 try {
321 HttpServletResponse response =
322 (HttpServletResponse)this.pageContext.getResponse();
323 WebApplicationContext context = (WebApplicationContext) pageContext.getServletContext().
324 getAttribute(WebApplicationContext.
325 ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
326 ResourceManagerIface resource = (ResourceManagerIface) context.getBean(GridConstants.RESOURCEMANAGER);
327 String result = resource.getResourceAsString(GridConstants.FRAME_SCRIPT);
328 String url = calculateURL();
329 result = StringUtils.replace(result,GridConstants.NAME_TOKEN,name);
330 result = StringUtils.replace(result,GridConstants.STYLE_CLASS_TOKEN,styleClass);
331 result = StringUtils.replace(result,GridConstants.SRC_TOKEN,url);
332 writer.println(result);
333 } catch (Exception ex) {
334 throw new JspException(ex);
335 }
336 return this.SKIP_BODY;
337 }
338
339 /***
340 * Return the complete URL to which this hyperlink will direct the user.
341 *
342 * @exception JspException if an exception is thrown calculating the value
343 */
344 protected String calculateURL() throws JspException {
345
346
347 Map params = TagUtils.getInstance().computeParameters
348 (pageContext, paramId, paramName, paramProperty, null,
349 paramMap, paramMapProperty, null, false);
350
351 String url = null;
352 try {
353 url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href,
354 page, action, module, params, anchor, false, true);
355 } catch (MalformedURLException e) {
356 TagUtils.getInstance().saveException(pageContext, e);
357 throw new JspException
358 ("Exception writing url");
359 }
360 return (url);
361
362 }
363 /***
364 * Reset the properties to be reused
365 */
366 public void release() {
367 forward = null;
368 action = null;
369 href = null;
370 page = null;
371 module = null;
372 anchor = null;
373 paramId = null;
374 paramName = null;
375 paramProperty = null;
376 paramMap = null;
377 paramMapProperty = null;
378 styleClass = null;
379 name = null;
380 linkName=null;
381 }
382 }