KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > LinkedFix


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.fix;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.text.edits.TextEditGroup;
18
19 import org.eclipse.core.runtime.CoreException;
20
21 import org.eclipse.ltk.core.refactoring.TextChange;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.dom.CompilationUnit;
25
26 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
27 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
28
29 public class LinkedFix extends AbstractFix {
30     
31     public static abstract class AbstractLinkedFixRewriteOperation extends AbstractFixRewriteOperation implements ILinkedFixRewriteOperation {
32
33         /* (non-Javadoc)
34          * @see org.eclipse.jdt.internal.corext.fix.AbstractFix.IFixRewriteOperation#rewriteAST(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite, java.util.List)
35          */

36         public void rewriteAST(CompilationUnitRewrite cuRewrite, List JavaDoc textEditGroups) throws CoreException {
37             rewriteAST(cuRewrite, textEditGroups, new LinkedProposalModel());
38         }
39
40     }
41     
42     private final IFixRewriteOperation[] fFixRewrites;
43     private final CompilationUnit fCompilationUnit;
44     private final LinkedProposalModel fLinkedProposalModel;
45
46     protected LinkedFix(String JavaDoc name, CompilationUnit compilationUnit, IFixRewriteOperation[] fixRewrites) {
47         super(name, compilationUnit, fixRewrites);
48         fCompilationUnit= compilationUnit;
49         fFixRewrites= fixRewrites;
50         fLinkedProposalModel= new LinkedProposalModel();
51     }
52         
53     public LinkedProposalModel getLinkedPositions() {
54         return fLinkedProposalModel;
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.jdt.internal.corext.fix.IFix#createChange()
59      */

60     public TextChange createChange() throws CoreException {
61         if (fFixRewrites == null || fFixRewrites.length == 0)
62             return null;
63
64         CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite((ICompilationUnit)fCompilationUnit.getJavaElement(), fCompilationUnit);
65     
66         List JavaDoc/*<TextEditGroup>*/ groups= new ArrayList JavaDoc();
67
68         fLinkedProposalModel.clear();
69         
70         for (int i= 0; i < fFixRewrites.length; i++) {
71             IFixRewriteOperation operation= fFixRewrites[i];
72             if (operation instanceof ILinkedFixRewriteOperation) {
73                 ILinkedFixRewriteOperation linkedOperation= (ILinkedFixRewriteOperation)operation;
74                 linkedOperation.rewriteAST(cuRewrite, groups, fLinkedProposalModel);
75             } else {
76                 operation.rewriteAST(cuRewrite, groups);
77             }
78         }
79         
80         CompilationUnitChange result= cuRewrite.createChange(getDescription(), true, null);
81         
82         for (Iterator JavaDoc iter= groups.iterator(); iter.hasNext();) {
83             TextEditGroup group= (TextEditGroup)iter.next();
84             result.addTextEditGroup(group);
85         }
86         return result;
87     }
88 }
89
Popular Tags