001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.tools;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014: import java.util.Vector;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.jdt.core.IClasspathEntry;
022: import org.eclipse.jdt.core.IJavaProject;
023: import org.eclipse.jdt.core.JavaCore;
024: import org.eclipse.jdt.core.JavaModelException;
025: import org.eclipse.jface.dialogs.Dialog;
026: import org.eclipse.jface.operation.IRunnableWithProgress;
027: import org.eclipse.jface.viewers.CheckboxTableViewer;
028: import org.eclipse.jface.viewers.IStructuredContentProvider;
029: import org.eclipse.jface.viewers.ITableLabelProvider;
030: import org.eclipse.jface.viewers.LabelProvider;
031: import org.eclipse.jface.wizard.WizardPage;
032: import org.eclipse.pde.core.IBaseModel;
033: import org.eclipse.pde.core.build.IBuild;
034: import org.eclipse.pde.core.build.IBuildEntry;
035: import org.eclipse.pde.core.plugin.IPluginBase;
036: import org.eclipse.pde.core.plugin.IPluginLibrary;
037: import org.eclipse.pde.core.plugin.IPluginModelFactory;
038: import org.eclipse.pde.internal.core.TargetPlatformHelper;
039: import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
040: import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel;
041: import org.eclipse.pde.internal.core.ibundle.IBundle;
042: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
043: import org.eclipse.pde.internal.core.natures.PDE;
044: import org.eclipse.pde.internal.core.util.CoreUtility;
045: import org.eclipse.pde.internal.core.util.IdUtil;
046: import org.eclipse.pde.internal.ui.IHelpContextIds;
047: import org.eclipse.pde.internal.ui.PDEPlugin;
048: import org.eclipse.pde.internal.ui.PDEUIMessages;
049: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
050: import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
051: import org.eclipse.pde.internal.ui.util.ModelModification;
052: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
053: import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
054: import org.eclipse.swt.SWT;
055: import org.eclipse.swt.graphics.Image;
056: import org.eclipse.swt.layout.GridLayout;
057: import org.eclipse.swt.widgets.Composite;
058: import org.eclipse.ui.PlatformUI;
059: import org.eclipse.ui.actions.WorkspaceModifyOperation;
060: import org.eclipse.ui.ide.IDE;
061: import org.osgi.framework.Constants;
062:
063: public class ConvertedProjectsPage extends WizardPage {
064: private CheckboxTableViewer projectViewer;
065: private TablePart tablePart;
066: private IProject[] fSelected;
067: private IProject[] fUnconverted;
068:
069: private String fLibraryName;
070: private String[] fSrcEntries;
071: private String[] fLibEntries;
072:
073: public class ProjectContentProvider extends DefaultContentProvider
074: implements IStructuredContentProvider {
075: public Object[] getElements(Object parent) {
076: if (fUnconverted != null)
077: return fUnconverted;
078: return new Object[0];
079: }
080: }
081:
082: public class ProjectLabelProvider extends LabelProvider implements
083: ITableLabelProvider {
084: public String getColumnText(Object obj, int index) {
085: if (index == 0)
086: return ((IProject) obj).getName();
087: return ""; //$NON-NLS-1$
088: }
089:
090: public Image getColumnImage(Object obj, int index) {
091: return PlatformUI.getWorkbench().getSharedImages()
092: .getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
093: }
094: }
095:
096: class TablePart extends WizardCheckboxTablePart {
097: public TablePart(String mainLabel) {
098: super (mainLabel);
099: }
100:
101: public void updateCounter(int count) {
102: super .updateCounter(count);
103: setPageComplete(count > 0);
104: }
105: }
106:
107: public ConvertedProjectsPage(IProject[] projects,
108: Vector initialSelection) {
109: super ("convertedProjects"); //$NON-NLS-1$
110: setTitle(PDEUIMessages.ConvertedProjectWizard_title);
111: setDescription(PDEUIMessages.ConvertedProjectWizard_desc);
112: tablePart = new TablePart(
113: PDEUIMessages.ConvertedProjectWizard_projectList);
114: this .fSelected = (IProject[]) initialSelection
115: .toArray(new IProject[initialSelection.size()]);
116: this .fUnconverted = projects;
117: }
118:
119: public void createControl(Composite parent) {
120: Composite container = new Composite(parent, SWT.NONE);
121: GridLayout layout = new GridLayout();
122: layout.numColumns = 2;
123: layout.marginHeight = 0;
124: layout.marginWidth = 5;
125: container.setLayout(layout);
126:
127: tablePart.createControl(container);
128:
129: projectViewer = tablePart.getTableViewer();
130: projectViewer.setContentProvider(new ProjectContentProvider());
131: projectViewer.setLabelProvider(new ProjectLabelProvider());
132: projectViewer.setInput(PDEPlugin.getWorkspace());
133:
134: tablePart.setSelection(fSelected);
135: tablePart.updateCounter(fSelected.length);
136:
137: setControl(container);
138: Dialog.applyDialogFont(container);
139: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
140: IHelpContextIds.CONVERTED_PROJECTS);
141: }
142:
143: private String createInitialName(String id) {
144: int loc = id.lastIndexOf('.');
145: if (loc == -1)
146: return id;
147: StringBuffer buf = new StringBuffer(id.substring(loc + 1));
148: buf.setCharAt(0, Character.toUpperCase(buf.charAt(0)));
149: return buf.toString();
150: }
151:
152: private void createManifestFile(IFile file, IProgressMonitor monitor)
153: throws CoreException {
154: WorkspaceBundlePluginModel model = new WorkspaceBundlePluginModel(
155: file, null);
156: model.load();
157: IBundle pluginBundle = model.getBundleModel().getBundle();
158:
159: String pluginId = pluginBundle
160: .getHeader(Constants.BUNDLE_SYMBOLICNAME);
161: String pluginName = pluginBundle
162: .getHeader(Constants.BUNDLE_NAME);
163: String pluginVersion = pluginBundle
164: .getHeader(Constants.BUNDLE_VERSION);
165:
166: boolean missingInfo = (pluginId == null || pluginName == null || pluginVersion == null);
167:
168: //If no ID exists, create one
169: if (pluginId == null) {
170: pluginId = IdUtil.getValidId(file.getProject().getName());
171: }
172: //At this point, the plug-in ID is not null
173:
174: //If no version number exists, create one
175: if (pluginVersion == null) {
176: pluginVersion = "1.0.0"; //$NON-NLS-1$
177: }
178:
179: //If no name exists, create one using the non-null pluginID
180: if (pluginName == null) {
181: pluginName = createInitialName(pluginId);
182: }
183:
184: pluginBundle.setHeader(Constants.BUNDLE_SYMBOLICNAME, pluginId);
185: pluginBundle.setHeader(Constants.BUNDLE_VERSION, pluginVersion);
186: pluginBundle.setHeader(Constants.BUNDLE_NAME, pluginName);
187:
188: if (missingInfo) {
189: IPluginModelFactory factory = model.getPluginFactory();
190: IPluginBase base = model.getPluginBase();
191: if (fLibraryName != null && !fLibraryName.equals(".")) { //$NON-NLS-1$
192: IPluginLibrary library = factory.createLibrary();
193: library.setName(fLibraryName);
194: library.setExported(true);
195: base.add(library);
196: }
197: for (int i = 0; i < fLibEntries.length; i++) {
198: IPluginLibrary library = factory.createLibrary();
199: library.setName(fLibEntries[i]);
200: library.setExported(true);
201: base.add(library);
202: }
203: if (TargetPlatformHelper.getTargetVersion() >= 3.1)
204: pluginBundle.setHeader(
205: Constants.BUNDLE_MANIFESTVERSION, "2"); //$NON-NLS-1$
206: }
207:
208: model.save();
209: monitor.done();
210: organizeExports(file.getProject());
211: }
212:
213: private boolean isOldTarget() {
214: return TargetPlatformHelper.getTargetVersion() < 3.1;
215: }
216:
217: public boolean finish() {
218: final Object[] selected = tablePart.getSelection();
219:
220: IRunnableWithProgress operation = new WorkspaceModifyOperation() {
221: public void execute(IProgressMonitor monitor) {
222: try {
223: convertProjects(selected, monitor);
224: } catch (CoreException e) {
225: PDEPlugin.logException(e);
226: } finally {
227: monitor.done();
228: }
229: }
230: };
231: try {
232: getContainer().run(false, true, operation);
233: } catch (InvocationTargetException e) {
234: PDEPlugin.logException(e);
235: return false;
236: } catch (InterruptedException e) {
237: PDEPlugin.logException(e);
238: return false;
239: }
240: return true;
241: }
242:
243: public void convertProject(IProject project,
244: IProgressMonitor monitor) throws CoreException {
245:
246: CoreUtility.addNatureToProject(project, PDE.PLUGIN_NATURE,
247: monitor);
248:
249: loadClasspathEntries(project, monitor);
250: loadLibraryName(project);
251:
252: createManifestFile(project
253: .getFile(PDEModelUtility.F_MANIFEST_FP), monitor);
254:
255: IFile buildFile = project.getFile(PDEModelUtility.F_BUILD);
256: if (!buildFile.exists()) {
257: WorkspaceBuildModel model = new WorkspaceBuildModel(
258: buildFile);
259: IBuild build = model.getBuild(true);
260: IBuildEntry entry = model.getFactory().createEntry(
261: "bin.includes"); //$NON-NLS-1$
262: if (project.getFile("plugin.xml").exists()) //$NON-NLS-1$
263: entry.addToken("plugin.xml"); //$NON-NLS-1$
264: if (project.getFile("META-INF/MANIFEST.MF").exists()) //$NON-NLS-1$
265: entry.addToken("META-INF/"); //$NON-NLS-1$
266: for (int i = 0; i < fLibEntries.length; i++) {
267: entry.addToken(fLibEntries[i]);
268: }
269:
270: if (fSrcEntries.length > 0) {
271: entry.addToken(fLibraryName);
272: IBuildEntry source = model.getFactory().createEntry(
273: "source." + fLibraryName); //$NON-NLS-1$
274: for (int i = 0; i < fSrcEntries.length; i++) {
275: source.addToken(fSrcEntries[i]);
276: }
277: build.add(source);
278: }
279: if (entry.getTokens().length > 0)
280: build.add(entry);
281:
282: model.save();
283: }
284: }
285:
286: private void convertProjects(Object[] selected,
287: IProgressMonitor monitor) throws CoreException {
288: monitor.beginTask(
289: PDEUIMessages.ConvertedProjectWizard_converting,
290: selected.length);
291: for (int i = 0; i < selected.length; i++) {
292: convertProject((IProject) selected[i], monitor);
293: monitor.worked(1);
294: }
295: monitor.done();
296: }
297:
298: private void loadClasspathEntries(IProject project,
299: IProgressMonitor monitor) {
300: IJavaProject javaProject = JavaCore.create(project);
301: IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
302: ArrayList sources = new ArrayList();
303: ArrayList libraries = new ArrayList();
304: try {
305: currentClassPath = javaProject.getRawClasspath();
306: } catch (JavaModelException e) {
307: }
308: for (int i = 0; i < currentClassPath.length; i++) {
309: int contentType = currentClassPath[i].getEntryKind();
310: if (contentType == IClasspathEntry.CPE_SOURCE) {
311: String relativePath = getRelativePath(
312: currentClassPath[i], project);
313: if (relativePath.equals("")) { //$NON-NLS-1$
314: sources.add("."); //$NON-NLS-1$
315: } else {
316: sources.add(relativePath + "/"); //$NON-NLS-1$
317: }
318: } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
319: String path = getRelativePath(currentClassPath[i],
320: project);
321: if (path.length() > 0)
322: libraries.add(path);
323: else
324: libraries.add("."); //$NON-NLS-1$
325: }
326: }
327: fSrcEntries = (String[]) sources.toArray(new String[sources
328: .size()]);
329: fLibEntries = (String[]) libraries.toArray(new String[libraries
330: .size()]);
331:
332: IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
333: System.arraycopy(currentClassPath, 0, classPath, 0,
334: currentClassPath.length);
335: classPath[classPath.length - 1] = ClasspathComputer
336: .createContainerEntry();
337: try {
338: javaProject.setRawClasspath(classPath, monitor);
339: } catch (JavaModelException e) {
340: }
341: }
342:
343: private String getRelativePath(IClasspathEntry cpe, IProject project) {
344: IPath path = project.getFile(cpe.getPath())
345: .getProjectRelativePath();
346: return path.removeFirstSegments(1).toString();
347: }
348:
349: private void loadLibraryName(IProject project) {
350: if (isOldTarget()
351: || (fLibEntries.length > 0 && fSrcEntries.length > 0)) {
352: String libName = project.getName();
353: int i = libName.lastIndexOf("."); //$NON-NLS-1$
354: if (i != -1)
355: libName = libName.substring(i + 1);
356: fLibraryName = libName + ".jar"; //$NON-NLS-1$
357: } else {
358: fLibraryName = "."; //$NON-NLS-1$
359: }
360: }
361:
362: private void organizeExports(final IProject project) {
363: PDEModelUtility.modifyModel(new ModelModification(project
364: .getFile(PDEModelUtility.F_MANIFEST_FP)) {
365: protected void modifyModel(IBaseModel model,
366: IProgressMonitor monitor) throws CoreException {
367: if (!(model instanceof IBundlePluginModelBase))
368: return;
369: OrganizeManifest.organizeExportPackages(
370: ((IBundlePluginModelBase) model)
371: .getBundleModel().getBundle(), project,
372: true, true);
373: }
374: }, null);
375: }
376: }
|