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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*******************************************************************************
* Copyright (c) 2005, 2013 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.internal.ui.views.plugins;
import java.util.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.pde.core.plugin.*;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.project.BundleProjectService;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.pde.internal.ui.wizards.imports.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.ScmUrlImportDescription;
import org.eclipse.team.core.importing.provisional.IBundleImporter;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionGroup;
@SuppressWarnings("restriction")
public class ImportActionGroup extends ActionGroup {
class ImportAction extends Action {
IStructuredSelection fSel;
int fImportType;
ImportAction(int importType, IStructuredSelection selection) {
fSel = selection;
fImportType = importType;
switch (fImportType) {
case PluginImportOperation.IMPORT_BINARY :
setText(PDEUIMessages.PluginsView_asBinaryProject);
break;
case PluginImportOperation.IMPORT_BINARY_WITH_LINKS :
setText(PDEUIMessages.ImportActionGroup_binaryWithLinkedContent);
break;
case PluginImportOperation.IMPORT_WITH_SOURCE :
setText(PDEUIMessages.PluginsView_asSourceProject);
break;
case PluginImportOperation.IMPORT_FROM_REPOSITORY :
setText(PDEUIMessages.ImportActionGroup_Repository_project);
break;
}
}
@Override
public void run() {
handleImport(fImportType, fSel);
}
}
@Override
public void fillContextMenu(IMenuManager menu) {
ActionContext context = getContext();
ISelection selection = context.getSelection();
if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
IStructuredSelection sSelection = (IStructuredSelection) selection;
String menuName = null;
if (sSelection.getFirstElement() instanceof IPluginExtension || sSelection.getFirstElement() instanceof IPluginExtensionPoint)
menuName = PDEUIMessages.ImportActionGroup_importContributingPlugin;
else
menuName = PDEUIMessages.PluginsView_import;
MenuManager importMenu = new MenuManager(menuName);
importMenu.add(new ImportAction(PluginImportOperation.IMPORT_BINARY, sSelection));
importMenu.add(new ImportAction(PluginImportOperation.IMPORT_BINARY_WITH_LINKS, sSelection));
importMenu.add(new ImportAction(PluginImportOperation.IMPORT_WITH_SOURCE, sSelection));
importMenu.add(new ImportAction(PluginImportOperation.IMPORT_FROM_REPOSITORY, sSelection));
menu.add(importMenu);
}
}
static void handleImport(int importType, IStructuredSelection selection) {
ArrayList<IPluginModelBase> externalModels = new ArrayList<>();
for (Iterator<?> iter = selection.iterator(); iter.hasNext();) {
IPluginModelBase model = getModel(iter.next());
if (model != null && model.getUnderlyingResource() == null)
externalModels.add(model);
}
Display display = Display.getCurrent();
if (display == null)
display = Display.getDefault();
IPluginModelBase[] models = externalModels.toArray(new IPluginModelBase[externalModels.size()]);
if (importType == PluginImportOperation.IMPORT_FROM_REPOSITORY) {
Map<IBundleImporter, ScmUrlImportDescription[]> importMap = getImportDescriptions(display.getActiveShell(), models);
if (importMap != null) {
RepositoryImportWizard wizard = new RepositoryImportWizard(importMap);
WizardDialog dialog = new WizardDialog(display.getActiveShell(), wizard);
dialog.open();
}
} else {
PluginImportWizard.doImportOperation(display.getActiveShell(), importType, models, false);
}
}
/**
* Return a map of {@link IBundleImporter} > Array of {@link ScmUrlImportDescription} to be imported.
*
* @param shell shell to open message dialogs on, if required
* @param models candidate models
* @return map of importer to import descriptions
*/
private static Map<IBundleImporter, ScmUrlImportDescription[]> getImportDescriptions(Shell shell, IPluginModelBase[] models) {
BundleProjectService service = (BundleProjectService) BundleProjectService.getDefault();
try {
Map<IBundleImporter, ScmUrlImportDescription[]> descriptions = service.getImportDescriptions(models); // all possible descriptions
if (!descriptions.isEmpty()) {
return descriptions;
}
// no applicable importers for selected models
MessageDialog.openInformation(shell, PDEUIMessages.ImportWizard_title, PDEUIMessages.ImportActionGroup_cannot_import);
} catch (CoreException e) {
PDEPlugin.log(e);
MessageDialog.openError(shell, PDEUIMessages.ImportWizard_title, e.getMessage());
}
return null;
}
public static boolean canImport(IStructuredSelection selection) {
for (Iterator<?> iter = selection.iterator(); iter.hasNext();) {
IPluginModelBase model = getModel(iter.next());
if (model != null && model.getUnderlyingResource() == null)
return true;
}
return false;
}
private static IPluginModelBase getModel(Object next) {
IPluginModelBase model = null;
if (next instanceof IPluginModelBase) {
model = (IPluginModelBase) next;
} else if (next instanceof IPluginBase) {
model = ((IPluginBase) next).getPluginModel();
} else if (next instanceof IPluginExtension) {
model = ((IPluginExtension) next).getPluginModel();
} else if (next instanceof IPluginExtensionPoint) {
model = ((IPluginExtensionPoint) next).getPluginModel();
} else if (next instanceof BundleDescription) {
model = PDECore.getDefault().getModelManager().findModel((BundleDescription) next);
} else if (next instanceof BundleSpecification) {
// Required for contents of Target Platform State View
BundleDescription desc = (BundleDescription) ((BundleSpecification) next).getSupplier();
if (desc != null) {
model = PDECore.getDefault().getModelManager().findModel(desc);
}
} else if (next instanceof IPackageFragmentRoot) {
// Required for context menu on PDE classpath container entries
IPackageFragmentRoot root = (IPackageFragmentRoot) next;
if (root.isExternal()) {
String path = root.getPath().toOSString();
IPluginModelBase[] externalModels = PDECore.getDefault().getModelManager().getExternalModels();
for (IPluginModelBase externalModel : externalModels) {
if (path.equals(externalModel.getInstallLocation())) {
return externalModel;
}
}
}
}
return model;
}
}
|