step 11、构建 树形结构类
java 代码
- /**
- * 地理信息数结构显示
- */
- package com.winchannel.common.tree;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author huguoqing
- *
- */
- public class GeographyTreeViewManager {
- public String strTreeStylePath;
- protected boolean bVerticalLine;
- protected GeographyTreeItem root;
- protected List childList;
- public GeographyTreeViewManager()
- {
- strTreeStylePath = "/images/treeview/treeview_default/";
- bVerticalLine = true;
- root = null;
- childList = new ArrayList();
- root = new GeographyTreeItem(null);
- root.setDisplayChildren(true);
- }
- public GeographyTreeViewManager(String strRoot)
- {
- strTreeStylePath = "/images/treeview/treeview_default/";
- bVerticalLine = true;
- root = null;
- childList = new ArrayList();
- root = new GeographyTreeItem(strRoot);
- root.setDisplayChildren(true);
- }
- public GeographyTreeViewManager(GeographyTreeItem root)
- {
- strTreeStylePath = "/images/treeview/treeview_default/";
- bVerticalLine = true;
- this.root = null;
- childList = new ArrayList();
- this.root = root;
- root.setDisplayChildren(true);
- }
- /**
- * 设置树结构样式及图标存放路径
- * @param strPath
- */
- public void setTreeStylePath(String strPath)
- {
- strTreeStylePath = strPath;
- }
- /**
- * 获取树结构样式及图标存放路径
- * @return
- */
- public String getTreeStylePath()
- {
- return strTreeStylePath;
- }
- /**
- *
- * @param bLine
- */
- public void setVerticalLine(boolean bLine)
- {
- bVerticalLine = bLine;
- }
- /**
- *
- * @return boolean
- */
- public boolean getVerticalLine()
- {
- return bVerticalLine;
- }
- /**
- *
- * @return
- */
- public GeographyTreeItem getRoot()
- {
- return root;
- }
- public void addItem(GeographyTreeItem item)
- {
- root.addChild(item);
- }
- public String getScript()
- {
- StringBuffer stHTML = new StringBuffer(10000);
- stHTML.append("<!-- TreeView component -->/\r/\n");
- stHTML.append("<LINK href=\"" + strTreeStylePath + "treeview.css\" rel=stylesheet>/\r/\n");
- stHTML.append("/\r/\n");
- stHTML.append("<SCRIPT language=JavaScript>/\r/\n");
- stHTML.append("《!--/\r/\n");
- stHTML.append("var treeview_ob_tb, treeview_ob_url, treeview_selected_node_id;/\r/\n");
- stHTML.append("function treeview_ob_wk(os, url) {/\r/\n");
- stHTML.append("/\tvar ot = os.parentNode.nextSibling.firstChild.nextSibling;/\r/\n");
- stHTML.append("/\tvar lensrc = (os.src.length - 8);/\r/\n");
- stHTML.append("/\tvar s = os.src.substr(lensrc, 8);/\r/\n");
- stHTML.append("/\tif (s == /\"inus.gif/\") {/\r/\n");
- stHTML.append("/\t/\tot.style.display = /\"none/\";/\r/\n");
- stHTML.append("/\t/\tos.src = /\"" + strTreeStylePath + "plusik.gif/\";/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("/\tif (s == /\"usik.gif/\") {/\r/\n");
- stHTML.append("/\t/\tot.style.display = /\"block/\";/\r/\n");
- stHTML.append("/\t/\tos.src = /\"" + strTreeStylePath + "minus.gif/\";/\r/\n");
- stHTML.append("/\t/\tif (url != /\"/\") {/\r/\n");
- stHTML.append("/\t/\t/\tvar s = os.parentNode.nextSibling.firstChild.nextSibling.innerText;/\r/\n");
- stHTML.append("/\t/\t/\tif (s != /\"正读取数据,请稍候 .../\") {/\r/\n");
- stHTML.append("/\t/\t/\t/\treturn;/\r/\n");
- stHTML.append("/\t/\t/\t}/\r/\n");
- stHTML.append("/\t/\t/\ttreeview_ob_url = url;/\r/\n");
- stHTML.append("/\t/\t/\ttreeview_ob_tb = os;/\r/\n");
- stHTML.append(" oDownload.startDownload(treeview_ob_url, treeview_ob_tm);/\r/\n");
- stHTML.append("/\t/\t/\t//window.setTimeout(/\"treeview_ob_tm()/\", 100);/\r/\n");
- stHTML.append("/\t/\t}/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("/\tif (s == /\"ik_l.gif/\") {/\r/\n");
- stHTML.append("/\t/\tot.style.display = /\"block/\";/\r/\n");
- stHTML.append("/\t/\tos.src = /\"" + strTreeStylePath + "minus_l.gif/\";/\r/\n");
- stHTML.append("/\t/\tif (url != /\"/\") {/\r/\n");
- stHTML.append("/\t/\t/\tvar s = os.parentNode.nextSibling.firstChild.nextSibling.innerText;/\r/\n");
- stHTML.append("/\t/\t/\tif (s != /\"正读取数据,请稍候 .../\") {/\r/\n");
- stHTML.append("/\t/\t/\t/\treturn;/\r/\n");
- stHTML.append("/\t/\t/\t}/\r/\n");
- stHTML.append("/\t/\t/\ttreeview_ob_url = url;/\r/\n");
- stHTML.append("/\t/\t/\ttreeview_ob_tb = os;/\r/\n");
- stHTML.append(" oDownload.startDownload(treeview_ob_url, treeview_ob_tm);/\r/\n");
- stHTML.append("/\t/\t/\t//window.setTimeout(/\"treeview_ob_tm()/\", 100);/\r/\n");
- stHTML.append("/\t/\t}/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("/\tif (s == /\"us_l.gif/\") {/\r/\n");
- stHTML.append("/\t/\tot.style.display = /\"none/\";/\r/\n");
- stHTML.append("/\t/\tos.src = /\"" + strTreeStylePath + "plusik_l.gif/\";/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("}/\r/\n");
- stHTML.append("/\r/\n");
- stHTML.append("function treeview_ob_tm(s) {/\r/\n");
- stHTML.append("/\t//var xmlhttp = new ActiveXObject(/\"Microsoft.XMLHTTP/\");/\r/\n");
- stHTML.append("/\t//xmlhttp.open(/\"GET/\", treeview_ob_url, false);/\r/\n");
- stHTML.append("/\t//xmlhttp.send(/\"/\");/\r/\n");
- stHTML.append("/\t//treeview_ob_tb.parentNode.nextSibling.firstChild.nextSibling.outerHTML = xmlhttp.responseText;/\r/\n");
- stHTML.append("/\ttreeview_ob_tb.parentNode.nextSibling.firstChild.nextSibling.outerHTML = s;/\r/\n");
- stHTML.append("}/\r/\n");
- stHTML.append("/\r/\n");
- stHTML.append("function treeview_ob_ft(os) {/\r/\n");
- //stHTML.append("/\talert(/\"柴外/\");/\r/\n");
- stHTML.append("/\tif (typeof op != /\"undefined/\") {/\r/\n");
- stHTML.append("/\t/\top.style.backgroundColor = bg;/\r/\n");
- stHTML.append("/\t/\top.style.border = /\"none/\";/\r/\n");
- stHTML.append(" }/\r/\n");
- stHTML.append("/\tbg = os.style.backgroundColor;/\r/\n");
- stHTML.append("/\tos.style.backgroundColor = /\"ccddee/\";/\r/\n");
- stHTML.append("/\tos.style.border = /\"0px solid #666666/\";/\r/\n");
- stHTML.append("/\ttreeview_selected_node_id = os.id;/\r/\n");
- stHTML.append("/\top = os;/\r/\n");
- stHTML.append("}/\r/\n");
- stHTML.append("/\r/\n");
- stHTML.append("function treeview_ob_modify(os) {/\r/\n");
- stHTML.append("/\ttreeview_ob_ft(os);/\r/\n");
- stHTML.append("/\tif(os == null || os.value == '')/\r/\n");
- stHTML.append("/\t{/\r/\n");
- stHTML.append("/\t/\treturn;/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("/\t_edit();/\r/\n");
- stHTML.append("}/\r/\n");
- stHTML.append("/\r/\n");
- stHTML.append("function treeview_ob_os(e){/\r/\n");
- stHTML.append("/\tvar os = e.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.firstChild;/\r/\n");
- stHTML.append("/\tif (os != null) {/\r/\n");
- stHTML.append("/\tif ((typeof os != /\"undefined/\") && (os.tagName == /\"IMG/\")) {/\r/\n");
- stHTML.append("/\t/\tvar lensrc = (os.src.length - 8);/\r/\n");
- stHTML.append("/\t/\tvar s = os.src.substr(lensrc, 8);/\r/\n");
- stHTML.append("/\t/\tif ((s == /\"inus.gif/\") || (s == /\"usik.gif/\") || (s == /\"us_l.gif/\") || (s == /\"ik_l.gif/\")) {/\r/\n");
- stHTML.append("/\t/\t/\te.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.firstChild.click();/\r/\n");
- stHTML.append("/\t/\t}/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("/\telse {/\r/\n");
- stHTML.append("/\t/\ttreeview_ob_os(e.parentNode);/\r/\n");
- stHTML.append("/\t}/\r/\n");
- stHTML.append("}}/\r/\n");
- stHTML.append("/\r/\n");
- stHTML.append("//--》/\r/\n");
- stHTML.append("</SCRIPT>/\r/\n");
- return stHTML.toString();
- }
- public String getRootHTML()
- {
- StringBuffer stHTML = new StringBuffer(10000);
- stHTML.append(root.getHTML(strTreeStylePath, bVerticalLine));
- stHTML.append("<!-- End Of TreeView-->/\r/\n");
- return stHTML.toString();
- }
- public String getHTML()
- {
- StringBuffer stHTML = new StringBuffer(10000);
- stHTML.append(getScript());
- stHTML.append(getRootHTML());
- return stHTML.toString();
- }
- public static void main(String argv[])
- {
- GeographyTreeViewManager tvm = new GeographyTreeViewManager();
- GeographyTreeItem item = new GeographyTreeItem("a1");
- item.addChild(new GeographyTreeItem("a1_1"));
- item.addChild(new GeographyTreeItem("a1_2"));
- item.addChild(new GeographyTreeItem("a1_3"));
- item.addChild(new GeographyTreeItem("a1_4"));
- item.addChild(new GeographyTreeItem("a1_5"));
- GeographyTreeItem item2 = new GeographyTreeItem("a2");
- GeographyTreeItem item2_1 = new GeographyTreeItem("a2_1");
- item2_1.addChild(new GeographyTreeItem("a2_1_1"));
- item2.addChild(item2_1);
- item2.addChild(new GeographyTreeItem("a2_2"));
- tvm.addItem(item);
- tvm.addItem(item2);
- System.out.println(tvm.getHTML());
- }
- }