1 11 package org.eclipse.pde.internal.ui.wizards; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 16 import org.eclipse.osgi.service.resolver.ExportPackageDescription; 17 import org.eclipse.osgi.service.resolver.VersionRange; 18 import org.eclipse.pde.core.plugin.IFragment; 19 import org.eclipse.pde.core.plugin.IFragmentModel; 20 import org.eclipse.pde.core.plugin.IPluginImport; 21 import org.eclipse.pde.core.plugin.IPluginModel; 22 import org.eclipse.pde.core.plugin.IPluginModelBase; 23 import org.eclipse.pde.core.plugin.PluginRegistry; 24 import org.eclipse.pde.internal.core.PDECore; 25 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 26 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 27 import org.eclipse.pde.internal.core.text.bundle.ImportPackageHeader; 28 import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject; 29 import org.eclipse.pde.internal.ui.PDEPlugin; 30 import org.eclipse.pde.internal.ui.PDEUIMessages; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 33 import org.osgi.framework.Constants; 34 35 public class PluginSelectionDialog extends ElementListSelectionDialog { 36 37 public PluginSelectionDialog(Shell parentShell, boolean includeFragments, 38 boolean multipleSelection) { 39 this(parentShell, getElements(includeFragments), multipleSelection); 40 } 41 42 public PluginSelectionDialog(Shell parentShell, IPluginModelBase[] models, 43 boolean multipleSelection) { 44 super(parentShell, PDEPlugin.getDefault().getLabelProvider()); 45 setTitle(PDEUIMessages.PluginSelectionDialog_title); 46 setMessage(PDEUIMessages.PluginSelectionDialog_message); 47 setElements(models); 48 setMultipleSelection(multipleSelection); 49 PDEPlugin.getDefault().getLabelProvider().connect(this); 50 } 51 52 public boolean close() { 53 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 54 return super.close(); 55 } 56 57 private static IPluginModelBase[] getElements(boolean includeFragments) { 58 return PluginRegistry.getActiveModels(includeFragments); 59 } 60 61 public static HashSet getExistingImports(IPluginModelBase model, boolean includeImportPkg) { 62 HashSet existingImports = new HashSet (); 63 addSelfAndDirectImports(existingImports, model); 64 if (model instanceof IFragmentModel) { 65 IFragment fragment = ((IFragmentModel)model).getFragment(); 66 IPluginModelBase host = PluginRegistry.findModel(fragment.getPluginId()); 67 if (host instanceof IPluginModel) { 68 addSelfAndDirectImports(existingImports, host); 69 } 70 } 71 if (includeImportPkg && model instanceof IBundlePluginModelBase) { 72 addImportedPackages((IBundlePluginModelBase)model, existingImports); 73 } 74 return existingImports; 75 } 76 77 private static void addSelfAndDirectImports(HashSet set, IPluginModelBase model) { 78 set.add(model.getPluginBase().getId()); 79 IPluginImport[] imports = model.getPluginBase().getImports(); 80 for (int i = 0; i < imports.length; i++) { 81 String id = imports[i].getId(); 82 if (set.add(id)) { 83 addReexportedImport(set, id); 84 } 85 } 86 } 87 88 private static void addReexportedImport(HashSet set, String id) { 89 IPluginModelBase model = PluginRegistry.findModel(id); 90 if (model != null) { 91 IPluginImport[] imports = model.getPluginBase().getImports(); 92 for (int i = 0; i < imports.length; i++) { 93 if (imports[i].isReexported() && set.add(imports[i].getId())) { 94 addReexportedImport(set, imports[i].getId()); 95 } 96 } 97 } 98 } 99 100 private static void addImportedPackages(IBundlePluginModelBase base, HashSet existingImports) { 101 HashMap map = getImportPackages(base); 102 if (map == null) 103 return; 104 105 ExportPackageDescription exported [] = PDECore.getDefault().getModelManager().getState().getState().getExportedPackages(); 106 for (int i = 0; i < exported.length; i++) { 107 ImportPackageObject ipo = (ImportPackageObject)map.get(exported[i].getName()); 109 if (ipo != null) { 111 String version = ipo.getVersion(); 113 if (version != null) 114 try { 115 if (!new VersionRange(version).isIncluded(exported[i].getVersion())) 116 continue; 117 } catch (NumberFormatException e){ 119 continue; 120 } 121 existingImports.add(exported[i].getSupplier().getSymbolicName()); 122 } 123 } 124 } 125 126 private static HashMap getImportPackages(IBundlePluginModelBase base) { 128 IBundleModel bmodel = base.getBundleModel(); 129 if (bmodel != null) { 130 ImportPackageHeader header = (ImportPackageHeader)bmodel.getBundle().getManifestHeader(Constants.IMPORT_PACKAGE); 131 if (header != null) { 132 HashMap map = new HashMap (); 134 ImportPackageObject[] packages = header.getPackages(); 135 for (int i = 0; i < packages.length; i++) 136 map.put(packages[i].getName(), packages[i]); 137 return map; 138 } 139 } 140 return null; 141 } 142 } 143 | Popular Tags |