1 11 package org.eclipse.debug.internal.ui.viewers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jface.viewers.TreePath; 17 18 23 public class ModelNode { 24 25 private Object fElement; private boolean fIsContainer; private ModelNode fParent; private ModelNode[] fChildren; private boolean fDisposed; 31 public ModelNode(ModelNode parent, Object element) { 32 fParent = parent; 33 fElement = element; 34 } 35 36 public synchronized Object getElement() { 37 return fElement; 38 } 39 40 public synchronized void remap(Object element) { 41 fElement = element; 42 } 43 44 public ModelNode getParentNode() { 45 return fParent; 46 } 47 48 public synchronized boolean isContainer() { 49 return fIsContainer; 50 } 51 52 public synchronized ModelNode[] getChildrenNodes() { 53 return fChildren; 54 } 55 56 public synchronized boolean isDisposed() { 57 return fDisposed; 58 } 59 60 public synchronized void dispose() { 61 fDisposed = true; 62 ModelNode[] childrenNodes = getChildrenNodes(); 63 if (childrenNodes != null) { 64 for (int i = 0; i < childrenNodes.length; i++) { 65 childrenNodes[i].dispose(); 66 } 67 } 68 } 69 70 75 public synchronized boolean correspondsTo(TreePath path) { 76 int index = path.getSegmentCount() - 1; 77 ModelNode node = this; 78 while (index >= 0 && node != null) { 79 Object pathElement = path.getSegment(index); 80 if (pathElement.equals(node.getElement())) { 81 index--; 82 node = node.getParentNode(); 83 } else { 84 return false; 85 } 86 } 87 return index == -1; 88 } 89 90 95 public synchronized TreePath getTreePath() { 96 List path = new ArrayList (); 97 ModelNode node = this; 98 while (node != null) { 99 path.add(0, node.getElement()); 100 node = node.getParentNode(); 101 } 102 return new TreePath(path.toArray()); 103 } 104 105 110 public synchronized void addChild(ModelNode child) { 111 if (fChildren == null) { 112 fChildren = new ModelNode[] {child}; 113 } else { 114 ModelNode[] kids = new ModelNode[fChildren.length + 1]; 115 System.arraycopy(fChildren, 0, kids, 0, fChildren.length); 116 kids[fChildren.length] = child; 117 fChildren = kids; 118 } 119 } 120 121 126 public synchronized void removeChild(ModelNode child) { 127 if (fChildren != null) { 128 for (int i = 0; i < fChildren.length; i++) { 129 ModelNode kid = fChildren[i]; 130 if (child == kid) { 131 ModelNode[] newNodes= new ModelNode[fChildren.length - 1]; 132 System.arraycopy(fChildren, 0, newNodes, 0, i); 133 if (i < newNodes.length) { 134 System.arraycopy(fChildren, i + 1, newNodes, i, newNodes.length - i); 135 } 136 fChildren = newNodes; 137 return; 138 } 139 } 140 } 141 } 142 143 148 public synchronized void setChildren(ModelNode[] children) { 149 if (children != null && children.length == 0) { 150 fChildren = null; 151 setIsContainer(false); 152 } else { 153 fChildren = children; 154 } 155 } 156 157 162 public synchronized int getChildCount() { 163 if (fChildren == null) { 164 if (isContainer()) { 165 return 1; 166 } 167 return 0; 168 } 169 return fChildren.length; 170 } 171 172 177 public synchronized int getChildIndex(ModelNode child) { 178 if (fChildren != null) { 179 for (int i = 0; i < fChildren.length; i++) { 180 if (child == fChildren[i]) { 181 return i; 182 } 183 } 184 } 185 return -1; 186 } 187 188 193 public synchronized void setIsContainer(boolean container) { 194 fIsContainer = container; 195 } 196 197 public String toString() { 198 StringBuffer buf = new StringBuffer (); 199 if (isDisposed()) { 200 buf.append("[DISPOSED] "); } 202 if (isContainer()) { 203 buf.append("[+] "); } 205 buf.append(getElement()); 206 return buf.toString(); 207 } 208 209 } 210 | Popular Tags |