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 org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.pde.core.IBaseModel;
22 import org.eclipse.pde.core.plugin.*;
23 import org.eclipse.pde.internal.core.ICoreConstants;
24 import org.eclipse.pde.internal.core.ibundle.*;
25 import org.eclipse.pde.internal.core.plugin.WorkspaceFragmentModel;
26 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModel;
27 import org.eclipse.pde.internal.core.project.PDEProject;
28 import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader;
29 import org.eclipse.pde.internal.ui.PDEPlugin;
30 import org.eclipse.pde.internal.ui.PDEUIMessages;
31 import org.eclipse.pde.internal.ui.util.ModelModification;
32 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
33 import org.eclipse.swt.widgets.Shell;
34 import org.osgi.framework.Constants;
35 
36 public abstract class BaseManifestOperation implements IRunnableWithProgress {
37 
38 	private Shell fShell;
39 	protected String fPluginId;
40 
BaseManifestOperation(Shell shell, String pluginId)41 	public BaseManifestOperation(Shell shell, String pluginId) {
42 		fShell = shell;
43 		fPluginId = pluginId;
44 	}
45 
getShell()46 	protected Shell getShell() {
47 		return fShell;
48 	}
49 
getFile()50 	protected IFile getFile() {
51 		IPluginModelBase model = PluginRegistry.findModel(fPluginId);
52 		IProject project = model.getUnderlyingResource().getProject();
53 		return model instanceof IFragmentModel ? PDEProject.getFragmentXml(project) : PDEProject.getPluginXml(project);
54 	}
55 
getModel(IFile file)56 	protected IPluginModelBase getModel(IFile file) {
57 		if (ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR.equals(file.getName()))
58 			return new WorkspacePluginModel(file, false);
59 		return new WorkspaceFragmentModel(file, false);
60 	}
61 
updateSingleton(IProgressMonitor monitor)62 	protected void updateSingleton(IProgressMonitor monitor) throws CoreException {
63 		IPluginModelBase plugin = PluginRegistry.findModel(fPluginId);
64 		if (plugin instanceof IBundlePluginModel) {
65 			IFile file = (IFile) plugin.getUnderlyingResource();
66 			IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] {file}, fShell);
67 			if (status.getSeverity() != IStatus.OK)
68 				throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.pde.ui", IStatus.ERROR, NLS.bind(PDEUIMessages.ProductDefinitionOperation_readOnly, fPluginId), null)); //$NON-NLS-1$
69 
70 			ModelModification mod = new ModelModification(file) {
71 				@Override
72 				protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
73 					if (!(model instanceof IBundlePluginModelBase))
74 						return;
75 					IBundlePluginModelBase modelBase = (IBundlePluginModelBase) model;
76 					IBundle bundle = modelBase.getBundleModel().getBundle();
77 					IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
78 					if (header instanceof BundleSymbolicNameHeader) {
79 						BundleSymbolicNameHeader symbolic = (BundleSymbolicNameHeader) header;
80 						if (!symbolic.isSingleton())
81 							symbolic.setSingleton(true);
82 					}
83 				}
84 			};
85 			PDEModelUtility.modifyModel(mod, null);
86 		}
87 	}
88 }
89