KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > tab > TabTag


1 package fr.improve.struts.taglib.layout.tab;
2
3 import java.net.URLDecoder JavaDoc;
4 import java.net.URLEncoder JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import javax.servlet.http.Cookie JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import javax.servlet.jsp.JspException JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import fr.improve.struts.taglib.layout.LabelledTag;
19 import fr.improve.struts.taglib.layout.el.Expression;
20 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
21 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
22 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
23 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeLayoutEvent;
24 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeListener;
25 import fr.improve.struts.taglib.layout.util.LayoutUtils;
26 import fr.improve.struts.taglib.layout.util.TagUtils;
27
28 /**
29  * @author jnribette
30  *
31  * To change this generated comment edit the template variable "typecomment":
32  * Window>Preferences>Java>Templates.
33  * To enable and disable the creation of type comments go to
34  * Window>Preferences>Java>Code Generation.
35  */

36 public class TabTag extends LabelledTag implements LayoutEventListener, StaticCodeIncludeListener {
37     /**
38      * Key for ATG fix.
39      */

40     public static final String JavaDoc ATG_FIX_KEY = "fr.improve.struts.taglib.layout.tab.TabTag.ATG_FIX_KEY";
41     
42     /**
43      * Log.
44      */

45     protected static final Log LOG = LogFactory.getLog(TabsTag.class);
46     
47     /**
48      * The width of the tab title.
49      */

50     private String JavaDoc width;
51     
52     /**
53      * The url to invoke when this tab is selected. Default is to show the tab.
54      */

55     private String JavaDoc href;
56     private String JavaDoc forward;
57     private String JavaDoc page;
58     
59     /**
60      * The reqCode to submit when this tab is selected. Default is to show the tab
61      */

62     private String JavaDoc reqCode;
63     
64     private boolean include;
65     
66     private String JavaDoc staticCode = "";
67             
68     public int doStartLayoutTag() throws JspException JavaDoc {
69         TabsTag lc_tabs = (TabsTag) findAncestorWithClass(this, TabsTag.class);
70         if (lc_tabs==null) {
71             throw new JspException JavaDoc("Invalid use of <layout:tab>");
72         }
73         
74         // Should we include the tab body ?
75
include = false;
76         
77         String JavaDoc lc_href = null;
78         if (href!=null || forward!=null || page!=null) {
79             // We should call an url when the tab is selected
80
// We don't need to generate its content.
81
lc_href = LayoutUtils.computeURL(pageContext, forward, Expression.evaluate(href, pageContext), page, null, null, null, null, false, null);
82         } else if (reqCode!=null) {
83             // We should submit the current form when the tab is selected.
84
// We don't need to generate its content.
85
} else {
86             // We should display the tab content without any server request
87
// when the tab is selected.
88
// We need to generate its content.
89
include = true;
90         }
91         
92         // Is the tab selected ? No if a tab has already been selected.
93
boolean lc_selected = isTabSelected(lc_tabs);
94         if (lc_selected) {
95             include = true;
96         }
97         String JavaDoc lc_id = lc_tabs.addTab(key, getLabel(), lc_href, reqCode, width, lc_selected);
98                         
99         if (include) {
100             TagUtils.write(pageContext, "<div id=\"");
101             TagUtils.write(pageContext, lc_id);
102             if (!lc_selected) {
103                 TagUtils.write(pageContext, "\" style=\"display:none;");
104             }
105             TagUtils.write(pageContext, "\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"clsAction\">");
106                         
107             return EVAL_BODY_INCLUDE;
108         } else {
109             return SKIP_BODY;
110         }
111     }
112
113     /**
114      * Return true if the current tab is selected.
115      */

116     protected boolean isTabSelected(TabsTag lc_tabs) {
117         // Easy, if a tab has already been selected.
118
boolean lc_selected = !lc_tabs.isTabAlreadySelected();
119         
120         // No tab selected yet.
121
if (lc_selected && lc_tabs.getSelectedTabKeyName()!=null) {
122             String JavaDoc lc_selectedTab;
123             LOG.debug("Looking for the selection of tab " + key + " in tabs " + lc_tabs.getSelectedTabKeyName());
124             
125             // Look for a cookie.
126
lc_selectedTab = getSelectedTabNameFormCookie((HttpServletRequest JavaDoc)pageContext.getRequest(), lc_tabs.getSelectedTabKeyName());
127             if (lc_selectedTab!=null && LOG.isDebugEnabled()) {
128                 LOG.debug("Selected tab from cookie=" + lc_selectedTab);
129             }
130             
131             // Look for a request attribute.
132
if (lc_selectedTab==null) {
133                 lc_selectedTab = (String JavaDoc) pageContext.findAttribute(lc_tabs.getSelectedTabKeyName());
134                 if (lc_selectedTab!=null && LOG.isDebugEnabled()) {
135                     LOG.debug("Selected tab from attribute="+lc_selectedTab);
136                 }
137             }
138             
139             // Look for a request parameter.
140
if (lc_selectedTab==null) {
141                 lc_selectedTab = pageContext.getRequest().getParameter(lc_tabs.getSelectedTabKeyName());
142                 if (lc_selectedTab!=null && LOG.isDebugEnabled()) {
143                     LOG.debug("Selected tag from parameter="+lc_selectedTab);
144                 }
145             }
146             
147             // Check the selected tab key value.
148
if (lc_selectedTab!=null && !lc_selectedTab.equals(key)) {
149                 LOG.debug("Deselecting non matching tab");
150                 lc_selected = false;
151             }
152             if (lc_selected){
153                 LOG.debug("Selecting matching tab");
154                 lc_tabs.setSelectedTabKey(key);
155             }
156         } else {
157             LOG.debug("Tab already selected in tabs or default selection to first tab");
158         }
159         return lc_selected;
160     }
161     
162     private static boolean isATGFix(HttpServletRequest JavaDoc in_request, String JavaDoc in_group) {
163         return in_request.getAttribute(ATG_FIX_KEY + "." + in_group)!=null;
164     }
165     private static void setATGFix(HttpServletRequest JavaDoc in_request, String JavaDoc in_group) {
166         if (!"false".equals(LayoutUtils.getSkin(in_request.getSession()).getProperty("tabs.cookie.fix", "false"))) {
167             LOG.debug("Ignore tab cookie value");
168             in_request.setAttribute(ATG_FIX_KEY + "." + in_group, "");
169         }
170     }
171
172     public static String JavaDoc getSelectedTabNameFormCookie(HttpServletRequest JavaDoc in_request, String JavaDoc in_keyName) {
173         Cookie JavaDoc[] lc_cookies = in_request.getCookies();
174         LOG.debug("Looking for tabs cookie with key=" + in_keyName);
175         
176         if (isATGFix(in_request, in_keyName)) {
177             // ATG bug quirck
178
LOG.debug("Ignoring buggy tab cookie value");
179             return null;
180         }
181         
182         if (lc_cookies==null) {
183             // no cookie at all..
184
LOG.debug("No cookie");
185             return null;
186         }
187                 
188         for (int i=0;i<lc_cookies.length;i++) {
189             Cookie JavaDoc lc_cookie = lc_cookies[i];
190             if (lc_cookie.getName().equals("selectedTab")) {
191                 String JavaDoc lc_value = lc_cookie.getValue();
192                 lc_value = URLDecoder.decode(lc_value);
193                 if (lc_value==null) {
194                     LOG.debug("No cookie value");
195                     return null;
196                 }
197                 LOG.debug("Cookie value=" + lc_value);
198                 int lc_keyNameStart = 0;
199                 String JavaDoc lc_keyName = null;
200                 int lc_keyValueStart = -1;
201                 String JavaDoc lc_keyValue = null;
202                 for (int j=0; j < lc_value.length(); j++) {
203                     switch (lc_value.charAt(j)) {
204                         case '=':
205                             if (lc_keyNameStart!=-1) {
206                                 // We found a key.
207
lc_keyName = lc_value.substring(lc_keyNameStart, j).trim();
208                                 lc_keyValueStart = j+1;
209                             }
210                             break;
211                         case ';':
212                             if (lc_keyName!=null) {
213                                 // We found a key/value pair !
214
lc_keyValue = lc_value.substring(lc_keyValueStart, j).trim();
215                                 //System.out.println("found : " + lc_keyName + " = " + lc_keyValue);
216
if (in_keyName.equals(lc_keyName)) {
217                                     return lc_keyValue;
218                                 }
219                             }
220                             lc_keyName = null;
221                             lc_keyValue = null;
222                             lc_keyNameStart = j+1;
223                             lc_keyValueStart = -1;
224                             break;
225                     }
226                 }
227             }
228         }
229         return null;
230     }
231     
232     public static void setSelectedTabNameFromCookie(HttpServletRequest JavaDoc in_request, HttpServletResponse JavaDoc in_response, String JavaDoc in_group, String JavaDoc in_keyName) {
233         // Get cookie.
234
Cookie JavaDoc[] lc_cookies = in_request.getCookies();
235         Cookie JavaDoc lc_oldCookie = null;
236         Map JavaDoc lc_tabsInformation = new HashMap JavaDoc();
237         if (lc_cookies!=null) {
238             for (int i=0;i<lc_cookies.length;i++) {
239                 Cookie JavaDoc lc_cookie = lc_cookies[i];
240                 if (lc_cookie.getName().equals("selectedTab")) {
241                     String JavaDoc lc_value = lc_cookie.getValue();
242                     StringTokenizer JavaDoc lc_tokenizer = new StringTokenizer JavaDoc(URLDecoder.decode(lc_value), ";=");
243                     while (lc_tokenizer. hasMoreTokens()) {
244                         String JavaDoc lc_keyName = lc_tokenizer.nextToken();
245                         String JavaDoc lc_keyValue = null;
246                         if (lc_tokenizer.hasMoreTokens()) {
247                             lc_keyValue = lc_tokenizer.nextToken();
248                         }
249                         lc_tabsInformation.put(lc_keyName, lc_keyValue);
250                     }
251                     lc_oldCookie = lc_cookie;
252                     break;
253                 }
254             }
255         }
256         lc_tabsInformation.put(in_group, in_keyName);
257         StringBuffer JavaDoc lc_value = new StringBuffer JavaDoc();
258         Iterator JavaDoc lc_it = lc_tabsInformation.keySet().iterator();
259         while (lc_it.hasNext()) {
260             String JavaDoc lc_tabName = (String JavaDoc) lc_it.next();
261             String JavaDoc lc_tabValue = (String JavaDoc) lc_tabsInformation.get(lc_tabName);
262             lc_value.append(lc_tabName).append('=').append(lc_tabValue).append(';');
263         }
264         String JavaDoc lc_newCookieValue = URLEncoder.encode(lc_value.toString());
265         Cookie JavaDoc lc_cookie = new Cookie JavaDoc("selectedTab", lc_newCookieValue);
266         lc_cookie.setPath(in_request.getContextPath());
267         if (lc_oldCookie!=null) {
268             // update old cookie : it will be used by the jsp.
269
// note that this doesn't work in ATG Dynamo, so we also say for this server to ignore the cookie value.
270
lc_oldCookie.setValue(lc_newCookieValue);
271             setATGFix(in_request, in_group);
272         }
273         in_response.addCookie(lc_cookie);
274     }
275     
276     public int doEndLayoutTag() throws JspException JavaDoc {
277         if (include) {
278             TagUtils.write(pageContext, "</table></div>");
279         }
280         if (staticCode.length()!=0) {
281             TagUtils.write(pageContext, staticCode);
282             staticCode = "";
283         }
284         return EVAL_PAGE;
285     }
286     
287     public void release() {
288         super.release();
289         width = null;
290         href = null;
291         forward = null;
292         page = null;
293         reqCode = null;
294     }
295     
296     /**
297      * Deal with StartLayoutEvents.
298      */

299     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
300         return in_event.consume(pageContext, "<tr>");
301     }
302     /**
303      * Deal with EndLayoutEvents.
304      */

305     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
306         return in_event.consume(pageContext, "</tr>");
307     }
308     
309     /**
310      * Use to print the calendar static code in a visible layer.
311      */

312     public Object JavaDoc processStaticCodeIncludeEvent(StaticCodeIncludeLayoutEvent in_event) throws JspException JavaDoc {
313         staticCode += in_event.sendToParent(this);
314         return "";
315     }
316     
317
318     /**
319      * Returns the width.
320      * @return String
321      */

322     public String JavaDoc getWidth() {
323         return width;
324     }
325
326     /**
327      * Sets the width.
328      * @param width The width to set
329      */

330     public void setWidth(String JavaDoc width) {
331         this.width = width;
332     }
333
334     /**
335      * Returns the forward.
336      * @return String
337      */

338     public String JavaDoc getForward() {
339         return forward;
340     }
341
342     /**
343      * Returns the href.
344      * @return String
345      */

346     public String JavaDoc getHref() {
347         return href;
348     }
349
350     /**
351      * Sets the forward.
352      * @param forward The forward to set
353      */

354     public void setForward(String JavaDoc forward) {
355         this.forward = forward;
356     }
357
358     /**
359      * Sets the href.
360      * @param href The href to set
361      */

362     public void setHref(String JavaDoc href) {
363         this.href = href;
364     }
365
366     /**
367      * Returns the page.
368      * @return String
369      */

370     public String JavaDoc getPage() {
371         return page;
372     }
373
374     /**
375      * Sets the page.
376      * @param page The page to set
377      */

378     public void setPage(String JavaDoc page) {
379         this.page = page;
380     }
381
382     public String JavaDoc getReqCode() {
383         return reqCode;
384     }
385     public void setReqCode(String JavaDoc reqCode) {
386         this.reqCode = reqCode;
387     }
388 }
389
Popular Tags