1 12 package org.eclipse.jdt.internal.ui.callhierarchy; 13 14 import java.lang.reflect.InvocationTargetException ; 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 [] EMPTY_ARRAY = new Object [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 64 public Object [] getChildren(Object parentElement) { 65 if (parentElement instanceof TreeRoot) { 66 TreeRoot dummyRoot = (TreeRoot) parentElement; 67 68 return new Object [] { 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 [] 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 [] 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 e) { 93 ExceptionHandler.handle(e, CallHierarchyMessages.CallHierarchyContentProvider_searchError_title, CallHierarchyMessages.CallHierarchyContentProvider_searchError_message); 94 return EMPTY_ARRAY; 95 } catch (InterruptedException e) { 96 return new Object [] { 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 109 public Object [] getElements(Object inputElement) { 110 return getChildren(inputElement); 111 } 112 113 116 public Object getParent(Object element) { 117 if (element instanceof MethodWrapper) { 118 return ((MethodWrapper) element).getParent(); 119 } 120 121 return null; 122 } 123 124 127 public void dispose() { 128 } 130 131 134 public boolean hasChildren(Object element) { 135 if (element == TreeRoot.EMPTY_ROOT || element == TreeTermination.SEARCH_CANCELED) { 136 return false; 137 } 138 139 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 return true; 156 } 157 158 return false; } 160 161 165 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 166 if (oldInput instanceof TreeRoot) { 167 Object 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 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 192 public void doneFetching() { 193 if (fPart != null) { 194 fPart.setCancelEnabled(false); 195 } 196 } 197 198 201 public void startFetching() { 202 if (fPart != null) { 203 fPart.setCancelEnabled(true); 204 } 205 } 206 } 207 | Popular Tags |