📄 cstreebean.java
字号:
if(((labels[i] = rs.getString(labelColumns[i]))) == null || labels[i].length() == 0) break; if((urls[i] = rs.getString(urlColumns[i])) != null && urls[i].length() == 0) urls[i] = null; // force empty url columns to null for easy testing later... } for(j=0; j < i-1; j++) { if(tn[j] == null || !labels[j].equals(tn[j].getName())) { // then this is the first or a new item tn[j] = new TreeNode(nodeID++, labels[j], urls[j]); if(j > 0) tn[j-1].addChild(tn[j]); else root.addChild(tn[j]); continue; } } if(j > 0) tn[j-1].addChild( new TreeLeaf( labels[j], urls[j]) ); else root.addChild(new TreeLeaf(labels[j],urls[j])); } trees.put(treeName, root); // Store the tree in the static Hashtable } catch(SQLException e) { System.out.println(e.toString()); sqlerror = true; } try { con.close(); } catch(SQLException ignored) {} return !sqlerror; } /** * This method is just a call to the method below with a null argument, meaning that the tree * shouldn't get it's state from the session. */ public final String renderHTML() { return renderHTML((HttpServletRequest)null); } /** * This method first of all builds a Hashtable of open nodes from the request parameter * for easy lookup later. It then submits the root node of the tree named in the bean * parameter 'treeName' (or the default tree) for rendering as HTML. If a HttpServletRequest * is supplied, the method will try to obtain '<treeName>.oldopen' from the session. */ public final String renderHTML( HttpServletRequest req ) { if(populateJdbc() == false) { // ensures the tree is populated return "<p><strong>The CDTreeBean has not been successfully initialised. Check the server's stdout for details.</strong></p>"; } // Try to obtain 'oldopen' from the session if a request object was passed if(req != null) { HttpSession s = req.getSession(false); String tmp = s != null ? (String)s.getAttribute(treeName + ".oldopen") : null; if(tmp != null && tmp.length() > 0) oldopen = tmp; } // Make a hash table of the currently open nodes (from the URL parameter) StringTokenizer st = new StringTokenizer(oldopen, ":"); String token; while(st.hasMoreElements()) { try { token = st.nextToken(); openNodes.put(Integer.valueOf(token), token); // strb.append(token).append(":"); } catch(NumberFormatException e) { System.out.println("Warning: TreeBean was passed dodgy parameters!"); } } // now add the requested node to the open list (if not a close-request) if(open >= 0) { openNodes.put(new Integer(open), String.valueOf(open)); // strb.append(open); } else { openNodes.remove(new Integer(-open)); } StringBuffer strb = new StringBuffer(); Enumeration on = openNodes.elements(); while(on.hasMoreElements()) { strb.append(on.nextElement()).append(":"); } this.oldopen = strb.toString(); // Try to write 'oldopen' to the session if a request object was passed if(req != null) { HttpSession s = req.getSession(false); if(s != null) s.setAttribute(treeName + ".oldopen", oldopen); } StringBuffer html = new StringBuffer(renderHTML((Tree)trees.get(treeName))); return html.toString(); } private boolean isVisible(TreeNode node) { if(openNodes.containsKey(new Integer(node.getId()))) return true; return false; } /** * Renders the specified tree as HTML by iterating over all it's children. Will call itself * recursively for any visible node children. */ private final String renderHTML(Tree tree) { StringBuffer buf = new StringBuffer(); buf.append("\n<TABLE border=\"0\""); if(treeStyle != null && treeStyle.length() > 0) buf.append(" class=\"").append(treeStyle).append("\""); buf.append(">\n"); for( int i=0; i < tree.size(); i++ ) { TreeObject treeObject = tree.getChild(i); if( treeObject.getType() == Tree.NODE ) { TreeNode node = (TreeNode)treeObject; buf.append( renderNodeHTML(node) ); if( isVisible(node) ) { buf.append("<tr><td> </td><td>").append( renderHTML( node.getChildren() ) ).append("</td></tr>"); } } else { TreeLeaf leaf = (TreeLeaf)treeObject; buf.append( renderLeafHTML(leaf) ); } } buf.append("\n</TABLE>\n"); return buf.toString(); } /** * Renders the specified tree node as HTML. */ private final String renderNodeHTML(TreeNode node) { StringBuffer buf = new StringBuffer(); boolean visible = isVisible(node); buf.append( "<tr><td><a "); if(absopen == node.getId()) // active link reference buf.append("name='A' "); buf.append("href='").append(treePage).append("?"); buf.append("oldopen=").append(oldopen).append("&open=") .append(visible ? -(node.getId()) : node.getId()) .append("' target=\"_self\">" ); if( visible ) { buf.append( "<img src='").append(openImage).append("' border='0'>" ); } else { buf.append( "<img src='").append(closedImage).append("' border='0'>" ); } buf.append( "</a>" ); buf.append( "</td><td>" ); String link = node.getLink(); if( link != null ) { buf.append( "<a href='"); if(leafPage != null && leafPage.length() > 0) // if a leafPage is specified, then prepend to the link buf.append(leafPage); buf.append(link).append("' "); if(leafTarget != null && leafTarget.length() > 0) buf.append("target='").append(leafTarget).append("'"); buf.append(">" ).append( node.getName() ).append( "</a>" ); } else { buf.append( node.getName() ); } buf.append( "</td></tr>" ); return buf.toString(); } /** * Renders the specified tree leaf as HTML. */ private final String renderLeafHTML( TreeLeaf leaf ) { StringBuffer buf = new StringBuffer(); buf.append( "<tr><td>").append("<img src='").append(leafImage).append("' border='0'>") .append("</td><td><a href='"); if(leafPage != null && leafPage.length() > 0) // if a leafPage is specified, then prepend to the link buf.append(leafPage); buf.append(leaf.getLink()).append("' "); if(leafTarget != null && leafTarget.length() > 0) buf.append("target='").append(leafTarget).append("'"); buf.append(">").append( leaf.getName() ).append( "</a></td></tr>" ); return buf.toString(); } /** * Accepts a comma separated list of tokens and returns them as a string array. */ private String[] csvToArray(String csv) { StringTokenizer st = new StringTokenizer(csv,","); String[] buf = new String[st.countTokens()]; int i = 0; while(st.hasMoreTokens()) buf[i++] = st.nextToken(); return buf; } public String adjustScrollPosition() {// return "<SCRIPT> window.location.href=\"#A\"; if(window.scrollTo) scrollTo(0,(document.body ? document.body.scrollTop : pageYOffset) - 20); </SCRIPT>"; return "<SCRIPT> window.location.href=\"#A\"; window.scroll(0, -20); </SCRIPT>"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -