KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > elements > adapters > VariableContentAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.debug.internal.ui.elements.adapters;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
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.viewers.provisional.AsynchronousContentAdapter;
26 import org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext;
27 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition;
28 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
29 import org.eclipse.debug.ui.IDebugUIConstants;
30 import org.eclipse.ui.IWorkbenchPart;
31
32 public class VariableContentAdapter extends AsynchronousContentAdapter {
33
34     protected Object JavaDoc[] getChildren(Object JavaDoc parent, IPresentationContext context) throws CoreException {
35         IVariable variable = (IVariable) parent;
36         IValue value = variable.getValue();
37         if (value != null) {
38             return getValueChildren(variable, value, context);
39         }
40         return EMPTY;
41     }
42     
43     protected boolean hasChildren(Object JavaDoc element, IPresentationContext context) throws CoreException {
44         IValue value = ((IVariable)element).getValue();
45         return value.hasVariables();
46     }
47     
48     /**
49      * Returns children for the given value, creating array partitions if
50      * required
51      *
52      * @param parent expression or variable containing the given value
53      * @param value the value to retrieve children for
54      * @param context the context in which children have been requested
55      * @return children for the given value, creating array partitions if
56      * required
57      * @throws CoreException
58      */

59     protected Object JavaDoc[] getValueChildren(IDebugElement parent, IValue value, IPresentationContext context) throws CoreException {
60         if (value == null) {
61             return EMPTY;
62         }
63         IValue logicalValue = getLogicalValue(value, context);
64         if (logicalValue instanceof IIndexedValue) {
65             IIndexedValue indexedValue = (IIndexedValue) logicalValue;
66             int partitionSize = computeParitionSize(indexedValue);
67             if (partitionSize > 1) {
68                 int offset = indexedValue.getInitialOffset();
69                 int length = indexedValue.getSize();
70                 int numPartitions = length / partitionSize;
71                 int remainder = length % partitionSize;
72                 if (remainder > 0) {
73                     numPartitions++;
74                 }
75                 IVariable[] partitions = new IVariable[numPartitions];
76                 for (int i = 0; i < (numPartitions - 1); i++) {
77                     partitions[i] = new IndexedVariablePartition(parent, indexedValue, offset, partitionSize);
78                     offset = offset + partitionSize;
79                 }
80                 if (remainder == 0) {
81                     remainder = partitionSize;
82                 }
83                 partitions[numPartitions - 1] = new IndexedVariablePartition(parent, indexedValue, offset, remainder);
84                 return partitions;
85             }
86         }
87         if (logicalValue == null) {
88             // safeguard against an structure type returning null
89
logicalValue = value;
90         }
91         return logicalValue.getVariables();
92     }
93
94     /**
95      * Returns the partition size to use for the given indexed value. The
96      * partition size is computed by determining the number of levels that an
97      * indexed collection must be nested in order to partition the collection
98      * sub-collections of the preferred partition size.
99      *
100      * @param value
101      * indexed value
102      * @return size of paritions the value should be subdivided into
103      */

104     protected int computeParitionSize(IIndexedValue value) {
105         int partitionSize = 1;
106         try {
107             int length = value.getSize();
108             int partitionDepth = 0;
109             int preferredSize = getArrayPartitionSize();
110             int remainder = length % preferredSize;
111             length = length / preferredSize;
112             while (length > 0) {
113                 if (remainder == 0 && length == 1) {
114                     break;
115                 }
116                 partitionDepth++;
117                 remainder = length % preferredSize;
118                 length = length / preferredSize;
119             }
120             for (int i = 0; i < partitionDepth; i++) {
121                 partitionSize = partitionSize * preferredSize;
122             }
123         } catch (DebugException e) {
124         }
125         return partitionSize;
126     }
127
128     /**
129      * Returns any logical value for the raw value in the specified context
130      *
131      * @param value
132      * @param context
133      * @return
134      */

135     protected IValue getLogicalValue(IValue value, IPresentationContext context) {
136         return getLogicalValue(value, new ArrayList JavaDoc(), context);
137     }
138
139     /**
140      * Returns any logical value for the raw value. This method will recurse
141      * over the returned value until the same structure is encountered again (to
142      * avoid infinite recursion).
143      *
144      * @param value
145      * @param previousStructureIds
146      * the list of logical structures that have already been applied
147      * to the returned value during the recursion of this method.
148      * Callers should always pass in a new, empty list.
149      * @return
150      */

151     protected IValue getLogicalValue(IValue value, List JavaDoc previousStructureIds, IPresentationContext context) {
152         if (isShowLogicalStructure(context)) {
153             ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value);
154             if (types.length > 0) {
155                 ILogicalStructureType type = DebugPlugin.getDefaultStructureType(types);
156                 if (type != null && !previousStructureIds.contains(type.getId())) {
157                     try {
158                         value = type.getLogicalStructure(value);
159                         previousStructureIds.add(type.getId());
160                         return getLogicalValue(value, previousStructureIds, context);
161                     } catch (CoreException e) {
162                         // unable to display logical structure
163
}
164                 }
165             }
166         }
167         return value;
168     }
169
170     /**
171      * Return whether to show compute a logical structure or a raw structure
172      * in the specified context
173      *
174      * @return whether to show compute a logical structure or a raw structure
175      * in the specified context
176      */

177     protected boolean isShowLogicalStructure(IPresentationContext context) {
178         IWorkbenchPart part = context.getPart();
179         if (part instanceof VariablesView) {
180             return ((VariablesView) part).isShowLogicalStructure();
181         }
182         return false;
183     }
184
185     /**
186      * Returns the number of entries that should be displayed in each partition
187      * of an indexed collection.
188      *
189      * @return the number of entries that should be displayed in each partition
190      * of an indexed collection
191      */

192     protected int getArrayPartitionSize() {
193         // TODO: should fix this with a user preference
194
return 100;
195     }
196     
197     protected boolean supportsPartId(String JavaDoc id) {
198         return id.equals(IDebugUIConstants.ID_EXPRESSION_VIEW) || id.equals(IDebugUIConstants.ID_VARIABLE_VIEW) || id.equals(IDebugUIConstants.ID_REGISTER_VIEW);
199     }
200 }
201
Popular Tags