1 26 27 package org.objectweb.jonas.webapp.taglib; 28 29 import java.io.IOException ; 30 import java.net.URLEncoder ; 31 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.JspWriter ; 35 36 37 38 39 40 public class TreeControlRenderTag extends TreeControlTag { 41 44 static final String IMAGE_NODE_OPEN = "node_open.gif"; 45 static final String IMAGE_NODE_OPEN_FIRST = "node_open_first.gif"; 46 static final String IMAGE_NODE_OPEN_MIDDLE = "node_open_middle.gif"; 47 static final String IMAGE_NODE_OPEN_LAST = "node_open_last.gif"; 48 static final String IMAGE_NODE_CLOSE = "node_close.gif"; 49 static final String IMAGE_NODE_CLOSE_FIRST = "node_close_first.gif"; 50 static final String IMAGE_NODE_CLOSE_MIDDLE = "node_close_middle.gif"; 51 static final String IMAGE_NODE_CLOSE_LAST = "node_close_last.gif"; 52 static final String IMAGE_BLANK = "noline.gif"; 53 static final String IMAGE_LINE_FIRST = "line_first.gif"; 54 static final String IMAGE_LINE_LAST = "line_last.gif"; 55 static final String IMAGE_LINE_MIDDLE = "line_middle.gif"; 56 static final String IMAGE_LINE_VERTICAL = "line.gif"; 57 58 60 63 private boolean mb_FirstNodeRendered; 64 private int mi_MaxChar; 65 66 68 73 public int doEndTag() 74 throws JspException { 75 79 80 TreeControl treeControl = getTreeControl(); 81 JspWriter out = pageContext.getOut(); 82 try { 83 out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\""); 84 if (style != null) { 85 out.print(" class=\""); 86 out.print(style); 87 out.print("\""); 88 } 89 out.println(">"); 90 int level = 0; 91 mb_FirstNodeRendered = false; 92 mi_MaxChar = 0; 93 TreeControlNode node = treeControl.getRoot(); 94 render(out, node, level, treeControl.getWidth(), true); 95 96 out.print("<tr>"); 98 for (int i = 0; i < treeControl.getWidth() + 2; i++) { 99 out.print("<td></td>"); 100 } 101 StringBuffer sb = new StringBuffer ("<td nowrap>"); 102 for (int i = 0; i < (mi_MaxChar + 1); i++) { 103 sb.append(" "); 104 } 105 sb.append("</td>"); 106 out.print(sb.toString()); 107 out.println("</tr>"); 108 109 out.println("</table>"); 110 } 111 catch (IOException e) { 112 throw new JspException (e); 113 } 114 return (EVAL_PAGE); 115 116 } 117 118 120 132 protected void render(JspWriter out, TreeControlNode node, int level, int width, boolean last) 133 throws IOException { 134 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 135 136 139 if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && (node.getLabel() == null)) { 140 TreeControlNode children[] = node.findChildren(); 142 int firstIndex = children.length - 1; 143 int lastIndex = children.length - 1; 144 int newLevel = level + 1; 145 for (int i = 0; i < children.length; i++) { 146 render(out, children[i], newLevel, width, i == lastIndex); 147 } 148 return; 149 } 150 151 out.println(" <tr valign=\"middle\">"); 153 154 for (int i = 0; i < level; i++) { 156 int levels = level - i; 157 TreeControlNode parent = node; 158 for (int j = 1; j <= levels; j++) { 159 parent = parent.getParent(); 160 } 161 if (parent.isLast()) { 162 out.print(" <td></td>"); 163 } 164 else { 165 out.print(" <td><img SRC=\""); 166 out.print(images); 167 out.print("/"); 168 out.print("tree/"); 169 out.print(IMAGE_LINE_VERTICAL); 170 out.print("\" border=\"0\"></td>"); 171 } 172 out.println(); 173 } 174 175 177 String encodedNodeName = URLEncoder.encode(node.getName(),"UTF-8"); 182 183 String action = replace(getAction(), "${name}", encodedNodeName); 184 185 String updateTreeAction = replace(getAction(), "tree=${name}", "select=" + encodedNodeName); 186 updateTreeAction = ((HttpServletResponse ) pageContext.getResponse()).encodeURL( 187 updateTreeAction); 188 189 out.print(" <td>"); 190 if ((action != null) && !node.isLeaf()) { 191 out.print("<a HREF=\""); 192 out.print(response.encodeURL(action)); 193 out.print("\">"); 194 } 195 out.print("<img SRC=\""); 196 out.print(images); 197 out.print("/"); 198 out.print("tree/"); 199 if (node.isLeaf()) { 200 if (mb_FirstNodeRendered == false) { 201 if (node.isLast()) { 202 out.print(IMAGE_BLANK); 203 } 204 else { 205 out.print(IMAGE_LINE_FIRST); 206 } 207 } 208 else if (node.isLast()) { 209 out.print(IMAGE_LINE_LAST); 210 } 211 else { 212 out.print(IMAGE_LINE_MIDDLE); 213 } 214 } 215 else if (node.isExpanded()) { 216 if (mb_FirstNodeRendered == false) { 217 if (node.isLast()) { 218 out.print(IMAGE_NODE_OPEN); 219 } 220 else { 221 out.print(IMAGE_NODE_OPEN_FIRST); 222 } 223 } 224 else if (node.isLast()) { 225 out.print(IMAGE_NODE_OPEN_LAST); 226 } 227 else { 228 out.print(IMAGE_NODE_OPEN_MIDDLE); 229 } 230 } 231 else { 232 if (mb_FirstNodeRendered == false) { 233 if (node.isLast()) { 234 out.print(IMAGE_NODE_CLOSE); 235 } 236 else { 237 out.print(IMAGE_NODE_CLOSE_FIRST); 238 } 239 } 240 else if (node.isLast()) { 241 out.print(IMAGE_NODE_CLOSE_LAST); 242 } 243 else { 244 out.print(IMAGE_NODE_CLOSE_MIDDLE); 245 } 246 } 247 out.print("\" border=\"0\" align=\"absmiddle\">"); 248 if ((action != null) && !node.isLeaf()) { 249 out.print("</a>"); 250 } 251 out.println("</td>"); 252 253 String hyperlink = null; 255 if (node.getAction() != null) { 256 hyperlink = ((HttpServletResponse ) pageContext.getResponse()).encodeURL(node.getAction()); 257 } 258 259 out.print(" <td>"); 261 262 out.print("<a name=\""); 264 out.print(encodedNodeName); 265 out.print("\"></a>"); 266 267 if (node.getIcon() != null) { 268 if (hyperlink != null) { 269 out.print("<a HREF=\""); 270 out.print(hyperlink); 271 out.print("\""); 272 String target = node.getTarget(); 273 if (target != null) { 274 out.print(" target=\""); 275 out.print(target); 276 out.print("\""); 277 } 278 out.print(" onclick=\""); 280 out.print("self.location.href='" + updateTreeAction + "'"); 281 out.print("\""); 282 out.print(">"); 283 } 284 out.print("<img SRC=\""); 285 out.print(images); 286 out.print("/"); 287 out.print(node.getIcon()); 288 out.print("\" border=\"0\" align=\"absmiddle\">"); 289 if (hyperlink != null) { 290 out.print("</a>"); 291 } 292 } 293 out.println("</td>"); 294 295 int iColspan = width - level + 1; 297 if (iColspan > 1) { 298 out.print(" <td width=\"100%\" colspan=\""); 299 out.print(iColspan); 300 out.print("\" nowrap>"); 301 } 302 else { 303 out.print(" <td width=\"100%\" nowrap>"); 304 } 305 if (node.getLabel() != null) { 306 if (node.getLabel().length() > mi_MaxChar) { 307 mi_MaxChar = node.getLabel().length(); 308 } 309 out.print(" "); 312 String labelStyle = null; 313 if (node.isSelected() && (styleSelected != null)) { 314 labelStyle = styleSelected; 315 } 316 else if (!node.isSelected() && (styleUnselected != null)) { 317 labelStyle = styleUnselected; 318 } 319 if (hyperlink != null) { 320 out.print("<a HREF=\""); 321 out.print(hyperlink); 322 out.print("\""); 323 String target = node.getTarget(); 324 if (target != null) { 325 out.print(" target=\""); 326 out.print(target); 327 out.print("\""); 328 } 329 if (labelStyle != null) { 330 out.print(" class=\""); 331 out.print(labelStyle); 332 out.print("\""); 333 } 334 out.print(" onclick=\""); 336 out.print("self.location.href='" + updateTreeAction + "'"); 337 out.print("\""); 338 out.print(">"); 339 } 340 else if (labelStyle != null) { 341 out.print("<span class=\""); 342 out.print(labelStyle); 343 out.print("\">"); 344 } 345 out.print(node.getLabel()); 346 if (hyperlink != null) { 347 out.print("</a>"); 348 } 349 else if (labelStyle != null) { 350 out.print("</span>"); 351 } 352 } 353 out.println("</td>"); 354 355 out.println(" </tr>"); 357 mb_FirstNodeRendered = true; 359 360 if (node.isExpanded()) { 362 TreeControlNode children[] = node.findChildren(); 363 int lastIndex = children.length - 1; 364 int newLevel = level + 1; 365 for (int i = 0; i < children.length; i++) { 366 render(out, children[i], newLevel, width, i == lastIndex); 367 } 368 } 369 370 } 371 } 372 | Popular Tags |