KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > collection > CollectionDetailTag


1 /*
2  * Created on 26 mars 2004
3  *
4  * Copyright Improve SA 2004.
5  * All rights reserved.
6  */

7 package fr.improve.struts.taglib.layout.collection;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
14
15 import fr.improve.struts.taglib.layout.util.LayoutUtils;
16 import fr.improve.struts.taglib.layout.util.TagUtils;
17
18 /**
19  * This tag allow to specify a property to display
20  * in a detail field when a line of a list is selected
21  *
22  * @author jnribette
23  */

24 public class CollectionDetailTag extends TagSupport JavaDoc {
25     protected String JavaDoc name;
26     protected String JavaDoc property;
27     protected boolean skip = false;
28     protected BaseCollectionTag collectionTag;
29     
30     protected static final String JavaDoc DETAIL_KEY = "fr.improve.struts.taglib.layout.collection.CollectionDetailTag.DETAIL_KEY";
31     
32     public int doStartTag() throws JspException JavaDoc {
33         try {
34             collectionTag =
35                 (BaseCollectionTag) findAncestorWithClass(this, BaseCollectionTag.class);
36             if (collectionTag.getId()==null) {
37                 throw new JspException JavaDoc("Invalid use of collectionDetail tag: parent collection tag attribute 'id' should be set");
38             }
39             if (collectionTag.getIndexId()==null) {
40                 throw new JspException JavaDoc("Invalid use of collectionDetail tag: parent collection tag attribute 'indexId' should be set");
41             }
42             if (collectionTag.isFirst()) {
43                 return SKIP_BODY;
44             }
45             if (name!=null) {
46                 if (!collectionTag.getSpans().containsKey(name)) {
47                     skip = true;
48                     return SKIP_BODY;
49                 } else {
50                     pageContext.setAttribute(CollectionItemTag.SPAN_KEY, collectionTag.getSpans().get(name));
51                 }
52             }
53             
54         } catch (ClassCastException JavaDoc e) {
55             throw new JspException JavaDoc("Invalid use of collectionDetail tag");
56         } catch (NullPointerException JavaDoc e) {
57             throw new JspException JavaDoc("Invalid use of collectionDetail tag");
58         }
59         return SKIP_BODY;
60     }
61     
62     public int doEndTag() throws JspException JavaDoc {
63         if (collectionTag.isFirst()) {
64             Map JavaDoc lc_map = getDetailInfo();
65             if (!lc_map.containsKey(collectionTag.getId())) {
66                 // Put -1 in the map for this if.
67
lc_map.put(collectionTag.getId(), new Integer JavaDoc(-1));
68                 
69                 // Declare a javascript variable to hold the details info.
70
renderInit(collectionTag.getId());
71                 
72                 // Ask the collection tag to generate javascript to update the details when a line is selected.
73
String JavaDoc onover = collectionTag.getOnRowMouseOver();
74                 String JavaDoc onout = collectionTag.getOnRowMouseOut();
75                 collectionTag.setOnRowMouseOver("showDetail(" + collectionTag.getId() + ",${" + collectionTag.getIndexId() + "});" + onover);
76                 collectionTag.setOnRowMouseOut("clearDetail(" + collectionTag.getId() + ");" + onout);
77             }
78             return EVAL_PAGE;
79         }
80         if (skip) {
81             skip = false;
82             collectionTag.incrementColumn();
83             return EVAL_PAGE;
84         }
85         
86         Map JavaDoc lc_map = getDetailInfo();
87         Integer JavaDoc lc_int = (Integer JavaDoc) lc_map.get(collectionTag.getId());
88         if (lc_int.intValue()< collectionTag.getIndex()) {
89             lc_map.put(collectionTag.getId(), new Integer JavaDoc(collectionTag.getIndex()));
90             renderNext(collectionTag.getId(), collectionTag.getIndex());
91         }
92         
93         Object JavaDoc lc_value = buildContent();
94         renderDetail(collectionTag.getId(), property, collectionTag.getIndex(), lc_value==null ? "" : lc_value.toString());
95         
96         reset();
97         return EVAL_PAGE;
98     }
99     
100     private Map JavaDoc getDetailInfo() {
101         Map JavaDoc lc_map = (Map JavaDoc) pageContext.getAttribute(DETAIL_KEY);
102         if (lc_map==null) {
103             lc_map = new HashMap JavaDoc();
104             pageContext.setAttribute(DETAIL_KEY, lc_map);
105         }
106         return lc_map;
107     }
108
109     /**
110      * This method get the value to display from the collection.
111      */

112     protected Object JavaDoc buildContent() throws JspException JavaDoc {
113         Object JavaDoc lc_cell = null;
114         if (name == null) {
115             // The item to add is a property of a bean in the current collection.
116
lc_cell = collectionTag.getBean();
117         } else {
118             // The item to add is a property of a bean in the context.
119
lc_cell = pageContext.findAttribute(name);
120         }
121         if (lc_cell != null && property != null) {
122             // Get the property of the bean.
123
lc_cell = getPropertyValue(lc_cell, property);
124         }
125         return lc_cell;
126     }
127     
128     /**
129      * This method simply uses BeanUtils (indirectly) to return the value of the property 'in_property'
130      * of in_bean. This method is intended to be overrided if needed.
131      */

132     protected Object JavaDoc getPropertyValue(Object JavaDoc in_bean, String JavaDoc in_property) throws JspException JavaDoc {
133         return LayoutUtils.getProperty(in_bean, in_property);
134     }
135     
136     /**
137      * This method writes the required javascript code to the response.
138      * @param in_name the name of the object
139      * @param in_property the detail property
140      * @param in_value the value to render
141      */

142     protected void renderDetail(String JavaDoc in_name, String JavaDoc in_property, int in_index, String JavaDoc in_value) throws JspException JavaDoc {
143         TagUtils.write(pageContext, "<script>");
144         TagUtils.write(pageContext, in_name);
145         TagUtils.write(pageContext, "[");
146         TagUtils.write(pageContext, String.valueOf(in_index));
147         TagUtils.write(pageContext, "]['");
148         TagUtils.write(pageContext, in_property);
149         TagUtils.write(pageContext, "'] = \"");
150         TagUtils.write(pageContext, filterDoubleQuotes(in_value));
151         TagUtils.write(pageContext, "\"; ");
152         TagUtils.write(pageContext, "</script>\n");
153     }
154     
155     /**
156      * Filter double quotes.
157      */

158     protected String JavaDoc filterDoubleQuotes(String JavaDoc in_value) {
159         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(in_value.length());
160         int i =0;
161         char c;
162         for (i = 0; i < in_value.length(); i++) {
163             c = in_value.charAt(i);
164             if (c=='"') {
165                 sb.append('\"');
166             } else {
167                 sb.append(c);
168             }
169         }
170         return sb.toString();
171     }
172         
173     
174     /**
175      * This method writes the required Javascript code to declare the variable to the response.
176      */

177     public void renderInit(String JavaDoc in_name) throws JspException JavaDoc {
178         TagUtils.write(pageContext, "<script>var ");
179         TagUtils.write(pageContext, in_name);
180         TagUtils.write(pageContext, " = new Array();");
181         TagUtils.write(pageContext, "</script>\n");
182     }
183     
184     public void renderNext(String JavaDoc in_name, int in_index) throws JspException JavaDoc {
185         TagUtils.write(pageContext, "<script>");
186         TagUtils.write(pageContext, in_name);
187         TagUtils.write(pageContext, "[");
188         TagUtils.write(pageContext, String.valueOf(in_index));
189         TagUtils.write(pageContext, "] = new Object();");
190         TagUtils.write(pageContext, "</script>\n");
191     }
192     
193     protected void reset() {
194         collectionTag = null;
195         skip = false;
196     }
197     
198     public void release() {
199         property = null;
200     }
201     /**
202      * @return Returns the name.
203      */

204     public String JavaDoc getName() {
205         return name;
206     }
207     /**
208      * @param name The name to set.
209      */

210     public void setName(String JavaDoc name) {
211         this.name = name;
212     }
213     /**
214      * @return Returns the property.
215      */

216     public String JavaDoc getProperty() {
217         return property;
218     }
219     /**
220      * @param property The property to set.
221      */

222     public void setProperty(String JavaDoc property) {
223         this.property = property;
224     }
225 }
226
Popular Tags