KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > PluginSelectionDialog


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;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
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 JavaDoc getExistingImports(IPluginModelBase model, boolean includeImportPkg) {
62         HashSet JavaDoc existingImports = new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc id = imports[i].getId();
82             if (set.add(id)) {
83                 addReexportedImport(set, id);
84             }
85         }
86     }
87
88     private static void addReexportedImport(HashSet JavaDoc set, String JavaDoc 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 JavaDoc existingImports) {
101         HashMap JavaDoc 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             // iterate through all the exported packages
108
ImportPackageObject ipo = (ImportPackageObject)map.get(exported[i].getName());
109             // if we find an exported package that matches a pkg in the map, then the exported package matches a package on our import-package statement
110
if (ipo != null) {
111                 // check version to make sure we only add bundles from valid packages
112
String JavaDoc version = ipo.getVersion();
113                 if (version != null)
114                     try {
115                         if (!new VersionRange(version).isIncluded(exported[i].getVersion()))
116                             continue;
117                         // NFE if ImportPackageObject's version is improperly formatted - ignore any matching imported packages since version is invalid
118
} catch (NumberFormatException JavaDoc e){
119                         continue;
120                     }
121                 existingImports.add(exported[i].getSupplier().getSymbolicName());
122             }
123         }
124     }
125     
126     // returns null instead of empty map so we know not to iterate through exported packages
127
private static HashMap JavaDoc 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                 // create a map of all the packages we import
133
HashMap JavaDoc map = new HashMap JavaDoc();
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