<s> package org . eclipse . jdt . internal . core ; import java . util . HashMap ; import java . util . Iterator ; import java . util . Map ; import org . eclipse . core . resources . IResource ; import org . eclipse . core . resources . ResourcesPlugin ; import org . eclipse . core . runtime . Assert ; import org . eclipse . core . runtime . jobs . ISchedulingRule ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IJavaModelStatusConstants ; import org . eclipse . jdt . core . IRegion ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . dom . AST ; import org . eclipse . jdt . core . dom . ASTNode ; import org . eclipse . jdt . core . dom . ASTParser ; import org . eclipse . jdt . core . dom . CompilationUnit ; import org . eclipse . jdt . core . dom . rewrite . ASTRewrite ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . text . edits . TextEdit ; public class DeleteElementsOperation extends MultiOperation { protected Map childrenToRemove ; protected ASTParser parser ; public DeleteElementsOperation ( IJavaElement [ ] elementsToDelete , boolean force ) { super ( elementsToDelete , force ) ; initASTParser ( ) ; } private void deleteElement ( IJavaElement elementToRemove , ICompilationUnit cu ) throws JavaModelException { cu . makeConsistent ( this . progressMonitor ) ; this . parser . setSource ( cu ) ; CompilationUnit astCU = ( CompilationUnit ) this . parser . createAST ( this . progressMonitor ) ; ASTNode node = ( ( JavaElement ) elementToRemove ) . findNode ( astCU ) ; if ( node == null ) Assert . isTrue ( false , "" + elementToRemove . getElementName ( ) + " in " + cu . getElementName ( ) ) ; AST ast = astCU . getAST ( ) ; ASTRewrite rewriter = ASTRewrite . create ( ast ) ; rewriter . remove ( node , null ) ; TextEdit edits = rewriter . rewriteAST ( ) ; applyTextEdit ( cu , edits ) ; } private void initASTParser ( ) { this . parser = ASTParser . newParser ( AST . JLS3 ) ; } protected String getMainTaskName ( ) { return Messages . operation_deleteElementProgress ; } protected ISchedulingRule getSchedulingRule ( ) { if ( this . elementsToProcess != null && this . elementsToProcess . length == 1 ) { IResource resource = this . elementsToProcess [ 0 ] . getResource ( ) ; if ( resource != null ) return ResourcesPlugin . getWorkspace ( ) . getRuleFactory ( ) . modifyRule ( resource ) ; } return super . getSchedulingRule ( ) ; } protected void groupElements ( ) throws JavaModelException { this . childrenToRemove = new HashMap ( 1 ) ; int uniqueCUs = 0 ; for ( int i = 0 , length = this . elementsToProcess . length ; i < length ; i ++ ) { IJavaElement e = this . elementsToProcess [ i ] ; ICompilationUnit cu = getCompilationUnitFor ( e ) ; if ( cu == null ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . READ_ONLY , e ) ) ; } else { IRegion region = ( IRegion ) this . childrenToRemove . get ( cu ) ; if ( region == null ) { region = new Region ( ) ; this . childrenToRemove . put ( cu , region ) ; uniqueCUs += 1 ; } region . add ( e ) ; } } this . elementsToProcess = new IJavaElement [ uniqueCUs ] ; Iterator iter = this . childrenToRemove . keySet ( ) . iterator ( ) ; int i = 0 ; while ( iter . hasNext ( ) ) { this . elementsToProcess [ i ++ ] = ( IJavaElement ) iter . next ( ) ; } } protected void processElement ( IJavaElement element ) throws JavaModelException { ICompilationUnit cu = ( ICompilationUnit ) element ; int numberOfImports = cu . getImports ( ) . length ; JavaElementDelta delta = new JavaElementDelta ( cu ) ; IJavaElement [ ] cuElements = ( ( IRegion ) this . childrenToRemove . get ( cu ) ) . getElements ( ) ; for ( int i = 0 , length = cuElements . length ; i < length ; i ++ ) { IJavaElement e = cuElements [ i ] ; if ( e . exists ( ) ) { deleteElement ( e , cu ) ; delta . removed ( e ) ; if ( e . getElementType ( ) == IJavaElement . IMPORT_DECLARATION ) { numberOfImports -- ; if ( numberOfImports == 0 ) { delta . removed ( cu . getImportContainer ( ) ) ; } } } } if ( delta . getAffectedChildren ( ) . length > 0 ) { cu . save ( getSubProgressMonitor ( 1 ) , this . force ) ; if ( ! cu . isWorkingCopy ( ) ) { addDelta ( delta ) ; setAttribute ( HAS_MODIFIED_RESOURCE_ATTR , TRUE ) ; } } } protected void processElements ( ) throws JavaModelException { groupElements ( ) ; super . processElements ( ) ; } protected void verify ( IJavaElement element ) throws JavaModelException { IJavaElement [ ] children = ( ( IRegion ) this . childrenToRemove . get ( element ) ) . getElements ( ) ; for ( int i = 0 ; i < children . length ; i ++ ) { IJavaElement child = children [ i ] ; if ( child . getCorrespondingResource ( ) != null ) error ( IJavaModelStatusConstants . INVALID_ELEMENT_TYPES , child ) ; if ( child . isReadOnly ( ) ) error ( IJavaModelStatusConstants . READ_ONLY , child ) ; } } } </s>