]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 2011 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 org.eclipse.core.runtime.Assert; | |
14 | import org.eclipse.core.runtime.CoreException; | |
15 | import org.eclipse.core.runtime.IProgressMonitor; | |
16 | import org.eclipse.core.runtime.IStatus; | |
17 | import org.eclipse.core.runtime.Status; | |
18 | ||
19 | import org.eclipse.text.edits.TextEditGroup; | |
20 | ||
21 | import org.eclipse.ltk.core.refactoring.GroupCategory; | |
22 | import org.eclipse.ltk.core.refactoring.GroupCategorySet; | |
23 | ||
24 | import org.eclipse.jdt.core.ICompilationUnit; | |
25 | import org.eclipse.jdt.core.dom.ASTNode; | |
26 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
27 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
28 | import org.eclipse.jdt.core.dom.Type; | |
29 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | |
30 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; | |
31 | import org.eclipse.jdt.core.refactoring.CompilationUnitChange; | |
32 | ||
33 | import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; | |
34 | import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; | |
35 | import org.eclipse.jdt.internal.corext.util.Messages; | |
36 | ||
37 | import org.eclipse.jdt.ui.JavaUI; | |
38 | ||
39 | public class CompilationUnitRewriteOperationsFix extends AbstractFix { | |
40 | ||
41 | public abstract static class CompilationUnitRewriteOperation { | |
42 | ||
43 | public abstract void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel linkedModel) throws CoreException; | |
44 | ||
45 | protected Type importType(final ITypeBinding toImport, final ASTNode accessor, ImportRewrite imports, final CompilationUnit compilationUnit) { | |
46 | ImportRewriteContext importContext= new ContextSensitiveImportRewriteContext(compilationUnit, accessor.getStartPosition(), imports); | |
47 | return imports.addImport(toImport, compilationUnit.getAST(), importContext); | |
48 | } | |
49 | ||
50 | protected TextEditGroup createTextEditGroup(String label, CompilationUnitRewrite rewrite) { | |
51 | if (label.length() > 0) { | |
52 | return rewrite.createCategorizedGroupDescription(label, new GroupCategorySet(new GroupCategory(label, label, label))); | |
53 | } else { | |
54 | return rewrite.createGroupDescription(label); | |
55 | } | |
56 | } | |
57 | ||
58 | public String getAdditionalInfo() { | |
59 | return null; | |
60 | } | |
61 | } | |
62 | ||
63 | private final CompilationUnitRewriteOperation[] fOperations; | |
64 | private final CompilationUnit fCompilationUnit; | |
65 | private final LinkedProposalModel fLinkedProposalModel; | |
66 | ||
67 | public CompilationUnitRewriteOperationsFix(String name, CompilationUnit compilationUnit, CompilationUnitRewriteOperation operation) { | |
68 | this(name, compilationUnit, new CompilationUnitRewriteOperation[] { operation }); | |
69 | Assert.isNotNull(operation); | |
70 | } | |
71 | ||
72 | public CompilationUnitRewriteOperationsFix(String name, CompilationUnit compilationUnit, CompilationUnitRewriteOperation[] operations) { | |
73 | super(name); | |
74 | Assert.isNotNull(operations); | |
75 | Assert.isLegal(operations.length > 0); | |
76 | fCompilationUnit= compilationUnit; | |
77 | fOperations= operations; | |
78 | fLinkedProposalModel= new LinkedProposalModel(); | |
79 | } | |
80 | ||
81 | /** | |
82 | * {@inheritDoc} | |
83 | */ | |
84 | @Override | |
85 | public LinkedProposalModel getLinkedPositions() { | |
86 | if (!fLinkedProposalModel.hasLinkedPositions()) | |
87 | return null; | |
88 | ||
89 | return fLinkedProposalModel; | |
90 | } | |
91 | ||
92 | /** | |
93 | * {@inheritDoc} | |
94 | */ | |
95 | public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException { | |
96 | CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite((ICompilationUnit)fCompilationUnit.getJavaElement(), fCompilationUnit); | |
97 | ||
98 | fLinkedProposalModel.clear(); | |
99 | for (int i= 0; i < fOperations.length; i++) { | |
100 | CompilationUnitRewriteOperation operation= fOperations[i]; | |
101 | operation.rewriteAST(cuRewrite, fLinkedProposalModel); | |
102 | } | |
103 | ||
104 | CompilationUnitChange result= cuRewrite.createChange(getDisplayString(), true, null); | |
105 | if (result == null) | |
106 | throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, Messages.format(FixMessages.CompilationUnitRewriteOperationsFix_nullChangeError, getDisplayString()))); | |
107 | ||
108 | return result; | |
109 | } | |
110 | ||
111 | /** | |
112 | * {@inheritDoc} | |
113 | */ | |
114 | @Override | |
115 | public String getAdditionalProposalInfo(){ | |
116 | StringBuffer sb= new StringBuffer(); | |
117 | for (int i= 0; i < fOperations.length; i++) { | |
118 | CompilationUnitRewriteOperation operation= fOperations[i]; | |
119 | String info= operation.getAdditionalInfo(); | |
120 | if (info != null) | |
121 | sb.append(info); | |
122 | } | |
123 | ||
124 | if (sb.length() == 0) | |
125 | return null; | |
126 | ||
127 | return sb.toString(); | |
128 | } | |
129 | ||
130 | } |