1 16 17 18 package org.apache.webapp.admin; 19 20 21 import java.io.IOException ; 22 import java.net.URLEncoder ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspWriter ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 29 30 64 65 public class TreeControlTag extends TagSupport { 66 67 68 71 static final String DEFAULT_IMAGES = "images"; 72 73 74 77 static final String IMAGE_HANDLE_DOWN_LAST = "handledownlast.gif"; 78 static final String IMAGE_HANDLE_DOWN_MIDDLE = "handledownmiddle.gif"; 79 static final String IMAGE_HANDLE_RIGHT_LAST = "handlerightlast.gif"; 80 static final String IMAGE_HANDLE_RIGHT_MIDDLE = "handlerightmiddle.gif"; 81 static final String IMAGE_LINE_LAST = "linelastnode.gif"; 82 static final String IMAGE_LINE_MIDDLE = "linemiddlenode.gif"; 83 static final String IMAGE_LINE_VERTICAL = "linevertical.gif"; 84 85 86 88 89 95 protected String action = null; 96 97 public String getAction() { 98 return (this.action); 99 } 100 101 public void setAction(String action) { 102 this.action = action; 103 } 104 105 106 110 protected String images = DEFAULT_IMAGES; 111 112 public String getImages() { 113 return (this.images); 114 } 115 116 public void setImages(String images) { 117 this.images = images; 118 } 119 120 121 126 protected String scope = null; 127 128 public String getScope() { 129 return (this.scope); 130 } 131 132 public void setScope(String scope) { 133 if (!"page".equals(scope) && 134 !"request".equals(scope) && 135 !"session".equals(scope) && 136 !"application".equals(scope)) 137 throw new IllegalArgumentException ("Invalid scope '" + 138 scope + "'"); 139 this.scope = scope; 140 } 141 142 143 146 protected String style = null; 147 148 public String getStyle() { 149 return (this.style); 150 } 151 152 public void setStyle(String style) { 153 this.style = style; 154 } 155 156 157 161 protected String styleSelected = null; 162 163 public String getStyleSelected() { 164 return (this.styleSelected); 165 } 166 167 public void setStyleSelected(String styleSelected) { 168 this.styleSelected = styleSelected; 169 } 170 171 172 176 protected String styleUnselected = null; 177 178 public String getStyleUnselected() { 179 return (this.styleUnselected); 180 } 181 182 public void setStyleUnselected(String styleUnselected) { 183 this.styleUnselected = styleUnselected; 184 } 185 186 187 191 protected String tree = null; 192 193 public String getTree() { 194 return (this.tree); 195 } 196 197 public void setTree(String tree) { 198 this.tree = tree; 199 } 200 201 202 204 205 210 public int doEndTag() throws JspException { 211 212 TreeControl treeControl = getTreeControl(); 213 JspWriter out = pageContext.getOut(); 214 try { 215 out.print 216 ("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\""); 217 if (style != null) { 218 out.print(" class=\""); 219 out.print(style); 220 out.print("\""); 221 } 222 out.println(">"); 223 int level = 0; 224 TreeControlNode node = treeControl.getRoot(); 225 render(out, node, level, treeControl.getWidth(), true); 226 out.println("</table>"); 227 } catch (IOException e) { 228 throw new JspException (e); 229 } 230 231 return (EVAL_PAGE); 232 233 } 234 235 236 239 public void release() { 240 241 this.action = null; 242 this.images = DEFAULT_IMAGES; 243 this.scope = null; 244 this.style = null; 245 this.styleSelected = null; 246 this.styleUnselected = null; 247 this.tree = null; 248 249 } 250 251 252 254 255 261 protected TreeControl getTreeControl() throws JspException { 262 263 Object treeControl = null; 264 if (scope == null) 265 treeControl = pageContext.findAttribute(tree); 266 else if ("page".equals(scope)) 267 treeControl = 268 pageContext.getAttribute(tree, PageContext.PAGE_SCOPE); 269 else if ("request".equals(scope)) 270 treeControl = 271 pageContext.getAttribute(tree, PageContext.REQUEST_SCOPE); 272 else if ("session".equals(scope)) 273 treeControl = 274 pageContext.getAttribute(tree, PageContext.SESSION_SCOPE); 275 else if ("application".equals(scope)) 276 treeControl = 277 pageContext.getAttribute(tree, PageContext.APPLICATION_SCOPE); 278 if (treeControl == null) 279 throw new JspException ("Cannot find tree control attribute '" + 280 tree + "'"); 281 else if (!(treeControl instanceof TreeControl)) 282 throw new JspException ("Invalid tree control attribute '" + 283 tree + "'"); 284 else 285 return ((TreeControl) treeControl); 286 287 } 288 289 290 302 protected void render(JspWriter out, TreeControlNode node, 303 int level, int width, boolean last) 304 throws IOException { 305 306 HttpServletResponse response = 307 (HttpServletResponse ) pageContext.getResponse(); 308 309 312 if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && 313 (node.getLabel() == null)) { 314 TreeControlNode children[] = node.findChildren(); 316 int lastIndex = children.length - 1; 317 int newLevel = level + 1; 318 for (int i = 0; i < children.length; i++) { 319 render(out, children[i], newLevel, width, i == lastIndex); 320 } 321 return; 322 } 323 324 out.println(" <tr valign=\"middle\">"); 326 327 for (int i = 0; i < level; i++) { 329 int levels = level - i; 330 TreeControlNode parent = node; 331 for (int j = 1; j <= levels; j++) 332 parent = parent.getParent(); 333 if (parent.isLast()) 334 out.print(" <td></td>"); 335 else { 336 out.print(" <td><img SRC=\""); 337 out.print(images); 338 out.print("/"); 339 out.print(IMAGE_LINE_VERTICAL); 340 out.print("\" alt=\"\" border=\"0\"></td>"); 341 } 342 out.println(); 343 } 344 345 347 String encodedNodeName = URLEncoder.encode(node.getName(),TomcatTreeBuilder.URL_ENCODING); 352 353 String action = replace(getAction(), "{name}", encodedNodeName); 354 355 356 String updateTreeAction = 357 replace(getAction(), "tree={name}", "select=" + encodedNodeName); 358 updateTreeAction = 359 ((HttpServletResponse ) pageContext.getResponse()). 360 encodeURL(updateTreeAction); 361 362 out.print(" <td>"); 363 if ((action != null) && !node.isLeaf()) { 364 out.print("<a HREF=\""); 365 out.print(response.encodeURL(action)); 366 out.print("\">"); 367 } 368 out.print("<img SRC=\""); 369 out.print(images); 370 out.print("/"); 371 if (node.isLeaf()) { 372 if (node.isLast()) 373 out.print(IMAGE_LINE_LAST); 374 else 375 out.print(IMAGE_LINE_MIDDLE); 376 out.print("\" alt=\""); 377 } else if (node.isExpanded()) { 378 if (node.isLast()) 379 out.print(IMAGE_HANDLE_DOWN_LAST); 380 else 381 out.print(IMAGE_HANDLE_DOWN_MIDDLE); 382 out.print("\" alt=\"close node"); 383 } else { 384 if (node.isLast()) 385 out.print(IMAGE_HANDLE_RIGHT_LAST); 386 else 387 out.print(IMAGE_HANDLE_RIGHT_MIDDLE); 388 out.print("\" alt=\"expand node"); 389 } 390 out.print("\" border=\"0\">"); 391 if ((action != null) && !node.isLeaf()) 392 out.print("</a>"); 393 out.println("</td>"); 394 395 String hyperlink = null; 397 if (node.getAction() != null) 398 hyperlink = ((HttpServletResponse ) pageContext.getResponse()). 399 encodeURL(node.getAction()); 400 401 out.print(" <td colspan=\""); 403 out.print(width - level + 1); 404 out.print("\">"); 405 if (node.getIcon() != null) { 406 if (hyperlink != null) { 407 out.print("<a HREF=\""); 408 out.print(hyperlink); 409 out.print("\""); 410 String target = node.getTarget(); 411 if(target != null) { 412 out.print(" target=\""); 413 out.print(target); 414 out.print("\""); 415 } 416 out.print(" onclick=\""); 418 out.print("self.location.href='" + updateTreeAction + "'"); 419 out.print("\""); 420 out.print(">"); 421 } 422 out.print("<img SRC=\""); 423 out.print(images); 424 out.print("/"); 425 out.print(node.getIcon()); 426 out.print("\" alt=\""); 427 out.print("\" border=\"0\">"); 428 if (hyperlink != null) 429 out.print("</a>"); 430 } 431 432 434 if (node.getLabel() != null) { 435 String labelStyle = null; 436 if (node.isSelected() && (styleSelected != null)) 437 labelStyle = styleSelected; 438 else if (!node.isSelected() && (styleUnselected != null)) 439 labelStyle = styleUnselected; 440 if (hyperlink != null) { 441 out.print(" <a HREF=\""); 444 out.print(hyperlink); 445 out.print("\""); 446 String target = node.getTarget(); 447 if(target != null) { 448 out.print(" target=\""); 449 out.print(target); 450 out.print("\""); 451 } 452 if (labelStyle != null) { 453 out.print(" class=\""); 454 out.print(labelStyle); 455 out.print("\""); 456 } 457 out.print(" onclick=\""); 459 out.print("self.location.href='" + updateTreeAction + "'"); 460 out.print("\""); 461 out.print(">"); 462 } else if (labelStyle != null) { 463 out.print("<span class=\""); 464 out.print(labelStyle); 465 out.print("\">"); 466 } 467 out.print(node.getLabel()); 468 if (hyperlink != null) 469 out.print("</a>"); 470 else if (labelStyle != null) 471 out.print("</span>"); 472 } 473 out.println("</td>"); 474 475 out.println(" </tr>"); 477 478 if (node.isExpanded()) { 480 TreeControlNode children[] = node.findChildren(); 481 int lastIndex = children.length - 1; 482 int newLevel = level + 1; 483 for (int i = 0; i < children.length; i++) { 484 render(out, children[i], newLevel, width, i == lastIndex); 485 } 486 } 487 488 } 489 490 491 499 protected String replace(String template, String placeholder, 500 String value) { 501 502 if (template == null) 503 return (null); 504 if ((placeholder == null) || (value == null)) 505 return (template); 506 while (true) { 507 int index = template.indexOf(placeholder); 508 if (index < 0) 509 break; 510 StringBuffer temp = new StringBuffer (template.substring(0, index)); 511 temp.append(value); 512 temp.append(template.substring(index + placeholder.length())); 513 template = temp.toString(); 514 } 515 return (template); 516 517 } 518 519 520 } 521 | Popular Tags |