1 11 12 package org.eclipse.jdt.internal.core; 13 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 17 import org.eclipse.jdt.core.*; 18 19 23 public class ModelUpdater { 24 25 HashSet projectsToUpdate = new HashSet (); 26 27 30 protected void addToParentInfo(Openable child) { 31 32 Openable parent = (Openable) child.getParent(); 33 if (parent != null && parent.isOpen()) { 34 try { 35 JavaElementInfo info = (JavaElementInfo)parent.getElementInfo(); 36 info.addChild(child); 37 } catch (JavaModelException e) { 38 } 40 } 41 } 42 43 46 protected static void close(Openable element) { 47 48 try { 49 element.close(); 50 } catch (JavaModelException e) { 51 } 53 } 54 55 64 protected void elementAdded(Openable element) { 65 66 int elementType = element.getElementType(); 67 if (elementType == IJavaElement.JAVA_PROJECT) { 68 addToParentInfo(element); 71 this.projectsToUpdate.add(element); 72 } else { 73 addToParentInfo(element); 74 75 close(element); 85 } 86 87 switch (elementType) { 88 case IJavaElement.PACKAGE_FRAGMENT_ROOT : 89 this.projectsToUpdate.add(element.getJavaProject()); 91 break; 92 case IJavaElement.PACKAGE_FRAGMENT : 93 JavaProject project = (JavaProject) element.getJavaProject(); 95 project.resetCaches(); 96 break; 97 } 98 } 99 100 106 protected void elementChanged(Openable element) { 107 108 close(element); 109 } 110 111 118 protected void elementRemoved(Openable element) { 119 120 if (element.isOpen()) { 121 close(element); 122 } 123 removeFromParentInfo(element); 124 int elementType = element.getElementType(); 125 126 switch (elementType) { 127 case IJavaElement.JAVA_MODEL : 128 JavaModelManager.getJavaModelManager().getIndexManager().reset(); 129 break; 130 case IJavaElement.JAVA_PROJECT : 131 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 132 JavaProject javaProject = (JavaProject) element; 133 manager.removePerProjectInfo(javaProject); 134 manager.containerRemove(javaProject); 135 break; 136 case IJavaElement.PACKAGE_FRAGMENT_ROOT : 137 this.projectsToUpdate.add(element.getJavaProject()); 138 break; 139 case IJavaElement.PACKAGE_FRAGMENT : 140 JavaProject project = (JavaProject) element.getJavaProject(); 142 project.resetCaches(); 143 break; 144 } 145 } 146 147 152 public void processJavaDelta(IJavaElementDelta delta) { 153 154 158 try { 159 this.traverseDelta(delta, null, null); 161 Iterator iterator = this.projectsToUpdate.iterator(); 163 while (iterator.hasNext()) { 164 JavaProject project = (JavaProject) iterator.next(); 165 project.updatePackageFragmentRoots(); 166 } 167 } finally { 168 this.projectsToUpdate = new HashSet (); 169 } 170 } 171 172 177 protected void removeFromParentInfo(Openable child) { 178 179 Openable parent = (Openable) child.getParent(); 180 if (parent != null && parent.isOpen()) { 181 try { 182 JavaElementInfo info = (JavaElementInfo)parent.getElementInfo(); 183 info.removeChild(child); 184 } catch (JavaModelException e) { 185 } 187 } 188 } 189 190 197 protected void traverseDelta( 198 IJavaElementDelta delta, 199 IPackageFragmentRoot root, 200 IJavaProject project) { 201 202 boolean processChildren = true; 203 204 Openable element = (Openable) delta.getElement(); 205 switch (element.getElementType()) { 206 case IJavaElement.JAVA_PROJECT : 207 project = (IJavaProject) element; 208 break; 209 case IJavaElement.PACKAGE_FRAGMENT_ROOT : 210 root = (IPackageFragmentRoot) element; 211 break; 212 case IJavaElement.COMPILATION_UNIT : 213 CompilationUnit cu = (CompilationUnit)element; 215 if (cu.isWorkingCopy() && !cu.isPrimary()) { 216 return; 217 } 218 case IJavaElement.CLASS_FILE : 219 processChildren = false; 220 break; 221 } 222 223 switch (delta.getKind()) { 224 case IJavaElementDelta.ADDED : 225 elementAdded(element); 226 break; 227 case IJavaElementDelta.REMOVED : 228 elementRemoved(element); 229 break; 230 case IJavaElementDelta.CHANGED : 231 if ((delta.getFlags() & IJavaElementDelta.F_CONTENT) != 0){ 232 elementChanged(element); 233 } 234 break; 235 } 236 if (processChildren) { 237 IJavaElementDelta[] children = delta.getAffectedChildren(); 238 for (int i = 0; i < children.length; i++) { 239 IJavaElementDelta childDelta = children[i]; 240 this.traverseDelta(childDelta, root, project); 241 } 242 } 243 } 244 } 245 | Popular Tags |