00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 package org.objectweb.jonas.webapp.taglib;
00063
00064 import java.io.IOException;
00065 import java.net.URLEncoder;
00066
00067 import javax.servlet.http.HttpServletResponse;
00068 import javax.servlet.jsp.JspException;
00069 import javax.servlet.jsp.JspWriter;
00070 import javax.servlet.jsp.PageContext;
00071 import javax.servlet.jsp.tagext.TagSupport;
00072
00073
00109 public class TreeControlTag extends TagSupport {
00110
00114 static final String DEFAULT_IMAGES = "images";
00115
00119 static final String IMAGE_HANDLE_DOWN_LAST = "handledownlast.gif";
00120 static final String IMAGE_HANDLE_DOWN_MIDDLE = "handledownmiddle.gif";
00121 static final String IMAGE_HANDLE_RIGHT_LAST = "handlerightlast.gif";
00122 static final String IMAGE_HANDLE_RIGHT_MIDDLE = "handlerightmiddle.gif";
00123 static final String IMAGE_LINE_LAST = "linelastnode.gif";
00124 static final String IMAGE_LINE_MIDDLE = "linemiddlenode.gif";
00125 static final String IMAGE_LINE_VERTICAL = "linevertical.gif";
00126
00127
00128
00135 protected String action = null;
00136
00137 public String getAction() {
00138 return (this.action);
00139 }
00140
00141 public void setAction(String action) {
00142 this.action = action;
00143 }
00144
00149 protected String images = DEFAULT_IMAGES;
00150
00151 public String getImages() {
00152 return (this.images);
00153 }
00154
00155 public void setImages(String images) {
00156 this.images = images;
00157 }
00158
00164 protected String scope = null;
00165
00166 public String getScope() {
00167 return (this.scope);
00168 }
00169
00170 public void setScope(String scope) {
00171 if (!"page".equals(scope) && !"request".equals(scope) && !"session".equals(scope)
00172 && !"application".equals(scope)) {
00173 throw new IllegalArgumentException("Invalid scope '" + scope + "'");
00174 }
00175 this.scope = scope;
00176 }
00177
00181 protected String style = null;
00182
00183 public String getStyle() {
00184 return (this.style);
00185 }
00186
00187 public void setStyle(String style) {
00188 this.style = style;
00189 }
00190
00195 protected String styleSelected = null;
00196
00197 public String getStyleSelected() {
00198 return (this.styleSelected);
00199 }
00200
00201 public void setStyleSelected(String styleSelected) {
00202 this.styleSelected = styleSelected;
00203 }
00204
00209 protected String styleUnselected = null;
00210
00211 public String getStyleUnselected() {
00212 return (this.styleUnselected);
00213 }
00214
00215 public void setStyleUnselected(String styleUnselected) {
00216 this.styleUnselected = styleUnselected;
00217 }
00218
00223 protected String tree = null;
00224
00225 public String getTree() {
00226 return (this.tree);
00227 }
00228
00229 public void setTree(String tree) {
00230 this.tree = tree;
00231 }
00232
00233
00234
00240 public int doEndTag()
00241 throws JspException {
00242
00243 TreeControl treeControl = getTreeControl();
00244 JspWriter out = pageContext.getOut();
00245 try {
00246 out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"");
00247 if (style != null) {
00248 out.print(" class=\"");
00249 out.print(style);
00250 out.print("\"");
00251 }
00252 out.println(">");
00253 int level = 0;
00254 TreeControlNode node = treeControl.getRoot();
00255 render(out, node, level, treeControl.getWidth(), true);
00256 out.println("</table>");
00257 }
00258 catch (IOException e) {
00259 throw new JspException(e);
00260 }
00261
00262 return (EVAL_PAGE);
00263
00264 }
00265
00269 public void release() {
00270
00271 this.action = null;
00272 this.images = DEFAULT_IMAGES;
00273 this.scope = null;
00274 this.style = null;
00275 this.styleSelected = null;
00276 this.styleUnselected = null;
00277 this.tree = null;
00278
00279 }
00280
00281
00282
00289 protected TreeControl getTreeControl()
00290 throws JspException {
00291
00292 Object treeControl = null;
00293 if (scope == null) {
00294 treeControl = pageContext.findAttribute(tree);
00295 }
00296 else if ("page".equals(scope)) {
00297 treeControl = pageContext.getAttribute(tree, PageContext.PAGE_SCOPE);
00298 }
00299 else if ("request".equals(scope)) {
00300 treeControl = pageContext.getAttribute(tree, PageContext.REQUEST_SCOPE);
00301 }
00302 else if ("session".equals(scope)) {
00303 treeControl = pageContext.getAttribute(tree, PageContext.SESSION_SCOPE);
00304 }
00305 else if ("application".equals(scope)) {
00306 treeControl = pageContext.getAttribute(tree, PageContext.APPLICATION_SCOPE);
00307 }
00308 if (treeControl == null) {
00309 throw new JspException("Cannot find tree control attribute '" + tree + "'");
00310 }
00311 else if (!(treeControl instanceof TreeControl)) {
00312 throw new JspException("Invalid tree control attribute '" + tree + "'");
00313 }
00314 else {
00315 return ((TreeControl) treeControl);
00316 }
00317
00318 }
00319
00332 protected void render(JspWriter out, TreeControlNode node, int level, int width, boolean last)
00333 throws IOException {
00334
00335 HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
00336
00337
00338
00339
00340 if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && (node.getLabel() == null)) {
00341
00342 TreeControlNode children[] = node.findChildren();
00343 int lastIndex = children.length - 1;
00344 int newLevel = level + 1;
00345 for (int i = 0; i < children.length; i++) {
00346 render(out, children[i], newLevel, width, i == lastIndex);
00347 }
00348 return;
00349 }
00350
00351
00352 out.println(" <tr valign=\"middle\">");
00353
00354
00355 for (int i = 0; i < level; i++) {
00356 int levels = level - i;
00357 TreeControlNode parent = node;
00358 for (int j = 1; j <= levels; j++) {
00359 parent = parent.getParent();
00360 }
00361 if (parent.isLast()) {
00362 out.print(" <td></td>");
00363 }
00364 else {
00365 out.print(" <td><img src=\"");
00366 out.print(images);
00367 out.print("/");
00368 out.print(IMAGE_LINE_VERTICAL);
00369 out.print("\" border=\"0\"></td>");
00370 }
00371 out.println();
00372 }
00373
00374
00375
00376
00377
00378
00379
00380 String encodedNodeName = URLEncoder.encode(node.getName(),"UTF-8");
00381
00382 String action = replace(getAction(), "${name}", encodedNodeName);
00383
00384 String updateTreeAction = replace(getAction(), "tree=${name}", "select=" + encodedNodeName);
00385 updateTreeAction = ((HttpServletResponse) pageContext.getResponse()).encodeURL(
00386 updateTreeAction);
00387
00388 out.print(" <td>");
00389 if ((action != null) && !node.isLeaf()) {
00390 out.print("<a href=\"");
00391 out.print(response.encodeURL(action));
00392 out.print("\">");
00393 }
00394 out.print("<img src=\"");
00395 out.print(images);
00396 out.print("/");
00397 if (node.isLeaf()) {
00398 if (node.isLast()) {
00399 out.print(IMAGE_LINE_LAST);
00400 }
00401 else {
00402 out.print(IMAGE_LINE_MIDDLE);
00403 }
00404 }
00405 else if (node.isExpanded()) {
00406 if (node.isLast()) {
00407 out.print(IMAGE_HANDLE_DOWN_LAST);
00408 }
00409 else {
00410 out.print(IMAGE_HANDLE_DOWN_MIDDLE);
00411 }
00412 }
00413 else {
00414 if (node.isLast()) {
00415 out.print(IMAGE_HANDLE_RIGHT_LAST);
00416 }
00417 else {
00418 out.print(IMAGE_HANDLE_RIGHT_MIDDLE);
00419 }
00420 }
00421 out.print("\" border=\"0\">");
00422 if ((action != null) && !node.isLeaf()) {
00423 out.print("</a>");
00424 }
00425 out.println("</td>");
00426
00427
00428 String hyperlink = null;
00429 if (node.getAction() != null) {
00430 hyperlink = ((HttpServletResponse) pageContext.getResponse()).encodeURL(node.getAction());
00431
00432
00433 }
00434 out.print(" <td colspan=\"");
00435 out.print(width - level + 1);
00436 out.print("\">");
00437 if (node.getIcon() != null) {
00438 if (hyperlink != null) {
00439 out.print("<a href=\"");
00440 out.print(hyperlink);
00441 out.print("\"");
00442 String target = node.getTarget();
00443 if (target != null) {
00444 out.print(" target=\"");
00445 out.print(target);
00446 out.print("\"");
00447 }
00448
00449 out.print(" onclick=\"");
00450 out.print("self.location.href='" + updateTreeAction + "'");
00451 out.print("\"");
00452 out.print(">");
00453 }
00454 out.print("<img src=\"");
00455 out.print(images);
00456 out.print("/");
00457 out.print(node.getIcon());
00458 out.print("\" border=\"0\">");
00459 if (hyperlink != null) {
00460 out.print("</a>");
00461 }
00462 }
00463
00464
00465
00466 if (node.getLabel() != null) {
00467 String labelStyle = null;
00468 if (node.isSelected() && (styleSelected != null)) {
00469 labelStyle = styleSelected;
00470 }
00471 else if (!node.isSelected() && (styleUnselected != null)) {
00472 labelStyle = styleUnselected;
00473 }
00474 if (hyperlink != null) {
00475
00476
00477 out.print(" <a href=\"");
00478 out.print(hyperlink);
00479 out.print("\"");
00480 String target = node.getTarget();
00481 if (target != null) {
00482 out.print(" target=\"");
00483 out.print(target);
00484 out.print("\"");
00485 }
00486 if (labelStyle != null) {
00487 out.print(" class=\"");
00488 out.print(labelStyle);
00489 out.print("\"");
00490 }
00491
00492 out.print(" onclick=\"");
00493 out.print("self.location.href='" + updateTreeAction + "'");
00494 out.print("\"");
00495 out.print(">");
00496 }
00497 else if (labelStyle != null) {
00498 out.print("<span class=\"");
00499 out.print(labelStyle);
00500 out.print("\">");
00501 }
00502 out.print(node.getLabel());
00503 if (hyperlink != null) {
00504 out.print("</a>");
00505 }
00506 else if (labelStyle != null) {
00507 out.print("</span>");
00508 }
00509 }
00510 out.println("</td>");
00511
00512
00513 out.println(" </tr>");
00514
00515
00516 if (node.isExpanded()) {
00517 TreeControlNode children[] = node.findChildren();
00518 int lastIndex = children.length - 1;
00519 int newLevel = level + 1;
00520 for (int i = 0; i < children.length; i++) {
00521 render(out, children[i], newLevel, width, i == lastIndex);
00522 }
00523 }
00524
00525 }
00526
00535 protected String replace(String template, String placeholder, String value) {
00536
00537 if (template == null) {
00538 return (null);
00539 }
00540 if ((placeholder == null) || (value == null)) {
00541 return (template);
00542 }
00543 while (true) {
00544 int index = template.indexOf(placeholder);
00545 if (index < 0) {
00546 break;
00547 }
00548 StringBuffer temp = new StringBuffer(template.substring(0, index));
00549 temp.append(value);
00550 temp.append(template.substring(index + placeholder.length()));
00551 template = temp.toString();
00552 }
00553 return (template);
00554
00555 }
00556
00557 }