1 61 62 package org.objectweb.jonas.webapp.taglib; 63 64 import java.io.IOException ; 65 import java.net.URLEncoder ; 66 67 import javax.servlet.http.HttpServletResponse ; 68 import javax.servlet.jsp.JspException ; 69 import javax.servlet.jsp.JspWriter ; 70 import javax.servlet.jsp.PageContext ; 71 import javax.servlet.jsp.tagext.TagSupport ; 72 73 74 108 109 public class TreeControlTag extends TagSupport { 110 111 114 static final String DEFAULT_IMAGES = "images"; 115 116 119 static final String IMAGE_HANDLE_DOWN_LAST = "handledownlast.gif"; 120 static final String IMAGE_HANDLE_DOWN_MIDDLE = "handledownmiddle.gif"; 121 static final String IMAGE_HANDLE_RIGHT_LAST = "handlerightlast.gif"; 122 static final String IMAGE_HANDLE_RIGHT_MIDDLE = "handlerightmiddle.gif"; 123 static final String IMAGE_LINE_LAST = "linelastnode.gif"; 124 static final String IMAGE_LINE_MIDDLE = "linemiddlenode.gif"; 125 static final String IMAGE_LINE_VERTICAL = "linevertical.gif"; 126 127 129 135 protected String action = null; 136 137 public String getAction() { 138 return (this.action); 139 } 140 141 public void setAction(String action) { 142 this.action = action; 143 } 144 145 149 protected String images = DEFAULT_IMAGES; 150 151 public String getImages() { 152 return (this.images); 153 } 154 155 public void setImages(String images) { 156 this.images = images; 157 } 158 159 164 protected String scope = null; 165 166 public String getScope() { 167 return (this.scope); 168 } 169 170 public void setScope(String scope) { 171 if (!"page".equals(scope) && !"request".equals(scope) && !"session".equals(scope) 172 && !"application".equals(scope)) { 173 throw new IllegalArgumentException ("Invalid scope '" + scope + "'"); 174 } 175 this.scope = scope; 176 } 177 178 181 protected String style = null; 182 183 public String getStyle() { 184 return (this.style); 185 } 186 187 public void setStyle(String style) { 188 this.style = style; 189 } 190 191 195 protected String styleSelected = null; 196 197 public String getStyleSelected() { 198 return (this.styleSelected); 199 } 200 201 public void setStyleSelected(String styleSelected) { 202 this.styleSelected = styleSelected; 203 } 204 205 209 protected String styleUnselected = null; 210 211 public String getStyleUnselected() { 212 return (this.styleUnselected); 213 } 214 215 public void setStyleUnselected(String styleUnselected) { 216 this.styleUnselected = styleUnselected; 217 } 218 219 223 protected String tree = null; 224 225 public String getTree() { 226 return (this.tree); 227 } 228 229 public void setTree(String tree) { 230 this.tree = tree; 231 } 232 233 235 240 public int doEndTag() 241 throws JspException { 242 243 TreeControl treeControl = getTreeControl(); 244 JspWriter out = pageContext.getOut(); 245 try { 246 out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\""); 247 if (style != null) { 248 out.print(" class=\""); 249 out.print(style); 250 out.print("\""); 251 } 252 out.println(">"); 253 int level = 0; 254 TreeControlNode node = treeControl.getRoot(); 255 render(out, node, level, treeControl.getWidth(), true); 256 out.println("</table>"); 257 } 258 catch (IOException e) { 259 throw new JspException (e); 260 } 261 262 return (EVAL_PAGE); 263 264 } 265 266 269 public void release() { 270 271 this.action = null; 272 this.images = DEFAULT_IMAGES; 273 this.scope = null; 274 this.style = null; 275 this.styleSelected = null; 276 this.styleUnselected = null; 277 this.tree = null; 278 279 } 280 281 283 289 protected TreeControl getTreeControl() 290 throws JspException { 291 292 Object treeControl = null; 293 if (scope == null) { 294 treeControl = pageContext.findAttribute(tree); 295 } 296 else if ("page".equals(scope)) { 297 treeControl = pageContext.getAttribute(tree, PageContext.PAGE_SCOPE); 298 } 299 else if ("request".equals(scope)) { 300 treeControl = pageContext.getAttribute(tree, PageContext.REQUEST_SCOPE); 301 } 302 else if ("session".equals(scope)) { 303 treeControl = pageContext.getAttribute(tree, PageContext.SESSION_SCOPE); 304 } 305 else if ("application".equals(scope)) { 306 treeControl = pageContext.getAttribute(tree, PageContext.APPLICATION_SCOPE); 307 } 308 if (treeControl == null) { 309 throw new JspException ("Cannot find tree control attribute '" + tree + "'"); 310 } 311 else if (!(treeControl instanceof TreeControl)) { 312 throw new JspException ("Invalid tree control attribute '" + tree + "'"); 313 } 314 else { 315 return ((TreeControl) treeControl); 316 } 317 318 } 319 320 332 protected void render(JspWriter out, TreeControlNode node, int level, int width, boolean last) 333 throws IOException { 334 335 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 336 337 340 if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && (node.getLabel() == null)) { 341 TreeControlNode children[] = node.findChildren(); 343 int lastIndex = children.length - 1; 344 int newLevel = level + 1; 345 for (int i = 0; i < children.length; i++) { 346 render(out, children[i], newLevel, width, i == lastIndex); 347 } 348 return; 349 } 350 351 out.println(" <tr valign=\"middle\">"); 353 354 for (int i = 0; i < level; i++) { 356 int levels = level - i; 357 TreeControlNode parent = node; 358 for (int j = 1; j <= levels; j++) { 359 parent = parent.getParent(); 360 } 361 if (parent.isLast()) { 362 out.print(" <td></td>"); 363 } 364 else { 365 out.print(" <td><img SRC=\""); 366 out.print(images); 367 out.print("/"); 368 out.print(IMAGE_LINE_VERTICAL); 369 out.print("\" border=\"0\"></td>"); 370 } 371 out.println(); 372 } 373 374 376 String encodedNodeName = URLEncoder.encode(node.getName(),"UTF-8"); 381 382 String action = replace(getAction(), "${name}", encodedNodeName); 383 384 String updateTreeAction = replace(getAction(), "tree=${name}", "select=" + encodedNodeName); 385 updateTreeAction = ((HttpServletResponse ) pageContext.getResponse()).encodeURL( 386 updateTreeAction); 387 388 out.print(" <td>"); 389 if ((action != null) && !node.isLeaf()) { 390 out.print("<a HREF=\""); 391 out.print(response.encodeURL(action)); 392 out.print("\">"); 393 } 394 out.print("<img SRC=\""); 395 out.print(images); 396 out.print("/"); 397 if (node.isLeaf()) { 398 if (node.isLast()) { 399 out.print(IMAGE_LINE_LAST); 400 } 401 else { 402 out.print(IMAGE_LINE_MIDDLE); 403 } 404 } 405 else if (node.isExpanded()) { 406 if (node.isLast()) { 407 out.print(IMAGE_HANDLE_DOWN_LAST); 408 } 409 else { 410 out.print(IMAGE_HANDLE_DOWN_MIDDLE); 411 } 412 } 413 else { 414 if (node.isLast()) { 415 out.print(IMAGE_HANDLE_RIGHT_LAST); 416 } 417 else { 418 out.print(IMAGE_HANDLE_RIGHT_MIDDLE); 419 } 420 } 421 out.print("\" border=\"0\">"); 422 if ((action != null) && !node.isLeaf()) { 423 out.print("</a>"); 424 } 425 out.println("</td>"); 426 427 String hyperlink = null; 429 if (node.getAction() != null) { 430 hyperlink = ((HttpServletResponse ) pageContext.getResponse()).encodeURL(node.getAction()); 431 432 } 434 out.print(" <td colspan=\""); 435 out.print(width - level + 1); 436 out.print("\">"); 437 if (node.getIcon() != null) { 438 if (hyperlink != null) { 439 out.print("<a HREF=\""); 440 out.print(hyperlink); 441 out.print("\""); 442 String target = node.getTarget(); 443 if (target != null) { 444 out.print(" target=\""); 445 out.print(target); 446 out.print("\""); 447 } 448 out.print(" onclick=\""); 450 out.print("self.location.href='" + updateTreeAction + "'"); 451 out.print("\""); 452 out.print(">"); 453 } 454 out.print("<img SRC=\""); 455 out.print(images); 456 out.print("/"); 457 out.print(node.getIcon()); 458 out.print("\" border=\"0\">"); 459 if (hyperlink != null) { 460 out.print("</a>"); 461 } 462 } 463 464 466 if (node.getLabel() != null) { 467 String labelStyle = null; 468 if (node.isSelected() && (styleSelected != null)) { 469 labelStyle = styleSelected; 470 } 471 else if (!node.isSelected() && (styleUnselected != null)) { 472 labelStyle = styleUnselected; 473 } 474 if (hyperlink != null) { 475 out.print(" <a HREF=\""); 478 out.print(hyperlink); 479 out.print("\""); 480 String target = node.getTarget(); 481 if (target != null) { 482 out.print(" target=\""); 483 out.print(target); 484 out.print("\""); 485 } 486 if (labelStyle != null) { 487 out.print(" class=\""); 488 out.print(labelStyle); 489 out.print("\""); 490 } 491 out.print(" onclick=\""); 493 out.print("self.location.href='" + updateTreeAction + "'"); 494 out.print("\""); 495 out.print(">"); 496 } 497 else if (labelStyle != null) { 498 out.print("<span class=\""); 499 out.print(labelStyle); 500 out.print("\">"); 501 } 502 out.print(node.getLabel()); 503 if (hyperlink != null) { 504 out.print("</a>"); 505 } 506 else if (labelStyle != null) { 507 out.print("</span>"); 508 } 509 } 510 out.println("</td>"); 511 512 out.println(" </tr>"); 514 515 if (node.isExpanded()) { 517 TreeControlNode children[] = node.findChildren(); 518 int lastIndex = children.length - 1; 519 int newLevel = level + 1; 520 for (int i = 0; i < children.length; i++) { 521 render(out, children[i], newLevel, width, i == lastIndex); 522 } 523 } 524 525 } 526 527 535 protected String replace(String template, String placeholder, String value) { 536 537 if (template == null) { 538 return (null); 539 } 540 if ((placeholder == null) || (value == null)) { 541 return (template); 542 } 543 while (true) { 544 int index = template.indexOf(placeholder); 545 if (index < 0) { 546 break; 547 } 548 StringBuffer temp = new StringBuffer (template.substring(0, index)); 549 temp.append(value); 550 temp.append(template.substring(index + placeholder.length())); 551 template = temp.toString(); 552 } 553 return (template); 554 555 } 556 557 } 558 | Popular Tags |