1 15 16 package com.jdon.strutsutil.treeview; 17 18 import java.io.IOException ; 19 import java.net.URLEncoder ; 20 import javax.servlet.http.*; 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.JspWriter ; 23 import javax.servlet.jsp.tagext.TagSupport ; 24 25 43 public class TreeControlTag extends TagSupport { 44 45 private final static String module = TreeControlTag.class.getName(); 46 47 50 private static final String DEFAULT_IMAGES = "images"; 51 52 public static final String TREE_NAME = "tree-control"; 53 public static final String TREE_NAME_INIT = "tree-control-init"; 54 55 56 private static final String CSS_STYLE = "tree-control"; 57 private static final String CSS_STYLE_SELECTED = "tree-control-selected"; 58 private static final String CSS_STYLE_UN_SELECTED = "tree-control-unselected"; 59 60 63 private static final String IMAGE_HANDLE_DOWN_LAST = "handledownlast.gif"; 64 private static final String IMAGE_HANDLE_DOWN_MIDDLE = "handledownmiddle.gif"; 65 private static final String IMAGE_HANDLE_RIGHT_LAST = "handlerightlast.gif"; 66 private static final String IMAGE_HANDLE_RIGHT_MIDDLE = 67 "handlerightmiddle.gif"; 68 private static final String IMAGE_LINE_LAST = "linelastnode.gif"; 69 private static final String IMAGE_LINE_MIDDLE = "linemiddlenode.gif"; 70 private static final String IMAGE_LINE_VERTICAL = "linevertical.gif"; 71 72 73 75 protected String treeAction = null; 76 public String getTreeAction() { 77 return (this.treeAction); 78 } 79 80 public void setTreeAction(String treeAction) { 81 this.treeAction = treeAction; 82 } 83 84 protected String nodeAction = null; 85 public String getNodeAction() { 86 return (this.nodeAction); 87 } 88 89 public void setNodeAction(String nodeAction) { 90 this.nodeAction = nodeAction; 91 } 92 93 protected String target = null; 94 95 public String getTarget() { 96 return (this.target); 97 } 98 99 public void setTarget(String target) { 100 this.target = target; 101 } 102 103 protected String images = DEFAULT_IMAGES; 104 public String getImages() { 105 return (this.images); 106 } 107 108 public void setImages(String images) { 109 this.images = images; 110 } 111 112 public int doEndTag() throws JspException { 113 114 TreeControl treeControl = (TreeControl) pageContext.findAttribute(TREE_NAME); 115 JspWriter out = pageContext.getOut(); 116 117 try { 118 if (treeControl == null) { 119 out.print("Please login again, or refresh again"); 120 return (EVAL_PAGE); 121 } 122 123 out.print 124 ("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\""); 125 out.print(" class=\""); 126 out.print(CSS_STYLE); 127 out.print("\""); 128 out.println(">"); 129 int level = 0; 130 ViewNode rootNode = treeControl.getRoot(); 131 render(out, rootNode, level, treeControl.getTreeWidth(), true); 132 out.println("</table>"); 133 } catch (IOException e) { 134 throw new JspException (e); 135 } catch (Exception e) { 136 throw new JspException ("Please refresh this page!"); 137 } 138 139 return (EVAL_PAGE); 140 141 } 142 143 protected void render(JspWriter out, ViewNode node, 145 int level, int width, boolean last) throws IOException { 146 147 HttpServletResponse response = 148 (HttpServletResponse) pageContext.getResponse(); 149 150 HttpServletRequest request = 151 (HttpServletRequest) pageContext.getRequest(); 152 String url_Path = request.getContextPath(); 153 154 157 if (node.isRoot()) { 159 ViewNode children[] = node.findChildren(); 161 int lastIndex = children.length - 1; 163 int newLevel = level + 1; 164 for (int i = 0; i < children.length; i++) { 165 render(out, children[i], newLevel, width, i == lastIndex); 166 } 167 return; 168 } 169 170 out.println(" <tr valign=\"middle\">"); 172 173 for (int i = 0; i < level; i++) { 174 int levels = level - i; 175 ViewNode parent = node; 176 for (int j = 1; j <= levels; j++) 177 parent = parent.getParent(); 178 if (parent.isLast()) 179 out.print(" <td></td>"); 180 else { out.print(" <td><img SRC=\""); 182 out.print(images); 183 out.print("/"); 184 out.print(IMAGE_LINE_VERTICAL); 185 out.print("\" alt=\"\" border=\"0\"></td>"); 186 } 187 out.println(); 188 } 189 190 displayNodePic(out, node, url_Path); 191 displayNode(out, node, url_Path, level, width); 192 193 out.println(" </tr>"); 195 196 if (node.isExpanded()) { 198 ViewNode children[] = node.findChildren(); 199 int lastIndex = children.length - 1; 200 int newLevel = level + 1; 201 for (int i = 0; i < children.length; i++) { 202 render(out, children[i], newLevel, width, i == lastIndex); 203 } 204 } 205 206 } 207 208 216 private void displayNodePic(JspWriter out, ViewNode node, String url_Path) throws 217 IOException { 218 HttpServletResponse response = 219 (HttpServletResponse) pageContext.getResponse(); 220 221 StringBuffer url = new StringBuffer (url_Path); 222 url.append(treeAction); 223 if (treeAction.indexOf("?") < 0) 224 url.append("?"); 225 else 226 url.append("&"); 227 url.append("key=").append(node.getKey()); 228 229 out.print(" <td>"); 230 if ( (nodeAction != null) && !node.isLeaf()) { 231 out.print("<a HREF=\""); 232 out.print(response.encodeURL(url.toString())); 233 out.print("\">"); 234 } 235 out.print("<img SRC=\""); 236 out.print(images); 237 out.print("/"); 238 if (node.isLeaf()) { 239 if (node.isLast()) 240 out.print(IMAGE_LINE_LAST); 241 else 242 out.print(IMAGE_LINE_MIDDLE); 243 out.print("\" alt=\""); 244 } else if (node.isExpanded()) { 245 if (node.isLast()) 246 out.print(IMAGE_HANDLE_DOWN_LAST); 247 else 248 out.print(IMAGE_HANDLE_DOWN_MIDDLE); 249 out.print("\" alt=\"close node"); 250 } else { 251 if (node.isLast()) 252 out.print(IMAGE_HANDLE_RIGHT_LAST); 253 else 254 out.print(IMAGE_HANDLE_RIGHT_MIDDLE); 255 out.print("\" alt=\"expand node"); 256 } 257 out.print("\" border=\"0\">"); 258 if ( (nodeAction != null) && !node.isLeaf()) 259 out.print("</a>"); 260 out.println("</td>"); 261 } 262 263 268 private void displayNode(JspWriter out, ViewNode node, String url_Path, 269 int level, int width) throws IOException { 270 271 HttpServletResponse response = 272 (HttpServletResponse) pageContext.getResponse(); 273 274 String encodedNodeKey = URLEncoder.encode(node.getKey(), "UTF-8"); 276 String action = replace(nodeAction, "${key}", encodedNodeKey); 277 StringBuffer url = new StringBuffer (url_Path); 278 url.append(action); 279 if (nodeAction.indexOf("?") < 0) 280 url.append("?"); 281 else 282 url.append("&"); 283 url.append("key=").append(node.getKey()); 284 String treeNodeAction = url.toString(); 285 286 url = new StringBuffer (url_Path); 287 url.append(treeAction); 288 if (treeAction.indexOf("?") < 0) 289 url.append("?"); 290 else 291 url.append("&"); 292 url.append("select=").append(node.getKey()); 293 String updateNodeAction = url.toString(); 294 295 out.print(" <td colspan=\""); 297 out.print(width - level + 1); 298 out.print("\">"); 299 300 302 if (node.getLabel() != null) { 303 String labelStyle = null; 304 if (node.isSelected()) 305 labelStyle = CSS_STYLE_SELECTED; 306 else if (!node.isSelected()) 307 labelStyle = CSS_STYLE_UN_SELECTED; 308 if (treeNodeAction != null) { 309 out.print(" <a HREF=\""); 312 out.print(treeNodeAction); 313 out.print("\""); 314 if (target != null) { 315 out.print(" target=\""); 316 out.print(target); 317 out.print("\""); 318 } 319 if (labelStyle != null) { 320 out.print(" class=\""); 321 out.print(labelStyle); 322 out.print("\""); 323 } 324 out.print(" onclick=\""); 326 out.print("self.location.href='" + updateNodeAction + "'"); 327 out.print("\""); 328 out.print(">"); 329 } else if (labelStyle != null) { 330 out.print("<span class=\""); 331 out.print(labelStyle); 332 out.print("\">"); 333 } 334 out.print(node.getLabel()); 335 if (treeNodeAction != null) 336 out.print("</a>"); 337 else if (labelStyle != null) 338 out.print("</span>"); 339 } 340 341 out.println("</td>"); 342 343 345 } 346 347 355 protected String replace(String template, String placeholder, 356 String value) { 357 358 if (template == null) 359 return (null); 360 if ( (placeholder == null) || (value == null)) 361 return (template); 362 while (true) { 363 int index = template.indexOf(placeholder); 364 if (index < 0) 365 break; 366 StringBuffer temp = new StringBuffer (template.substring(0, index)); 367 temp.append(value); 368 temp.append(template.substring(index + placeholder.length())); 369 template = temp.toString(); 370 } 371 return (template); 372 373 } 374 375 } 376 | Popular Tags |