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 private static EventDataBase eventDB; 13 private static HashMap identityMap = new HashMap(); 15 private static LinkedList identityList = new LinkedList(); 17 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 private static IdentityNode root; 28 29 33 private IdentityNode(Identity id) { 34 identity = id; 35 setUserObject(this); 36 visibles = null; 38 } 39 40 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 55 public static IdentityNode get(Long 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 Identity id = eventDB.getIdentity(key); 64 n = new IdentityNode(id); 65 66 IdentityNode parent = get(new Long (id.linkKey)); 68 parent.add(n); 69 70 identityMap.put(key, n); 72 identityList.add(n); 73 } catch (Exception 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 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 moveRight(n); 119 } 120 visibles = null; 121 } 122 } 123 124 public static void unhideAll() { 125 root.setHidden(false); 126 } 127 128 132 public static IdentityNode getRootNode() { 133 return root; 134 } 135 136 public IdentityKind getKind() { 137 return identity.kind; 138 } 139 140 public String getName() { 141 return identity.name; 142 } 143 144 public String getType() { 145 return identity.type; 146 } 147 148 public String toString() { 149 return getShortName() + " " + isVisible(); 150 } 151 152 public String getShortName() { 153 String result = ""; 154 155 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 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 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 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 visibles = null; 199 } 200 201 204 public void collapse() { 205 if (!isRoot()) { 206 IdentityNode p = (IdentityNode)getParent(); 207 p.collapseChildren(); 208 } 209 visibles = null; 211 } 212 213 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 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 getObjectName(Long key) { 269 return get(key).getName(); 270 } 271 272 public static String getObjectType(Long key) { 273 return get(key).getType(); 274 } 275 276 public static String getProcessId(Long 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 getNodeName(Long 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 getNodeIp(Long 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 getCmpName(Long 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 getCmpType(Long 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 getCntName(Long 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 getCntType(Long 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 |