1 11 package org.eclipse.debug.internal.ui.elements.adapters; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.ILogicalStructureType; 21 import org.eclipse.debug.core.model.IDebugElement; 22 import org.eclipse.debug.core.model.IIndexedValue; 23 import org.eclipse.debug.core.model.IValue; 24 import org.eclipse.debug.core.model.IVariable; 25 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition; 26 import org.eclipse.debug.internal.ui.views.variables.RemoteVariableContentManager; 27 import org.eclipse.debug.ui.DeferredDebugElementWorkbenchAdapter; 28 import org.eclipse.ui.progress.IElementCollector; 29 30 31 34 public class DeferredVariable extends DeferredDebugElementWorkbenchAdapter { 35 36 private static final IVariable[] EMPTY_VARS = new IVariable[0]; 37 38 41 public Object [] getChildren(Object parent) { 42 if (parent instanceof IVariable) { 43 try { 44 IVariable variable = (IVariable) parent; 45 IValue value = variable.getValue(); 46 if (value != null) { 47 return getValueChildren(variable, value); 48 } 49 } catch (DebugException e) { 50 } 51 } 52 return EMPTY; 53 } 54 55 58 public void fetchDeferredChildren(Object object, IElementCollector collector, IProgressMonitor monitor) { 59 if (monitor.isCanceled()) { 60 return; 61 } 62 Object [] children = getChildren(object); 63 if (monitor.isCanceled()) { 64 return; 65 } 66 if (children.length > 0) { 67 if (collector instanceof RemoteVariableContentManager.VariableCollector) { 68 RemoteVariableContentManager.VariableCollector remoteCollector = (RemoteVariableContentManager.VariableCollector) collector; 69 for (int i = 0; i < children.length; i++) { 70 if (monitor.isCanceled()) { 71 return; 72 } 73 Object child = children[i]; 74 remoteCollector.setHasChildren(child, hasChildren(child)); 75 } 76 } 77 collector.add(children, monitor); 78 } 79 collector.done(); 80 } 81 82 protected boolean hasChildren(Object child) { 83 if (child instanceof IVariable) { 84 IVariable var = (IVariable) child; 85 try { 86 IValue value = var.getValue(); 87 return value.hasVariables(); 88 } catch (DebugException e) { 89 } 90 } 91 return false; 92 } 93 94 97 public Object getParent(Object element) { 98 return null; 99 } 100 101 109 protected IVariable[] getValueChildren(IDebugElement parent, IValue value) throws DebugException { 110 if (value == null) { 111 return EMPTY_VARS; 112 } 113 IValue logicalValue = getLogicalValue(value); 114 if (logicalValue instanceof IIndexedValue) { 115 IIndexedValue indexedValue = (IIndexedValue)logicalValue; 116 int partitionSize = computeParitionSize(indexedValue); 117 if (partitionSize > 1) { 118 int offset = indexedValue.getInitialOffset(); 119 int length = indexedValue.getSize(); 120 int numPartitions = length / partitionSize; 121 int remainder = length % partitionSize; 122 if (remainder > 0) { 123 numPartitions++; 124 } 125 IVariable[] partitions = new IVariable[numPartitions]; 126 for (int i = 0; i < (numPartitions - 1); i++) { 127 partitions[i] = new IndexedVariablePartition(parent, indexedValue, offset, partitionSize); 128 offset = offset + partitionSize; 129 } 130 if (remainder == 0) { 131 remainder = partitionSize; 132 } 133 partitions[numPartitions - 1] = new IndexedVariablePartition(parent, indexedValue, offset, remainder); 134 return partitions; 135 } 136 } 137 if (logicalValue == null) { 138 logicalValue = value; 140 } 141 return logicalValue.getVariables(); 142 } 143 144 150 protected IValue getLogicalValue(IValue value) { 151 return getLogicalValue(value, new ArrayList ()); 152 } 153 154 165 private IValue getLogicalValue(IValue value, List previousStructureIds) { 166 if (isShowLogicalStructure()) { 167 ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value); 168 if (types.length > 0) { 169 ILogicalStructureType type = DebugPlugin.getDefaultStructureType(types); 170 if (type != null && !previousStructureIds.contains(type.getId())) { 171 try { 172 value= type.getLogicalStructure(value); 173 previousStructureIds.add(type.getId()); 174 return getLogicalValue(value, previousStructureIds); 175 } catch (CoreException e) { 176 } 178 } 179 } 180 } 181 return value; 182 } 183 184 189 protected boolean isShowLogicalStructure() { 190 return false; 191 } 192 193 202 protected int computeParitionSize(IIndexedValue value) { 203 int partitionSize = 1; 204 try { 205 int length = value.getSize(); 206 int partitionDepth = 0; 207 int preferredSize = getArrayPartitionSize(); 208 int remainder = length % preferredSize; 209 length = length / preferredSize; 210 while (length > 0) { 211 if (remainder == 0 && length == 1) { 212 break; 213 } 214 partitionDepth++; 215 remainder = length % preferredSize; 216 length = length / preferredSize; 217 } 218 for (int i = 0; i < partitionDepth; i++) { 219 partitionSize = partitionSize * preferredSize; 220 } 221 } catch (DebugException e) { 222 } 223 return partitionSize; 224 } 225 226 233 protected int getArrayPartitionSize() { 234 return 100; 236 } 237 } 238 | Popular Tags |