1 61 62 63 package org.nextime.ion.backoffice.tree; 64 65 66 import java.io.IOException ; 67 import java.net.URLEncoder ; 68 import javax.servlet.http.HttpServletResponse ; 69 import javax.servlet.jsp.JspException ; 70 import javax.servlet.jsp.JspWriter ; 71 import javax.servlet.jsp.PageContext ; 72 import javax.servlet.jsp.tagext.TagSupport ; 73 74 75 109 110 public class TreeControlTag extends TagSupport { 111 112 113 116 static final String DEFAULT_IMAGES = "images"; 117 118 119 122 static final String IMAGE_HANDLE_DOWN_LAST = "handledownlast.gif"; 123 static final String IMAGE_HANDLE_DOWN_MIDDLE = "handledownmiddle.gif"; 124 static final String IMAGE_HANDLE_RIGHT_LAST = "handlerightlast.gif"; 125 static final String IMAGE_HANDLE_RIGHT_MIDDLE = "handlerightmiddle.gif"; 126 static final String IMAGE_LINE_LAST = "linelastnode.gif"; 127 static final String IMAGE_LINE_MIDDLE = "linemiddlenode.gif"; 128 static final String IMAGE_LINE_VERTICAL = "linevertical.gif"; 129 130 131 133 134 140 protected String action = null; 141 142 public String getAction() { 143 return (this.action); 144 } 145 146 public void setAction(String action) { 147 this.action = action; 148 } 149 150 151 155 protected String images = DEFAULT_IMAGES; 156 157 public String getImages() { 158 return (this.images); 159 } 160 161 public void setImages(String images) { 162 this.images = images; 163 } 164 165 166 171 protected String scope = null; 172 173 public String getScope() { 174 return (this.scope); 175 } 176 177 public void setScope(String scope) { 178 if (!"page".equals(scope) && 179 !"request".equals(scope) && 180 !"session".equals(scope) && 181 !"application".equals(scope)) 182 throw new IllegalArgumentException ("Invalid scope '" + 183 scope + "'"); 184 this.scope = scope; 185 } 186 187 188 191 protected String style = null; 192 193 public String getStyle() { 194 return (this.style); 195 } 196 197 public void setStyle(String style) { 198 this.style = style; 199 } 200 201 202 206 protected String styleSelected = null; 207 208 public String getStyleSelected() { 209 return (this.styleSelected); 210 } 211 212 public void setStyleSelected(String styleSelected) { 213 this.styleSelected = styleSelected; 214 } 215 216 217 221 protected String styleUnselected = null; 222 223 public String getStyleUnselected() { 224 return (this.styleUnselected); 225 } 226 227 public void setStyleUnselected(String styleUnselected) { 228 this.styleUnselected = styleUnselected; 229 } 230 231 232 236 protected String tree = null; 237 238 public String getTree() { 239 return (this.tree); 240 } 241 242 public void setTree(String tree) { 243 this.tree = tree; 244 } 245 246 247 249 250 255 public int doEndTag() throws JspException { 256 257 TreeControl treeControl = getTreeControl(); 258 JspWriter out = pageContext.getOut(); 259 try { 260 out.print 261 ("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\""); 262 if (style != null) { 263 out.print(" class=\""); 264 out.print(style); 265 out.print("\""); 266 } 267 out.println(">"); 268 int level = 0; 269 TreeControlNode node = treeControl.getRoot(); 270 render(out, node, level, treeControl.getWidth(), true); 271 out.println("</table>"); 272 } catch (IOException e) { 273 throw new JspException (e); 274 } 275 276 return (EVAL_PAGE); 277 278 } 279 280 281 284 public void release() { 285 286 this.action = null; 287 this.images = DEFAULT_IMAGES; 288 this.scope = null; 289 this.style = null; 290 this.styleSelected = null; 291 this.styleUnselected = null; 292 this.tree = null; 293 294 } 295 296 297 299 300 306 protected TreeControl getTreeControl() throws JspException { 307 308 Object treeControl = null; 309 if (scope == null) 310 treeControl = pageContext.findAttribute(tree); 311 else if ("page".equals(scope)) 312 treeControl = 313 pageContext.getAttribute(tree, PageContext.PAGE_SCOPE); 314 else if ("request".equals(scope)) 315 treeControl = 316 pageContext.getAttribute(tree, PageContext.REQUEST_SCOPE); 317 else if ("session".equals(scope)) 318 treeControl = 319 pageContext.getAttribute(tree, PageContext.SESSION_SCOPE); 320 else if ("application".equals(scope)) 321 treeControl = 322 pageContext.getAttribute(tree, PageContext.APPLICATION_SCOPE); 323 if (treeControl == null) 324 throw new JspException ("Cannot find tree control attribute '" + 325 tree + "'"); 326 else if (!(treeControl instanceof TreeControl)) 327 throw new JspException ("Invalid tree control attribute '" + 328 tree + "'"); 329 else 330 return ((TreeControl) treeControl); 331 332 } 333 334 335 347 protected void render(JspWriter out, TreeControlNode node, 348 int level, int width, boolean last) 349 throws IOException { 350 351 HttpServletResponse response = 352 (HttpServletResponse ) pageContext.getResponse(); 353 354 357 if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && 358 (node.getLabel() == null)) { 359 TreeControlNode children[] = node.findChildren(); 361 int lastIndex = children.length - 1; 362 int newLevel = level + 1; 363 for (int i = 0; i < children.length; i++) { 364 render(out, children[i], newLevel, width, i == lastIndex); 365 } 366 return; 367 } 368 369 out.println(" <tr valign=\"middle\">"); 371 372 for (int i = 0; i < level; i++) { 374 int levels = level - i; 375 TreeControlNode parent = node; 376 for (int j = 1; j <= levels; j++) 377 parent = parent.getParent(); 378 if (parent.isLast()) 379 out.print(" <td></td>"); 380 else { 381 out.print(" <td><img SRC=\""); 382 out.print(images); 383 out.print("/"); 384 out.print(IMAGE_LINE_VERTICAL); 385 out.print("\" border=\"0\"></td>"); 386 } 387 out.println(); 388 } 389 390 392 String encodedNodeName = URLEncoder.encode(node.getName()); 397 398 String action = replace(getAction(), "${name}", encodedNodeName); 399 400 401 String updateTreeAction = 402 replace(getAction(), "tree=${name}", "select=" + encodedNodeName); 403 updateTreeAction = 404 ((HttpServletResponse ) pageContext.getResponse()). 405 encodeURL(updateTreeAction); 406 407 out.print(" <td>"); 408 if ((action != null) && !node.isLeaf()) { 409 out.print("<a HREF=\""); 410 out.print(response.encodeURL(action)); 411 out.print("\">"); 412 } 413 out.print("<img SRC=\""); 414 out.print(images); 415 out.print("/"); 416 if (node.isLeaf()) { 417 if (node.isLast()) 418 out.print(IMAGE_LINE_LAST); 419 else 420 out.print(IMAGE_LINE_MIDDLE); 421 } else if (node.isExpanded()) { 422 if (node.isLast()) 423 out.print(IMAGE_HANDLE_DOWN_LAST); 424 else 425 out.print(IMAGE_HANDLE_DOWN_MIDDLE); 426 } else { 427 if (node.isLast()) 428 out.print(IMAGE_HANDLE_RIGHT_LAST); 429 else 430 out.print(IMAGE_HANDLE_RIGHT_MIDDLE); 431 } 432 out.print("\" border=\"0\" align=\"absmiddle\">"); 433 if ((action != null) && !node.isLeaf()) 434 out.print("</a>"); 435 out.println("</td>"); 436 437 String hyperlink = null; 439 if (node.getAction() != null) 440 hyperlink = ((HttpServletResponse ) pageContext.getResponse()). 441 encodeURL(node.getAction()); 442 443 out.print(" <td colspan=\""); 445 out.print(width - level + 1); 446 out.print("\"><nobr>"); 447 if (node.getIcon() != null) { 448 if (hyperlink != null) { 449 out.print("<a HREF=\""); 450 out.print(hyperlink); 451 out.print("\""); 452 String target = node.getTarget(); 453 if(target != null) { 454 out.print(" target=\""); 455 out.print(target); 456 out.print("\""); 457 } 458 out.print(" onclick=\""); 460 out.print("self.location.href='" + updateTreeAction + "'"); 461 out.print("\""); 462 out.print(">"); 463 } 464 out.print("<img SRC=\""); 465 out.print(images); 466 out.print("/"); 467 out.print(node.getIcon()); 468 out.print("\" border=\"0\" align=\"absmiddle\">"); 469 if (hyperlink != null) 470 out.print("</a>"); 471 } 472 473 475 if (node.getLabel() != null) { 476 String labelStyle = null; 477 if (node.isSelected() && (styleSelected != null)) 478 labelStyle = styleSelected; 479 else if (!node.isSelected() && (styleUnselected != null)) 480 labelStyle = styleUnselected; 481 if (hyperlink != null) { 482 out.print(" <a HREF=\""); 485 out.print(hyperlink); 486 out.print("\""); 487 String target = node.getTarget(); 488 if(target != null) { 489 out.print(" target=\""); 490 out.print(target); 491 out.print("\""); 492 } 493 if (labelStyle != null) { 494 out.print(" class=\""); 495 out.print(labelStyle); 496 out.print("\""); 497 } 498 out.print(" onclick=\""); 500 out.print("self.location.href='" + updateTreeAction + "'"); 501 out.print("\""); 502 out.print(">"); 503 } else if (labelStyle != null) { 504 out.print("<span class=\""); 505 out.print(labelStyle); 506 out.print("\">"); 507 } 508 out.print(node.getLabel()); 509 if (hyperlink != null) 510 out.print("</a>"); 511 else if (labelStyle != null) 512 out.print("</span>"); 513 } 514 out.println("</nobr></td>"); 515 516 out.println(" </tr>"); 518 519 if (node.isExpanded()) { 521 TreeControlNode children[] = node.findChildren(); 522 int lastIndex = children.length - 1; 523 int newLevel = level + 1; 524 for (int i = 0; i < children.length; i++) { 525 render(out, children[i], newLevel, width, i == lastIndex); 526 } 527 } 528 529 } 530 531 532 540 protected String replace(String template, String placeholder, 541 String value) { 542 543 if (template == null) 544 return (null); 545 if ((placeholder == null) || (value == null)) 546 return (template); 547 while (true) { 548 int index = template.indexOf(placeholder); 549 if (index < 0) 550 break; 551 StringBuffer temp = new StringBuffer (template.substring(0, index)); 552 temp.append(value); 553 temp.append(template.substring(index + placeholder.length())); 554 template = temp.toString(); 555 } 556 return (template); 557 558 } 559 560 561 } 562 | Popular Tags |