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 private static HashMap threadMap = new HashMap(); 11 private static LinkedList threadList = new LinkedList(); 13 14 private static ThreadNode root = new ThreadNode(""); 15 16 private static ThreadNode[] visibles; 18 19 private String id; 20 21 private boolean isExpanded = true; 22 private boolean isCollapsed = false; 23 private boolean isHidden = false; 24 25 29 public static ThreadNode get(String processId, String threadId) { 30 ThreadNode n = (ThreadNode)threadMap.get(threadId); 31 if (n == null) { 32 n = new ThreadNode(threadId); 33 n.isExpanded = false; 34 35 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 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 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 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 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 122 private ThreadNode(String id) { 123 this.id = id; 124 setUserObject(this); 125 visibles = null; 127 } 128 129 public String getName() { 130 return id; 131 } 132 133 public String toString() { 134 return id + " " + isVisible(); 135 } 136 137 public String getShortName() { 138 String result = null; 139 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 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 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 visibles = null; 174 } 175 176 179 public void collapse() { 180 if (!isRoot()) { 181 ThreadNode p = (ThreadNode)getParent(); 182 p.collapseChildren(); 183 } 184 visibles = null; 186 } 187 188 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 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 |