KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > RemoteVariablesContentProvider


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.views.variables;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.debug.internal.ui.views.IDebugExceptionHandler;
19 import org.eclipse.debug.internal.ui.views.RemoteTreeContentManager;
20 import org.eclipse.debug.internal.ui.views.RemoteTreeViewer;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.ui.IWorkbenchPartSite;
23 import org.eclipse.ui.model.BaseWorkbenchContentProvider;
24
25 /**
26  * Provide the contents for a variables viewer.
27  */

28 public class RemoteVariablesContentProvider extends BaseWorkbenchContentProvider {
29     
30     /**
31      * A table that maps children to their parent element
32      * such that this content provider can walk back up the
33      * parent chain (since values do not know their
34      * parent).
35      * Map of <code>IVariable</code> (child) -> <code>IVariable</code> (parent).
36      */

37     private HashMap JavaDoc fParentCache;
38     
39     /**
40      * Handler for exceptions as content is retrieved
41      */

42     private IDebugExceptionHandler fExceptionHandler = null;
43     
44     /**
45      * Flag indicating whether contributed content providers should be used or not.
46      */

47     private boolean fUseObjectBrowsers;
48     
49     /**
50      * Remote content manager to retrieve content in the background.
51      */

52     private RemoteVariableContentManager fManager;
53     
54     /**
55      * Constructs a new provider
56      */

57     public RemoteVariablesContentProvider(RemoteTreeViewer viewer, IWorkbenchPartSite site, VariablesView view) {
58         fManager = (RemoteVariableContentManager)createContentManager(viewer, site, view);
59         fParentCache = new HashMap JavaDoc(10);
60     }
61     
62     protected RemoteTreeContentManager createContentManager(RemoteTreeViewer viewer, IWorkbenchPartSite site, VariablesView view) {
63         return new RemoteVariableContentManager(this, viewer, site, view);
64     }
65
66     /**
67      * @see ITreeContentProvider#getChildren(Object)
68      */

69     public Object JavaDoc[] getChildren(Object JavaDoc parent) {
70         Object JavaDoc[] children = fManager.getChildren(parent);
71         if (children == null) {
72             children = super.getChildren(parent);
73         }
74         if (children != null) {
75             cache(parent, children);
76             return children;
77         }
78         return new Object JavaDoc[0];
79     }
80
81     /**
82      * Caches the given elememts as children of the given
83      * parent.
84      *
85      * @param parent parent element
86      * @param children children elements
87      */

88     protected void cache(Object JavaDoc parent, Object JavaDoc[] children) {
89         for (int i = 0; i < children.length; i++) {
90             Object JavaDoc child = children[i];
91             // avoid cycles in the cache, which can happen for
92
// recursive data structures
93
if (!fParentCache.containsKey(child)) {
94                 fParentCache.put(child, parent);
95             }
96         }
97     }
98     
99     /**
100      * @see ITreeContentProvider#getParent(Object)
101      */

102     public Object JavaDoc getParent(Object JavaDoc item) {
103         return fParentCache.get(item);
104     }
105
106     /**
107      * Unregisters this content provider from the debug plugin so that
108      * this object can be garbage-collected.
109      */

110     public void dispose() {
111         fManager.clearHasChildrenCache();
112         fManager.cancel();
113         fParentCache= null;
114         setExceptionHandler(null);
115     }
116     
117     protected void clearCache() {
118         if (fParentCache != null) {
119             fParentCache.clear();
120         }
121     }
122     
123     /**
124      * Remove the cached parent for the given children
125      *
126      * @param children for which to remove cached parents
127      */

128     public void removeCache(Object JavaDoc[] children) {
129         if (fParentCache == null) {
130             return;
131         }
132         for (int i = 0; i < children.length; i++) {
133             fParentCache.remove(children[i]);
134         }
135     }
136     
137     /**
138      * @see ITreeContentProvider#hasChildren(Object)
139      */

140     public boolean hasChildren(Object JavaDoc element) {
141         return fManager.mayHaveChildren(element);
142     }
143         
144     /**
145      * @see IContentProvider#inputChanged(Viewer, Object, Object)
146      */

147     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
148         clearCache();
149         fManager.cancel();
150         fManager.clearHasChildrenCache();
151     }
152     
153     /**
154      * Return all cached decendants of the given parent.
155      *
156      * @param parent the element whose decendants are to be calculated
157      * @return list of decendants that have been cached for
158      * the given parent
159      */

160     public List JavaDoc getCachedDecendants(Object JavaDoc parent) {
161         Iterator JavaDoc children = fParentCache.keySet().iterator();
162         List JavaDoc cachedChildren = new ArrayList JavaDoc(10);
163         while (children.hasNext()) {
164             Object JavaDoc child = children.next();
165             if (isCachedDecendant(child, parent)) {
166                 cachedChildren.add(child);
167             }
168         }
169         return cachedChildren;
170     }
171     
172     /**
173      * Returns whether the given child is a cached descendant
174      * of the given parent.
175      *
176      * @return whether the given child is a cached descendant
177      * of the given parent
178      */

179     protected boolean isCachedDecendant(Object JavaDoc child, Object JavaDoc parent) {
180         Object JavaDoc p = getParent(child);
181         while (p != null) {
182             if (p.equals(parent)) {
183                 return true;
184             }
185             p = getParent(p);
186         }
187         return false;
188     }
189
190     /**
191      * Sets an exception handler for this content provider.
192      *
193      * @param handler debug exception handler or <code>null</code>
194      */

195     protected void setExceptionHandler(IDebugExceptionHandler handler) {
196         fExceptionHandler = handler;
197     }
198     
199     /**
200      * Returns the exception handler for this content provider.
201      *
202      * @return debug exception handler or <code>null</code>
203      */

204     protected IDebugExceptionHandler getExceptionHandler() {
205         return fExceptionHandler;
206     }
207     
208     /**
209      * Show logical structure of values
210      */

211     public void setShowLogicalStructure(boolean flag) {
212         fUseObjectBrowsers = flag;
213     }
214     
215     public boolean isShowLogicalStructure() {
216         return fUseObjectBrowsers;
217     }
218     
219     public RemoteVariableContentManager getContentManager() {
220         return fManager;
221     }
222     
223 }
224
225
Popular Tags