KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > callhierarchy > CallHierarchyContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.callhierarchy;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.operation.IRunnableContext;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.jface.viewers.AbstractTreeViewer;
20 import org.eclipse.jface.viewers.ITreeContentProvider;
21 import org.eclipse.jface.viewers.Viewer;
22
23 import org.eclipse.jdt.core.IJavaElement;
24
25 import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
28 import org.eclipse.ui.progress.DeferredTreeContentManager;
29
30 public class CallHierarchyContentProvider implements ITreeContentProvider {
31     private final static Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[0];
32
33     private DeferredTreeContentManager fManager;
34     private CallHierarchyViewPart fPart;
35     
36     private class MethodWrapperRunnable implements IRunnableWithProgress {
37         private MethodWrapper fMethodWrapper;
38         private MethodWrapper[] fCalls= null;
39
40         MethodWrapperRunnable(MethodWrapper methodWrapper) {
41             fMethodWrapper= methodWrapper;
42         }
43                 
44         public void run(IProgressMonitor pm) {
45             fCalls= fMethodWrapper.getCalls(pm);
46         }
47         
48         MethodWrapper[] getCalls() {
49             if (fCalls != null) {
50                 return fCalls;
51             }
52             return new MethodWrapper[0];
53         }
54     }
55
56     public CallHierarchyContentProvider(CallHierarchyViewPart part) {
57         super();
58         fPart= part;
59     }
60
61     /**
62      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
63      */

64     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
65         if (parentElement instanceof TreeRoot) {
66             TreeRoot dummyRoot = (TreeRoot) parentElement;
67
68             return new Object JavaDoc[] { dummyRoot.getRoot() };
69         } else if (parentElement instanceof MethodWrapper) {
70             MethodWrapper methodWrapper = ((MethodWrapper) parentElement);
71
72             if (shouldStopTraversion(methodWrapper)) {
73                 return EMPTY_ARRAY;
74             } else {
75                 if (fManager != null) {
76                     Object JavaDoc[] children = fManager.getChildren(new DeferredMethodWrapper(this, methodWrapper));
77                     if (children != null)
78                         return children;
79                 }
80                 return fetchChildren(methodWrapper);
81             }
82         }
83
84         return EMPTY_ARRAY;
85     }
86
87     protected Object JavaDoc[] fetchChildren(MethodWrapper methodWrapper) {
88         IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow();
89         MethodWrapperRunnable runnable= new MethodWrapperRunnable(methodWrapper);
90         try {
91             context.run(true, true, runnable);
92         } catch (InvocationTargetException JavaDoc e) {
93             ExceptionHandler.handle(e, CallHierarchyMessages.CallHierarchyContentProvider_searchError_title, CallHierarchyMessages.CallHierarchyContentProvider_searchError_message);
94             return EMPTY_ARRAY;
95         } catch (InterruptedException JavaDoc e) {
96             return new Object JavaDoc[] { TreeTermination.SEARCH_CANCELED };
97         }
98         
99         return runnable.getCalls();
100     }
101
102     private boolean shouldStopTraversion(MethodWrapper methodWrapper) {
103         return (methodWrapper.getLevel() > CallHierarchyUI.getDefault().getMaxCallDepth()) || methodWrapper.isRecursive();
104     }
105
106     /**
107      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
108      */

109     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
110         return getChildren(inputElement);
111     }
112
113     /**
114      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
115      */

116     public Object JavaDoc getParent(Object JavaDoc element) {
117         if (element instanceof MethodWrapper) {
118             return ((MethodWrapper) element).getParent();
119         }
120
121         return null;
122     }
123
124     /**
125      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
126      */

127     public void dispose() {
128         // Nothing to dispose
129
}
130
131     /**
132      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
133      */

134     public boolean hasChildren(Object JavaDoc element) {
135         if (element == TreeRoot.EMPTY_ROOT || element == TreeTermination.SEARCH_CANCELED) {
136             return false;
137         }
138
139         // Only methods can have subelements, so there's no need to fool the
140
// user into believing that there is more
141
if (element instanceof MethodWrapper) {
142             MethodWrapper methodWrapper= (MethodWrapper) element;
143             if (methodWrapper.getMember().getElementType() != IJavaElement.METHOD) {
144                 return false;
145             }
146             if (shouldStopTraversion(methodWrapper)) {
147                 return false;
148             }
149             return true;
150         } else if (element instanceof TreeRoot) {
151             return true;
152         } else if (element instanceof DeferredMethodWrapper) {
153             // Err on the safe side by returning true even though
154
// we don't know for sure that there are children.
155
return true;
156         }
157
158         return false; // the "Update ..." placeholder has no children
159
}
160
161     /**
162      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
163      * java.lang.Object, java.lang.Object)
164      */

165     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
166         if (oldInput instanceof TreeRoot) {
167             Object JavaDoc root = ((TreeRoot) oldInput).getRoot();
168             if (root instanceof MethodWrapper) {
169                 cancelJobs((MethodWrapper) root);
170             }
171         }
172         if (viewer instanceof AbstractTreeViewer) {
173             fManager = new DeferredTreeContentManager(this, (AbstractTreeViewer) viewer, fPart.getSite());
174         }
175     }
176
177     /**
178      * Cancel all current jobs.
179      */

180     void cancelJobs(MethodWrapper wrapper) {
181         if (fManager != null && wrapper != null) {
182             fManager.cancel(wrapper);
183             if (fPart != null) {
184                 fPart.setCancelEnabled(false);
185             }
186         }
187     }
188
189     /**
190      *
191      */

192     public void doneFetching() {
193         if (fPart != null) {
194             fPart.setCancelEnabled(false);
195         }
196     }
197
198     /**
199      *
200      */

201     public void startFetching() {
202         if (fPart != null) {
203             fPart.setCancelEnabled(true);
204         }
205     }
206 }
207
Popular Tags