📄 htmltreerenderer.java
字号:
out.writeAttribute(HTML.CELLSPACING_ATTR, "0", null); out.writeAttribute(HTML.BORDER_ATTR, "0", null); out.startElement(HTML.TR_ELEM, null); } protected void afterNodeEncode(FacesContext context, ResponseWriter out) throws IOException { out.endElement(HTML.TR_ELEM); out.endElement(HTML.TABLE_ELEM); } /** * Handles the encoding related to the navigation functionality. * * @param context FacesContext * @param out ResponseWriter * @param tree HtmlTree * @return The additional navigation image to display inside the node (if any). Only used with client-side toggle. * @throws IOException */ private UIComponent encodeNavigation(FacesContext context, ResponseWriter out, HtmlTree tree) throws IOException { TreeNode node = tree.getNode(); String nodeId = tree.getNodeId(); String spanId = TOGGLE_SPAN + ":" + tree.getId() + ":" + nodeId;//TOGGLE_SPAN + nodeId; boolean showLines = getBoolean(tree, JSFAttr.SHOW_LINES, true); boolean clientSideToggle = getBoolean(tree, JSFAttr.CLIENT_SIDE_TOGGLE, true); UIComponent nodeTypeFacet = tree.getFacet(node.getType()); String navSrc = null; String altSrc = null; UIComponent nodeImgFacet = null; int bitMask = NOTHING; bitMask += (node.getChildCount()>0) ? CHILDREN : NOTHING; bitMask += (tree.isNodeExpanded()) ? EXPANDED : NOTHING; bitMask += (tree.isLastChild(tree.getNodeId())) ? LAST : NOTHING; bitMask += (showLines) ? LINES : NOTHING; switch (bitMask) { case (NOTHING): case (LAST): navSrc = "spacer.gif"; break; case (LINES): navSrc = "line-middle.gif"; break; case (LINES + LAST): navSrc = "line-last.gif"; break; case (CHILDREN): case (CHILDREN + LAST): navSrc = "nav-plus.gif"; altSrc = "nav-minus.gif"; break; case (CHILDREN + LINES): navSrc = "nav-plus-line-middle.gif"; altSrc = "nav-minus-line-middle.gif"; break; case (CHILDREN + LINES + LAST): navSrc = "nav-plus-line-last.gif"; altSrc = "nav-minus-line-last.gif"; break; case (CHILDREN + EXPANDED): case (CHILDREN + EXPANDED + LAST): navSrc = "nav-minus.gif"; altSrc = "nav-plus.gif"; break; case (CHILDREN + EXPANDED + LINES): navSrc = "nav-minus-line-middle.gif"; altSrc = "nav-plus-line-middle.gif"; break; case (CHILDREN + EXPANDED + LINES + LAST): navSrc = "nav-minus-line-last.gif"; altSrc = "nav-plus-line-last.gif"; break; default: throw new IllegalArgumentException("Invalid bit mask of " + bitMask); } // adjust navSrc and altSrc so that the images can be retrieved using the extensions filter String navSrcUrl = getImageSrc(null, tree, navSrc); navSrc = getImageSrc(context, tree, navSrc); altSrc = getImageSrc(context, tree, altSrc); // render nav cell out.startElement(HTML.TD_ELEM, tree); out.writeAttribute(HTML.WIDTH_ATTR, "19", null); out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null); out.writeAttribute("valign", "top", null); if ((bitMask & LINES)!=0 && (bitMask & LAST)==0) { out.writeURIAttribute("background", getImageSrc(context, tree, "line-trunk.gif"), null); } // add the appropriate image for the nav control UIGraphic image = new UIGraphic(); image.setId(context.getViewRoot().createUniqueId()); image.setUrl(navSrcUrl); Map imageAttrs = image.getAttributes(); imageAttrs.put(HTML.WIDTH_ATTR, "19"); imageAttrs.put(HTML.HEIGHT_ATTR, "18"); imageAttrs.put(HTML.BORDER_ATTR, "0"); if (clientSideToggle) { /** * With client side toggle, user has the option to specify open/closed images for the node (in addition to * the navigtion ones provided by the component.) */ String expandImgSrc = ""; String collapseImgSrc = ""; String nodeImageId = ""; UIComponent expandFacet = nodeTypeFacet.getFacet("expand"); if (expandFacet != null) { UIGraphic expandImg = (UIGraphic)expandFacet; expandImgSrc = expandImg.getUrl(); if (expandImg.isRendered()) { expandImg.setId(context.getViewRoot().createUniqueId()); nodeImageId = expandImg.getClientId(context); nodeImgFacet = expandFacet; } } UIComponent collapseFacet = nodeTypeFacet.getFacet("collapse"); if (collapseFacet != null) { UIGraphic collapseImg = (UIGraphic)collapseFacet; collapseImgSrc = collapseImg.getUrl(); if (collapseImg.isRendered()) { collapseImg.setId(context.getViewRoot().createUniqueId()); nodeImageId = collapseImg.getClientId(context); nodeImgFacet = collapseFacet; } } if (node.getChildCount() > 0) { String onClick = new StringBuffer() .append("treeNavClick('") .append(spanId) .append("', '") .append(image.getClientId(context)) .append("', '") .append(navSrc) .append("', '") .append(altSrc) .append("', '") .append(nodeImageId) .append("', '") .append(expandImgSrc) .append("', '") .append(collapseImgSrc) .append("', '") .append(tree.getId()) .append("', '") .append(nodeId) .append("');") .toString(); imageAttrs.put(HTML.ONCLICK_ATTR, onClick); imageAttrs.put(HTML.STYLE_ATTR, "cursor:hand;cursor:pointer"); } encodeRecursive(context, image); } else { // set up the expand control and remove whatever children (if any) this control had previously UICommand expandControl = tree.getExpandControl(); expandControl.setId(context.getViewRoot().createUniqueId()); expandControl.getChildren().clear(); UIParameter param = new UIParameter(); param.setName(tree.getId() + NamingContainer.SEPARATOR_CHAR + NAV_COMMAND); param.setValue(tree.getNodeId()); expandControl.getChildren().add(param); expandControl.getChildren().add(image); encodeRecursive(context, expandControl); } out.endElement(HTML.TD_ELEM); return nodeImgFacet; } private void encodeRecursive(FacesContext context, UIComponent component) throws IOException { /**@todo consider moving this common functionality to a base class or utility class */ if (!component.isRendered()) return; component.encodeBegin(context); if (component.getRendersChildren()) { component.encodeChildren(context); } else { List childList = component.getChildren(); for (int i=0; i < childList.size(); i++) { UIComponent child = (UIComponent)childList.get(i); encodeRecursive(context, child); } } component.encodeEnd(context); } /** * Encodes any stand-alone javascript functions that are needed. Uses either the extension filter, or a * user-supplied location for the javascript files. * * @param context FacesContext * @param component UIComponent * @throws IOException */ private void encodeJavascript(FacesContext context, UIComponent component) throws IOException { // check to see if javascript has already been written (which could happen if more than one tree on the same page) if (context.getExternalContext().getRequestMap().containsKey(JAVASCRIPT_ENCODED)) { return; } // render javascript function for client-side toggle (it won't be used if user has opted for server-side toggle) ResponseWriter out = context.getResponseWriter(); String javascriptLocation = (String)component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION); if (javascriptLocation == null) { AddResource.addJavaScriptHere(HtmlTreeRenderer.class, "javascript/tree.js", context); AddResource.addJavaScriptHere(HtmlTreeRenderer.class, "javascript/cookielib.js", context); } else { out.startElement(HTML.SCRIPT_ELEM, null); out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null); out.writeAttribute(HTML.SRC_ATTR, javascriptLocation + "/tree.js", null); out.endElement(HTML.SCRIPT_ELEM); out.startElement(HTML.SCRIPT_ELEM, null); out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null); out.writeAttribute(HTML.SRC_ATTR, javascriptLocation + "/cookielib.js", null); out.endElement(HTML.SCRIPT_ELEM); } context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE); } /** * Get the appropriate image src location. Uses the extension filter mechanism for images if no image * location has been specified. * * @param context The {@link FacesContex}. NOTE: If <code>null</code> then context path information * will not be used with extensions filter (assuming no imageLocation specified.) * @param component UIComponent * @param imageName The name of the image file to use. * @return The image src information. */ private String getImageSrc(FacesContext context, UIComponent component, String imageName) { String imageLocation = (String)component.getAttributes().get(JSFAttr.IMAGE_LOCATION); if (imageLocation == null) { return AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/" + imageName, context); } else { return imageLocation + "/" + imageName; } } /** * Helper method for getting the boolean value of an attribute. If the attribute is not specified, * then return the default value. * * @param component The component for which the attributes are to be checked. * @param attributeName The name of the boolean attribute. * @param defaultValue The default value of the attribute (to be returned if no value found). * @return boolean */ protected boolean getBoolean(UIComponent component, String attributeName, boolean defaultValue) { Boolean booleanAttr = (Boolean)component.getAttributes().get(attributeName); if (booleanAttr == null) { return defaultValue; } else { return booleanAttr.booleanValue(); } } private Map getCookieAttr(Cookie cookie) { Map attribMap = new HashMap(); try { String cookieValue = URLDecoder.decode(cookie.getValue(),ENCODING); String[] attribArray = cookieValue.split(ATTRIB_DELIM); for (int j = 0; j < attribArray.length; j++) { int index = attribArray[j].indexOf(ATTRIB_KEYVAL); String name = attribArray[j].substring(0, index); String value = attribArray[j].substring(index + 1); attribMap.put(name, value); } } catch (UnsupportedEncodingException e) { throw new RuntimeException("Error parsing tree cookies", e); } return attribMap; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -