1 /*******************************************************************************
2  *  Copyright (c) 2005, 2015 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.wizards.product;
15 
16 import java.util.Set;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.pde.core.plugin.*;
19 import org.eclipse.pde.internal.core.iproduct.IProduct;
20 import org.eclipse.pde.internal.core.iproduct.IProductModelFactory;
21 import org.eclipse.pde.internal.ui.search.dependencies.DependencyCalculator;
22 
23 public class ProductFromExtensionOperation extends BaseProductCreationOperation {
24 
25 	private String fId;
26 
ProductFromExtensionOperation(IFile file, String productId)27 	public ProductFromExtensionOperation(IFile file, String productId) {
28 		super(file);
29 		fId = productId;
30 	}
31 
32 	@Override
initializeProduct(IProduct product)33 	protected void initializeProduct(IProduct product) {
34 		if (fId == null)
35 			return;
36 		IProductModelFactory factory = product.getModel().getFactory();
37 		initializeProductInfo(factory, product, fId);
38 		addPlugins(factory, product, getPlugins());
39 		super.initializeProduct(product);
40 	}
41 
getPlugins()42 	private String[] getPlugins() {
43 		int lastDot = fId.lastIndexOf('.');
44 		if (lastDot == -1)
45 			return new String[0];
46 
47 		DependencyCalculator calculator = new DependencyCalculator(false);
48 		// add plugin declaring product and its pre-reqs
49 		IPluginModelBase model = PluginRegistry.findModel(fId.substring(0, lastDot));
50 		if (model != null)
51 			calculator.findDependency(model);
52 
53 		// add plugin declaring product application and its pre-reqs
54 		IPluginElement element = getProductExtension(fId);
55 		if (element != null) {
56 			IPluginAttribute attr = element.getAttribute("application"); //$NON-NLS-1$
57 			if (attr != null) {
58 				String appId = attr.getValue();
59 				lastDot = appId.lastIndexOf('.');
60 				if (lastDot != -1) {
61 					model = PluginRegistry.findModel(appId.substring(0, lastDot));
62 					if (model != null) {
63 						calculator.findDependency(model);
64 					}
65 				}
66 			}
67 		}
68 		Set<?> ids = calculator.getBundleIDs();
69 		return ids.toArray(new String[ids.size()]);
70 	}
71 
72 }
73