KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > treeview > TreeAction


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.strutsutil.treeview;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import javax.servlet.http.HttpSession JavaDoc;
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 /**
32  * 树形结构åˆ?始设置程åº?
33  * 1. 生æˆ?根节点
34  * 2. 从数æ?®åº“获å?–根节点下å­?节点集å?ˆ
35  * 3. 创建TreeControlä¿?存在HttpSession中
36  * getRootChildernNode方法返回的Collection是ViewNode的集å?ˆ
37  *
38  * å’ŒTreeControlå’ŒTreeControlTag 以å?ŠViewNode组æˆ?树形视图显示。
39  *
40  *
41  *
42  */

43
44 public abstract class TreeAction extends Action {
45
46   private final static String JavaDoc module = TreeAction.class.getName();
47
48   public ActionForward execute(ActionMapping mapping,
49                                ActionForm form,
50                                HttpServletRequest JavaDoc request,
51                                HttpServletResponse JavaDoc response) throws
52       Exception JavaDoc {
53
54     TreeControl control = getTreeControl(request);
55     if (control.getRoot() == null) {
56        if (request.getParameterMap().containsKey("init")) { //åˆ?始化树
57
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   /**
69    * 当调用无å?‚数,看æˆ?是åˆ?始化,é‡?æ–°å†?从å?Žå?°èŽ·å¾—root
70    *
71      //root node å?Œå?‘å…³è?” control
72      //æ¯?一个Root Node 对应一个control
73
74    * @param request HttpServletRequest
75    * @throws Exception
76    * @return TreeControl
77    */

78   private TreeControl getTreeControl(HttpServletRequest JavaDoc request) throws
79       Exception JavaDoc {
80     HttpSession JavaDoc session = request.getSession();
81     TreeControl control = (TreeControl) session.getAttribute(TreeControlTag.
82         TREE_NAME);
83     try{
84       //第一次调用
85
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 JavaDoc ex){
93       Debug.logError("[JdonFramework] getTreeControl error: " + ex, module);
94     }
95     return control;
96   }
97
98   /**
99    * æ ¹æ?®å?Žå?°Root得到显示的根节点
100    * @param request HttpServletRequest
101    * @throws Exception
102    * @return ViewNode
103    */

104   private ViewNode getRootViewNode(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
105     String JavaDoc 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 JavaDoc request, ViewNode root)
117       throws Exception JavaDoc {
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 JavaDoc ex) {
128       Debug.logError(ex, module);
129       throw new Exception JavaDoc(" tree not init ");
130     }
131   }
132
133   /**
134    * 刷新树
135    * @param request HttpServletRequest
136    * @param control TreeControl
137    * @throws Exception
138    */

139   private void refreshTree(HttpServletRequest JavaDoc request, TreeControl control) throws Exception JavaDoc {
140     String JavaDoc 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           //原æ?¥æ˜¯å? å?ˆçš„,现在需è¦?展开
147
//需�查询数�库,将该节点下的�节点查询获�
148
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     // Handle a select item event
157
String JavaDoc select = request.getParameter("select");
158     if (select != null) {
159         control.selectNode(select);
160     }
161
162   }
163
164
165   /**
166    * 需è¦?查询数æ?®åº“,将该节点下的å­?节点查询获å?–
167    * @param request
168    * @param node
169    */

170   public void fetchChildern(HttpServletRequest JavaDoc 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 JavaDoc ex) {
181       Debug.logError(ex, module);
182     }
183   }
184
185
186   public abstract Collection getRootChildern(HttpServletRequest JavaDoc request) throws
187       Exception JavaDoc;
188
189   public abstract String JavaDoc getRootNodeID(HttpServletRequest JavaDoc request) throws Exception JavaDoc;
190
191   public abstract void initRoot(HttpServletRequest JavaDoc request) throws Exception JavaDoc;
192
193   public abstract Collection getChildern(HttpServletRequest JavaDoc request,
194                                          ViewNode node) throws Exception JavaDoc;
195
196 }
197
Popular Tags