KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > viewer > ThreadNode


1 package org.coach.tracing.server.viewer;
2
3 import javax.swing.*;
4 import javax.swing.tree.*;
5 import java.awt.*;
6 import java.util.*;
7
8 public class ThreadNode extends DefaultMutableTreeNode {
9     // Maintain all identities in a linear map for fast lookup.
10
private static HashMap threadMap = new HashMap();
11     // display order list
12
private static LinkedList threadList = new LinkedList();
13
14     private static ThreadNode root = new ThreadNode("");
15
16     // Array of currently visible identies.
17
private static ThreadNode[] visibles;
18
19     private String JavaDoc id;
20         
21     private boolean isExpanded = true;
22     private boolean isCollapsed = false;
23     private boolean isHidden = false;
24
25     /**
26      * Search for an Identity object in the hierarchy tree.
27      * If the key is not in the tree the identity is obtained from the database.
28      */

29     public static ThreadNode get(String JavaDoc processId, String JavaDoc threadId) {
30         ThreadNode n = (ThreadNode)threadMap.get(threadId);
31         if (n == null) {
32             n = new ThreadNode(threadId);
33             n.isExpanded = false;
34             
35             // Obtain the parent node and add the new identity as its child.
36
ThreadNode parent = (ThreadNode)threadMap.get(processId);
37             if (parent == null) {
38                 parent = new ThreadNode(processId);
39                 parent.isExpanded = true;
40                 threadMap.put(processId, parent);
41                 threadList.add(parent);
42                 root.add(parent);
43             }
44             
45             parent.add(n);
46             
47             // Update the identity map for fast lookup.
48
threadMap.put(threadId, n);
49             threadList.add(n);
50         }
51         return n;
52     }
53
54     public static void clear() {
55         threadMap.clear();
56         threadList.clear();
57         root = new ThreadNode("");
58         visibles = null;
59     }
60     
61     public static ThreadNode getRootNode() {
62         return root;
63     }
64     
65     public static ThreadNode[] getVisibles() {
66         if (visibles == null) {
67             LinkedList s = new LinkedList();
68             Iterator it = threadList.iterator();
69             while(it.hasNext()) {
70                 ThreadNode n = (ThreadNode)it.next();
71                 // Iterate over all leaf object nodes.
72
if (n.isVisible()) {
73                     s.add(n);
74                 }
75             }
76             visibles = new ThreadNode[s.size()];
77             s.toArray(visibles);
78         }
79         return visibles;
80     }
81     
82     public static void moveLeft(ThreadNode n) {
83         int idx = threadList.indexOf(n);
84         if (idx > 0) {
85             threadList.remove(idx);
86             threadList.add(idx - 1, n);
87             if (!((ThreadNode)threadList.get(idx)).isVisible()) {
88                 // The previous node was not visible, so move again.
89
// Otherwise the user does not see the move on his screen.
90
moveLeft(n);
91             }
92             visibles = null;
93         }
94     }
95
96     public static void moveRight(ThreadNode n) {
97         int idx = threadList.indexOf(n);
98         if (idx >= 0 && idx < threadList.size() - 1) {
99             threadList.remove(idx);
100             threadList.add(idx + 1, n);
101             if (!((ThreadNode)threadList.get(idx)).isVisible()) {
102                 // The previous node was not visible, so move again.
103
// Otherwise the user does not see the move on his screen.
104
moveRight(n);
105             }
106             visibles = null;
107         }
108     }
109
110     public static void unhideAll() {
111         Iterator it = threadList.iterator();
112         while(it.hasNext()) {
113             ThreadNode n = (ThreadNode)it.next();
114             n.isHidden = false;
115         }
116     }
117
118     /**
119      * private constructor. The identity tree structure is created through the
120      * static get operation.
121      */

122     private ThreadNode(String JavaDoc id) {
123         this.id = id;
124         setUserObject(this);
125         // invalidate the list of visible identities.
126
visibles = null;
127     }
128     
129     public String JavaDoc getName() {
130         return id;
131     }
132
133     public String JavaDoc toString() {
134         return id + " " + isVisible();
135     }
136
137     public String JavaDoc getShortName() {
138         String JavaDoc result = null;
139         //trim from end to rightmost dot.
140
int index = id.lastIndexOf("_");
141         if (index > 0) {
142             result = id.substring(0, index);
143             index = result.lastIndexOf("/");
144             if (index > 0) {
145                 result = result.substring(index + 1);
146             }
147             return result;
148         }
149         return result;
150     }
151
152     // If a node is hidden, all its children are hidden too.
153
public void setHidden(boolean hide) {
154         this.isHidden = hide;
155         int cnt = getChildCount();
156         for (int i = 0; i < cnt; i++) {
157             ThreadNode mo = (ThreadNode)getChildAt(i);
158             mo.setHidden(hide);
159         }
160         // invalidate the list of visible identities.
161
visibles = null;
162     }
163     
164     public void expand() {
165         int cnt = getChildCount();
166         for (int i = 0; i < cnt; i++) {
167             ThreadNode n = (ThreadNode)getChildAt(i);
168             n.collapseChildren();
169         }
170         isExpanded = true;
171         isCollapsed = false;
172         // invalidate the list of visible identities.
173
visibles = null;
174     }
175
176     /**
177      * When a node is collapsed, its parent becomes visible.
178      */

179     public void collapse() {
180         if (!isRoot()) {
181             ThreadNode p = (ThreadNode)getParent();
182             p.collapseChildren();
183         }
184         // invalidate the list of visible identities.
185
visibles = null;
186     }
187
188     /**
189      * Collapse the whole subtree.
190      */

191     private void collapseChildren() {
192         if (!isRoot()) {
193             Enumeration e = depthFirstEnumeration();
194             while(e.hasMoreElements()) {
195                 ThreadNode n = (ThreadNode)e.nextElement();
196                 n.isCollapsed = true;
197             }
198             isCollapsed = false;
199             isExpanded = false;
200         }
201     }
202
203     public boolean isVisible() {
204         if (isRoot() || isHidden || isCollapsed) {
205             return false;
206         }
207         
208         if (!isLeaf() && isExpanded) {
209             return false;
210         }
211
212         return true;
213     }
214     
215     /**
216      * Returns the node that is used to map the GUI for this node.
217      */

218     public ThreadNode getVisible() {
219         if (isRoot()) {
220             return null;
221         }
222         
223         if (isVisible()) {
224             return this;
225         } else {
226             ThreadNode p = (ThreadNode)getParent();
227             return p.getVisible();
228         }
229     }
230        
231     public boolean isHidden() {
232         if (isRoot()) {
233             return false;
234         }
235         
236         if (isHidden) {
237             return true;
238         }
239             
240         return ((ThreadNode)getParent()).isHidden();
241     }
242 }
Popular Tags