KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jdt.core.IClasspathEntry;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.viewers.CheckboxTableViewer;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.ITableLabelProvider;
31 import org.eclipse.jface.viewers.LabelProvider;
32 import org.eclipse.jface.wizard.WizardPage;
33 import org.eclipse.pde.core.IBaseModel;
34 import org.eclipse.pde.core.build.IBuild;
35 import org.eclipse.pde.core.build.IBuildEntry;
36 import org.eclipse.pde.core.plugin.IPluginBase;
37 import org.eclipse.pde.core.plugin.IPluginLibrary;
38 import org.eclipse.pde.core.plugin.IPluginModelFactory;
39 import org.eclipse.pde.internal.core.TargetPlatformHelper;
40 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
41 import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel;
42 import org.eclipse.pde.internal.core.ibundle.IBundle;
43 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
44 import org.eclipse.pde.internal.core.natures.PDE;
45 import org.eclipse.pde.internal.core.util.CoreUtility;
46 import org.eclipse.pde.internal.core.util.IdUtil;
47 import org.eclipse.pde.internal.ui.IHelpContextIds;
48 import org.eclipse.pde.internal.ui.PDEPlugin;
49 import org.eclipse.pde.internal.ui.PDEUIMessages;
50 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
51 import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
52 import org.eclipse.pde.internal.ui.util.ModelModification;
53 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
54 import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
55 import org.eclipse.swt.SWT;
56 import org.eclipse.swt.graphics.Image;
57 import org.eclipse.swt.layout.GridLayout;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.ui.PlatformUI;
60 import org.eclipse.ui.actions.WorkspaceModifyOperation;
61 import org.eclipse.ui.ide.IDE;
62 import org.osgi.framework.Constants;
63     
64 public class ConvertedProjectsPage extends WizardPage {
65     private CheckboxTableViewer projectViewer;
66     private TablePart tablePart;
67     private IProject[] fSelected;
68     private IProject[] fUnconverted;
69     
70     private String JavaDoc fLibraryName;
71     private String JavaDoc[] fSrcEntries;
72     private String JavaDoc[] fLibEntries;
73     
74     public class ProjectContentProvider
75         extends DefaultContentProvider
76         implements IStructuredContentProvider {
77         public Object JavaDoc[] getElements(Object JavaDoc parent) {
78             if (fUnconverted!= null)
79                 return fUnconverted;
80             return new Object JavaDoc[0];
81         }
82     }
83
84     public class ProjectLabelProvider
85         extends LabelProvider
86         implements ITableLabelProvider {
87         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
88             if (index == 0)
89                 return ((IProject) obj).getName();
90             return ""; //$NON-NLS-1$
91
}
92         public Image getColumnImage(Object JavaDoc obj, int index) {
93             return PlatformUI.getWorkbench().getSharedImages().getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
94         }
95     }
96
97     class TablePart extends WizardCheckboxTablePart {
98         public TablePart(String JavaDoc mainLabel) {
99             super(mainLabel);
100         }
101         public void updateCounter(int count) {
102             super.updateCounter(count);
103             setPageComplete(count > 0);
104         }
105     }
106
107     public ConvertedProjectsPage(IProject[] projects, Vector JavaDoc initialSelection) {
108         super("convertedProjects"); //$NON-NLS-1$
109
setTitle(PDEUIMessages.ConvertedProjectWizard_title);
110         setDescription(PDEUIMessages.ConvertedProjectWizard_desc);
111         tablePart = new TablePart(PDEUIMessages.ConvertedProjectWizard_projectList);
112         this.fSelected = (IProject[])initialSelection.toArray(new IProject[initialSelection.size()]);
113         this.fUnconverted = projects;
114     }
115     
116     public void createControl(Composite parent) {
117         Composite container = new Composite(parent, SWT.NONE);
118         GridLayout layout = new GridLayout();
119         layout.numColumns = 2;
120         layout.marginHeight = 0;
121         layout.marginWidth = 5;
122         container.setLayout(layout);
123
124         tablePart.createControl(container);
125
126         projectViewer = tablePart.getTableViewer();
127         projectViewer.setContentProvider(new ProjectContentProvider());
128         projectViewer.setLabelProvider(new ProjectLabelProvider());
129         projectViewer.setInput(PDEPlugin.getWorkspace());
130     
131         tablePart.setSelection(fSelected);
132         tablePart.updateCounter(fSelected.length);
133
134         setControl(container);
135         Dialog.applyDialogFont(container);
136         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.CONVERTED_PROJECTS);
137     }
138
139
140     private String JavaDoc createInitialName(String JavaDoc id) {
141         int loc = id.lastIndexOf('.');
142         if (loc == -1)
143             return id;
144         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(id.substring(loc + 1));
145         buf.setCharAt(0, Character.toUpperCase(buf.charAt(0)));
146         return buf.toString();
147     }
148     private void createManifestFile(IFile file, IProgressMonitor monitor)
149         throws CoreException {
150         WorkspaceBundlePluginModel model = new WorkspaceBundlePluginModel(file, null);
151         model.load();
152         IBundle pluginBundle = model.getBundleModel().getBundle();
153
154         String JavaDoc pluginId = pluginBundle.getHeader(Constants.BUNDLE_SYMBOLICNAME);
155         String JavaDoc pluginName = pluginBundle.getHeader(Constants.BUNDLE_NAME);
156         String JavaDoc pluginVersion = pluginBundle.getHeader(Constants.BUNDLE_VERSION);
157
158         boolean missingInfo = (pluginId == null || pluginName == null || pluginVersion == null);
159         
160         //If no ID exists, create one
161
if(pluginId == null)
162         { pluginId = IdUtil.getValidId(file.getProject().getName());
163         }
164         //At this point, the plug-in ID is not null
165

166         //If no version number exists, create one
167
if(pluginVersion == null)
168         { pluginVersion = "1.0.0"; //$NON-NLS-1$
169
}
170         
171         //If no name exists, create one using the non-null pluginID
172
if(pluginName == null)
173         { pluginName = createInitialName(pluginId);
174         }
175         
176         pluginBundle.setHeader(Constants.BUNDLE_SYMBOLICNAME, pluginId);
177         pluginBundle.setHeader(Constants.BUNDLE_VERSION, pluginVersion);
178         pluginBundle.setHeader(Constants.BUNDLE_NAME, pluginName);
179
180         if(missingInfo)
181         { IPluginModelFactory factory = model.getPluginFactory();
182             IPluginBase base = model.getPluginBase();
183             if (fLibraryName != null && !fLibraryName.equals(".")) { //$NON-NLS-1$
184
IPluginLibrary library = factory.createLibrary();
185                 library.setName(fLibraryName);
186                 library.setExported(true);
187                 base.add(library);
188             }
189             for (int i = 0; i < fLibEntries.length; i++) {
190                 IPluginLibrary library = factory.createLibrary();
191                 library.setName(fLibEntries[i]);
192                 library.setExported(true);
193                 base.add(library);
194             }
195             if (TargetPlatformHelper.getTargetVersion() >= 3.1)
196                 pluginBundle.setHeader(Constants.BUNDLE_MANIFESTVERSION, "2"); //$NON-NLS-1$
197
}
198
199         model.save();
200         monitor.done();
201         organizeExports(file.getProject());
202     }
203
204     private boolean isOldTarget() {
205         return TargetPlatformHelper.getTargetVersion() < 3.1;
206     }
207     
208     public boolean finish() {
209         final Object JavaDoc [] selected = tablePart.getSelection();
210         
211         IRunnableWithProgress operation = new WorkspaceModifyOperation() {
212             public void execute(IProgressMonitor monitor) {
213                 try {
214                     convertProjects(selected, monitor);
215                 } catch (CoreException e) {
216                     PDEPlugin.logException(e);
217                 } finally {
218                     monitor.done();
219                 }
220             }
221         };
222         try {
223             getContainer().run(false, true, operation);
224         } catch (InvocationTargetException JavaDoc e) {
225             PDEPlugin.logException(e);
226             return false;
227         } catch (InterruptedException JavaDoc e) {
228             PDEPlugin.logException(e);
229             return false;
230         }
231         return true;
232     }
233
234     public void convertProject(IProject project, IProgressMonitor monitor)
235             throws CoreException {
236         
237         CoreUtility.addNatureToProject(project, PDE.PLUGIN_NATURE, monitor);
238         
239         loadClasspathEntries(project, monitor);
240         loadLibraryName(project);
241         
242         createManifestFile(project.getFile(PDEModelUtility.F_MANIFEST_FP), monitor);
243
244         IFile buildFile = project.getFile(PDEModelUtility.F_BUILD);
245         if (!buildFile.exists()) {
246             WorkspaceBuildModel model = new WorkspaceBuildModel(buildFile);
247             IBuild build = model.getBuild(true);
248             IBuildEntry entry = model.getFactory().createEntry("bin.includes"); //$NON-NLS-1$
249
if (project.getFile("plugin.xml").exists()) //$NON-NLS-1$
250
entry.addToken("plugin.xml"); //$NON-NLS-1$
251
if (project.getFile("META-INF/MANIFEST.MF").exists()) //$NON-NLS-1$
252
entry.addToken("META-INF/"); //$NON-NLS-1$
253
for (int i = 0; i < fLibEntries.length; i++) {
254                 entry.addToken(fLibEntries[i]);
255             }
256             
257             if (fSrcEntries.length > 0) {
258                 entry.addToken(fLibraryName);
259                 IBuildEntry source = model.getFactory().createEntry("source." + fLibraryName); //$NON-NLS-1$
260
for (int i = 0; i < fSrcEntries.length; i++) {
261                     source.addToken(fSrcEntries[i]);
262                 }
263                 build.add(source);
264             }
265             if (entry.getTokens().length > 0)
266                 build.add(entry);
267             
268             model.save();
269         }
270     }
271     
272     private void convertProjects(Object JavaDoc[] selected, IProgressMonitor monitor)
273             throws CoreException {
274         monitor.beginTask(PDEUIMessages.ConvertedProjectWizard_converting, selected.length);
275         for (int i = 0; i < selected.length; i++) {
276             convertProject((IProject) selected[i], monitor);
277             monitor.worked(1);
278         }
279         monitor.done();
280     }
281     
282     private void loadClasspathEntries(IProject project, IProgressMonitor monitor) {
283         IJavaProject javaProject = JavaCore.create(project);
284         IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
285         ArrayList JavaDoc sources = new ArrayList JavaDoc();
286         ArrayList JavaDoc libraries = new ArrayList JavaDoc();
287         try {
288             currentClassPath = javaProject.getRawClasspath();
289         } catch (JavaModelException e) {
290         }
291         for (int i = 0; i < currentClassPath.length; i++) {
292             int contentType = currentClassPath[i].getEntryKind();
293             if (contentType == IClasspathEntry.CPE_SOURCE) {
294                 String JavaDoc relativePath = getRelativePath(currentClassPath[i], project);
295                 if (relativePath.equals("")) { //$NON-NLS-1$
296
sources.add("."); //$NON-NLS-1$
297
} else {
298                     sources.add(relativePath + "/"); //$NON-NLS-1$
299
}
300             } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
301                 String JavaDoc path = getRelativePath(currentClassPath[i], project);
302                 if (path.length() > 0)
303                     libraries.add(path);
304                 else
305                     libraries.add("."); //$NON-NLS-1$
306
}
307         }
308         fSrcEntries = (String JavaDoc[])sources.toArray(new String JavaDoc[sources.size()]);
309         fLibEntries = (String JavaDoc[])libraries.toArray(new String JavaDoc[libraries.size()]);
310         
311         IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
312         System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length);
313         classPath[classPath.length - 1] = ClasspathComputer.createContainerEntry();
314         try {
315             javaProject.setRawClasspath(classPath, monitor);
316         } catch (JavaModelException e) {
317         }
318     }
319     
320     private String JavaDoc getRelativePath(IClasspathEntry cpe, IProject project) {
321         IPath path = project.getFile(cpe.getPath()).getProjectRelativePath();
322         return path.removeFirstSegments(1).toString();
323     }
324     
325     private void loadLibraryName(IProject project) {
326         if (isOldTarget() ||
327                 (fLibEntries.length > 0 && fSrcEntries.length > 0)) {
328             String JavaDoc libName = project.getName();
329             int i = libName.lastIndexOf("."); //$NON-NLS-1$
330
if (i != -1)
331                 libName = libName.substring(i + 1);
332             fLibraryName = libName + ".jar"; //$NON-NLS-1$
333
} else {
334             fLibraryName = "."; //$NON-NLS-1$
335
}
336     }
337     
338     private void organizeExports(final IProject project) {
339         PDEModelUtility.modifyModel(new ModelModification(
340                 project.getFile(PDEModelUtility.F_MANIFEST_FP)) {
341             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
342                 if (!(model instanceof IBundlePluginModelBase))
343                     return;
344                 OrganizeManifest.organizeExportPackages(
345                         ((IBundlePluginModelBase)model).getBundleModel().getBundle(),
346                         project, true, true);
347             }
348         }, null);
349     }
350 }
351
Popular Tags