TreeControlRenderTag.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Michel-Ange ANTON
00022  * --------------------------------------------------------------------------
00023  * $Id: TreeControlRenderTag.java,v 1.7 2003/11/12 17:19:38 antonma Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 
00027 package org.objectweb.jonas.webapp.taglib;
00028 
00029 import java.io.IOException;
00030 import java.net.URLEncoder;
00031 
00032 import javax.servlet.http.HttpServletResponse;
00033 import javax.servlet.jsp.JspException;
00034 import javax.servlet.jsp.JspWriter;
00035 
00036 
00037 
00038 
00039 
00040 public class TreeControlRenderTag extends TreeControlTag {
00044     static final String IMAGE_NODE_OPEN = "node_open.gif";
00045     static final String IMAGE_NODE_OPEN_FIRST = "node_open_first.gif";
00046     static final String IMAGE_NODE_OPEN_MIDDLE = "node_open_middle.gif";
00047     static final String IMAGE_NODE_OPEN_LAST = "node_open_last.gif";
00048     static final String IMAGE_NODE_CLOSE = "node_close.gif";
00049     static final String IMAGE_NODE_CLOSE_FIRST = "node_close_first.gif";
00050     static final String IMAGE_NODE_CLOSE_MIDDLE = "node_close_middle.gif";
00051     static final String IMAGE_NODE_CLOSE_LAST = "node_close_last.gif";
00052     static final String IMAGE_BLANK = "noline.gif";
00053     static final String IMAGE_LINE_FIRST = "line_first.gif";
00054     static final String IMAGE_LINE_LAST = "line_last.gif";
00055     static final String IMAGE_LINE_MIDDLE = "line_middle.gif";
00056     static final String IMAGE_LINE_VERTICAL = "line.gif";
00057 
00058 // --------------------------------------------------------- Instance Variables
00059 
00063     private boolean mb_FirstNodeRendered;
00064     private int mi_MaxChar;
00065 
00066 // --------------------------------------------------------- Public Methods
00067 
00073     public int doEndTag()
00074         throws JspException {
00075         /*
00076               mb_FirstNodeRendered = false;
00077               return super.doEndTag();
00078          */
00079 
00080         TreeControl treeControl = getTreeControl();
00081         JspWriter out = pageContext.getOut();
00082         try {
00083             out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"");
00084             if (style != null) {
00085                 out.print(" class=\"");
00086                 out.print(style);
00087                 out.print("\"");
00088             }
00089             out.println(">");
00090             int level = 0;
00091             mb_FirstNodeRendered = false;
00092             mi_MaxChar = 0;
00093             TreeControlNode node = treeControl.getRoot();
00094             render(out, node, level, treeControl.getWidth(), true);
00095 
00096             // Avoid bug Netscape 4
00097             out.print("<tr>");
00098             for (int i = 0; i < treeControl.getWidth() + 2; i++) {
00099                 out.print("<td></td>");
00100             }
00101             StringBuffer sb = new StringBuffer("<td nowrap>");
00102             for (int i = 0; i < (mi_MaxChar + 1); i++) {
00103                 sb.append("&nbsp;&nbsp;");
00104             }
00105             sb.append("</td>");
00106             out.print(sb.toString());
00107             out.println("</tr>");
00108 
00109             out.println("</table>");
00110         }
00111         catch (IOException e) {
00112             throw new JspException(e);
00113         }
00114         return (EVAL_PAGE);
00115 
00116     }
00117 
00118 // ------------------------------------------------------ Protected Methods
00119 
00132     protected void render(JspWriter out, TreeControlNode node, int level, int width, boolean last)
00133         throws IOException {
00134         HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
00135 
00136         // if the node is root node and the label value is
00137         // null, then do not render root node in the tree.
00138 
00139         if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && (node.getLabel() == null)) {
00140             // Render the children of this node
00141             TreeControlNode children[] = node.findChildren();
00142             int firstIndex = children.length - 1;
00143             int lastIndex = children.length - 1;
00144             int newLevel = level + 1;
00145             for (int i = 0; i < children.length; i++) {
00146                 render(out, children[i], newLevel, width, i == lastIndex);
00147             }
00148             return;
00149         }
00150 
00151         // Render the beginning of this node
00152         out.println("  <tr valign=\"middle\">");
00153 
00154         // Create the appropriate number of indents
00155         for (int i = 0; i < level; i++) {
00156             int levels = level - i;
00157             TreeControlNode parent = node;
00158             for (int j = 1; j <= levels; j++) {
00159                 parent = parent.getParent();
00160             }
00161             if (parent.isLast()) {
00162                 out.print("    <td></td>");
00163             }
00164             else {
00165                 out.print("    <td><img src=\"");
00166                 out.print(images);
00167                 out.print("/");
00168                 out.print("tree/");
00169                 out.print(IMAGE_LINE_VERTICAL);
00170                 out.print("\" border=\"0\"></td>");
00171             }
00172             out.println();
00173         }
00174 
00175         // Render the tree state image for this node
00176 
00177         // HACK to take into account special characters like = and &
00178         // in the node name, could remove this code if encode URL
00179         // and later request.getParameter() could deal with = and &
00180         // character in parameter values.
00181         String encodedNodeName = URLEncoder.encode(node.getName(),"UTF-8");
00182 
00183         String action = replace(getAction(), "${name}", encodedNodeName);
00184 
00185         String updateTreeAction = replace(getAction(), "tree=${name}", "select=" + encodedNodeName);
00186         updateTreeAction = ((HttpServletResponse) pageContext.getResponse()).encodeURL(
00187             updateTreeAction);
00188 
00189         out.print("    <td>");
00190         if ((action != null) && !node.isLeaf()) {
00191             out.print("<a href=\"");
00192             out.print(response.encodeURL(action));
00193             out.print("\">");
00194         }
00195         out.print("<img src=\"");
00196         out.print(images);
00197         out.print("/");
00198         out.print("tree/");
00199         if (node.isLeaf()) {
00200             if (mb_FirstNodeRendered == false) {
00201                 if (node.isLast()) {
00202                     out.print(IMAGE_BLANK);
00203                 }
00204                 else {
00205                     out.print(IMAGE_LINE_FIRST);
00206                 }
00207             }
00208             else if (node.isLast()) {
00209                 out.print(IMAGE_LINE_LAST);
00210             }
00211             else {
00212                 out.print(IMAGE_LINE_MIDDLE);
00213             }
00214         }
00215         else if (node.isExpanded()) {
00216             if (mb_FirstNodeRendered == false) {
00217                 if (node.isLast()) {
00218                     out.print(IMAGE_NODE_OPEN);
00219                 }
00220                 else {
00221                     out.print(IMAGE_NODE_OPEN_FIRST);
00222                 }
00223             }
00224             else if (node.isLast()) {
00225                 out.print(IMAGE_NODE_OPEN_LAST);
00226             }
00227             else {
00228                 out.print(IMAGE_NODE_OPEN_MIDDLE);
00229             }
00230         }
00231         else {
00232             if (mb_FirstNodeRendered == false) {
00233                 if (node.isLast()) {
00234                     out.print(IMAGE_NODE_CLOSE);
00235                 }
00236                 else {
00237                     out.print(IMAGE_NODE_CLOSE_FIRST);
00238                 }
00239             }
00240             else if (node.isLast()) {
00241                 out.print(IMAGE_NODE_CLOSE_LAST);
00242             }
00243             else {
00244                 out.print(IMAGE_NODE_CLOSE_MIDDLE);
00245             }
00246         }
00247         out.print("\" border=\"0\" align=\"absmiddle\">");
00248         if ((action != null) && !node.isLeaf()) {
00249             out.print("</a>");
00250         }
00251         out.println("</td>");
00252 
00253         // Calculate the hyperlink for this node (if any)
00254         String hyperlink = null;
00255         if (node.getAction() != null) {
00256             hyperlink = ((HttpServletResponse) pageContext.getResponse()).encodeURL(node.getAction());
00257         }
00258 
00259         // Render the icon for this node (if any)
00260         out.print("    <td>");
00261 
00262         // Anchor name
00263         out.print("<a name=\"");
00264         out.print(encodedNodeName);
00265         out.print("\"></a>");
00266 
00267         if (node.getIcon() != null) {
00268             if (hyperlink != null) {
00269                 out.print("<a href=\"");
00270                 out.print(hyperlink);
00271                 out.print("\"");
00272                 String target = node.getTarget();
00273                 if (target != null) {
00274                     out.print(" target=\"");
00275                     out.print(target);
00276                     out.print("\"");
00277                 }
00278                 // to refresh the tree in the same 'self' frame
00279                 out.print(" onclick=\"");
00280                 out.print("self.location.href='" + updateTreeAction + "'");
00281                 out.print("\"");
00282                 out.print(">");
00283             }
00284             out.print("<img src=\"");
00285             out.print(images);
00286             out.print("/");
00287             out.print(node.getIcon());
00288             out.print("\" border=\"0\" align=\"absmiddle\">");
00289             if (hyperlink != null) {
00290                 out.print("</a>");
00291             }
00292         }
00293         out.println("</td>");
00294 
00295         // Render the label for this node (if any)
00296         int iColspan = width - level + 1;
00297         if (iColspan > 1) {
00298             out.print("    <td width=\"100%\" colspan=\"");
00299             out.print(iColspan);
00300             out.print("\" nowrap>");
00301         }
00302         else {
00303             out.print("    <td width=\"100%\" nowrap>");
00304         }
00305         if (node.getLabel() != null) {
00306             if (node.getLabel().length() > mi_MaxChar) {
00307                 mi_MaxChar = node.getLabel().length();
00308             }
00309             // Note the leading space so that the text has some space
00310             // between it and any preceding images
00311             out.print("&nbsp;");
00312             String labelStyle = null;
00313             if (node.isSelected() && (styleSelected != null)) {
00314                 labelStyle = styleSelected;
00315             }
00316             else if (!node.isSelected() && (styleUnselected != null)) {
00317                 labelStyle = styleUnselected;
00318             }
00319             if (hyperlink != null) {
00320                 out.print("<a href=\"");
00321                 out.print(hyperlink);
00322                 out.print("\"");
00323                 String target = node.getTarget();
00324                 if (target != null) {
00325                     out.print(" target=\"");
00326                     out.print(target);
00327                     out.print("\"");
00328                 }
00329                 if (labelStyle != null) {
00330                     out.print(" class=\"");
00331                     out.print(labelStyle);
00332                     out.print("\"");
00333                 }
00334                 // to refresh the tree in the same 'self' frame
00335                 out.print(" onclick=\"");
00336                 out.print("self.location.href='" + updateTreeAction + "'");
00337                 out.print("\"");
00338                 out.print(">");
00339             }
00340             else if (labelStyle != null) {
00341                 out.print("<span class=\"");
00342                 out.print(labelStyle);
00343                 out.print("\">");
00344             }
00345             out.print(node.getLabel());
00346             if (hyperlink != null) {
00347                 out.print("</a>");
00348             }
00349             else if (labelStyle != null) {
00350                 out.print("</span>");
00351             }
00352         }
00353         out.println("</td>");
00354 
00355         // Render the end of this node
00356         out.println("  </tr>");
00357         // Remember the first node is rendered
00358         mb_FirstNodeRendered = true;
00359 
00360         // Render the children of this node
00361         if (node.isExpanded()) {
00362             TreeControlNode children[] = node.findChildren();
00363             int lastIndex = children.length - 1;
00364             int newLevel = level + 1;
00365             for (int i = 0; i < children.length; i++) {
00366                 render(out, children[i], newLevel, width, i == lastIndex);
00367             }
00368         }
00369 
00370     }
00371 }

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