Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2bff5eb609a138b5e71781c3574f16eec457ee05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*******************************************************************************
 * Copyright (c) 2008, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Bartosz Michalik <bartosz.michalik@gmail.com> - bug 109440
 *******************************************************************************/
package org.eclipse.pde.internal.ui.wizards.tools;

import java.util.jar.Manifest;
import org.eclipse.core.resources.IProject;

import java.io.File;
import java.util.*;
import java.util.jar.JarFile;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.*;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.pde.internal.core.natures.PDE;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.wizards.plugin.NewLibraryPluginProjectWizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.*;

public class ConvertJarsAction implements IObjectActionDelegate {

	private IStructuredSelection selection;
	private IWorkbench workbench;

	public ConvertJarsAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		workbench = targetPart.getSite().getWorkbenchWindow().getWorkbench();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		Map<Manifest, Object> filesMap = new HashMap<Manifest, Object>();
		Set<IProject> projectSelection = new HashSet<IProject>();
		Iterator<?> i = selection.toList().iterator();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		while (i.hasNext()) {
			IPackageFragmentRoot pfr = (IPackageFragmentRoot) i.next();
			try {
				projectSelection.add(pfr.getJavaProject().getProject());
				IClasspathEntry rawClasspathEntry = pfr.getRawClasspathEntry();
				IPath path = rawClasspathEntry.getPath();
				IFile iFile = root.getFile(path);
				if (iFile.exists()) {
					JarFile jFile = new JarFile(iFile.getLocation().toString());
					if (!filesMap.containsKey(jFile.getManifest())) {
						filesMap.put(jFile.getManifest(), iFile);
					}
				} else {
					String pathStr = path.toString();
					JarFile file = new JarFile(pathStr);
					if (!filesMap.containsKey(file.getManifest())) {
						filesMap.put(file.getManifest(), new File(file.getName()));
					}
				}
			} catch (Exception e) {
				PDEPlugin.logException(e);
			}
		}
		NewLibraryPluginProjectWizard wizard = new NewLibraryPluginProjectWizard(filesMap.values(), projectSelection);
		wizard.init(workbench, selection);
		WizardDialog dialog = new WizardDialog(Display.getDefault().getActiveShell(), wizard);
		dialog.open();
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection s) {
		boolean enabled = true;
		if (s instanceof IStructuredSelection) {
			selection = (IStructuredSelection) s;
			if (selection.size() == 0)
				return;
			Iterator<?> i = selection.iterator();
			while (i.hasNext()) {
				Object obj = i.next();
				if (obj instanceof IPackageFragmentRoot) {
					try {
						IPackageFragmentRoot packageFragment = (IPackageFragmentRoot) obj;
						if (packageFragment.getKind() == IPackageFragmentRoot.K_BINARY) {
							if (PDE.hasPluginNature(packageFragment.getJavaProject().getProject())) {
								if (packageFragment.getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_LIBRARY)
									continue;
							}
						}
					} catch (JavaModelException e) {
					}
				}
				enabled = false;
				break;
			}
		} else {
			enabled = false;
			this.selection = null;
		}
		action.setEnabled(enabled);
	}

}

Back to the top