KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.elements.adapters;
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.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 /**
32  * Default deferred content provider for a variable
33  */

34 public class DeferredVariable extends DeferredDebugElementWorkbenchAdapter {
35     
36     private static final IVariable[] EMPTY_VARS = new IVariable[0];
37     
38     /* (non-Javadoc)
39      * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
40      */

41     public Object JavaDoc[] getChildren(Object JavaDoc 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     /* (non-Javadoc)
56      * @see org.eclipse.ui.progress.IDeferredWorkbenchAdapter#fetchDeferredChildren(java.lang.Object, org.eclipse.ui.progress.IElementCollector, org.eclipse.core.runtime.IProgressMonitor)
57      */

58     public void fetchDeferredChildren(Object JavaDoc object, IElementCollector collector, IProgressMonitor monitor) {
59         if (monitor.isCanceled()) {
60             return;
61         }
62         Object JavaDoc[] 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 JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
95      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
96      */

97     public Object JavaDoc getParent(Object JavaDoc element) {
98         return null;
99     }
100
101     /**
102      * Returns children for the given value, creating array paritions if required
103      *
104      * @param parent expression or variable containing the given value
105      * @param value the value to retrieve children for
106      * @return children for the given value, creating array paritions if required
107      * @throws DebugException
108      */

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             // safeguard against an structure type returning null
139
logicalValue = value;
140         }
141         return logicalValue.getVariables();
142     }
143     
144     /**
145      * Returns any logical value for the raw value.
146      *
147      * @param value
148      * @return
149      */

150     protected IValue getLogicalValue(IValue value) {
151         return getLogicalValue(value, new ArrayList JavaDoc());
152     }
153     
154     /**
155      * Returns any logical value for the raw value. This method will recurse
156      * over the returned value until the same structure is encountered again
157      * (to avoid infinite recursion).
158      *
159      * @param value
160      * @param previousStructureIds the list of logical structures that have already
161      * been applied to the returned value during the recursion of this method. Callers
162      * should always pass in a new, empty list.
163      * @return
164      */

165     private IValue getLogicalValue(IValue value, List JavaDoc 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                         // unable to display logical structure
177
}
178                 }
179             }
180         }
181         return value;
182     }
183
184     /**
185      * Return wether to show compute a logical structure or a raw structure.
186      *
187      * @return wether to show compute a logical structure or a raw structure
188      */

189     protected boolean isShowLogicalStructure() {
190         return false;
191     }
192     
193     /**
194      * Returns the partition size to use for the given indexed value.
195      * The partition size is computed by determining the number of levels
196      * that an indexed collection must be nested in order to partition
197      * the collection sub-collections of the preferred partition size.
198      *
199      * @param value indexed value
200      * @return size of paritions the value should be subdivided into
201      */

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     /**
227      * Returns the number of entries that should be displayed in each
228      * partition of an indexed collection.
229      *
230      * @return the number of entries that should be displayed in each
231      * partition of an indexed collection
232      */

233     protected int getArrayPartitionSize() {
234         // TODO: should fix this with a user pref
235
return 100;
236     }
237 }
238
Popular Tags