KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.coach.tracing.api.*;
8 import org.coach.tracing.server.EventDataBase;
9
10 public class IdentityNode extends DefaultMutableTreeNode {
11     // Facet reference to the remote database.
12
private static EventDataBase eventDB;
13     // Maintain all identities in a linear map for fast lookup.
14
private static HashMap identityMap = new HashMap();
15     // display order list
16
private static LinkedList identityList = new LinkedList();
17     // Array of currently visible identies.
18
private static IdentityNode[] visibles;
19
20     private Identity identity;
21         
22     private boolean isExpanded = true;
23     private boolean isCollapsed = false;
24     private boolean isHidden = false;
25
26     // The root of the identity hierarchy.
27
private static IdentityNode root;
28
29     /**
30      * private constructor. The identity tree structure is created through the
31      * static get operation.
32      */

33     private IdentityNode(Identity id) {
34         identity = id;
35         setUserObject(this);
36         // invalidate the list of visible identities.
37
visibles = null;
38     }
39
40     /**
41      * Sets the event database reference and initializes the identity hierarchy.
42      */

43     public static void setEventDB(EventDataBase db) {
44         eventDB = db;
45         identityMap.clear();
46         identityList.clear();
47         root = new IdentityNode(new Identity(IdentityKind.CCM_NODE, "root", "", (long)-1));
48         visibles = null;
49     }
50             
51     /**
52      * Search for an Identity object in the hierarchy tree.
53      * If the key is not in the tree the identity is obtained from the database.
54      */

55     public static IdentityNode get(Long JavaDoc key) {
56         if (key == null || key.longValue() == -1) {
57             return root;
58         }
59         IdentityNode n = (IdentityNode)identityMap.get(key);
60         if (n == null) {
61             try {
62                 // The identity is not in the map, read it from the database.
63
Identity id = eventDB.getIdentity(key);
64                 n = new IdentityNode(id);
65                 
66                 // Obtain the parent node and add the new identity as its child.
67
IdentityNode parent = get(new Long JavaDoc(id.linkKey));
68                 parent.add(n);
69                 
70                 // Update the identity map for fast lookup.
71
identityMap.put(key, n);
72                 identityList.add(n);
73             } catch (Exception JavaDoc e) {
74                 e.printStackTrace();
75             }
76         }
77         return n;
78     }
79
80     public static IdentityNode[] getVisibles() {
81         if (visibles == null) {
82             LinkedList s = new LinkedList();
83             Iterator it = identityList.iterator();
84             while(it.hasNext()) {
85                 IdentityNode n = (IdentityNode)it.next();
86                 if (n.isVisible()) {
87                     s.add(n);
88                 }
89             }
90             visibles = new IdentityNode[s.size()];
91             s.toArray(visibles);
92         }
93         return visibles;
94     }
95     
96     public static void moveLeft(IdentityNode n) {
97         int idx = identityList.indexOf(n);
98         if (idx > 0) {
99             identityList.remove(idx);
100             identityList.add(idx - 1, n);
101             if (!((IdentityNode)identityList.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
moveLeft(n);
105             }
106             visibles = null;
107         }
108     }
109
110     public static void moveRight(IdentityNode n) {
111         int idx = identityList.indexOf(n);
112         if (idx >= 0 && idx < identityList.size() - 1) {
113             identityList.remove(idx);
114             identityList.add(idx + 1, n);
115             if (!((IdentityNode)identityList.get(idx)).isVisible()) {
116                 // The previous node was not visible, so move again.
117
// Otherwise the user does not see the move on his screen.
118
moveRight(n);
119             }
120             visibles = null;
121         }
122     }
123
124     public static void unhideAll() {
125         root.setHidden(false);
126     }
127
128     /**
129      * Return the root of the identity hierarchy.
130      * The root is only a holder for the tree and does not represent an identity itself.
131      */

132     public static IdentityNode getRootNode() {
133         return root;
134     }
135     
136     public IdentityKind getKind() {
137         return identity.kind;
138     }
139
140     public String JavaDoc getName() {
141         return identity.name;
142     }
143
144     public String JavaDoc getType() {
145         return identity.type;
146     }
147
148     public String JavaDoc toString() {
149         return getShortName() + " " + isVisible();
150     }
151
152     public String JavaDoc getShortName() {
153         String JavaDoc result = "";
154
155         //trim from end to rightmost dot.
156
int index = identity.name.indexOf("@");
157         if (index > 0) {
158             result = identity.name.substring(0, index);
159             index = result.lastIndexOf("/");
160             if (index > 0) {
161                 result = result.substring(index + 1);
162             }
163             return result;
164         }
165
166         //trim from end to rightmost dot.
167
index = identity.name.lastIndexOf("/");
168
169         if ((index > 0) && (index < identity.name.length() - 1)) {
170             result = identity.name.substring(index + 1);
171         } else {
172             result = identity.name;
173         }
174         return result;
175     }
176
177     // If a node is hidden, all its children are hidden too.
178
public void setHidden(boolean hide) {
179         this.isHidden = hide;
180         int cnt = getChildCount();
181         for (int i = 0; i < cnt; i++) {
182             IdentityNode mo = (IdentityNode)getChildAt(i);
183             mo.setHidden(hide);
184         }
185         // invalidate the list of visible identities.
186
visibles = null;
187     }
188     
189     public void expand() {
190         int cnt = getChildCount();
191         for (int i = 0; i < cnt; i++) {
192             IdentityNode n = (IdentityNode)getChildAt(i);
193             n.collapseChildren();
194         }
195         isExpanded = true;
196         isCollapsed = false;
197         // invalidate the list of visible identities.
198
visibles = null;
199     }
200
201     /**
202      * When a node is collapsed, its parent becomes visible.
203      */

204     public void collapse() {
205         if (!isRoot()) {
206             IdentityNode p = (IdentityNode)getParent();
207             p.collapseChildren();
208         }
209         // invalidate the list of visible identities.
210
visibles = null;
211     }
212
213     /**
214      * Collapse the whole subtree.
215      */

216     private void collapseChildren() {
217         if (!isRoot()) {
218             Enumeration e = depthFirstEnumeration();
219             while(e.hasMoreElements()) {
220                 IdentityNode n = (IdentityNode)e.nextElement();
221                 n.isCollapsed = true;
222             }
223             isCollapsed = false;
224             isExpanded = false;
225         }
226     }
227
228     public boolean isVisible() {
229         if (isRoot() || isHidden || isCollapsed) {
230             return false;
231         }
232         
233         if (!isLeaf() && isExpanded) {
234             return false;
235         }
236
237         return true;
238     }
239     
240     /**
241      * Returns the node that is used to map the GUI for this node.
242      */

243     public IdentityNode getVisible() {
244         if (isRoot()) {
245             return null;
246         }
247         
248         if (isVisible()) {
249             return this;
250         } else {
251             IdentityNode p = (IdentityNode)getParent();
252             return p.getVisible();
253         }
254     }
255        
256     public boolean isHidden() {
257         if (isRoot()) {
258             return false;
259         }
260         
261         if (isHidden) {
262             return true;
263         }
264             
265         return ((IdentityNode)getParent()).isHidden();
266     }
267
268     public static String JavaDoc getObjectName(Long JavaDoc key) {
269         return get(key).getName();
270     }
271
272     public static String JavaDoc getObjectType(Long JavaDoc key) {
273         return get(key).getType();
274     }
275     
276     public static String JavaDoc getProcessId(Long JavaDoc key) {
277         IdentityNode n = get(key);
278         while (n.getKind() != IdentityKind.CCM_PROCESS) {
279             n = (IdentityNode)n.getParent();
280             if (n.isRoot()) {
281                 return "";
282             }
283         }
284         return n.getName();
285     }
286
287     public static String JavaDoc getNodeName(Long JavaDoc key) {
288         IdentityNode n = get(key);
289         while (n.getKind() != IdentityKind.CCM_NODE) {
290             n = (IdentityNode)n.getParent();
291             if (n.isRoot()) {
292                 return "";
293             }
294         }
295         return n.getName();
296     }
297
298     public static String JavaDoc getNodeIp(Long JavaDoc key) {
299         IdentityNode n = get(key);
300         while (n.getKind() != IdentityKind.CCM_NODE) {
301             n = (IdentityNode)n.getParent();
302             if (n.isRoot()) {
303                 return "";
304             }
305         }
306         return n.getType();
307     }
308
309    public static String JavaDoc getCmpName(Long JavaDoc key) {
310         IdentityNode n = get(key);
311         while (n.getKind() != IdentityKind.CCM_COMPONENT) {
312             n = (IdentityNode)n.getParent();
313             if (n.isRoot()) {
314                 return "";
315             }
316         }
317         return n.getName();
318     }
319
320     public static String JavaDoc getCmpType(Long JavaDoc key) {
321         IdentityNode n = get(key);
322         while (n.getKind() != IdentityKind.CCM_COMPONENT) {
323             n = (IdentityNode)n.getParent();
324             if (n.isRoot()) {
325                 return "";
326             }
327         }
328         return n.getType();
329     }
330
331     public static String JavaDoc getCntName(Long JavaDoc key) {
332         IdentityNode n = get(key);
333         while (n.getKind() != IdentityKind.CCM_CONTAINER) {
334             n = (IdentityNode)n.getParent();
335             if (n.isRoot()) {
336                 return "";
337             }
338         }
339         return n.getName();
340     }
341
342     public static String JavaDoc getCntType(Long JavaDoc key) {
343         IdentityNode n = get(key);
344         while (n.getKind() != IdentityKind.CCM_CONTAINER) {
345             n = (IdentityNode)n.getParent();
346             if (n.isRoot()) {
347                 return "";
348             }
349         }
350         return n.getType();
351     }
352 }
353
Popular Tags