1 24 package org.ofbiz.widget.html; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.servlet.ServletContext ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.ofbiz.base.util.StringUtil; 36 import org.ofbiz.base.util.UtilFormatOut; 37 import org.ofbiz.base.util.UtilValidate; 38 import org.ofbiz.webapp.control.RequestHandler; 39 import org.ofbiz.webapp.taglib.ContentUrlTag; 40 import org.ofbiz.widget.screen.ScreenRenderer; 41 import org.ofbiz.widget.screen.ScreenStringRenderer; 42 import org.ofbiz.widget.tree.ModelTree; 43 import org.ofbiz.widget.tree.TreeStringRenderer; 44 45 46 53 public class HtmlTreeRenderer implements TreeStringRenderer { 54 55 ScreenStringRenderer screenStringRenderer = null; 56 public static final String module = HtmlTreeRenderer.class.getName(); 57 58 public HtmlTreeRenderer() {} 59 60 public static String buildPathString(ModelTree modelTree, int depth) { 61 StringBuffer buf = new StringBuffer (); 62 for (int i=1; i <= depth; i++) { 63 int idx = modelTree.getNodeIndexAtDepth(i); 64 buf.append("."); 65 buf.append(Integer.toString(idx + 1)); 66 } 67 return buf.toString(); 68 } 69 70 public void renderNodeBegin(Writer writer, Map context, ModelTree.ModelNode node, int depth, boolean isLast) throws IOException { 71 72 String pathString = buildPathString(node.getModelTree(), depth); 73 String currentNodeTrailPiped = null; 74 List currentNodeTrail = node.getModelTree().getCurrentNodeTrail(); 75 String staticNodeTrailPiped = StringUtil.join(currentNodeTrail, "|"); 76 context.put("staticNodeTrailPiped", staticNodeTrailPiped); 77 context.put("nodePathString", pathString); 78 context.put("depth", Integer.toString(depth)); 79 String style = node.getWrapStyle(context); 80 if (UtilValidate.isNotEmpty(style)) { 81 writer.write("<div"); 82 writer.write(" class=\""); 83 writer.write(style); 84 writer.write("\""); 85 writer.write(">"); 86 } 87 88 String pkName = node.getPkName(); 89 String entityId = null; 90 String entryName = node.getEntryName(); 91 if (UtilValidate.isNotEmpty(entryName)) { 92 Map map = (Map )context.get(entryName); 93 entityId = (String )map.get(pkName); 94 } else { 95 entityId = (String ) context.get(pkName); 96 } 97 boolean hasChildren = node.hasChildren(context); 98 100 if (hasChildren && node.isExpandCollapse()) { 102 String targetEntityId = null; 103 List targetNodeTrail = node.getModelTree().getTrailList(); 104 if (depth < targetNodeTrail.size()) { 105 targetEntityId = (String )targetNodeTrail.get(depth); 106 } 107 110 ModelTree.ModelNode.Image expandCollapseImage = new ModelTree.ModelNode.Image(); 111 expandCollapseImage.setBorder("0"); 112 ModelTree.ModelNode.Link expandCollapseLink = new ModelTree.ModelNode.Link(); 113 String expandCollapseStyle = UtilFormatOut.checkEmpty(node.getExpandCollapseStyle(), "expandcollapse"); 114 expandCollapseLink.setStyle(expandCollapseStyle); 115 expandCollapseLink.setImage(expandCollapseImage); 116 118 int openDepth = node.getModelTree().getOpenDepth(); 119 if (depth >= openDepth && (targetEntityId == null || !targetEntityId.equals(entityId))) { 120 if( node.showPeers(depth)) { 122 context.put("processChildren", new Boolean (false)); 123 currentNodeTrailPiped = StringUtil.join(currentNodeTrail, "|"); 125 context.put("currentNodeTrailPiped", currentNodeTrailPiped); 126 expandCollapseImage.setSrc("/images/expand.gif"); 128 String target = node.getModelTree().getExpandCollapseRequest(context); 129 String trailName = node.getModelTree().getTrailName(context); 130 if (target.indexOf("?") < 0) target += "?"; 131 else target += "&"; 132 target += trailName + "=" + currentNodeTrailPiped; 133 target += "#" + staticNodeTrailPiped; 134 expandCollapseLink.setTarget(target); 136 } 137 } else { 138 context.put("processChildren", new Boolean (true)); 139 String lastContentId = (String )currentNodeTrail.remove(currentNodeTrail.size() - 1); 141 currentNodeTrailPiped = StringUtil.join(currentNodeTrail, "|"); 142 if (currentNodeTrailPiped == null) 143 currentNodeTrailPiped = ""; 144 context.put("currentNodeTrailPiped", currentNodeTrailPiped); 145 expandCollapseImage.setSrc("/images/collapse.gif"); 147 String target = node.getModelTree().getExpandCollapseRequest(context); 148 String trailName = node.getModelTree().getTrailName(context); 149 if (target.indexOf("?") < 0) target += "?"; 150 else target += "&"; 151 target += trailName + "=" + currentNodeTrailPiped; 152 target += "#" + staticNodeTrailPiped; 153 expandCollapseLink.setTarget(target); 154 currentNodeTrail.add(lastContentId); 156 currentNodeTrailPiped = StringUtil.join(currentNodeTrail, "|"); 157 if (currentNodeTrailPiped == null) 158 currentNodeTrailPiped = ""; 159 context.put("currentNodeTrailPiped", currentNodeTrailPiped); 160 } 161 renderLink( writer, context, expandCollapseLink); 162 } else if (!hasChildren){ 163 writer.write(" "); 164 context.put("processChildren", new Boolean (false)); 165 } 167 return; 168 } 169 170 public void renderNodeEnd(Writer writer, Map context, ModelTree.ModelNode node) throws IOException { 171 String style = node.getWrapStyle(context); 172 if (UtilValidate.isNotEmpty(style)) { 173 writer.write("</div>"); 174 } 175 return; 176 } 177 178 public void renderLabel(Writer writer, Map context, ModelTree.ModelNode.Label label) throws IOException { 179 writer.write("<span"); 181 String id = label.getId(context); 182 if (UtilValidate.isNotEmpty(id)) { 183 writer.write(" id=\""); 184 writer.write(id); 185 writer.write("\""); 186 } 187 String style = label.getStyle(context); 188 if (UtilValidate.isNotEmpty(style)) { 189 writer.write(" class=\""); 190 writer.write(style); 191 writer.write("\""); 192 } 193 writer.write(">"); 194 195 writer.write(label.getText(context)); 197 198 writer.write("</span>"); 200 201 appendWhitespace(writer); 202 } 203 204 205 public void renderLink(Writer writer, Map context, ModelTree.ModelNode.Link link) throws IOException { 206 writer.write("<a"); 208 String id = link.getId(context); 209 if (UtilValidate.isNotEmpty(id)) { 210 writer.write(" id=\""); 211 writer.write(id); 212 writer.write("\""); 213 } 214 String style = link.getStyle(context); 215 if (UtilValidate.isNotEmpty(style)) { 216 writer.write(" class=\""); 217 writer.write(style); 218 writer.write("\""); 219 } 220 String name = link.getName(context); 221 if (UtilValidate.isNotEmpty(name)) { 222 writer.write(" name=\""); 223 writer.write(name); 224 writer.write("\""); 225 } 226 String title = link.getTitle(context); 227 if (UtilValidate.isNotEmpty(title)) { 228 writer.write(" title=\""); 229 writer.write(title); 230 writer.write("\""); 231 } 232 String targetWindow = link.getTargetWindow(context); 233 if (UtilValidate.isNotEmpty(targetWindow)) { 234 writer.write(" target=\""); 235 writer.write(targetWindow); 236 writer.write("\""); 237 } 238 String target = link.getTarget(context); 239 if (UtilValidate.isNotEmpty(target)) { 240 writer.write(" HREF=\""); 241 String urlMode = link.getUrlMode(); 242 String prefix = link.getPrefix(context); 243 boolean fullPath = link.getFullPath(); 244 boolean secure = link.getSecure(); 245 boolean encode = link.getEncode(); 246 HttpServletResponse res = (HttpServletResponse ) context.get("response"); 247 HttpServletRequest req = (HttpServletRequest ) context.get("request"); 248 if (urlMode != null && urlMode.equalsIgnoreCase("intra-app")) { 249 if (req != null && res != null) { 250 ServletContext ctx = (ServletContext ) req.getAttribute("servletContext"); 251 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 252 String urlString = rh.makeLink(req, res, target, fullPath, secure, encode); 253 writer.write(urlString); 254 } else if (prefix != null) { 255 writer.write(prefix + target); 256 } else { 257 writer.write(target); 258 } 259 } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) { 260 StringBuffer newURL = new StringBuffer (); 261 ContentUrlTag.appendContentPrefix(req, newURL); 262 newURL.append(target); 263 writer.write(newURL.toString()); 264 } else { 265 writer.write(target); 266 } 267 268 writer.write("\""); 269 } 270 writer.write(">"); 271 272 ModelTree.ModelNode.Image img = link.getImage(); 274 if (img == null) 275 writer.write(link.getText(context)); 276 else 277 renderImage(writer, context, img); 278 279 writer.write("</a>"); 281 282 appendWhitespace(writer); 283 } 284 285 public void renderImage(Writer writer, Map context, ModelTree.ModelNode.Image image) throws IOException { 286 writer.write("<img "); 288 String id = image.getId(context); 289 if (UtilValidate.isNotEmpty(id)) { 290 writer.write(" id=\""); 291 writer.write(id); 292 writer.write("\""); 293 } 294 String style = image.getStyle(context); 295 if (UtilValidate.isNotEmpty(style)) { 296 writer.write(" class=\""); 297 writer.write(style); 298 writer.write("\""); 299 } 300 String wid = image.getWidth(context); 301 if (UtilValidate.isNotEmpty(wid)) { 302 writer.write(" width=\""); 303 writer.write(wid); 304 writer.write("\""); 305 } 306 String hgt = image.getHeight(context); 307 if (UtilValidate.isNotEmpty(hgt)) { 308 writer.write(" height=\""); 309 writer.write(hgt); 310 writer.write("\""); 311 } 312 String border = image.getBorder(context); 313 if (UtilValidate.isNotEmpty(border)) { 314 writer.write(" border=\""); 315 writer.write(border); 316 writer.write("\""); 317 } 318 String src = image.getSrc(context); 319 if (UtilValidate.isNotEmpty(src)) { 320 writer.write(" SRC=\""); 321 String urlMode = image.getUrlMode(); 322 boolean fullPath = false; 323 boolean secure = false; 324 boolean encode = false; 325 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 326 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 327 if (urlMode != null && urlMode.equalsIgnoreCase("intra-app")) { 328 if (request != null && response != null) { 329 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 330 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 331 String urlString = rh.makeLink(request, response, src, fullPath, secure, encode); 332 writer.write(urlString); 333 } else { 334 writer.write(src); 335 } 336 } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) { 337 if (request != null && response != null) { 338 StringBuffer newURL = new StringBuffer (); 339 ContentUrlTag.appendContentPrefix(request, newURL); 340 newURL.append(src); 341 writer.write(newURL.toString()); 342 } 343 } else { 344 writer.write(src); 345 } 346 347 writer.write("\""); 348 } 349 writer.write("/>"); 350 351 } 352 353 public void appendWhitespace(Writer writer) throws IOException { 354 writer.write("\r\n"); 356 } 358 359 public ScreenStringRenderer getScreenStringRenderer(Map context) { 360 361 ScreenRenderer screenRenderer = (ScreenRenderer)context.get("screens"); 362 if (screenRenderer != null) { 363 screenStringRenderer = screenRenderer.getScreenStringRenderer(); 364 } else { 365 if (screenStringRenderer == null) { 366 screenStringRenderer = new HtmlScreenRenderer(); 367 } 368 } 369 return screenStringRenderer; 370 } 371 } 372 | Popular Tags |