1 package fr.improve.struts.taglib.layout.tab; 2 3 import java.net.URLDecoder ; 4 import java.net.URLEncoder ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 import java.util.StringTokenizer ; 9 10 import javax.servlet.http.Cookie ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 import javax.servlet.jsp.JspException ; 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 36 public class TabTag extends LabelledTag implements LayoutEventListener, StaticCodeIncludeListener { 37 40 public static final String ATG_FIX_KEY = "fr.improve.struts.taglib.layout.tab.TabTag.ATG_FIX_KEY"; 41 42 45 protected static final Log LOG = LogFactory.getLog(TabsTag.class); 46 47 50 private String width; 51 52 55 private String href; 56 private String forward; 57 private String page; 58 59 62 private String reqCode; 63 64 private boolean include; 65 66 private String staticCode = ""; 67 68 public int doStartLayoutTag() throws JspException { 69 TabsTag lc_tabs = (TabsTag) findAncestorWithClass(this, TabsTag.class); 70 if (lc_tabs==null) { 71 throw new JspException ("Invalid use of <layout:tab>"); 72 } 73 74 include = false; 76 77 String lc_href = null; 78 if (href!=null || forward!=null || page!=null) { 79 lc_href = LayoutUtils.computeURL(pageContext, forward, Expression.evaluate(href, pageContext), page, null, null, null, null, false, null); 82 } else if (reqCode!=null) { 83 } else { 86 include = true; 90 } 91 92 boolean lc_selected = isTabSelected(lc_tabs); 94 if (lc_selected) { 95 include = true; 96 } 97 String 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 116 protected boolean isTabSelected(TabsTag lc_tabs) { 117 boolean lc_selected = !lc_tabs.isTabAlreadySelected(); 119 120 if (lc_selected && lc_tabs.getSelectedTabKeyName()!=null) { 122 String lc_selectedTab; 123 LOG.debug("Looking for the selection of tab " + key + " in tabs " + lc_tabs.getSelectedTabKeyName()); 124 125 lc_selectedTab = getSelectedTabNameFormCookie((HttpServletRequest )pageContext.getRequest(), lc_tabs.getSelectedTabKeyName()); 127 if (lc_selectedTab!=null && LOG.isDebugEnabled()) { 128 LOG.debug("Selected tab from cookie=" + lc_selectedTab); 129 } 130 131 if (lc_selectedTab==null) { 133 lc_selectedTab = (String ) 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 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 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 in_request, String in_group) { 163 return in_request.getAttribute(ATG_FIX_KEY + "." + in_group)!=null; 164 } 165 private static void setATGFix(HttpServletRequest in_request, String 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 getSelectedTabNameFormCookie(HttpServletRequest in_request, String in_keyName) { 173 Cookie [] 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 LOG.debug("Ignoring buggy tab cookie value"); 179 return null; 180 } 181 182 if (lc_cookies==null) { 183 LOG.debug("No cookie"); 185 return null; 186 } 187 188 for (int i=0;i<lc_cookies.length;i++) { 189 Cookie lc_cookie = lc_cookies[i]; 190 if (lc_cookie.getName().equals("selectedTab")) { 191 String 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 lc_keyName = null; 200 int lc_keyValueStart = -1; 201 String 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 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 lc_keyValue = lc_value.substring(lc_keyValueStart, j).trim(); 215 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 in_request, HttpServletResponse in_response, String in_group, String in_keyName) { 233 Cookie [] lc_cookies = in_request.getCookies(); 235 Cookie lc_oldCookie = null; 236 Map lc_tabsInformation = new HashMap (); 237 if (lc_cookies!=null) { 238 for (int i=0;i<lc_cookies.length;i++) { 239 Cookie lc_cookie = lc_cookies[i]; 240 if (lc_cookie.getName().equals("selectedTab")) { 241 String lc_value = lc_cookie.getValue(); 242 StringTokenizer lc_tokenizer = new StringTokenizer (URLDecoder.decode(lc_value), ";="); 243 while (lc_tokenizer. hasMoreTokens()) { 244 String lc_keyName = lc_tokenizer.nextToken(); 245 String 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 lc_value = new StringBuffer (); 258 Iterator lc_it = lc_tabsInformation.keySet().iterator(); 259 while (lc_it.hasNext()) { 260 String lc_tabName = (String ) lc_it.next(); 261 String lc_tabValue = (String ) lc_tabsInformation.get(lc_tabName); 262 lc_value.append(lc_tabName).append('=').append(lc_tabValue).append(';'); 263 } 264 String lc_newCookieValue = URLEncoder.encode(lc_value.toString()); 265 Cookie lc_cookie = new Cookie ("selectedTab", lc_newCookieValue); 266 lc_cookie.setPath(in_request.getContextPath()); 267 if (lc_oldCookie!=null) { 268 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 { 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 299 public Object processStartLayoutEvent(StartLayoutEvent in_event) throws JspException { 300 return in_event.consume(pageContext, "<tr>"); 301 } 302 305 public Object processEndLayoutEvent(EndLayoutEvent in_event) throws JspException { 306 return in_event.consume(pageContext, "</tr>"); 307 } 308 309 312 public Object processStaticCodeIncludeEvent(StaticCodeIncludeLayoutEvent in_event) throws JspException { 313 staticCode += in_event.sendToParent(this); 314 return ""; 315 } 316 317 318 322 public String getWidth() { 323 return width; 324 } 325 326 330 public void setWidth(String width) { 331 this.width = width; 332 } 333 334 338 public String getForward() { 339 return forward; 340 } 341 342 346 public String getHref() { 347 return href; 348 } 349 350 354 public void setForward(String forward) { 355 this.forward = forward; 356 } 357 358 362 public void setHref(String href) { 363 this.href = href; 364 } 365 366 370 public String getPage() { 371 return page; 372 } 373 374 378 public void setPage(String page) { 379 this.page = page; 380 } 381 382 public String getReqCode() { 383 return reqCode; 384 } 385 public void setReqCode(String reqCode) { 386 this.reqCode = reqCode; 387 } 388 } 389 | Popular Tags |