KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
19 import java.util.*;
20
21 /**
22  * 树形结构控制类,被类被ä¿?存在用户Session中,
23  * 用以记载树形结构的一些特点。
24  *
25  *
26  * ViewNode是记录节点微观信æ?¯ï¼Œè?šç„¦æ¯?个节点
27  * 该类则关注根节点和所有节点。
28  *
29  * 和节点ViewNodeå…±å?Œä½¿ç”¨ï¼Œå?¯è®°å½•æ ‘形结构的全部信æ?¯
30  *
31  */

32
33 public class TreeControl implements Serializable JavaDoc {
34
35   public static boolean init = true; //是å?¦åˆ?始化
36
//当�选中的节点
37
private ViewNode selected = null;
38   private ViewNode root = null;
39   private HashMap registry = new HashMap();
40
41
42   public TreeControl(ViewNode root) {
43     this.root = root;
44     root.setTree(this);
45     root.setLast(true);
46   }
47
48   public ViewNode getRoot() {
49     return (this.root);
50   }
51
52
53   public ViewNode findNode(String JavaDoc key) {
54     return ( (ViewNode) registry.get(key));
55   }
56
57   public void selectNode(String JavaDoc key) {
58     if (selected != null) {
59       selected.setSelected(false);
60       selected = null;
61     }
62     selected = findNode(key);
63     if (selected != null)
64       selected.setSelected(true);
65
66   }
67
68   public void addNode(ViewNode node) throws
69       IllegalArgumentException JavaDoc {
70     node.setTree(this);
71     registry.put(node.getKey(), node);
72   }
73
74   public int getTreeWidth() {
75     if (root == null)
76       return (0);
77     else
78       return (getWidth(root));
79
80   }
81
82   /**
83    * 该节点到树底端的è·?离
84    * 与node.getDepth()区别:
85    * å?Žè€…是该节点到树顶的è·?离。
86    * @param node
87    * @return
88    */

89   public int getWidth(ViewNode node) {
90     int width = node.getDepth();
91     ViewNode children[] = node.findChildren();
92     for (int i = 0; i < children.length; i++) {
93       int current = getWidth(children[i]);
94       if (current > width)
95         width = current;
96     }
97     return (width);
98   }
99
100
101 }
102
Popular Tags