Added
Link Here
|
1 |
package org.eclipse.pde.internal.ui.wizards.tools; |
2 |
|
3 |
import java.lang.reflect.InvocationTargetException; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Iterator; |
6 |
|
7 |
import org.eclipse.core.resources.IProject; |
8 |
import org.eclipse.core.runtime.CoreException; |
9 |
import org.eclipse.core.runtime.IProgressMonitor; |
10 |
import org.eclipse.core.runtime.OperationCanceledException; |
11 |
import org.eclipse.core.runtime.SubProgressMonitor; |
12 |
import org.eclipse.jface.dialogs.IDialogSettings; |
13 |
import org.eclipse.ltk.core.refactoring.Change; |
14 |
import org.eclipse.ltk.core.refactoring.CompositeChange; |
15 |
import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
16 |
import org.eclipse.ltk.core.refactoring.TextFileChange; |
17 |
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; |
18 |
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; |
19 |
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; |
20 |
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; |
21 |
import org.eclipse.osgi.util.NLS; |
22 |
import org.eclipse.pde.core.IBaseModel; |
23 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
24 |
import org.eclipse.pde.core.plugin.ISharedExtensionsModel; |
25 |
import org.eclipse.pde.internal.core.ibundle.IBundle; |
26 |
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; |
27 |
import org.eclipse.pde.internal.ui.PDEPlugin; |
28 |
import org.eclipse.pde.internal.ui.PDEUIMessages; |
29 |
import org.eclipse.pde.internal.ui.search.dependencies.AddNewDependenciesOperation; |
30 |
import org.eclipse.pde.internal.ui.search.dependencies.CalculateUsesOperation; |
31 |
import org.eclipse.pde.internal.ui.search.dependencies.GatherUnusedDependenciesOperation; |
32 |
import org.eclipse.pde.internal.ui.util.ModelModification; |
33 |
import org.eclipse.pde.internal.ui.util.PDEModelUtility; |
34 |
|
35 |
public class OrganizeManifestsProcessor extends RefactoringProcessor implements IOrganizeManifestsSettings { |
36 |
|
37 |
// if operation is executed without setting operations, these defaults will be used |
38 |
protected boolean fAddMissing = true; // add all packages to export-package |
39 |
protected boolean fMarkInternal = true; // mark export-package as internal |
40 |
protected String fPackageFilter = VALUE_DEFAULT_FILTER; |
41 |
protected boolean fRemoveUnresolved = true; // remove unresolved export-package |
42 |
protected boolean fCalculateUses = false; // calculate the 'uses' directive for exported packages |
43 |
protected boolean fModifyDep = true; // modify import-package / require-bundle |
44 |
protected boolean fRemoveDependencies = true; // if true: remove, else mark optional |
45 |
protected boolean fUnusedDependencies; // find/remove unused dependencies - long running op |
46 |
protected boolean fRemoveLazy = true; // remove lazy/auto start if no activator |
47 |
protected boolean fPrefixIconNL; // prefix icon paths with $nl$ |
48 |
protected boolean fUnusedKeys; // remove unused <bundle-localization>.properties keys |
49 |
protected boolean fAddDependencies; |
50 |
|
51 |
ArrayList fProjectList; |
52 |
private IProject fCurrentProject; |
53 |
private IBundle fCurrentBundle; |
54 |
private IPluginModelBase fCurrentExtensionsModel; |
55 |
|
56 |
public OrganizeManifestsProcessor(ArrayList projects) { |
57 |
fProjectList = projects; |
58 |
} |
59 |
|
60 |
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, |
61 |
CheckConditionsContext context) throws CoreException, |
62 |
OperationCanceledException { |
63 |
RefactoringStatus status = new RefactoringStatus(); |
64 |
for (Iterator i = fProjectList.iterator(); i.hasNext();) { |
65 |
if (!(i.next() instanceof IProject)) |
66 |
status.addFatalError(PDEUIMessages.OrganizeManifestsProcessor_invalidParam); |
67 |
} |
68 |
return status; |
69 |
} |
70 |
|
71 |
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) |
72 |
throws CoreException, OperationCanceledException { |
73 |
return null; |
74 |
} |
75 |
|
76 |
public Change createChange(IProgressMonitor pm) throws CoreException, |
77 |
OperationCanceledException { |
78 |
CompositeChange change = new CompositeChange(PDEUIMessages.OrganizeManifestsWizardPage_title); |
79 |
pm.beginTask(PDEUIMessages.OrganizeManifestJob_taskName, fProjectList.size()); |
80 |
for (Iterator i = fProjectList.iterator(); i.hasNext() && !pm.isCanceled();) |
81 |
change.add(cleanProject((IProject)i.next(), new SubProgressMonitor(pm, 1))); |
82 |
return change; |
83 |
} |
84 |
|
85 |
private Change cleanProject(IProject project, IProgressMonitor monitor) { |
86 |
fCurrentProject = project; |
87 |
fCurrentBundle = null; |
88 |
fCurrentExtensionsModel = null; |
89 |
CompositeChange change = new CompositeChange(NLS.bind(PDEUIMessages.OrganizeManifestsProcessor_rootMessage, new String[] {fCurrentProject.getName()})); |
90 |
monitor.beginTask(NLS.bind(PDEUIMessages.OrganizeManifestsProcessor_rootMessage, new String[] {fCurrentProject.getName()}), getTotalTicksPerProject()); |
91 |
|
92 |
final Exception[] ee = new Exception[1]; |
93 |
ModelModification modification = new ModelModification(fCurrentProject) { |
94 |
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
95 |
if (model instanceof IBundlePluginModelBase) |
96 |
try { |
97 |
runCleanup(monitor, (IBundlePluginModelBase)model); |
98 |
} catch (InvocationTargetException e) { |
99 |
ee[0] = e; |
100 |
} catch (InterruptedException e) { |
101 |
ee[0] = e; |
102 |
} |
103 |
} |
104 |
}; |
105 |
TextFileChange[] changes = PDEModelUtility.changesForModelModication(modification, monitor); |
106 |
for (int i = 0; i < changes.length; i++) |
107 |
change.add(changes[i]); |
108 |
changes = null; |
109 |
if (fUnusedKeys && fCurrentBundle != null && fCurrentExtensionsModel != null) { |
110 |
monitor.subTask(NLS.bind(PDEUIMessages.OrganizeManifestsOperation_unusedKeys, fCurrentProject.getName())); |
111 |
if (!monitor.isCanceled()) |
112 |
changes = OrganizeManifest.removeUnusedKeys(fCurrentProject, fCurrentBundle, fCurrentExtensionsModel); |
113 |
monitor.worked(1); |
114 |
if (changes != null) |
115 |
for (int i = 0; i < changes.length; i++) |
116 |
change.add(changes[i]); |
117 |
} |
118 |
if (ee[0] != null) |
119 |
PDEPlugin.log(ee[0]); |
120 |
return change; |
121 |
} |
122 |
|
123 |
private void runCleanup(IProgressMonitor monitor, IBundlePluginModelBase modelBase) throws InvocationTargetException, InterruptedException { |
124 |
|
125 |
fCurrentBundle = modelBase.getBundleModel().getBundle(); |
126 |
ISharedExtensionsModel sharedExtensionsModel = modelBase.getExtensionsModel(); |
127 |
fCurrentExtensionsModel = null; |
128 |
if (sharedExtensionsModel instanceof IPluginModelBase) |
129 |
fCurrentExtensionsModel = (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(fCurrentBundle, 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(fCurrentBundle, 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(fCurrentBundle, fRemoveDependencies); |
157 |
monitor.worked(1); |
158 |
|
159 |
if (!monitor.isCanceled()) |
160 |
OrganizeManifest.organizeRequireBundles(fCurrentBundle, 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(fCurrentBundle); |
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(fCurrentExtensionsModel); |
202 |
monitor.worked(1); |
203 |
} |
204 |
} |
205 |
|
206 |
public Object[] getElements() { |
207 |
return fProjectList.toArray(); |
208 |
} |
209 |
|
210 |
public String getIdentifier() { |
211 |
return getClass().getName(); |
212 |
} |
213 |
|
214 |
public String getProcessorName() { |
215 |
return PDEUIMessages.OrganizeManifestsWizardPage_title; |
216 |
} |
217 |
|
218 |
public boolean isApplicable() throws CoreException { |
219 |
return true; |
220 |
} |
221 |
|
222 |
public RefactoringParticipant[] loadParticipants(RefactoringStatus status, |
223 |
SharableParticipants sharedParticipants) throws CoreException { |
224 |
return new RefactoringParticipant[0]; |
225 |
} |
226 |
|
227 |
private int getTotalTicksPerProject() { |
228 |
int ticks = 0; |
229 |
if (fAddMissing) ticks += 1; |
230 |
if (fMarkInternal) ticks += 1; |
231 |
if (fRemoveUnresolved) ticks += 1; |
232 |
if (fCalculateUses) ticks += 4; |
233 |
if (fModifyDep) ticks += 2; |
234 |
if (fUnusedDependencies)ticks += 4; |
235 |
if (fAddDependencies) ticks += 4; |
236 |
if (fRemoveLazy) ticks += 1; |
237 |
if (fPrefixIconNL) ticks += 1; |
238 |
if (fUnusedKeys) ticks += 1; |
239 |
return ticks; |
240 |
} |
241 |
|
242 |
public void initSettings(IDialogSettings settings) { |
243 |
fAddMissing = !settings.getBoolean(PROP_ADD_MISSING); |
244 |
fMarkInternal = !settings.getBoolean(PROP_MARK_INTERNAL); |
245 |
fPackageFilter = settings.get(PROP_INTERAL_PACKAGE_FILTER); |
246 |
fRemoveUnresolved = !settings.getBoolean(PROP_REMOVE_UNRESOLVED_EX); |
247 |
fCalculateUses = settings.getBoolean(PROP_CALCULATE_USES); |
248 |
fModifyDep = !settings.getBoolean(PROP_MODIFY_DEP); |
249 |
fRemoveDependencies = !settings.getBoolean(PROP_RESOLVE_IMP_MARK_OPT); |
250 |
fUnusedDependencies = settings.getBoolean(PROP_UNUSED_DEPENDENCIES); |
251 |
fRemoveLazy = !settings.getBoolean(PROP_REMOVE_LAZY); |
252 |
fPrefixIconNL = settings.getBoolean(PROP_NLS_PATH); |
253 |
fUnusedKeys = settings.getBoolean(PROP_UNUSED_KEYS); |
254 |
fAddDependencies = settings.getBoolean(PROP_ADD_DEPENDENCIES); |
255 |
} |
256 |
protected void setAddMissing(boolean addMissing) { |
257 |
fAddMissing = addMissing; |
258 |
} |
259 |
protected void setMarkInternal(boolean markInternal) { |
260 |
fMarkInternal = markInternal; |
261 |
} |
262 |
protected void setPackageFilter(String packageFilter) { |
263 |
fPackageFilter = packageFilter; |
264 |
} |
265 |
protected void setRemoveUnresolved(boolean removeUnresolved) { |
266 |
fRemoveUnresolved = removeUnresolved; |
267 |
} |
268 |
protected void setCalculateUses(boolean calculateUses) { |
269 |
fCalculateUses = calculateUses; |
270 |
} |
271 |
protected void setModifyDep(boolean modifyDep) { |
272 |
fModifyDep = modifyDep; |
273 |
} |
274 |
protected void setRemoveDependencies(boolean removeDependencies) { |
275 |
fRemoveDependencies = removeDependencies; |
276 |
} |
277 |
protected void setUnusedDependencies(boolean unusedDependencies) { |
278 |
fUnusedDependencies = unusedDependencies; |
279 |
} |
280 |
protected void setRemoveLazy(boolean removeLazy) { |
281 |
fRemoveLazy = removeLazy; |
282 |
} |
283 |
protected void setPrefixIconNL(boolean prefixIconNL) { |
284 |
fPrefixIconNL = prefixIconNL; |
285 |
} |
286 |
protected void setUnusedKeys(boolean unusedKeys) { |
287 |
fUnusedKeys = unusedKeys; |
288 |
} |
289 |
protected void setAddDependencies(boolean addDependencies) { |
290 |
fAddDependencies = addDependencies; |
291 |
} |
292 |
} |