KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > LinkTag


1 package fr.improve.struts.taglib.layout;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.PageContext JavaDoc;
8
9 import org.apache.struts.taglib.html.Constants;
10 import org.apache.struts.taglib.logic.IterateTag;
11 import org.apache.struts.util.MessageResources;
12
13 import fr.improve.struts.taglib.layout.el.Expression;
14 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
15 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
16 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
17 import fr.improve.struts.taglib.layout.policy.AbstractPolicy;
18 import fr.improve.struts.taglib.layout.skin.Skin;
19 import fr.improve.struts.taglib.layout.util.FormUtilsInterface;
20 import fr.improve.struts.taglib.layout.util.LayoutUtils;
21 import fr.improve.struts.taglib.layout.util.ParentFinder;
22 import fr.improve.struts.taglib.layout.util.TagUtils;
23
24 /**
25  * Generate a URL-encoded hyperlink to the specified URI.
26  *
27  * Modified for struts-layout to call the javascript code checking if there are changes in the form values.
28  *
29  * @author Craig R. McClanahan, JN Ribette, F Bellingard
30  */

31
32 public class LinkTag extends org.apache.struts.taglib.html.BaseHandlerTag implements LayoutTag, LayoutEventListener {
33
34
35     // ----------------------------------------------------- Instance Variables
36

37
38     /**
39      * The body content of this tag (if any).
40      */

41     protected String JavaDoc text = null;
42
43
44     // ------------------------------------------------------------- Properties
45

46
47     /**
48      * The anchor to be added to the end of the generated hyperlink.
49      */

50     protected String JavaDoc anchor = null;
51
52     public String JavaDoc getAnchor() {
53         return (this.anchor);
54     }
55
56     public void setAnchor(String JavaDoc anchor) {
57         this.anchor = anchor;
58     }
59
60
61     /**
62      * The logical forward name from which to retrieve the hyperlink URI.
63      */

64     protected String JavaDoc forward = null;
65
66     public String JavaDoc getForward() {
67         return (this.forward);
68     }
69
70     public void setForward(String JavaDoc forward) {
71         this.forward = forward;
72     }
73
74
75     /**
76      * The hyperlink URI.
77      */

78     protected String JavaDoc href = null;
79     protected String JavaDoc jspHref;
80
81     public String JavaDoc getHref() {
82         return (this.href);
83     }
84
85     public void setHref(String JavaDoc href) {
86         this.href = href;
87     }
88
89
90     /**
91      * The link name for named links.
92      */

93     protected String JavaDoc linkName = null;
94
95     public String JavaDoc getLinkName() {
96         return (this.linkName);
97     }
98
99     public void setLinkName(String JavaDoc linkName) {
100         this.linkName = linkName;
101     }
102
103
104     /**
105      * The message resources for this package.
106      */

107     protected static MessageResources messages =
108      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
109
110
111     /**
112      * The JSP bean name for query parameters.
113      */

114     protected String JavaDoc name = null;
115
116     public String JavaDoc getName() {
117         return (this.name);
118     }
119
120     public void setName(String JavaDoc name) {
121         this.name = name;
122     }
123
124
125     /**
126      * The context-relative page URL (beginning with a slash) to which
127      * this hyperlink will be rendered.
128      */

129     protected String JavaDoc page = null;
130     protected String JavaDoc action;
131     protected String JavaDoc module;
132
133     public String JavaDoc getPage() {
134         return (this.page);
135     }
136
137     public void setPage(String JavaDoc page) {
138         this.page = page;
139     }
140
141
142     /**
143      * The single-parameter request parameter name to generate.
144      */

145     protected String JavaDoc paramId = null;
146
147     public String JavaDoc getParamId() {
148         return (this.paramId);
149     }
150
151     public void setParamId(String JavaDoc paramId) {
152         this.paramId = paramId;
153     }
154
155
156     /**
157      * The single-parameter JSP bean name.
158      */

159     protected String JavaDoc paramName = null;
160
161     public String JavaDoc getParamName() {
162         return (this.paramName);
163     }
164
165     public void setParamName(String JavaDoc paramName) {
166         this.paramName = paramName;
167     }
168
169
170     /**
171      * The single-parameter JSP bean property.
172      */

173     protected String JavaDoc paramProperty = null;
174
175     public String JavaDoc getParamProperty() {
176         return (this.paramProperty);
177     }
178
179     public void setParamProperty(String JavaDoc paramProperty) {
180         this.paramProperty = paramProperty;
181     }
182
183
184     /**
185      * The single-parameter JSP bean scope.
186      */

187     protected String JavaDoc paramScope = null;
188
189     public String JavaDoc getParamScope() {
190         return (this.paramScope);
191     }
192
193     public void setParamScope(String JavaDoc paramScope) {
194         this.paramScope = paramScope;
195     }
196
197
198     /**
199      * The JSP bean property name for query parameters.
200      */

201     protected String JavaDoc property = null;
202
203     public String JavaDoc getProperty() {
204         return (this.property);
205     }
206
207     public void setProperty(String JavaDoc property) {
208         this.property = property;
209     }
210
211
212     /**
213      * The scope of the bean specified by the name property, if any.
214      */

215     protected String JavaDoc scope = null;
216
217     public String JavaDoc getScope() {
218         return (this.scope);
219     }
220
221     public void setScope(String JavaDoc scope) {
222         this.scope = scope;
223     }
224
225
226     /**
227      * The window target.
228      */

229     protected String JavaDoc target = null;
230
231     public String JavaDoc getTarget() {
232         return (this.target);
233     }
234
235     public void setTarget(String JavaDoc target) {
236         this.target = target;
237     }
238
239
240     /**
241      * Include transaction token (if any) in the hyperlink?
242      */

243     protected boolean transaction = false;
244
245     public boolean getTransaction() {
246         return (this.transaction);
247     }
248
249     public void setTransaction(boolean transaction) {
250         this.transaction = transaction;
251     }
252
253     /**
254      * Name of parameter to generate to hold index number
255      */

256     protected String JavaDoc indexId = null;
257
258     public String JavaDoc getIndexId() {
259        return (this.indexId);
260     }
261
262     public void setIndexId(String JavaDoc indexId) {
263         this.indexId = indexId;
264     }
265     
266     protected boolean layout = true;
267     
268     public void setLayout(boolean in_layout) {
269         layout = in_layout;
270     }
271     public boolean isLayout() {
272         return layout;
273     }
274     
275     protected String JavaDoc scheme;
276     
277     public String JavaDoc getScheme() {
278         return scheme;
279     }
280
281     public void setScheme(String JavaDoc scheme) {
282         this.scheme = scheme;
283     }
284     
285     // --------------------------------------------------------- Public Methods
286

287     public final PageContext JavaDoc getPageContext() {
288       return pageContext;
289     }
290     
291     public final int doStartTag() throws JspException JavaDoc {
292       ParentFinder.registerTag(pageContext, this);
293       initDynamicValues();
294       return doStartLayoutTag();
295     }
296     
297     public final int doEndTag() throws JspException JavaDoc {
298       try {
299         return doEndLayoutTag();
300       } finally {
301         reset();
302         ParentFinder.deregisterTag(pageContext);
303       }
304     }
305
306     /**
307      * Init dynamic values.
308      */

309     protected void initDynamicValues() {
310         jspHref = href;
311         href = Expression.evaluate(jspHref, pageContext);
312     }
313
314     /**
315      * Render the beginning of the hyperlink.
316      * Indexed property since 1.1
317      *
318      * @exception JspException if a JSP exception has occurred
319      */

320     public int doStartLayoutTag() throws JspException JavaDoc {
321         
322         // if the action is about to be displayed, check the authorization first
323
if (policy != null) {
324             Skin lc_currentSkin = LayoutUtils.getSkin(pageContext.getSession());
325             AbstractPolicy lc_policy = lc_currentSkin.getPolicy();
326             String JavaDoc lc_action = null;
327             if (getHref() != null) {
328                 lc_action = getHref();
329             } else if (getPage() != null) {
330                 lc_action = getPage();
331             } else if (getForward() != null) {
332                 lc_action = getForward();
333             } else {
334                 throw new JspException JavaDoc("You must specify exactly one of the following attributes for the Link tag: href, page or forward.");
335             }
336             switch (lc_policy.getAuthorizedDisplayMode(getPolicy(), lc_action, getProperty(), pageContext)) {
337                 case AbstractPolicy.MODE_EDIT:
338                     break;
339                 case AbstractPolicy.MODE_NODISPLAY:
340                     display = false;
341                     break;
342                 case AbstractPolicy.MODE_INSPECT:
343                     showLink = false;
344                 break;
345                 default:
346                     throw new IllegalStateException JavaDoc(lc_policy.getClass().getName() + " returns an illegal value");
347             }
348         }
349             
350         // do nothing if the action is not displayed in this mode.
351
if (!display)
352             return SKIP_BODY;
353             
354         if (isLayout()) {
355             StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
356             beginLinkLayout(lc_buffer);
357             new StartLayoutEvent(this, lc_buffer.toString()).send();
358         }
359
360         // Special case for name anchors
361
if (linkName != null) {
362             StringBuffer JavaDoc results = new StringBuffer JavaDoc("<a name=\"");
363             results.append(linkName);
364             results.append("\">");
365             TagUtils.write(pageContext, results.toString());
366             return (EVAL_BODY_TAG);
367         }
368
369         // Calcule l'URL.
370
String JavaDoc url = computeURL();
371
372         // Generate the opening anchor element
373
StringBuffer JavaDoc results = new StringBuffer JavaDoc("<a ");
374         
375         if (showLink) {
376             results.append("href=\"");
377             results.append(url);
378             results.append("\"");
379             if (target != null) {
380                 results.append(" target=\"");
381                 results.append(target);
382                 results.append("\"");
383             }
384         }
385         results.append(prepareStyles());
386         results.append(prepareEventHandlers());
387         results.append(">");
388
389         // Print this element to our output writer
390
TagUtils.write(pageContext, results.toString());
391
392         // Evaluate the body of this tag
393
this.text = null;
394         return (EVAL_BODY_TAG);
395
396     }
397
398     protected String JavaDoc computeURL() throws JspException JavaDoc {
399         // Generate the hyperlink URL
400
Map JavaDoc params = LayoutUtils.computeParameters
401             (pageContext, paramId, paramName, paramProperty, paramScope,
402              name, property, scope, transaction);
403
404         // if "indexed=true", add "index=x" parameter to query string
405
// since 1.1
406
if( indexId!=null ) {
407            // look for outer iterate tag
408
IterateTag iterateTag = (IterateTag) findAncestorWithClass(this, IterateTag.class);
409            if (iterateTag == null) {
410               // this tag should only be nested in iteratetag, if it's not, throw exception
411
JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("indexed.noEnclosingIterate"));
412               TagUtils.saveException(pageContext, e);
413               throw e;
414            }
415
416            //calculate index, and add as a parameter
417
if (params == null) {
418                params = new HashMap JavaDoc(); //create new HashMap if no other params
419
}
420             params.put(indexId, Integer.toString(iterateTag.getIndex()));
421         }
422
423         String JavaDoc url = null;
424         url = LayoutUtils.computeURL(pageContext, forward, href,
425                                           page, action, module, params, anchor, false, target,
426                                           Expression.evaluate(scheme, pageContext));
427         return url;
428     }
429
430
431
432     /**
433      * Save the associated label from the body content.
434      *
435      * @exception JspException if a JSP exception has occurred
436      */

437     public int doAfterBody() throws JspException JavaDoc {
438
439         if (bodyContent != null) {
440             String JavaDoc value = bodyContent.getString().trim();
441             if (value.length() > 0)
442                 text = value;
443         }
444         return (SKIP_BODY);
445
446     }
447
448
449     /**
450      * Render the end of the hyperlink.
451      *
452      * @exception JspException if a JSP exception has occurred
453      */

454     public int doEndLayoutTag() throws JspException JavaDoc {
455         // do nothing if the tag is no displayed
456
if (!display) {
457             display = true;
458             return EVAL_PAGE;
459         }
460
461         // Prepare the textual content and ending element of this hyperlink
462
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
463         if (text != null)
464             results.append(text);
465         results.append("</a>");
466         
467
468         // Render the remainder to the output stream
469
TagUtils.write(pageContext, results.toString());
470         
471         if (isLayout()) {
472             results.setLength(0);
473             endLinkLayout(results);
474             new EndLayoutEvent(this, results.toString()).send();
475         }
476         
477         // Evaluate the remainder of this page
478
return (EVAL_PAGE);
479
480     }
481     
482     protected void reset() {
483         href = jspHref;
484         jspHref = null;
485         
486         showLink = true;
487     }
488
489     /**
490      * Prepare to display the link.
491      */

492     protected void beginLinkLayout(StringBuffer JavaDoc in_buffer) throws JspException JavaDoc {
493         in_buffer.append("<td>&nbsp;</td><td");
494 // if (valign!=null) {
495
// in_buffer.append(" valign=\"");
496
// in_buffer.append(valign);
497
// in_buffer.append("\"");
498
// }
499
in_buffer.append(">");
500     }
501
502     /**
503      * End the display of the action.
504      */

505     protected void endLinkLayout(StringBuffer JavaDoc in_buffer) {
506         in_buffer.append("</td>");
507     }
508
509     /**
510      * Release any acquired resources.
511      */

512     public void release() {
513
514         super.release();
515         anchor = null;
516         forward = null;
517         href = null;
518         action = null;
519         module = null;
520         linkName = null;
521         name = null;
522         page = null;
523         paramId = null;
524         paramName = null;
525         paramProperty = null;
526         paramScope = null;
527         property = null;
528         scope = null;
529         target = null;
530         text = null;
531         transaction = false;
532         policy = null;
533         display = true;
534         layout = true;
535         scheme = null;
536     }
537
538     
539     protected boolean display = true;
540     protected boolean showLink = true;
541     protected String JavaDoc policy = null;
542
543     /**
544      * Set in wich form modes the action should be displayed or not.
545      * The format form in_mode is X,Y,Z where allowed values are D (Displayed) and N (not displayed) in the same order as the input field tags.
546      */

547     public void setMode(String JavaDoc in_mode) {
548         if (in_mode == null || in_mode.length() != 5) {
549             throw new IllegalArgumentException JavaDoc(
550                 "The specified mode" + in_mode + " is invalid");
551         }
552         int lc_formMode = LayoutUtils.getSkin(pageContext.getSession()).getFormUtils().getFormDisplayMode(pageContext);
553         char lc_displayMode;
554         switch (lc_formMode) {
555             case FormUtilsInterface.CREATE_MODE :
556                 lc_displayMode = in_mode.charAt(0);
557                 break;
558             case FormUtilsInterface.EDIT_MODE :
559                 lc_displayMode = in_mode.charAt(2);
560                 break;
561             case FormUtilsInterface.INSPECT_MODE :
562                 lc_displayMode = in_mode.charAt(4);
563                 break;
564             default :
565                 lc_displayMode = 'D';
566         }
567         display = lc_displayMode == 'D' || lc_displayMode == 'd';
568     }
569     
570     /**
571      * @return Returns the policy.
572      */

573     public String JavaDoc getPolicy()
574     {
575         return policy;
576     }
577
578     /**
579      * @param policy The policy to set.
580      */

581     public void setPolicy(String JavaDoc policy)
582     {
583         this.policy = policy;
584     }
585     
586     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
587         return Boolean.FALSE;
588     }
589     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
590         return Boolean.FALSE;
591     }
592     public String JavaDoc getAction() {
593         return action;
594     }
595     public void setAction(String JavaDoc action) {
596         this.action = action;
597     }
598
599     public String JavaDoc getModule() {
600         return module;
601     }
602
603     public void setModule(String JavaDoc module) {
604         this.module = module;
605     }
606 }
Popular Tags