<s> package org . eclipse . jdt . internal . core ; import java . util . HashSet ; import java . util . Iterator ; import org . eclipse . jdt . core . * ; public class ModelUpdater { HashSet projectsToUpdate = new HashSet ( ) ; protected void addToParentInfo ( Openable child ) { Openable parent = ( Openable ) child . getParent ( ) ; if ( parent != null && parent . isOpen ( ) ) { try { OpenableElementInfo info = ( OpenableElementInfo ) parent . getElementInfo ( ) ; info . addChild ( child ) ; } catch ( JavaModelException e ) { } } } protected static void close ( Openable element ) { try { element . close ( ) ; } catch ( JavaModelException e ) { } } protected void elementAdded ( Openable element ) { int elementType = element . getElementType ( ) ; if ( elementType == IJavaElement . JAVA_PROJECT ) { addToParentInfo ( element ) ; this . projectsToUpdate . add ( element ) ; } else { addToParentInfo ( element ) ; close ( element ) ; } switch ( elementType ) { case IJavaElement . PACKAGE_FRAGMENT_ROOT : this . projectsToUpdate . add ( element . getJavaProject ( ) ) ; break ; case IJavaElement . PACKAGE_FRAGMENT : JavaProject project = ( JavaProject ) element . getJavaProject ( ) ; project . resetCaches ( ) ; break ; } } protected void elementChanged ( Openable element ) { close ( element ) ; } protected void elementRemoved ( Openable element ) { if ( element . isOpen ( ) ) { close ( element ) ; } removeFromParentInfo ( element ) ; int elementType = element . getElementType ( ) ; switch ( elementType ) { case IJavaElement . JAVA_MODEL : JavaModelManager . getIndexManager ( ) . reset ( ) ; break ; case IJavaElement . JAVA_PROJECT : JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; JavaProject javaProject = ( JavaProject ) element ; manager . removePerProjectInfo ( javaProject , true ) ; manager . containerRemove ( javaProject ) ; break ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : this . projectsToUpdate . add ( element . getJavaProject ( ) ) ; break ; case IJavaElement . PACKAGE_FRAGMENT : JavaProject project = ( JavaProject ) element . getJavaProject ( ) ; project . resetCaches ( ) ; break ; } } public void processJavaDelta ( IJavaElementDelta delta ) { try { traverseDelta ( delta , null , null ) ; Iterator iterator = this . projectsToUpdate . iterator ( ) ; while ( iterator . hasNext ( ) ) { JavaProject project = ( JavaProject ) iterator . next ( ) ; project . resetCaches ( ) ; } } finally { this . projectsToUpdate = new HashSet ( ) ; } } protected void removeFromParentInfo ( Openable child ) { Openable parent = ( Openable ) child . getParent ( ) ; if ( parent != null && parent . isOpen ( ) ) { try { OpenableElementInfo info = ( OpenableElementInfo ) parent . getElementInfo ( ) ; info . removeChild ( child ) ; } catch ( JavaModelException e ) { } } } protected void traverseDelta ( IJavaElementDelta delta , IPackageFragmentRoot root , IJavaProject project ) { boolean processChildren = true ; Openable element = ( Openable ) delta . getElement ( ) ; switch ( element . getElementType ( ) ) { case IJavaElement . JAVA_PROJECT : project = ( IJavaProject ) element ; break ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : root = ( IPackageFragmentRoot ) element ; break ; case IJavaElement . COMPILATION_UNIT : CompilationUnit cu = ( CompilationUnit ) element ; if ( cu . isWorkingCopy ( ) && ! cu . isPrimary ( ) ) { return ; } case IJavaElement . CLASS_FILE : processChildren = false ; break ; } switch ( delta . getKind ( ) ) { case IJavaElementDelta . ADDED : elementAdded ( element ) ; break ; case IJavaElementDelta . REMOVED : elementRemoved ( element ) ; break ; case IJavaElementDelta . CHANGED : if ( ( delta . getFlags ( ) & IJavaElementDelta . F_CONTENT ) != 0 ) { elementChanged ( element ) ; } break ; } if ( processChildren ) { IJavaElementDelta [ ] children = delta . getAffectedChildren ( ) ; for ( int i = 0 ; i < children . length ; i ++ ) { IJavaElementDelta childDelta = children [ i ] ; traverseDelta ( childDelta , root , project ) ; } } } } </s>