1 11 package org.eclipse.jdt.ui; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.resources.IResourceDelta; 15 16 import org.eclipse.swt.widgets.Control; 17 18 import org.eclipse.jface.viewers.IBasicPropertyConstants; 19 import org.eclipse.jface.viewers.ITreeContentProvider; 20 import org.eclipse.jface.viewers.TreeViewer; 21 import org.eclipse.jface.viewers.Viewer; 22 23 import org.eclipse.jdt.core.*; 24 25 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 26 import org.eclipse.jdt.internal.ui.JavaPlugin; 27 28 37 public class JavaElementContentProvider extends StandardJavaElementContentProvider implements ITreeContentProvider, IElementChangedListener { 38 39 40 protected TreeViewer fViewer; 41 42 protected Object fInput; 43 44 47 public void dispose() { 48 super.dispose(); 49 JavaCore.removeElementChangedListener(this); 50 } 51 52 55 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 56 super.inputChanged(viewer, oldInput, newInput); 57 fViewer= (TreeViewer)viewer; 58 if (oldInput == null && newInput != null) { 59 JavaCore.addElementChangedListener(this); 60 } else if (oldInput != null && newInput == null) { 61 JavaCore.removeElementChangedListener(this); 62 } 63 fInput= newInput; 64 } 65 68 public JavaElementContentProvider() { 69 } 70 71 82 public JavaElementContentProvider(boolean provideMembers, boolean provideWorkingCopy) { 83 super(provideMembers, provideWorkingCopy); 84 } 85 86 89 public void elementChanged(final ElementChangedEvent event) { 90 try { 91 processDelta(event.getDelta()); 92 } catch(JavaModelException e) { 93 JavaPlugin.log(e); 94 } 95 } 96 97 106 protected void processDelta(IJavaElementDelta delta) throws JavaModelException { 107 int kind= delta.getKind(); 108 int flags= delta.getFlags(); 109 IJavaElement element= delta.getElement(); 110 111 if (element instanceof ICompilationUnit) { 112 if (!getProvideWorkingCopy()) 113 return; 114 115 ICompilationUnit cu= (ICompilationUnit) element; 116 if (!JavaModelUtil.isPrimary(cu) || !isOnClassPath((ICompilationUnit)element)) { 117 return; 118 } 119 } 120 121 if (((flags & IJavaElementDelta.F_CLOSED) != 0) || ((flags & IJavaElementDelta.F_OPENED) != 0)) { 123 postRefresh(element); 124 return; 125 } 126 127 if (kind == IJavaElementDelta.REMOVED) { 128 Object parent= internalGetParent(element); 129 postRemove(element); 130 if (parent instanceof IPackageFragment) 131 updatePackageIcon((IPackageFragment)parent); 132 if (isPackageFragmentEmpty(element.getParent())) { 135 if (fViewer.testFindItem(parent) != null) 136 postRefresh(internalGetParent(parent)); 137 } 138 return; 139 } 140 141 if (kind == IJavaElementDelta.ADDED) { 142 Object parent= internalGetParent(element); 143 if (parent instanceof IPackageFragment) { 146 Object grandparent= internalGetParent(parent); 147 if (parent.equals(fInput)) { 150 postRefresh(parent); 151 } else { 152 if (fViewer.testFindItem(parent) == null) 154 postRefresh(grandparent); 155 else { 156 postRefresh(parent); 157 } 158 } 159 } else { 160 postAdd(parent, element); 161 } 162 } 163 164 if (element instanceof ICompilationUnit) { 165 if (kind == IJavaElementDelta.CHANGED) { 166 postRefresh(element); 167 return; 168 } 169 } 170 if ((element instanceof ICompilationUnit) || (element instanceof IClassFile)) 172 return; 173 174 if (element instanceof IPackageFragmentRoot && ((flags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0)) 176 postRefresh(element); 177 178 if (isClassPathChange(delta)) { 179 postRefresh(element.getJavaProject()); 181 } 182 183 if (delta.getResourceDeltas() != null) { 184 IResourceDelta[] rd= delta.getResourceDeltas(); 185 for (int i= 0; i < rd.length; i++) { 186 processResourceDelta(rd[i], element); 187 } 188 } 189 190 IJavaElementDelta[] affectedChildren= delta.getAffectedChildren(); 191 if (affectedChildren.length > 1) { 192 if (element instanceof IPackageFragment) { 194 IJavaElement parent= (IJavaElement)internalGetParent(element); 195 if (element.equals(fInput)) { 198 postRefresh(element); 199 } else { 200 postRefresh(parent); 201 } 202 return; 203 } 204 if (element instanceof IPackageFragmentRoot) 206 postRefresh(skipProjectPackageFragmentRoot((IPackageFragmentRoot)element)); 207 else 208 postRefresh(element); 209 return; 210 } 211 for (int i= 0; i < affectedChildren.length; i++) { 212 processDelta(affectedChildren[i]); 213 } 214 } 215 216 private boolean isOnClassPath(ICompilationUnit element) { 217 IJavaProject project= element.getJavaProject(); 218 if (project == null || !project.exists()) 219 return false; 220 return project.isOnClasspath(element); 221 } 222 223 224 227 private void updatePackageIcon(final IJavaElement element) { 228 postRunnable(new Runnable () { 229 public void run() { 230 Control ctrl= fViewer.getControl(); 232 if (ctrl != null && !ctrl.isDisposed()) 233 fViewer.update(element, new String []{IBasicPropertyConstants.P_IMAGE}); 234 } 235 }); 236 } 237 238 241 private void processResourceDelta(IResourceDelta delta, Object parent) { 242 int status= delta.getKind(); 243 IResource resource= delta.getResource(); 244 if (resource == null) 246 return; 247 248 if ((status & IResourceDelta.REMOVED) != 0) { 250 if (parent instanceof IPackageFragment) 251 postRefresh(internalGetParent(parent)); 253 else 254 postRemove(resource); 255 } 256 if ((status & IResourceDelta.ADDED) != 0) { 257 if (parent instanceof IPackageFragment) 258 postRefresh(internalGetParent(parent)); 260 else 261 postAdd(parent, resource); 262 } 263 IResourceDelta[] affectedChildren= delta.getAffectedChildren(); 264 265 if (affectedChildren.length > 1) { 266 postRefresh(resource); 268 return; 269 } 270 271 for (int i= 0; i < affectedChildren.length; i++) 272 processResourceDelta(affectedChildren[i], resource); 273 } 274 275 private void postRefresh(final Object root) { 276 postRunnable(new Runnable () { 277 public void run() { 278 Control ctrl= fViewer.getControl(); 280 if (ctrl != null && !ctrl.isDisposed()) 281 fViewer.refresh(root); 282 } 283 }); 284 } 285 286 private void postAdd(final Object parent, final Object element) { 287 postRunnable(new Runnable () { 288 public void run() { 289 Control ctrl= fViewer.getControl(); 291 if (ctrl != null && !ctrl.isDisposed()) 292 fViewer.add(parent, element); 293 } 294 }); 295 } 296 297 private void postRemove(final Object element) { 298 postRunnable(new Runnable () { 299 public void run() { 300 Control ctrl= fViewer.getControl(); 302 if (ctrl != null && !ctrl.isDisposed()) 303 fViewer.remove(element); 304 } 305 }); 306 } 307 308 private void postRunnable(final Runnable r) { 309 Control ctrl= fViewer.getControl(); 310 if (ctrl != null && !ctrl.isDisposed()) { 311 ctrl.getDisplay().asyncExec(r); 312 } 313 } 314 } 315 | Popular Tags |