📄 modeltree.java
字号:
/* * $Id: ModelTree.java 6605 2006-01-29 04:17:04Z jonesde $ * * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.widget.tree;import java.io.IOException;import java.io.StringWriter;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.xml.parsers.ParserConfigurationException;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.StringUtil;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.collections.MapStack;import org.ofbiz.base.util.string.FlexibleStringExpander;import org.ofbiz.widget.screen.ModelScreen;import org.ofbiz.widget.screen.ScreenFactory;import org.ofbiz.widget.screen.ScreenStringRenderer;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.model.ModelField;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.service.LocalDispatcher;import org.w3c.dom.Element;import org.xml.sax.SAXException;//import com.clarkware.profiler.Profiler;/** * Widget Library - Tree model class * * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> * @version $Rev: 6605 $ * @since 3.1 */public class ModelTree { public static final String module = ModelTree.class.getName(); protected String name; protected String rootNodeName; protected String defaultRenderStyle; protected FlexibleStringExpander defaultWrapStyleExdr; protected List nodeList = new ArrayList(); protected Map nodeMap = new HashMap(); protected GenericDelegator delegator; protected LocalDispatcher dispatcher; protected FlexibleStringExpander expandCollapseRequestExdr; protected FlexibleStringExpander trailNameExdr; protected List trail = new ArrayList(); protected List currentNodeTrail; protected int openDepth; protected int postTrailOpenDepth; protected int [] nodeIndices = new int[20]; protected String defaultEntityName; protected String defaultPkName; protected boolean forceChildCheck; // ===== CONSTRUCTORS ===== /** Default Constructor */ /** XML Constructor */ public ModelTree() {} public ModelTree(Element treeElement, GenericDelegator delegator, LocalDispatcher dispatcher) { this.name = treeElement.getAttribute("name"); this.rootNodeName = treeElement.getAttribute("root-node-name"); this.defaultRenderStyle = UtilFormatOut.checkEmpty(treeElement.getAttribute("default-render-style"), "simple"); // A temporary hack to accommodate those who might still be using "render-style" instead of "default-render-style" if (UtilValidate.isEmpty(this.defaultRenderStyle) || this.defaultRenderStyle.equals("simple")) { String rStyle = treeElement.getAttribute("render-style"); if (UtilValidate.isNotEmpty(rStyle)) this.defaultRenderStyle = rStyle; } this.defaultWrapStyleExdr = new FlexibleStringExpander(treeElement.getAttribute("default-wrap-style")); this.expandCollapseRequestExdr = new FlexibleStringExpander(treeElement.getAttribute("expand-collapse-request")); this.trailNameExdr = new FlexibleStringExpander(UtilFormatOut.checkEmpty(treeElement.getAttribute("trail-name"), "trail")); this.delegator = delegator; this.dispatcher = dispatcher; this.forceChildCheck = !"false".equals(treeElement.getAttribute("force-child-check")); setDefaultEntityName(treeElement.getAttribute("entity-name")); try { openDepth = Integer.parseInt(treeElement.getAttribute("open-depth")); } catch(NumberFormatException e) { openDepth = 0; } try { postTrailOpenDepth = Integer.parseInt(treeElement.getAttribute("post-trail-open-depth")); } catch(NumberFormatException e) { postTrailOpenDepth = 999; } List nodeElements = UtilXml.childElementList(treeElement, "node"); Iterator nodeElementIter = nodeElements.iterator(); while (nodeElementIter.hasNext()) { Element nodeElementEntry = (Element) nodeElementIter.next(); ModelNode node = new ModelNode(nodeElementEntry, this); String nodeName = node.getName(); nodeList.add(node); nodeMap.put(nodeName,node); } if (nodeList.size() == 0) { throw new IllegalArgumentException("No node elements found for the tree definition with name: " + this.name); } } public String getName() { return name; } public void setDefaultEntityName(String name) { String nm = name; if (UtilValidate.isEmpty(nm)) { nm = "Content"; } this.defaultEntityName = nm; ModelEntity modelEntity = delegator.getModelEntity(this.defaultEntityName); if (modelEntity.getPksSize() == 1) { ModelField modelField = modelEntity.getOnlyPk(); this.defaultPkName = modelField.getName(); } } public String getDefaultEntityName() { return this.defaultEntityName; } public String getDefaultPkName() { return this.defaultPkName; } public String getRootNodeName() { return rootNodeName; } public String getWrapStyle(Map context) { return this.defaultWrapStyleExdr.expandString(context); } public int getOpenDepth() { return openDepth; } public int getPostTrailOpenDepth() { return postTrailOpenDepth; } public int getNodeIndexAtDepth(int i) { return nodeIndices[i]; } public void setNodeIndexAtDepth(int i, int val) { nodeIndices[i] = val; } public String getExpandCollapseRequest(Map context) { String expColReq = this.expandCollapseRequestExdr.expandString(context); if (UtilValidate.isEmpty(expColReq)) { HttpServletRequest request = (HttpServletRequest)context.get("request"); String s1 = request.getRequestURI(); int pos = s1.lastIndexOf("/"); if (pos >= 0) expColReq = s1.substring(pos + 1); else expColReq = s1; } return expColReq; } public String getTrailName(Map context) { return this.trailNameExdr.expandString(context); } public List getTrailList() { return trail; } public void setTrailList(List trailList) { this.trail = trailList; } public List getCurrentNodeTrail() { return currentNodeTrail; } /** * Renders this tree to a String, i.e. in a text format, as defined with the * TreeStringRenderer implementation. * * @param writer The Writer that the tree text will be written to * @param context Map containing the tree context; the following are * reserved words in this context: parameters (Map), isError (Boolean), * itemIndex (Integer, for lists only, otherwise null), bshInterpreter, * treeName (String, optional alternate name for tree, defaults to the * value of the name attribute) * @param treeStringRenderer An implementation of the TreeStringRenderer * interface that is responsible for the actual text generation for * different tree elements; implementing your own makes it possible to * use the same tree definitions for many types of tree UIs */ public void renderTreeString(StringBuffer buf, Map context, TreeStringRenderer treeStringRenderer) throws GeneralException { ModelNode node = (ModelNode)nodeMap.get(rootNodeName); /* List parentNodeTrail = (List)context.get("currentNodeTrail"); if (parentNodeTrail != null) currentNodeTrail = new ArrayList(parentNodeTrail); else */ currentNodeTrail = new ArrayList(); //Map requestParameters = (Map)context.get("requestParameters"); //String treeString = (String)requestParameters.get("trail"); String trailName = trailNameExdr.expandString(context); String treeString = (String)context.get(trailName); if (UtilValidate.isEmpty(treeString)) { Map parameters = (Map)context.get("parameters"); treeString = (String)parameters.get(trailName); } if (UtilValidate.isNotEmpty(treeString)) { trail = StringUtil.split(treeString, "|"); if (trail == null || trail.size() == 0) throw new RuntimeException("Tree 'trail' value is empty."); context.put("rootEntityId", trail.get(0)); context.put(defaultPkName, trail.get(0)); context.put("targetNodeTrail", trail); } else { Debug.logError("Trail value is empty.", module); } StringWriter writer = new StringWriter(); try { node.renderNodeString(writer, context, treeStringRenderer, 0, true); buf.append(writer.toString()); } catch (IOException e2) { String errMsg = "Error rendering included label with name [" + name + "] : " + e2.toString(); Debug.logError(e2, errMsg, module); throw new RuntimeException(errMsg); }// try {// FileOutputStream fw = new FileOutputStream(new File("/usr/local/agi/ofbiz/hot-deploy/ofbizdoc/misc/profile.data"));// Profiler.print(fw);// fw.close();// } catch (IOException e) {// Debug.logError("[PROFILER] " + e.getMessage(),"");// } } public LocalDispatcher getDispatcher() { return this.dispatcher; } public GenericDelegator getDelegator() { return this.delegator; } public String getRenderStyle() { return this.defaultRenderStyle; } public static class ModelNode { protected FlexibleStringExpander screenNameExdr; protected FlexibleStringExpander screenLocationExdr; protected String shareScope; protected Label label; protected Link link; protected Image image; protected List subNodeList = new ArrayList(); protected List actions = new ArrayList(); protected String name; protected ModelTree modelTree; protected List subNodeValues; protected String expandCollapseStyle; protected FlexibleStringExpander wrapStyleExdr; protected ModelTreeCondition condition; protected String renderStyle; protected String entryName; protected String entityName; protected String pkName; public ModelNode() {} public ModelNode(Element nodeElement, ModelTree modelTree) { this.modelTree = modelTree; this.name = nodeElement.getAttribute("name"); this.expandCollapseStyle = nodeElement.getAttribute("expand-collapse-style"); this.wrapStyleExdr = new FlexibleStringExpander(nodeElement.getAttribute("wrap-style")); this.renderStyle = nodeElement.getAttribute("render-style"); this.entryName = UtilFormatOut.checkEmpty(nodeElement.getAttribute("entry-name"), null); setEntityName(nodeElement.getAttribute("entity-name")); if (this.pkName == null || nodeElement.hasAttribute("join-field-name")) this.pkName = nodeElement.getAttribute("join-field-name"); Element actionElement = UtilXml.firstChildElement(nodeElement, "entity-one"); if (actionElement != null) { actions.add(new ModelTreeAction.EntityOne(this, actionElement)); } actionElement = UtilXml.firstChildElement(nodeElement, "service"); if (actionElement != null) { actions.add(new ModelTreeAction.Service(this, actionElement)); } actionElement = UtilXml.firstChildElement(nodeElement, "script"); if (actionElement != null) { actions.add(new ModelTreeAction.Script(this, actionElement)); } Element screenElement = UtilXml.firstChildElement(nodeElement, "include-screen"); if (screenElement != null) { this.screenNameExdr = new FlexibleStringExpander(screenElement.getAttribute("name")); this.screenLocationExdr = new FlexibleStringExpander(screenElement.getAttribute("location")); this.shareScope = screenElement.getAttribute("share-scope"); } Element labelElement = UtilXml.firstChildElement(nodeElement, "label"); if (labelElement != null) { this.label = new Label(labelElement); } Element linkElement = UtilXml.firstChildElement(nodeElement, "link"); if (linkElement != null) { this.link = new Link(linkElement); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -