KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > model > elements > VariableContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 package org.eclipse.debug.internal.ui.model.elements;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 /**
34  * @since 3.3
35  */

36 public class VariableContentProvider extends ElementContentProvider {
37
38     /**
39      * Cache of logical structures to avoid computing structures for different
40      * subranges.
41      */

42     private static LogicalStructureCache fgLogicalCache;
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildCount(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
46      */

47     protected int getChildCount(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
48         return getAllChildren(element, context).length;
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildren(java.lang.Object, int, int, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
53      */

54     protected Object JavaDoc[] getChildren(Object JavaDoc parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
55         return getElements(getAllChildren(parent, context), index, length);
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#hasChildren(java.lang.Object, org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext, org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate)
60      */

61     protected boolean hasChildren(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
62         return ((IVariable)element).getValue().hasVariables();
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#supportsContextId(java.lang.String)
67      */

68     protected boolean supportsContextId(String JavaDoc id) {
69          return id.equals(IDebugUIConstants.ID_EXPRESSION_VIEW) || id.equals(IDebugUIConstants.ID_VARIABLE_VIEW) || id.equals(IDebugUIConstants.ID_REGISTER_VIEW);
70     }
71     
72     /**
73      * Gets all the children variables for the parent
74      * @param parent the parent IVariable
75      * @param context the context the children will be presented in
76      * @return an array of all children or an empty array if none
77      * @throws CoreException
78      */

79     protected Object JavaDoc[] getAllChildren(Object JavaDoc 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     /**
89      * Return whether to show compute a logical structure or a raw structure
90      * in the specified context
91      *
92      * @return whether to show compute a logical structure or a raw structure
93      * in the specified context
94      */

95     protected boolean isShowLogicalStructure(IPresentationContext context) {
96         Boolean JavaDoc show = (Boolean JavaDoc) context.getProperty(VariablesView.PRESENTATION_SHOW_LOGICAL_STRUCTURES);
97         return show != null && show.booleanValue();
98     }
99
100     /**
101      * Returns the number of entries that should be displayed in each partition
102      * of an indexed collection.
103      *
104      * @return the number of entries that should be displayed in each partition
105      * of an indexed collection
106      */

107     protected int getArrayPartitionSize() {
108         // TODO: should fix this with a user preference
109
return 100;
110     }
111     
112     /**
113      * Returns any logical value for the raw value in the specified context
114      *
115      * @param value
116      * @param context
117      * @return logical value for the raw value
118      */

119     protected IValue getLogicalValue(IValue value, IPresentationContext context) throws CoreException {
120         return getLogicalValue(value, new ArrayList JavaDoc(), context);
121     }
122     
123     /**
124      * Returns children for the given value, creating array partitions if
125      * required
126      *
127      * @param parent expression or variable containing the given value
128      * @param value the value to retrieve children for
129      * @param context the context in which children have been requested
130      * @return children for the given value, creating array partitions if
131      * required
132      * @throws CoreException
133      */

134     protected Object JavaDoc[] 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             // safeguard against an structure type returning null
164
logicalValue = value;
165         }
166         return logicalValue.getVariables();
167     }
168
169     /**
170      * Returns the partition size to use for the given indexed value. The
171      * partition size is computed by determining the number of levels that an
172      * indexed collection must be nested in order to partition the collection
173      * sub-collections of the preferred partition size.
174      *
175      * @param value
176      * indexed value
177      * @return size of partitions the value should be subdivided into
178      */

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     /**
204      * Returns any logical value for the raw value. This method will recurse
205      * over the returned value until the same structure is encountered again (to
206      * avoid infinite recursion).
207      *
208      * @param value raw value to possibly be replaced by a logical value
209      * @param previousStructureIds
210      * the list of logical structures that have already been applied
211      * to the returned value during the recursion of this method.
212      * Callers should always pass in a new, empty list.
213      * @return logical value if one is calculated, otherwise the raw value is returned
214      */

215     protected IValue getLogicalValue(IValue value, List JavaDoc 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     /**
231      * Returns the logical structure cache to use to store calculated structures. If the cache does not
232      * exist yet, one is created and a debug event listener is added to clear the cache on RESUME and
233      * TERMINATE events.
234      *
235      * @return the logical structure cache to use
236      */

237     protected synchronized LogicalStructureCache getLogicalStructureCache(){
238         if (fgLogicalCache == null){
239             fgLogicalCache = new LogicalStructureCache();
240             // Add a listener to clear the cache when resuming or terminating
241
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