Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 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.pde.internal.ui.wizards.tools; |
12 |
|
13 |
import java.lang.reflect.InvocationTargetException; |
14 |
import java.util.ArrayList; |
15 |
import java.util.Iterator; |
16 |
|
17 |
import org.eclipse.core.resources.IProject; |
18 |
import org.eclipse.core.runtime.CoreException; |
19 |
import org.eclipse.core.runtime.IProgressMonitor; |
20 |
import org.eclipse.core.runtime.OperationCanceledException; |
21 |
import org.eclipse.core.runtime.SubProgressMonitor; |
22 |
import org.eclipse.ltk.core.refactoring.Change; |
23 |
import org.eclipse.ltk.core.refactoring.CompositeChange; |
24 |
import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
25 |
import org.eclipse.ltk.core.refactoring.TextFileChange; |
26 |
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; |
27 |
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; |
28 |
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; |
29 |
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; |
30 |
import org.eclipse.osgi.util.NLS; |
31 |
import org.eclipse.pde.core.IBaseModel; |
32 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
33 |
import org.eclipse.pde.core.plugin.ISharedExtensionsModel; |
34 |
import org.eclipse.pde.internal.core.ibundle.IBundle; |
35 |
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; |
36 |
import org.eclipse.pde.internal.ui.PDEPlugin; |
37 |
import org.eclipse.pde.internal.ui.PDEUIMessages; |
38 |
import org.eclipse.pde.internal.ui.search.dependencies.AddNewDependenciesOperation; |
39 |
import org.eclipse.pde.internal.ui.search.dependencies.CalculateUsesOperation; |
40 |
import org.eclipse.pde.internal.ui.search.dependencies.GatherUnusedDependenciesOperation; |
41 |
import org.eclipse.pde.internal.ui.util.ModelModification; |
42 |
import org.eclipse.pde.internal.ui.util.PDEModelUtility; |
43 |
|
44 |
public class OrganizeManifestsProcessor extends RefactoringProcessor implements IOrganizeManifestsSettings { |
45 |
|
46 |
// if operation is executed without setting operations, these defaults will be used |
47 |
protected boolean fAddMissing = true; // add all packages to export-package |
48 |
protected boolean fMarkInternal = true; // mark export-package as internal |
49 |
protected String fPackageFilter = VALUE_DEFAULT_FILTER; |
50 |
protected boolean fRemoveUnresolved = true; // remove unresolved export-package |
51 |
protected boolean fCalculateUses = false; // calculate the 'uses' directive for exported packages |
52 |
protected boolean fModifyDep = true; // modify import-package / require-bundle |
53 |
protected boolean fRemoveDependencies = true; // if true: remove, else mark optional |
54 |
protected boolean fUnusedDependencies = false; // find/remove unused dependencies - long running op |
55 |
protected boolean fRemoveLazy = true; // remove lazy/auto start if no activator |
56 |
protected boolean fPrefixIconNL = false; // prefix icon paths with $nl$ |
57 |
protected boolean fUnusedKeys = false; // remove unused <bundle-localization>.properties keys |
58 |
protected boolean fAddDependencies = false; |
59 |
|
60 |
ArrayList fProjectList; |
61 |
private IProject fCurrentProject; |
62 |
|
63 |
public OrganizeManifestsProcessor(ArrayList projects) { |
64 |
fProjectList = projects; |
65 |
} |
66 |
|
67 |
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, |
68 |
CheckConditionsContext context) throws CoreException, |
69 |
OperationCanceledException { |
70 |
RefactoringStatus status = new RefactoringStatus(); |
71 |
for (Iterator i = fProjectList.iterator(); i.hasNext();) { |
72 |
if (!(i.next() instanceof IProject)) |
73 |
status.addFatalError(PDEUIMessages.OrganizeManifestsProcessor_invalidParam); |
74 |
} |
75 |
return status; |
76 |
} |
77 |
|
78 |
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) |
79 |
throws CoreException, OperationCanceledException { |
80 |
return null; |
81 |
} |
82 |
|
83 |
public Change createChange(IProgressMonitor pm) throws CoreException, |
84 |
OperationCanceledException { |
85 |
CompositeChange change = new CompositeChange(""); //$NON-NLS-1$ |
86 |
change.markAsSynthetic(); |
87 |
pm.beginTask(PDEUIMessages.OrganizeManifestJob_taskName, fProjectList.size()); |
88 |
for (Iterator i = fProjectList.iterator(); i.hasNext() && !pm.isCanceled();) |
89 |
change.add(cleanProject((IProject)i.next(), new SubProgressMonitor(pm, 1))); |
90 |
return change; |
91 |
} |
92 |
|
93 |
private Change cleanProject(IProject project, IProgressMonitor monitor) { |
94 |
fCurrentProject = project; |
95 |
CompositeChange change = new CompositeChange(NLS.bind(PDEUIMessages.OrganizeManifestsProcessor_rootMessage, new String[] {fCurrentProject.getName()})); |
96 |
monitor.beginTask(NLS.bind(PDEUIMessages.OrganizeManifestsProcessor_rootMessage, new String[] {fCurrentProject.getName()}), getTotalTicksPerProject()); |
97 |
|
98 |
final TextFileChange[] result = { null }; |
99 |
final Exception[] ee = new Exception[1]; |
100 |
ModelModification modification = new ModelModification(fCurrentProject) { |
101 |
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
102 |
if (model instanceof IBundlePluginModelBase) |
103 |
try { |
104 |
runCleanup(monitor, (IBundlePluginModelBase)model, result); |
105 |
} catch (InvocationTargetException e) { |
106 |
ee[0] = e; |
107 |
} catch (InterruptedException e) { |
108 |
ee[0] = e; |
109 |
} |
110 |
} |
111 |
}; |
112 |
TextFileChange[] changes = PDEModelUtility.changesForModelModication(modification, monitor); |
113 |
for (int i = 0; i < changes.length; i++) |
114 |
change.add(changes[i]); |
115 |
if (result[0] != null) |
116 |
change.add(result[0]); |
117 |
if (ee[0] != null) |
118 |
PDEPlugin.log(ee[0]); |
119 |
return change; |
120 |
} |
121 |
|
122 |
private void runCleanup(IProgressMonitor monitor, IBundlePluginModelBase modelBase, |
123 |
TextFileChange[] result) throws InvocationTargetException, InterruptedException { |
124 |
|
125 |
IBundle currentBundle = modelBase.getBundleModel().getBundle(); |
126 |
ISharedExtensionsModel sharedExtensionsModel = modelBase.getExtensionsModel(); |
127 |
IPluginModelBase currentExtensionsModel = null; |
128 |
if (sharedExtensionsModel instanceof IPluginModelBase) |
129 |
currentExtensionsModel = (IPluginModelBase)sharedExtensionsModel; |
130 |
|
131 |
String projectName = fCurrentProject.getName(); |
132 |
|
133 |
if (fAddMissing || fRemoveUnresolved) { |
134 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_export, projectName)); |
135 |
if (!monitor.isCanceled()) |
136 |
OrganizeManifest.organizeExportPackages(currentBundle, fCurrentProject, fAddMissing, fRemoveUnresolved); |
137 |
if (fAddMissing) |
138 |
monitor.worked(1); |
139 |
if (fRemoveUnresolved) |
140 |
monitor.worked(1); |
141 |
} |
142 |
|
143 |
if (fMarkInternal) { |
144 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_filterInternal, projectName)); |
145 |
if (!monitor.isCanceled()) |
146 |
OrganizeManifest.markPackagesInternal(currentBundle, fPackageFilter); |
147 |
monitor.worked(1); |
148 |
} |
149 |
|
150 |
if (fModifyDep) { |
151 |
String message = fRemoveDependencies ? |
152 |
NLS.bind(PDEUIMessages.OrganizeManifestsOperation_removeUnresolved, projectName) : |
153 |
NLS.bind(PDEUIMessages.OrganizeManifestsOperation_markOptionalUnresolved, projectName); |
154 |
monitor.subTask(message); |
155 |
if (!monitor.isCanceled()) |
156 |
OrganizeManifest.organizeImportPackages(currentBundle, fRemoveDependencies); |
157 |
monitor.worked(1); |
158 |
|
159 |
if (!monitor.isCanceled()) |
160 |
OrganizeManifest.organizeRequireBundles(currentBundle, fRemoveDependencies); |
161 |
monitor.worked(1); |
162 |
} |
163 |
|
164 |
if (fCalculateUses) { |
165 |
// we don't set the subTask because it is done in the CalculateUsesOperation, for each package it scans |
166 |
if (!monitor.isCanceled()) { |
167 |
CalculateUsesOperation op = new CalculateUsesOperation(fCurrentProject, modelBase); |
168 |
op.run(new SubProgressMonitor(monitor, 2)); |
169 |
} |
170 |
} |
171 |
|
172 |
if (fAddDependencies) { |
173 |
monitor.subTask(NLS.bind (PDEUIMessages.OrganizeManifestsOperation_additionalDeps, projectName)); |
174 |
if (!monitor.isCanceled()) { |
175 |
AddNewDependenciesOperation op = new AddNewDependenciesOperation(fCurrentProject, modelBase); |
176 |
op.run(new SubProgressMonitor(monitor, 4)); |
177 |
} |
178 |
} |
179 |
|
180 |
if (fUnusedDependencies) { |
181 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_unusedDeps, projectName)); |
182 |
if (!monitor.isCanceled()) { |
183 |
SubProgressMonitor submon = new SubProgressMonitor(monitor, 4); |
184 |
GatherUnusedDependenciesOperation udo = new GatherUnusedDependenciesOperation(modelBase); |
185 |
udo.run(submon); |
186 |
GatherUnusedDependenciesOperation.removeDependencies(modelBase, udo.getList().toArray()); |
187 |
submon.done(); |
188 |
} |
189 |
} |
190 |
|
191 |
if (fRemoveLazy) { |
192 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_lazyStart, fCurrentProject.getName())); |
193 |
if (!monitor.isCanceled()) |
194 |
OrganizeManifest.removeUnneededLazyStart(currentBundle); |
195 |
monitor.worked(1); |
196 |
} |
197 |
|
198 |
if (fPrefixIconNL) { |
199 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_nlIconPath, projectName)); |
200 |
if (!monitor.isCanceled()) |
201 |
OrganizeManifest.prefixIconPaths(currentExtensionsModel); |
202 |
monitor.worked(1); |
203 |
} |
204 |
|
205 |
if (fUnusedKeys) { |
206 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_unusedKeys, projectName)); |
207 |
if (!monitor.isCanceled()) { |
208 |
TextFileChange[] results = OrganizeManifest.removeUnusedKeys(fCurrentProject, currentBundle, currentExtensionsModel); |
209 |
if (results.length > 0) |
210 |
result[0] = results[0]; |
211 |
} |
212 |
monitor.worked(1); |
213 |
} |
214 |
} |
215 |
|
216 |
public Object[] getElements() { |
217 |
return fProjectList.toArray(); |
218 |
} |
219 |
|
220 |
public String getIdentifier() { |
221 |
return getClass().getName(); |
222 |
} |
223 |
|
224 |
public String getProcessorName() { |
225 |
return PDEUIMessages.OrganizeManifestsWizardPage_title; |
226 |
} |
227 |
|
228 |
public boolean isApplicable() throws CoreException { |
229 |
return true; |
230 |
} |
231 |
|
232 |
public RefactoringParticipant[] loadParticipants(RefactoringStatus status, |
233 |
SharableParticipants sharedParticipants) throws CoreException { |
234 |
return new RefactoringParticipant[0]; |
235 |
} |
236 |
|
237 |
private int getTotalTicksPerProject() { |
238 |
int ticks = 0; |
239 |
if (fAddMissing) ticks += 1; |
240 |
if (fMarkInternal) ticks += 1; |
241 |
if (fRemoveUnresolved) ticks += 1; |
242 |
if (fCalculateUses) ticks += 4; |
243 |
if (fModifyDep) ticks += 2; |
244 |
if (fUnusedDependencies)ticks += 4; |
245 |
if (fAddDependencies) ticks += 4; |
246 |
if (fRemoveLazy) ticks += 1; |
247 |
if (fPrefixIconNL) ticks += 1; |
248 |
if (fUnusedKeys) ticks += 1; |
249 |
return ticks; |
250 |
} |
251 |
|
252 |
public void setAddMissing(boolean addMissing) { |
253 |
fAddMissing = addMissing; |
254 |
} |
255 |
public void setMarkInternal(boolean markInternal) { |
256 |
fMarkInternal = markInternal; |
257 |
} |
258 |
public void setPackageFilter(String packageFilter) { |
259 |
fPackageFilter = packageFilter; |
260 |
} |
261 |
public void setRemoveUnresolved(boolean removeUnresolved) { |
262 |
fRemoveUnresolved = removeUnresolved; |
263 |
} |
264 |
public void setCalculateUses(boolean calculateUses) { |
265 |
fCalculateUses = calculateUses; |
266 |
} |
267 |
public void setModifyDep(boolean modifyDep) { |
268 |
fModifyDep = modifyDep; |
269 |
} |
270 |
public void setRemoveDependencies(boolean removeDependencies) { |
271 |
fRemoveDependencies = removeDependencies; |
272 |
} |
273 |
public void setUnusedDependencies(boolean unusedDependencies) { |
274 |
fUnusedDependencies = unusedDependencies; |
275 |
} |
276 |
public void setRemoveLazy(boolean removeLazy) { |
277 |
fRemoveLazy = removeLazy; |
278 |
} |
279 |
public void setPrefixIconNL(boolean prefixIconNL) { |
280 |
fPrefixIconNL = prefixIconNL; |
281 |
} |
282 |
public void setUnusedKeys(boolean unusedKeys) { |
283 |
fUnusedKeys = unusedKeys; |
284 |
} |
285 |
public void setAddDependencies(boolean addDependencies) { |
286 |
fAddDependencies = addDependencies; |
287 |
} |
288 |
} |