]>
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.refactoring.nls; | |
12 | ||
13 | import org.eclipse.core.runtime.CoreException; | |
14 | import org.eclipse.core.runtime.IPath; | |
15 | import org.eclipse.core.runtime.IProgressMonitor; | |
16 | import org.eclipse.core.runtime.NullProgressMonitor; | |
17 | import org.eclipse.core.runtime.OperationCanceledException; | |
18 | import org.eclipse.core.runtime.SubProgressMonitor; | |
19 | ||
20 | import org.eclipse.core.resources.IFile; | |
21 | import org.eclipse.core.resources.IStorage; | |
22 | import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; | |
23 | ||
24 | import org.eclipse.core.filebuffers.FileBuffers; | |
25 | import org.eclipse.core.filebuffers.ITextFileBufferManager; | |
26 | import org.eclipse.core.filebuffers.LocationKind; | |
27 | ||
28 | import org.eclipse.text.edits.ReplaceEdit; | |
29 | import org.eclipse.text.edits.TextEditGroup; | |
30 | ||
31 | import org.eclipse.jface.text.IDocument; | |
32 | ||
33 | import org.eclipse.ltk.core.refactoring.Change; | |
34 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; | |
35 | import org.eclipse.ltk.core.refactoring.TextEditChangeGroup; | |
36 | import org.eclipse.ltk.core.refactoring.TextFileChange; | |
37 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; | |
38 | import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments; | |
39 | import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; | |
40 | import org.eclipse.ltk.core.refactoring.participants.RenameArguments; | |
41 | import org.eclipse.ltk.core.refactoring.participants.RenameParticipant; | |
42 | import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker; | |
43 | ||
44 | import org.eclipse.jdt.core.ICompilationUnit; | |
45 | import org.eclipse.jdt.core.IField; | |
46 | import org.eclipse.jdt.core.IImportDeclaration; | |
47 | import org.eclipse.jdt.core.IInitializer; | |
48 | import org.eclipse.jdt.core.IType; | |
49 | import org.eclipse.jdt.core.JavaModelException; | |
50 | import org.eclipse.jdt.core.dom.Modifier; | |
51 | ||
52 | import org.eclipse.jdt.internal.corext.util.Messages; | |
53 | ||
54 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
55 | ||
56 | /** | |
57 | * @since 3.4 | |
58 | */ | |
59 | public class NLSAccessorFieldRenameParticipant extends RenameParticipant { | |
60 | ||
61 | private IField fField; | |
62 | private String fNewName; | |
63 | private TextFileChange fChange; | |
64 | ||
65 | public NLSAccessorFieldRenameParticipant() { | |
66 | } | |
67 | ||
68 | /** | |
69 | * {@inheritDoc} | |
70 | */ | |
71 | @Override | |
72 | public String getName() { | |
73 | return NLSMessages.NLSAccessorFieldRenameParticipant_participantName; | |
74 | } | |
75 | ||
76 | /** | |
77 | * {@inheritDoc} | |
78 | */ | |
79 | @Override | |
80 | public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) throws OperationCanceledException { | |
81 | if (pm == null) | |
82 | pm= new NullProgressMonitor(); | |
83 | ||
84 | pm.beginTask("", 100); //$NON-NLS-1$ | |
85 | try { | |
86 | ICompilationUnit unit= fField.getCompilationUnit(); | |
87 | if (unit == null) | |
88 | return null; | |
89 | ||
90 | IType[] types= unit.getTypes(); | |
91 | if (types.length > 1) | |
92 | return null; | |
93 | ||
94 | if (!isPotentialNLSAccessor(unit)) | |
95 | return null; | |
96 | ||
97 | IStorage resourceBundle= NLSHintHelper.getResourceBundle(unit); | |
98 | if (!(resourceBundle instanceof IFile)) | |
99 | return null; | |
100 | ||
101 | pm.worked(50); | |
102 | ||
103 | IPath propertyFilePath= resourceBundle.getFullPath(); | |
104 | ||
105 | ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); | |
106 | try { | |
107 | manager.connect(propertyFilePath, LocationKind.IFILE, new SubProgressMonitor(pm, 25)); | |
108 | ||
109 | IDocument document= manager.getTextFileBuffer(propertyFilePath, LocationKind.IFILE).getDocument(); | |
110 | PropertyFileDocumentModel model= new PropertyFileDocumentModel(document); | |
111 | ||
112 | KeyValuePair oldPair= model.getKeyValuePair(fField.getElementName()); | |
113 | if (oldPair == null) | |
114 | return null; | |
115 | ||
116 | String value= oldPair.getValue(); | |
117 | KeyValuePair newPair= new KeyValuePair(fNewName, value); | |
118 | ReplaceEdit edit= model.replace(oldPair, newPair); | |
119 | if (edit == null) | |
120 | return null; | |
121 | ||
122 | fChange= new TextFileChange("", (IFile) resourceBundle); //$NON-NLS-1$ | |
123 | fChange.setEdit(edit); | |
124 | ||
125 | String changeDescription= Messages.format(NLSMessages.NLSAccessorFieldRenameParticipant_changeDescription, new Object[] { fField.getElementName(), fNewName }); | |
126 | fChange.addTextEditChangeGroup(new TextEditChangeGroup(fChange, new TextEditGroup(changeDescription, edit))); | |
127 | ||
128 | ResourceChangeChecker checker= (ResourceChangeChecker) context.getChecker(ResourceChangeChecker.class); | |
129 | IResourceChangeDescriptionFactory deltaFactory= checker.getDeltaFactory(); | |
130 | deltaFactory.change((IFile) resourceBundle); | |
131 | } finally { | |
132 | manager.disconnect(propertyFilePath, LocationKind.IFILE, new SubProgressMonitor(pm, 25)); | |
133 | } | |
134 | } catch (JavaModelException e) { | |
135 | JavaPlugin.log(e); | |
136 | return RefactoringStatus.createErrorStatus(NLSMessages.NLSAccessorFieldRenameParticipant_error_description); | |
137 | } catch (CoreException e) { | |
138 | JavaPlugin.log(e); | |
139 | return RefactoringStatus.createErrorStatus(NLSMessages.NLSAccessorFieldRenameParticipant_error_description); | |
140 | } finally { | |
141 | pm.done(); | |
142 | } | |
143 | ||
144 | return new RefactoringStatus(); | |
145 | } | |
146 | ||
147 | /** | |
148 | * {@inheritDoc} | |
149 | */ | |
150 | @Override | |
151 | public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { | |
152 | return fChange; | |
153 | } | |
154 | ||
155 | /** | |
156 | * {@inheritDoc} | |
157 | */ | |
158 | @Override | |
159 | protected boolean initialize(Object element) { | |
160 | fField= (IField) element; | |
161 | return true; | |
162 | } | |
163 | ||
164 | /** | |
165 | * {@inheritDoc} | |
166 | */ | |
167 | @Override | |
168 | public boolean initialize(RefactoringProcessor processor, Object element, RefactoringArguments arguments) { | |
169 | fNewName= ((RenameArguments) arguments).getNewName(); | |
170 | ||
171 | return super.initialize(processor, element, arguments); | |
172 | } | |
173 | ||
174 | /* | |
175 | * Be conservative, for every unit this returns true an AST will to be created! | |
176 | */ | |
177 | private static boolean isPotentialNLSAccessor(ICompilationUnit unit) throws JavaModelException { | |
178 | IType type= unit.getTypes()[0]; | |
179 | if (!type.exists()) | |
180 | return false; | |
181 | ||
182 | IField bundleNameField= getBundleNameField(type.getFields()); | |
183 | if (bundleNameField == null) | |
184 | return false; | |
185 | ||
186 | if (!importsOSGIUtil(unit)) | |
187 | return false; | |
188 | ||
189 | IInitializer[] initializers= type.getInitializers(); | |
190 | for (int i= 0; i < initializers.length; i++) { | |
191 | if (Modifier.isStatic(initializers[0].getFlags())) | |
192 | return true; | |
193 | } | |
194 | ||
195 | return false; | |
196 | } | |
197 | ||
198 | private static IField getBundleNameField(IField[] fields) { | |
199 | for (int i= 0; i < fields.length; i++) { | |
200 | if ("BUNDLE_NAME".equals(fields[i].getElementName())) //$NON-NLS-1$ | |
201 | return fields[i]; | |
202 | } | |
203 | ||
204 | return null; | |
205 | } | |
206 | ||
207 | private static boolean importsOSGIUtil(ICompilationUnit unit) throws JavaModelException { | |
208 | IImportDeclaration[] imports= unit.getImports(); | |
209 | for (int i= 0; i < imports.length; i++) { | |
210 | if (imports[i].getElementName().startsWith("org.eclipse.osgi.util.")) //$NON-NLS-1$ | |
211 | return true; | |
212 | } | |
213 | ||
214 | return false; | |
215 | } | |
216 | } |