1 15 16 package com.jdon.strutsutil.treeview; 17 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 import javax.servlet.http.HttpSession ; 21 import org.apache.struts.action.Action; 22 23 import org.apache.struts.action.ActionForm; 24 import org.apache.struts.action.ActionForward; 25 import org.apache.struts.action.ActionMapping; 26 27 import com.jdon.util.Debug; 28 29 import java.util.*; 30 31 43 44 public abstract class TreeAction extends Action { 45 46 private final static String module = TreeAction.class.getName(); 47 48 public ActionForward execute(ActionMapping mapping, 49 ActionForm form, 50 HttpServletRequest request, 51 HttpServletResponse response) throws 52 Exception { 53 54 TreeControl control = getTreeControl(request); 55 if (control.getRoot() == null) { 56 if (request.getParameterMap().containsKey("init")) { initRoot(request); 58 }else{ 59 Debug.logError("[JdonFramework]please init the tree at first !"); 60 } 61 } else { 62 refreshTree(request, control); 63 } 64 return (mapping.findForward(TreeControlTag.TREE_NAME)); 65 } 66 67 68 78 private TreeControl getTreeControl(HttpServletRequest request) throws 79 Exception { 80 HttpSession session = request.getSession(); 81 TreeControl control = (TreeControl) session.getAttribute(TreeControlTag. 82 TREE_NAME); 83 try{ 84 if ( (control == null) || (request.getParameterMap().isEmpty())) { 86 Debug.logVerbose("[JdonFramework] get tree from db..", module); 87 ViewNode root = getRootViewNode(request); 88 control = new TreeControl(root); 89 fetchRootChildern(request, root); 90 session.setAttribute(TreeControlTag.TREE_NAME, control); 91 } 92 }catch(Exception ex){ 93 Debug.logError("[JdonFramework] getTreeControl error: " + ex, module); 94 } 95 return control; 96 } 97 98 104 private ViewNode getRootViewNode(HttpServletRequest request) throws Exception { 105 String rooNodeID = getRootNodeID(request); 106 if (rooNodeID == null){ 107 Debug.logError("[JdonFramework] root Id is null ! " , module); 108 return null; 109 } 110 Debug.logVerbose("[JdonFramework] get the tree's root Node: " + rooNodeID, module); 111 ViewNode rNode = new ViewNode(rooNodeID, rooNodeID, true); 112 rNode.setRoot(true); 113 return rNode; 114 } 115 116 public void fetchRootChildern(HttpServletRequest request, ViewNode root) 117 throws Exception { 118 Collection childernNodeOfRoot = null; 119 try { 120 childernNodeOfRoot = getRootChildern(request); 121 122 Iterator iter = childernNodeOfRoot.iterator(); 123 while (iter.hasNext()) { 124 ViewNode nodeChild = (ViewNode) iter.next(); 125 root.addChild(nodeChild); 126 } 127 } catch (Exception ex) { 128 Debug.logError(ex, module); 129 throw new Exception (" tree not init "); 130 } 131 } 132 133 139 private void refreshTree(HttpServletRequest request, TreeControl control) throws Exception { 140 String key = request.getParameter("key"); 141 if (key != null) { 142 ViewNode node = control.findNode(key); 143 if (node != null) { 144 Debug.logVerbose("[JdonFramework]Found Node: " + key, module); 145 if (!node.isExpanded()) { 146 node.clearChildren(); 149 fetchChildern(request, node); 150 } 151 node.setExpanded(!node.isExpanded()); 152 } else { 153 Debug.logVerbose("[JdonFramework]Node is null : " + key); 154 } 155 } 156 String select = request.getParameter("select"); 158 if (select != null) { 159 control.selectNode(select); 160 } 161 162 } 163 164 165 170 public void fetchChildern(HttpServletRequest request, ViewNode node) { 171 Collection childernNode = null; 172 try { 173 childernNode = getChildern(request, node); 174 175 Iterator iter = childernNode.iterator(); 176 while (iter.hasNext()) { 177 ViewNode nodeChild = (ViewNode) iter.next(); 178 node.addChild(nodeChild); 179 } 180 } catch (Exception ex) { 181 Debug.logError(ex, module); 182 } 183 } 184 185 186 public abstract Collection getRootChildern(HttpServletRequest request) throws 187 Exception ; 188 189 public abstract String getRootNodeID(HttpServletRequest request) throws Exception ; 190 191 public abstract void initRoot(HttpServletRequest request) throws Exception ; 192 193 public abstract Collection getChildern(HttpServletRequest request, 194 ViewNode node) throws Exception ; 195 196 } 197 | Popular Tags |