1 /*******************************************************************************
2  * Copyright (c) 2005, 2016 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  *     EclipseSource Corporation - ongoing enhancements
14  *     Martin Karpisek <martin.karpisek@gmail.com> - Bug 441543
15  *******************************************************************************/
16 package org.eclipse.pde.internal.ui.wizards.product;
17 
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.*;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.pde.core.plugin.*;
26 import org.eclipse.pde.internal.core.iproduct.*;
27 import org.eclipse.pde.internal.core.product.SplashInfo;
28 import org.eclipse.pde.internal.core.product.WorkspaceProductModel;
29 import org.eclipse.pde.internal.ui.*;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.ui.*;
32 import org.eclipse.ui.actions.WorkspaceModifyOperation;
33 import org.eclipse.ui.branding.IProductConstants;
34 import org.eclipse.ui.ide.IDE;
35 import org.eclipse.ui.part.ISetSelectionTarget;
36 
37 public class BaseProductCreationOperation extends WorkspaceModifyOperation {
38 
39 	private IFile fFile;
40 
BaseProductCreationOperation(IFile file)41 	public BaseProductCreationOperation(IFile file) {
42 		fFile = file;
43 	}
44 
45 	@Override
execute(IProgressMonitor monitor)46 	protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
47 		monitor.beginTask(PDEUIMessages.BaseProductCreationOperation_taskName, 2);
48 		createContent();
49 		monitor.worked(1);
50 		openFile();
51 		monitor.done();
52 	}
53 
createContent()54 	private void createContent() {
55 		WorkspaceProductModel model = new WorkspaceProductModel(fFile, false);
56 		initializeProduct(model.getProduct());
57 		model.save();
58 		model.dispose();
59 	}
60 
initializeProduct(IProduct product)61 	protected void initializeProduct(IProduct product) {
62 		initializeProductId(product);
63 		IProductModelFactory factory = product.getModel().getFactory();
64 		IConfigurationFileInfo info = factory.createConfigFileInfo();
65 		info.setUse(null, "default"); //$NON-NLS-1$
66 		product.setConfigurationFileInfo(info);
67 		// preset some common VM args for macosx (bug 174232 comment #4)
68 		IArgumentsInfo args = factory.createLauncherArguments();
69 		args.setVMArguments("-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts", IArgumentsInfo.L_ARGS_MACOS); //$NON-NLS-1$
70 		product.setLauncherArguments(args);
71 	}
72 
getProductProperties(IPluginElement element)73 	private Properties getProductProperties(IPluginElement element) {
74 		Properties prop = new Properties();
75 		IPluginObject[] children = element.getChildren();
76 		for (IPluginObject childObject : children) {
77 			IPluginElement child = (IPluginElement) childObject;
78 			if (child.getName().equals("property")) { //$NON-NLS-1$
79 				String name = null;
80 				String value = null;
81 				IPluginAttribute attr = child.getAttribute("name"); //$NON-NLS-1$
82 				if (attr != null)
83 					name = attr.getValue();
84 				attr = child.getAttribute("value"); //$NON-NLS-1$
85 				if (attr != null)
86 					value = attr.getValue();
87 				if (name != null && value != null)
88 					prop.put(name, value);
89 			}
90 		}
91 		return prop;
92 	}
93 
getProductExtension(String productId)94 	protected IPluginElement getProductExtension(String productId) {
95 		int lastDot = productId.lastIndexOf('.');
96 		if (lastDot == -1)
97 			return null;
98 		String pluginId = productId.substring(0, lastDot);
99 		IPluginModelBase model = PluginRegistry.findModel(pluginId);
100 		if (model != null) {
101 			IPluginExtension[] extensions = model.getPluginBase().getExtensions();
102 			for (IPluginExtension extension : extensions) {
103 				if ("org.eclipse.core.runtime.products".equals(extension.getPoint()) //$NON-NLS-1$
104 						&& productId.substring(lastDot + 1).equals(extension.getId())) {
105 					IPluginObject[] children = extension.getChildren();
106 					if (children.length > 0) {
107 						IPluginElement object = (IPluginElement) children[0];
108 						if (object.getName().equals("product")) //$NON-NLS-1$
109 							return object;
110 					}
111 				}
112 			}
113 		}
114 		return null;
115 	}
116 
117 	/**
118 	 * Initializes product id to name of product file (without extension).
119 	 *
120 	 * @param product
121 	 *            whose id will be initialized
122 	 */
initializeProductId(IProduct product)123 	protected void initializeProductId(IProduct product) {
124 		if (fFile == null) {
125 			return;
126 		}
127 		String id = fFile.getName();
128 		int index = id.lastIndexOf('.');
129 		if (index >= 0) {
130 			id = id.substring(0, index);
131 		}
132 		product.setId(id);
133 	}
134 
initializeProductInfo(IProductModelFactory factory, IProduct product, String id)135 	protected void initializeProductInfo(IProductModelFactory factory, IProduct product, String id) {
136 		product.setProductId(id);
137 		product.setVersion("1.0.0.qualifier"); //$NON-NLS-1$
138 		IPluginElement element = getProductExtension(id);
139 		if (element != null) {
140 			IPluginAttribute attr = element.getAttribute("application"); //$NON-NLS-1$
141 			if (attr != null)
142 				product.setApplication(attr.getValue());
143 			attr = element.getAttribute("name"); //$NON-NLS-1$
144 			if (attr != null)
145 				product.setName(attr.getValue());
146 			Properties prop = getProductProperties(element);
147 			String aboutText = prop.getProperty(IProductConstants.ABOUT_TEXT);
148 			String aboutImage = prop.getProperty(IProductConstants.ABOUT_IMAGE);
149 			if (aboutText != null || aboutImage != null) {
150 				IAboutInfo info = factory.createAboutInfo();
151 				info.setText(aboutText);
152 				info.setImagePath(aboutImage);
153 				product.setAboutInfo(info);
154 			}
155 			IWindowImages winImages = factory.createWindowImages();
156 			String path = prop.getProperty(IProductConstants.WINDOW_IMAGES);
157 			if (path != null) {
158 				StringTokenizer tokenizer = new StringTokenizer(path, ",", true); //$NON-NLS-1$
159 				int size = 0;
160 				while (tokenizer.hasMoreTokens()) {
161 					String token = tokenizer.nextToken();
162 					if (token.equals(",")) //$NON-NLS-1$
163 						size++;
164 					else
165 						winImages.setImagePath(token, size);
166 				}
167 			}
168 			product.setWindowImages(winImages);
169 
170 			ISplashInfo splashInfo = factory.createSplashInfo();
171 			splashInfo.setForegroundColor(prop.getProperty(IProductConstants.STARTUP_FOREGROUND_COLOR), true);
172 			int[] barGeo = SplashInfo.getGeometryArray(prop.getProperty(IProductConstants.STARTUP_PROGRESS_RECT));
173 			splashInfo.setProgressGeometry(barGeo, true);
174 			int[] messageGeo = SplashInfo.getGeometryArray(prop.getProperty(IProductConstants.STARTUP_MESSAGE_RECT));
175 			splashInfo.setMessageGeometry(messageGeo, true);
176 			product.setSplashInfo(splashInfo);
177 		}
178 	}
179 
addPlugins(IProductModelFactory factory, IProduct product, Map<IPluginModelBase, String> plugins)180 	protected void addPlugins(IProductModelFactory factory, IProduct product, Map<IPluginModelBase, String> plugins) {
181 		IProductPlugin[] pplugins = new IProductPlugin[plugins.size()];
182 		List<IPluginConfiguration> configurations = new ArrayList<>(3);
183 		IPluginModelBase[] models = plugins.keySet().toArray(new IPluginModelBase[plugins.size()]);
184 		for (int i = 0; i < models.length; i++) {
185 			IPluginModelBase model = models[i];
186 
187 			// create plug-in model
188 			IProductPlugin pplugin = factory.createPlugin();
189 			pplugin.setId(model.getPluginBase().getId());
190 			pplugins[i] = pplugin;
191 
192 			// create plug-in configuration model
193 			String sl = plugins.get(model);
194 			if (!model.isFragmentModel() && !sl.equals("default:default")) { //$NON-NLS-1$
195 				IPluginConfiguration configuration = factory.createPluginConfiguration();
196 				configuration.setId(model.getPluginBase().getId());
197 				// TODO do we want to set the version here?
198 				String[] slinfo = sl.split(":"); //$NON-NLS-1$
199 				if (slinfo.length == 0)
200 					continue;
201 				if (slinfo[0].equals("default")) { //$NON-NLS-1$
202 					slinfo[0] = "0"; //$NON-NLS-1$
203 				}
204 				configuration.setStartLevel(Integer.valueOf(slinfo[0]).intValue());
205 				configuration.setAutoStart(slinfo[1].equals("true")); //$NON-NLS-1$
206 				configurations.add(configuration);
207 			}
208 		}
209 		product.addPlugins(pplugins);
210 		int size = configurations.size();
211 		if (size > 0)
212 			product.addPluginConfigurations(configurations.toArray(new IPluginConfiguration[size]));
213 	}
214 
addPlugins(IProductModelFactory factory, IProduct product, String[] plugins)215 	protected void addPlugins(IProductModelFactory factory, IProduct product, String[] plugins) {
216 		IProductPlugin[] pplugins = new IProductPlugin[plugins.length];
217 		for (int i = 0; i < plugins.length; i++) {
218 			IProductPlugin pplugin = factory.createPlugin();
219 			pplugin.setId(plugins[i]);
220 			pplugins[i] = pplugin;
221 		}
222 		product.addPlugins(pplugins);
223 	}
224 
openFile()225 	private void openFile() {
226 		Display.getCurrent().asyncExec(() -> {
227 			IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
228 			if (ww == null) {
229 				return;
230 			}
231 			IWorkbenchPage page = ww.getActivePage();
232 			if (page == null || !fFile.exists())
233 				return;
234 			IWorkbenchPart focusPart = page.getActivePart();
235 			if (focusPart instanceof ISetSelectionTarget) {
236 				ISelection selection = new StructuredSelection(fFile);
237 				((ISetSelectionTarget) focusPart).selectReveal(selection);
238 			}
239 			try {
240 				IDE.openEditor(page, fFile, IPDEUIConstants.PRODUCT_EDITOR_ID);
241 			} catch (PartInitException e) {
242 			}
243 		});
244 	}
245 
246 }
247