1 11 package org.eclipse.debug.internal.ui.viewers.model.provisional; 12 13 14 15 16 24 public class ModelDelta implements IModelDelta { 25 26 private IModelDelta fParent; 27 private Object fElement; 28 private int fFlags; 29 private ModelDelta[] fNodes = EMPTY_NODES; 30 private Object fReplacement; 31 private int fIndex = -1; 32 private int fChildCount = -1; 33 private static final ModelDelta[] EMPTY_NODES = new ModelDelta[0]; 34 35 41 public ModelDelta(Object element, int flags) { 42 fElement = element; 43 fFlags = flags; 44 } 45 46 54 public ModelDelta(Object element, Object replacement, int flags) { 55 fElement = element; 56 fReplacement = replacement; 57 fFlags = flags; 58 } 59 60 68 public ModelDelta(Object element, int index, int flags) { 69 fElement = element; 70 fIndex = index; 71 fFlags = flags; 72 } 73 74 83 public ModelDelta(Object element, int index, int flags, int childCount) { 84 fElement = element; 85 fIndex = index; 86 fFlags = flags; 87 fChildCount = childCount; 88 } 89 90 93 public Object getElement() { 94 return fElement; 95 } 96 97 100 public int getFlags() { 101 return fFlags; 102 } 103 104 112 public ModelDelta addNode(Object element, int flags) { 113 ModelDelta node = new ModelDelta(element, flags); 114 node.setParent(this); 115 addDelta(node); 116 return node; 117 } 118 119 125 public ModelDelta getChildDelta(Object element) { 126 if (fNodes != null) { 127 for (int i = 0; i < fNodes.length; i++) { 128 ModelDelta delta = fNodes[i]; 129 if (element.equals(delta.getElement())) { 130 return delta; 131 } 132 } 133 } 134 return null; 135 } 136 137 147 public ModelDelta addNode(Object element, Object replacement, int flags) { 148 ModelDelta node = new ModelDelta(element, replacement, flags); 149 node.setParent(this); 150 addDelta(node); 151 return node; 152 } 153 154 163 public ModelDelta addNode(Object element, int index, int flags) { 164 ModelDelta node = new ModelDelta(element, index, flags); 165 node.setParent(this); 166 addDelta(node); 167 return node; 168 } 169 170 180 public ModelDelta addNode(Object element, int index, int flags, int numChildren) { 181 ModelDelta node = new ModelDelta(element, index, flags, numChildren); 182 node.setParent(this); 183 addDelta(node); 184 return node; 185 } 186 187 192 void setParent(ModelDelta node) { 193 fParent = node; 194 } 195 196 199 public IModelDelta getParentDelta() { 200 return fParent; 201 } 202 203 206 public Object getReplacementElement() { 207 return fReplacement; 208 } 209 210 213 public int getIndex() { 214 return fIndex; 215 } 216 217 220 public IModelDelta[] getChildDeltas() { 221 return fNodes; 222 } 223 224 private void addDelta(ModelDelta delta) { 225 if (fNodes.length == 0) { 226 fNodes = new ModelDelta[]{delta}; 227 } else { 228 ModelDelta[] nodes = new ModelDelta[fNodes.length + 1]; 229 System.arraycopy(fNodes, 0, nodes, 0, fNodes.length); 230 nodes[fNodes.length] = delta; 231 fNodes = nodes; 232 } 233 } 234 235 public String toString() { 236 StringBuffer buf = new StringBuffer (); 237 buf.append("Model Delta Start\n"); appendDetail(buf, this); 239 buf.append("Model Delta End\n"); return buf.toString(); 241 } 242 243 private void appendDetail(StringBuffer buf, IModelDelta delta) { 244 buf.append("\tElement: "); buf.append(delta.getElement()); 246 buf.append('\n'); 247 buf.append("\t\tFlags: "); int flags = delta.getFlags(); 249 if (flags == 0) { 250 buf.append("NO_CHANGE"); } else { 252 if ((flags & IModelDelta.ADDED) > 0) { 253 buf.append("ADDED | "); } 255 if ((flags & IModelDelta.CONTENT) > 0) { 256 buf.append("CONTENT | "); } 258 if ((flags & IModelDelta.EXPAND) > 0) { 259 buf.append("EXPAND | "); } 261 if ((flags & IModelDelta.INSERTED) > 0) { 262 buf.append("INSERTED | "); } 264 if ((flags & IModelDelta.REMOVED) > 0) { 265 buf.append("REMOVED | "); } 267 if ((flags & IModelDelta.REPLACED) > 0) { 268 buf.append("REPLACED | "); } 270 if ((flags & IModelDelta.SELECT) > 0) { 271 buf.append("SELECT | "); } 273 if ((flags & IModelDelta.STATE) > 0) { 274 buf.append("STATE | "); } 276 if ((flags & IModelDelta.INSTALL) > 0) { 277 buf.append("INSTALL | "); } 279 if ((flags & IModelDelta.UNINSTALL) > 0) { 280 buf.append("UNINSTALL | "); } 282 } 283 buf.append('\n'); 284 buf.append("\t\tIndex: "); buf.append(delta.getIndex()); 286 buf.append(" Child Count: "); buf.append(delta.getChildCount()); 288 buf.append('\n'); 289 IModelDelta[] nodes = delta.getChildDeltas(); 290 for (int i = 0; i < nodes.length; i++) { 291 appendDetail(buf, nodes[i]); 292 } 293 } 294 295 298 public int getChildCount() { 299 return fChildCount; 300 } 301 302 305 public void accept(IModelDeltaVisitor visitor) { 306 doAccept(visitor, 0); 307 } 308 309 protected void doAccept(IModelDeltaVisitor visitor, int depth) { 310 if (visitor.visit(this, depth)) { 311 IModelDelta[] childDeltas = getChildDeltas(); 312 for (int i = 0; i < childDeltas.length; i++) { 313 ((ModelDelta)childDeltas[i]).doAccept(visitor, depth+1); 314 } 315 } 316 } 317 318 323 public void setElement(Object element) { 324 fElement = element; 325 } 326 327 332 public void setFlags(int flags) { 333 fFlags = flags; 334 } 335 } 336 | Popular Tags |