|                                                                                                              1
 11  package org.eclipse.debug.internal.ui.model.elements;
 12
 13  import java.util.ArrayList
  ; 14  import java.util.List
  ; 15
 16  import org.eclipse.core.runtime.CoreException;
 17  import org.eclipse.debug.core.DebugEvent;
 18  import org.eclipse.debug.core.DebugException;
 19  import org.eclipse.debug.core.DebugPlugin;
 20  import org.eclipse.debug.core.IDebugEventSetListener;
 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.IValue;
 25  import org.eclipse.debug.core.model.IVariable;
 26  import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
 27  import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
 28  import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition;
 29  import org.eclipse.debug.internal.ui.views.variables.LogicalStructureCache;
 30  import org.eclipse.debug.internal.ui.views.variables.VariablesView;
 31  import org.eclipse.debug.ui.IDebugUIConstants;
 32
 33
 36  public class VariableContentProvider extends ElementContentProvider {
 37
 38
 42      private static LogicalStructureCache fgLogicalCache;
 43
 44
 47      protected int getChildCount(Object
  element, IPresentationContext context, IViewerUpdate monitor) throws CoreException { 48          return getAllChildren(element, context).length;
 49      }
 50
 51
 54      protected Object
  [] getChildren(Object  parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException { 55          return getElements(getAllChildren(parent, context), index, length);
 56      }
 57
 58
 61      protected boolean hasChildren(Object
  element, IPresentationContext context, IViewerUpdate monitor) throws CoreException { 62          return ((IVariable)element).getValue().hasVariables();
 63      }
 64
 65
 68      protected boolean supportsContextId(String
  id) { 69           return id.equals(IDebugUIConstants.ID_EXPRESSION_VIEW) || id.equals(IDebugUIConstants.ID_VARIABLE_VIEW) || id.equals(IDebugUIConstants.ID_REGISTER_VIEW);
 70      }
 71
 72
 79      protected Object
  [] getAllChildren(Object  parent, IPresentationContext context) throws CoreException { 80          IVariable variable = (IVariable) parent;
 81          IValue value = variable.getValue();
 82          if (value != null) {
 83              return getValueChildren(variable, value, context);
 84          }
 85          return EMPTY;
 86      }
 87
 88
 95      protected boolean isShowLogicalStructure(IPresentationContext context) {
 96          Boolean
  show = (Boolean  ) context.getProperty(VariablesView.PRESENTATION_SHOW_LOGICAL_STRUCTURES); 97          return show != null && show.booleanValue();
 98      }
 99
 100
 107     protected int getArrayPartitionSize() {
 108                 return 100;
 110     }
 111
 112
 119     protected IValue getLogicalValue(IValue value, IPresentationContext context) throws CoreException {
 120         return getLogicalValue(value, new ArrayList
  (), context); 121     }
 122
 123
 134     protected Object
  [] getValueChildren(IDebugElement parent, IValue value, IPresentationContext context) throws CoreException { 135         if (value == null) {
 136             return EMPTY;
 137         }
 138         IValue logicalValue = getLogicalValue(value, context);
 139         if (logicalValue instanceof IIndexedValue) {
 140             IIndexedValue indexedValue = (IIndexedValue) logicalValue;
 141             int partitionSize = computeParitionSize(indexedValue);
 142             if (partitionSize > 1) {
 143                 int offset = indexedValue.getInitialOffset();
 144                 int length = indexedValue.getSize();
 145                 int numPartitions = length / partitionSize;
 146                 int remainder = length % partitionSize;
 147                 if (remainder > 0) {
 148                     numPartitions++;
 149                 }
 150                 IVariable[] partitions = new IVariable[numPartitions];
 151                 for (int i = 0; i < (numPartitions - 1); i++) {
 152                     partitions[i] = new IndexedVariablePartition(parent, indexedValue, offset, partitionSize);
 153                     offset = offset + partitionSize;
 154                 }
 155                 if (remainder == 0) {
 156                     remainder = partitionSize;
 157                 }
 158                 partitions[numPartitions - 1] = new IndexedVariablePartition(parent, indexedValue, offset, remainder);
 159                 return partitions;
 160             }
 161         }
 162         if (logicalValue == null) {
 163                         logicalValue = value;
 165         }
 166         return logicalValue.getVariables();
 167     }
 168
 169
 179     protected int computeParitionSize(IIndexedValue value) {
 180         int partitionSize = 1;
 181         try {
 182             int length = value.getSize();
 183             int partitionDepth = 0;
 184             int preferredSize = getArrayPartitionSize();
 185             int remainder = length % preferredSize;
 186             length = length / preferredSize;
 187             while (length > 0) {
 188                 if (remainder == 0 && length == 1) {
 189                     break;
 190                 }
 191                 partitionDepth++;
 192                 remainder = length % preferredSize;
 193                 length = length / preferredSize;
 194             }
 195             for (int i = 0; i < partitionDepth; i++) {
 196                 partitionSize = partitionSize * preferredSize;
 197             }
 198         } catch (DebugException e) {
 199         }
 200         return partitionSize;
 201     }
 202
 203
 215     protected IValue getLogicalValue(IValue value, List
  previousStructureIds, IPresentationContext context) throws CoreException { 216         if (isShowLogicalStructure(context)) {
 217             ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value);
 218             if (types.length > 0) {
 219                 ILogicalStructureType type = DebugPlugin.getDefaultStructureType(types);
 220                 if (type != null && !previousStructureIds.contains(type.getId())) {
 221                     IValue logicalValue = getLogicalStructureCache().getLogicalStructure(type, value);
 222                     previousStructureIds.add(type.getId());
 223                     return getLogicalValue(logicalValue, previousStructureIds, context);
 224                 }
 225             }
 226         }
 227         return value;
 228     }
 229
 230
 237     protected synchronized LogicalStructureCache getLogicalStructureCache(){
 238         if (fgLogicalCache == null){
 239             fgLogicalCache = new LogicalStructureCache();
 240                         DebugPlugin.getDefault().addDebugEventListener(new IDebugEventSetListener(){
 242                 public void handleDebugEvents(DebugEvent[] events) {
 243                     for (int i = 0; i < events.length; i++) {
 244                         if (events[i].getKind() == DebugEvent.TERMINATE){
 245                             fgLogicalCache.clear();
 246                             break;
 247                         } else if (events[i].getKind() == DebugEvent.RESUME && !events[i].isEvaluation()){
 248                             fgLogicalCache.clear();
 249                             break;
 250                         } else if (events[i].getKind() == DebugEvent.CHANGE && events[i].getDetail() == DebugEvent.CONTENT){
 251                             fgLogicalCache.clear();
 252                             break;
 253                         }
 254                     }
 255                 }
 256             });
 257         }
 258         return fgLogicalCache;
 259     }
 260
 261 }
 262
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |