KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > tools > MigratePluginWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.io.BufferedReader JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.io.FileWriter JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IProjectDescription;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.jobs.Job;
27 import org.eclipse.jface.dialogs.IDialogSettings;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.text.BadLocationException;
30 import org.eclipse.jface.text.Document;
31 import org.eclipse.jface.text.FindReplaceDocumentAdapter;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IRegion;
34 import org.eclipse.jface.text.Region;
35 import org.eclipse.jface.wizard.Wizard;
36 import org.eclipse.pde.core.plugin.IPluginExtension;
37 import org.eclipse.pde.core.plugin.IPluginImport;
38 import org.eclipse.pde.core.plugin.IPluginModelBase;
39 import org.eclipse.pde.internal.ui.PDEPlugin;
40 import org.eclipse.pde.internal.ui.PDEPluginImages;
41 import org.eclipse.pde.internal.ui.PDEUIMessages;
42
43 public class MigratePluginWizard extends Wizard {
44     private MigratePluginWizardPage page1;
45
46     private IPluginModelBase[] fSelected;
47     private IPluginModelBase[] fUnmigrated;
48
49     private static final String JavaDoc STORE_SECTION = "MigrationWizard"; //$NON-NLS-1$
50

51     public MigratePluginWizard(IPluginModelBase[] models,IPluginModelBase[] selected) {
52         IDialogSettings masterSettings = PDEPlugin.getDefault()
53                 .getDialogSettings();
54         setDialogSettings(getSettingsSection(masterSettings));
55         setDefaultPageImageDescriptor(PDEPluginImages.DESC_MIGRATE_30_WIZ);
56         setWindowTitle(PDEUIMessages.MigrationWizard_title);
57         setNeedsProgressMonitor(true);
58         this.fSelected = selected;
59         this.fUnmigrated = models;
60     }
61
62     public boolean performFinish() {
63         final IPluginModelBase[] models = page1.getSelected();
64         page1.storeSettings();
65         final boolean doUpdateClasspath = page1.isUpdateClasspathRequested();
66         final boolean doCleanProjects = page1.isCleanProjectsRequested();
67
68         IRunnableWithProgress operation = new IRunnableWithProgress() {
69             public void run(IProgressMonitor monitor)
70                     throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
71
72                 if (PDEPlugin.getWorkspace().validateEdit(
73                         getFilesToValidate(models), getContainer().getShell())
74                         .getSeverity() != IStatus.OK) {
75                     monitor.done();
76                     return;
77                 }
78
79                 int numUnits = doUpdateClasspath ? models.length * 2
80                         : models.length;
81                 monitor
82                         .beginTask(
83                                 PDEUIMessages.MigrationWizard_progress, numUnits);
84                 try {
85                     for (int i = 0; i < models.length; i++) {
86                         monitor.subTask(models[i].getPluginBase().getId());
87                         transform(models[i]);
88                         models[i].getUnderlyingResource().refreshLocal(
89                                 IResource.DEPTH_ZERO, null);
90                         monitor.worked(1);
91                         if (doCleanProjects) {
92                             IProject project = models[i]
93                                     .getUnderlyingResource().getProject();
94                             IProjectDescription desc = project.getDescription();
95                             desc.setReferencedProjects(new IProject[0]);
96                             project.setDescription(desc, null);
97                         }
98                         monitor.worked(1);
99                     }
100                     if (doUpdateClasspath) {
101                         Job j = new UpdateClasspathJob(models);
102                         j.setUser(true);
103                         j.schedule();
104                     }
105                 } catch (Exception JavaDoc e) {
106                     PDEPlugin.logException(e);
107                 } finally {
108                     monitor.done();
109                 }
110             }
111         };
112
113         try {
114             getContainer().run(true, false, operation);
115         } catch (InvocationTargetException JavaDoc e) {
116         } catch (InterruptedException JavaDoc e) {
117         }
118         return true;
119     }
120
121     private IFile[] getFilesToValidate(IPluginModelBase[] models) {
122         ArrayList JavaDoc files = new ArrayList JavaDoc();
123         for (int i = 0; i < models.length; i++) {
124             IProject project = models[i].getUnderlyingResource().getProject();
125             files.add(models[i].getUnderlyingResource());
126             files.add(project.getFile(".project")); //$NON-NLS-1$
127
files.add(project.getFile(".classpath")); //$NON-NLS-1$
128
}
129         return (IFile[]) files.toArray(new IFile[files.size()]);
130     }
131
132     private IDialogSettings getSettingsSection(IDialogSettings master) {
133         IDialogSettings setting = master.getSection(STORE_SECTION);
134         if (setting == null) {
135             setting = master.addNewSection(STORE_SECTION);
136         }
137         return setting;
138     }
139
140     public void addPages() {
141         page1 = new MigratePluginWizardPage(fUnmigrated, fSelected);
142         addPage(page1);
143     }
144
145     private void transform(IPluginModelBase model) throws Exception JavaDoc {
146         IResource file = model.getUnderlyingResource();
147         IDocument document = createDocument(file);
148         FindReplaceDocumentAdapter findAdapter = new FindReplaceDocumentAdapter(
149                 document);
150         addEclipseProcessingInstruction(document, findAdapter);
151         updateExtensions(document, findAdapter);
152         if (model.getPluginBase().getImports().length > 0)
153             addNewImports(document, findAdapter, getAdditionalImports(model));
154         writeFile(document, file);
155     }
156
157     private IDocument createDocument(IResource file) throws Exception JavaDoc {
158         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file
159                 .getLocation().toOSString()));
160         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
161         while (reader.ready()) {
162             buffer.append((char) reader.read());
163         }
164         reader.close();
165         return new Document(buffer.toString());
166     }
167
168     private void writeFile(IDocument document, IResource file) throws Exception JavaDoc {
169         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(file.getLocation()
170                 .toOSString()));
171         writer.write(document.get());
172         writer.close();
173     }
174
175     private void addEclipseProcessingInstruction(IDocument document,
176             FindReplaceDocumentAdapter adapter) {
177         try {
178             IRegion region = adapter.find(-1,
179                     "<\\?xml.*\\?>", true, true, false, true); //$NON-NLS-1$
180
if (region != null) {
181                 String JavaDoc text = document.get(region.getOffset(), region
182                         .getLength());
183                 adapter.replace(text + System.getProperty("line.separator") //$NON-NLS-1$
184
+ "<?eclipse version=\"3.0\"?>", //$NON-NLS-1$
185
false);
186             }
187         } catch (BadLocationException e) {
188         }
189     }
190
191     private void updateExtensions(IDocument document,
192             FindReplaceDocumentAdapter adapter) {
193         int start = 0;
194         for (;;) {
195             try {
196                 IRegion region = findNextExtension(adapter, start);
197                 if (region == null)
198                     break;
199                 IRegion idRegion = findPointAttributeRegion(adapter, region);
200                 if (idRegion != null) {
201                     String JavaDoc point = document.get(idRegion.getOffset(), idRegion
202                             .getLength());
203                     if (ExtensionPointMappings.isDeprecated(point.trim())) {
204                         adapter.replace(ExtensionPointMappings.getNewId(point
205                                 .trim()), false);
206                     }
207                 }
208                 start = region.getOffset() + region.getLength();
209             } catch (BadLocationException e) {
210             }
211         }
212     }
213
214     private IRegion findPointAttributeRegion(
215             FindReplaceDocumentAdapter adapter, IRegion parentRegion) {
216         try {
217             IRegion region = adapter.find(parentRegion.getOffset(),
218                     "\\s+point\\s*=\\s*\"", //$NON-NLS-1$
219
true, true, false, true);
220             if (region != null
221                     && region.getOffset() + region.getLength() <= parentRegion
222                             .getOffset()
223                             + parentRegion.getLength()) {
224                 region = adapter.find(region.getOffset() + region.getLength(),
225                         "[^\"]*", //$NON-NLS-1$
226
true, true, false, true);
227                 if (region != null
228                         && region.getOffset() + region.getLength() < parentRegion
229                                 .getOffset()
230                                 + parentRegion.getLength()) {
231                     return region;
232                 }
233             }
234         } catch (BadLocationException e) {
235         }
236         return null;
237     }
238
239     private IRegion findNextExtension(FindReplaceDocumentAdapter adapter,
240             int start) {
241         int offset = -1;
242         int length = -1;
243         try {
244             IRegion region = adapter.find(start,
245                     "<extension\\s+", true, true, false, true); //$NON-NLS-1$
246
if (region != null) {
247                 offset = region.getOffset();
248                 region = adapter.find(offset, ">", true, true, false, false); //$NON-NLS-1$
249
if (region != null) {
250                     length = region.getOffset() - offset + 1;
251                 }
252             }
253         } catch (BadLocationException e) {
254         }
255         return (offset != -1 && length != -1) ? new Region(offset, length)
256                 : null;
257     }
258
259     private String JavaDoc[] getAdditionalImports(IPluginModelBase model) {
260         ArrayList JavaDoc result = new ArrayList JavaDoc();
261         //TODO do no just add. If core.runtime exists, replace it.
262
//if (findImport(model, "org.eclipse.core.runtime") == null)
263
result
264                 .add("<import plugin=\"org.eclipse.core.runtime.compatibility\"/>"); //$NON-NLS-1$
265
IPluginImport uiImport = findImport(model, "org.eclipse.ui"); //$NON-NLS-1$
266
if (uiImport != null) {
267             ArrayList JavaDoc list = new ArrayList JavaDoc();
268             list.add("org.eclipse.ui.ide"); //$NON-NLS-1$
269
list.add("org.eclipse.ui.views"); //$NON-NLS-1$
270
list.add("org.eclipse.jface.text"); //$NON-NLS-1$
271
list.add("org.eclipse.ui.workbench.texteditor"); //$NON-NLS-1$
272
list.add("org.eclipse.ui.editors"); //$NON-NLS-1$
273
IPluginImport[] imports = model.getPluginBase().getImports();
274             for (int i = 0; i < imports.length; i++) {
275                 if (list.contains(imports[i].getId())) {
276                     list.remove(imports[i].getId());
277                 }
278             }
279             for (int i = 0; i < list.size(); i++) {
280                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("<import plugin=\""); //$NON-NLS-1$
281
buffer.append(list.get(i) + "\""); //$NON-NLS-1$
282
if (uiImport.isReexported())
283                     buffer.append(" export=\"true\""); //$NON-NLS-1$
284
if (uiImport.isOptional())
285                     buffer.append(" optional=\"true\""); //$NON-NLS-1$
286
buffer.append("/>"); //$NON-NLS-1$
287
result.add(buffer.toString());
288             }
289         } else if (needsAdditionalUIImport(model)) {
290             result.add("<import plugin=\"org.eclipse.ui\"/>"); //$NON-NLS-1$
291
}
292         if (needsHelpBaseImport(model))
293             result.add("<import plugin=\"org.eclipse.help.base\"/>"); //$NON-NLS-1$
294

295         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
296     }
297
298     private void addNewImports(IDocument document,
299             FindReplaceDocumentAdapter adapter, String JavaDoc[] imports) {
300         try {
301             if (imports.length == 0)
302                 return;
303
304             String JavaDoc space = ""; //$NON-NLS-1$
305
IRegion requiresRegion = adapter.find(0,
306                     "<requires>", true, false, false, false); //$NON-NLS-1$
307
if (requiresRegion != null) {
308                 IRegion spacerRegion = adapter.find(requiresRegion.getOffset()
309                         + requiresRegion.getLength(),
310                         "\\s*", true, true, false, true); //$NON-NLS-1$
311
if (spacerRegion != null) {
312                     space = document.get(spacerRegion.getOffset(), spacerRegion
313                             .getLength());
314                 }
315             }
316             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(space);
317             for (int i = 0; i < imports.length; i++) {
318                 buffer.append(imports[i] + space);
319             }
320             adapter.replace(buffer.toString(), false);
321         } catch (BadLocationException e) {
322         }
323     }
324
325     private boolean needsAdditionalUIImport(IPluginModelBase model) {
326         IPluginExtension[] extensions = model.getPluginBase().getExtensions();
327         for (int i = 0; i < extensions.length; i++) {
328             if (ExtensionPointMappings.hasMovedFromHelpToUI(extensions[i]
329                     .getPoint())
330                     && findImport(model, "org.eclipse.ui") == null) //$NON-NLS-1$
331
return true;
332         }
333         return false;
334     }
335
336     private boolean needsHelpBaseImport(IPluginModelBase model) {
337         IPluginExtension[] extensions = model.getPluginBase().getExtensions();
338         for (int i = 0; i < extensions.length; i++) {
339             if (ExtensionPointMappings.hasMovedFromHelpToBase(extensions[i]
340                     .getPoint())
341                     && findImport(model, "org.eclipse.help.base") == null) { //$NON-NLS-1$
342
return true;
343             }
344         }
345         return false;
346     }
347
348     private IPluginImport findImport(IPluginModelBase model, String JavaDoc importID) {
349         IPluginImport[] imports = model.getPluginBase().getImports();
350         for (int i = 0; i < imports.length; i++) {
351             if (imports[i].getId().equals(importID)) {
352                 return imports[i];
353             }
354         }
355         return null;
356     }
357
358 }
359
Popular Tags