TreeControlTag.java

00001 /*
00002  * $Header: /cvsroot/jonas/jonas/src/org/objectweb/jonas/webapp/taglib/TreeControlTag.java,v 1.2 2003/11/12 17:19:38 antonma Exp $
00003  * $Revision: 1.2 $
00004  * $Date: 2003/11/12 17:19:38 $
00005  *
00006  * ====================================================================
00007  *
00008  * The Apache Software License, Version 1.1
00009  *
00010  * Copyright (c) 2001 The Apache Software Foundation.  All rights
00011  * reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions
00015  * are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright
00021  *    notice, this list of conditions and the following disclaimer in
00022  *    the documentation and/or other materials provided with the
00023  *    distribution.
00024  *
00025  * 3. The end-user documentation included with the redistribution, if
00026  *    any, must include the following acknowlegement:
00027  *       "This product includes software developed by the
00028  *        Apache Software Foundation (http://www.apache.org/)."
00029  *    Alternately, this acknowlegement may appear in the software itself,
00030  *    if and wherever such third-party acknowlegements normally appear.
00031  *
00032  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
00033  *    Foundation" must not be used to endorse or promote products derived
00034  *    from this software without prior written permission. For written
00035  *    permission, please contact apache@apache.org.
00036  *
00037  * 5. Products derived from this software may not be called "Apache"
00038  *    nor may "Apache" appear in their names without prior written
00039  *    permission of the Apache Group.
00040  *
00041  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00042  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00043  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00044  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00045  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00046  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00047  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00048  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00049  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00050  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00051  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00052  * SUCH DAMAGE.
00053  * ====================================================================
00054  *
00055  * This software consists of voluntary contributions made by many
00056  * individuals on behalf of the Apache Software Foundation.  For more
00057  * information on the Apache Software Foundation, please see
00058  * <http://www.apache.org/>.
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 // ------------------------------------------------------------- Properties
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 // --------------------------------------------------------- Public Methods
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 // ------------------------------------------------------ Protected Methods
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         // if the node is root node and the label value is
00338         // null, then do not render root node in the tree.
00339 
00340         if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && (node.getLabel() == null)) {
00341             // Render the children of this node
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         // Render the beginning of this node
00352         out.println("  <tr valign=\"middle\">");
00353 
00354         // Create the appropriate number of indents
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         // Render the tree state image for this node
00375 
00376         // HACK to take into account special characters like = and &
00377         // in the node name, could remove this code if encode URL
00378         // and later request.getParameter() could deal with = and &
00379         // character in parameter values.
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         // Calculate the hyperlink for this node (if any)
00428         String hyperlink = null;
00429         if (node.getAction() != null) {
00430             hyperlink = ((HttpServletResponse) pageContext.getResponse()).encodeURL(node.getAction());
00431 
00432             // Render the icon for this node (if any)
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                 // to refresh the tree in the same 'self' frame
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         // Render the label for this node (if any)
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                 // Note the leading space so that the text has some space
00476                 // between it and any preceding images
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                 // to refresh the tree in the same 'self' frame
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         // Render the end of this node
00513         out.println("  </tr>");
00514 
00515         // Render the children of this node
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 }

Generated on Tue Feb 15 15:05:34 2005 for JOnAS by  doxygen 1.3.9.1