1 11 package org.eclipse.debug.internal.ui.views.variables; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.debug.core.DebugException; 20 import org.eclipse.debug.core.DebugPlugin; 21 import org.eclipse.debug.core.ILogicalStructureType; 22 import org.eclipse.debug.core.model.IDebugElement; 23 import org.eclipse.debug.core.model.IIndexedValue; 24 import org.eclipse.debug.core.model.IStackFrame; 25 import org.eclipse.debug.core.model.IValue; 26 import org.eclipse.debug.core.model.IVariable; 27 import org.eclipse.debug.internal.ui.DebugUIPlugin; 28 import org.eclipse.debug.internal.ui.views.IDebugExceptionHandler; 29 import org.eclipse.debug.ui.IDebugView; 30 import org.eclipse.jface.preference.IPreferenceStore; 31 import org.eclipse.jface.viewers.ITreeContentProvider; 32 import org.eclipse.jface.viewers.Viewer; 33 34 37 public class VariablesViewContentProvider implements ITreeContentProvider { 38 39 42 private IDebugView fDebugView; 43 44 51 private HashMap fParentCache; 52 53 56 private IDebugExceptionHandler fExceptionHandler = null; 57 58 61 private boolean fUseObjectBrowsers; 62 63 66 public VariablesViewContentProvider(IDebugView view) { 67 fParentCache = new HashMap (10); 68 setDebugView(view); 69 } 70 71 74 public Object [] getElements(Object parent) { 75 return getChildren(parent); 76 } 77 78 81 public Object [] getChildren(Object parent) { 82 Object [] children= null; 83 try { 84 if (parent instanceof IStackFrame) { 85 children = ((IStackFrame)parent).getVariables(); 86 } else if (parent instanceof IVariable) { 87 IVariable variable = (IVariable)parent; 88 IValue value = variable.getValue(); 89 children = getModelSpecificChildren(variable, value); 90 } 91 if (children != null) { 92 cache(parent, children); 93 return children; 94 } 95 } catch (DebugException de) { 96 if (getExceptionHandler() != null) { 97 getExceptionHandler().handleException(de); 98 } else { 99 DebugUIPlugin.log(de); 100 } 101 } 102 return new Object [0]; 103 } 104 105 protected IVariable[] getModelSpecificChildren(IDebugElement parent, IValue value) throws DebugException { 106 if (value== null) { 107 return new IVariable[0]; 108 } 109 return getValueChildren(parent, value); 110 } 111 112 120 protected IVariable[] getValueChildren(IDebugElement parent, IValue value) throws DebugException { 121 if (value == null) { 122 return null; 123 } 124 IValue logicalValue = getLogicalValue(value); 125 if (logicalValue instanceof IIndexedValue) { 126 IIndexedValue indexedValue = (IIndexedValue)logicalValue; 127 int partitionSize = computeParitionSize(indexedValue); 128 if (partitionSize > 1) { 129 int offset = indexedValue.getInitialOffset(); 130 int length = indexedValue.getSize(); 131 int numPartitions = length / partitionSize; 132 int remainder = length % partitionSize; 133 if (remainder > 0) { 134 numPartitions++; 135 } 136 IVariable[] partitions = new IVariable[numPartitions]; 137 for (int i = 0; i < (numPartitions - 1); i++) { 138 partitions[i] = new IndexedVariablePartition(parent, indexedValue, offset, partitionSize); 139 offset = offset + partitionSize; 140 } 141 if (remainder == 0) { 142 remainder = partitionSize; 143 } 144 partitions[numPartitions - 1] = new IndexedVariablePartition(parent, indexedValue, offset, remainder); 145 return partitions; 146 } 147 } 148 if (logicalValue == null) { 149 logicalValue = value; 151 } 152 return logicalValue.getVariables(); 153 } 154 155 164 private int computeParitionSize(IIndexedValue value) { 165 int partitionSize = 1; 166 try { 167 int length = value.getSize(); 168 int partitionDepth = 0; 169 int preferredSize = getArrayPartitionSize(); 170 int remainder = length % preferredSize; 171 length = length / preferredSize; 172 while (length > 0) { 173 if (remainder == 0 && length == 1) { 174 break; 175 } 176 partitionDepth++; 177 remainder = length % preferredSize; 178 length = length / preferredSize; 179 } 180 for (int i = 0; i < partitionDepth; i++) { 181 partitionSize = partitionSize * preferredSize; 182 } 183 } catch (DebugException e) { 184 } 185 return partitionSize; 186 } 187 188 194 private IValue getLogicalValue(IValue value) { 195 if (isShowLogicalStructure()) { 196 ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value); 197 if (types.length > 0) { 198 IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore(); 199 ILogicalStructureType type = null; 200 boolean exist = false; 201 for (int i = 0; i < types.length; i++) { 202 String key = VariablesView.LOGICAL_STRUCTURE_TYPE_PREFIX + types[i].getId(); 203 int setting = store.getInt(key); 204 if (setting != 0) { 206 exist = true; 207 if (setting == 1) { 208 type = types[i]; 209 break; 210 } 211 } else { 212 store.setValue(types[i].getId(), -1); 213 } 214 } 215 if (type == null && !exist) { 216 type = types[0]; 217 store.setValue(VariablesView.LOGICAL_STRUCTURE_TYPE_PREFIX + type.getId(), 1); 219 } 220 if (type != null) { 221 try { 222 return type.getLogicalStructure(value); 223 } catch (CoreException e) { 224 } 226 } 227 } 228 } 229 return value; 230 } 231 232 239 protected void cache(Object parent, Object [] children) { 240 for (int i = 0; i < children.length; i++) { 241 Object child = children[i]; 242 if (!fParentCache.containsKey(child)) { 245 fParentCache.put(child, parent); 246 } 247 } 248 } 249 250 253 public Object getParent(Object item) { 254 return fParentCache.get(item); 255 } 256 257 261 public void dispose() { 262 fParentCache= null; 263 setExceptionHandler(null); 264 } 265 266 protected void clearCache() { 267 if (fParentCache != null) { 268 fParentCache.clear(); 269 } 270 } 271 272 277 public void removeCache(Object [] children) { 278 if (fParentCache == null) { 279 return; 280 } 281 for (int i = 0; i < children.length; i++) { 282 fParentCache.remove(children[i]); 283 } 284 } 285 286 289 public boolean hasChildren(Object element) { 290 try { 291 if (element instanceof IVariable) { 292 if (element instanceof IndexedVariablePartition) { 293 return true; 294 } 295 element = ((IVariable)element).getValue(); 296 } 297 if (element instanceof IValue) { 298 return ((IValue)element).hasVariables(); 299 } 300 if (element instanceof IStackFrame) { 301 return ((IStackFrame)element).hasVariables(); 302 } 303 } catch (DebugException de) { 304 DebugUIPlugin.log(de); 305 return false; 306 } 307 return false; 308 } 309 310 313 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 314 clearCache(); 315 } 316 317 324 public List getCachedDecendants(Object parent) { 325 Iterator children = fParentCache.keySet().iterator(); 326 List cachedChildren = new ArrayList (10); 327 while (children.hasNext()) { 328 Object child = children.next(); 329 if (isCachedDecendant(child, parent)) { 330 cachedChildren.add(child); 331 } 332 } 333 return cachedChildren; 334 } 335 336 343 protected boolean isCachedDecendant(Object child, Object parent) { 344 Object p = getParent(child); 345 while (p != null) { 346 if (p.equals(parent)) { 347 return true; 348 } 349 p = getParent(p); 350 } 351 return false; 352 } 353 354 358 protected String getDebugModelId(IDebugElement debugElement) { 359 return debugElement.getModelIdentifier(); 360 } 361 362 367 protected void setExceptionHandler(IDebugExceptionHandler handler) { 368 fExceptionHandler = handler; 369 } 370 371 376 protected IDebugExceptionHandler getExceptionHandler() { 377 return fExceptionHandler; 378 } 379 380 383 public void setShowLogicalStructure(boolean flag) { 384 fUseObjectBrowsers = flag; 385 } 386 387 public boolean isShowLogicalStructure() { 388 return fUseObjectBrowsers; 389 } 390 391 private void setDebugView(IDebugView view) { 392 fDebugView = view; 393 } 394 395 protected IDebugView getDebugView() { 396 return fDebugView; 397 } 398 399 406 protected int getArrayPartitionSize() { 407 if (getDebugView() == null) { 408 return 100; 410 } 411 return ((VariablesView)getDebugView()).getArrayPartitionSize(); 412 } 413 414 } 415 416 | Popular Tags |