|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 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.ctxhelp; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import org.eclipse.core.resources.IFile; |
| 15 |
import org.eclipse.core.resources.IProject; |
| 16 |
import org.eclipse.core.runtime.*; |
| 17 |
import org.eclipse.pde.core.IBaseModel; |
| 18 |
import org.eclipse.pde.core.IModel; |
| 19 |
import org.eclipse.pde.core.build.*; |
| 20 |
import org.eclipse.pde.core.plugin.*; |
| 21 |
import org.eclipse.pde.internal.core.*; |
| 22 |
import org.eclipse.pde.internal.core.build.BuildObject; |
| 23 |
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; |
| 24 |
import org.eclipse.pde.internal.core.ibundle.*; |
| 25 |
import org.eclipse.pde.internal.core.ictxhelp.ICtxHelpConstants; |
| 26 |
import org.eclipse.pde.internal.core.plugin.*; |
| 27 |
import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader; |
| 28 |
import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader; |
| 29 |
import org.eclipse.pde.internal.core.util.PDETextHelper; |
| 30 |
import org.eclipse.pde.internal.ui.*; |
| 31 |
import org.eclipse.pde.internal.ui.editor.ctxhelp.CtxHelpEditor; |
| 32 |
import org.eclipse.pde.internal.ui.util.ModelModification; |
| 33 |
import org.eclipse.pde.internal.ui.util.PDEModelUtility; |
| 34 |
import org.eclipse.swt.widgets.Shell; |
| 35 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| 36 |
import org.osgi.framework.Constants; |
| 37 |
|
| 38 |
/** |
| 39 |
* Register Context Help Operation, registers a context help xml file in the plugin.xml. |
| 40 |
* Must be run in the UI thread. |
| 41 |
* @since 3.4 |
| 42 |
* @see RegisterCtxHelpOperation |
| 43 |
* @see CtxHelpEditor |
| 44 |
*/ |
| 45 |
public class RegisterCtxHelpOperation extends WorkspaceModifyOperation { |
| 46 |
|
| 47 |
static final String CTX_HELP_EXTENSION_POINT_ID = "org.eclipse.help.contexts"; //$NON-NLS-1$ |
| 48 |
static final String CTX_HELP_PLUGIN_ID = "org.eclipse.help"; //$NON-NLS-1$ |
| 49 |
|
| 50 |
public static final String CTX_HELP_ATTR_FILE = "file"; //$NON-NLS-1$ |
| 51 |
public final static String CTX_HELP_ATTR_PLUGIN = "plugin"; //$NON-NLS-1$ |
| 52 |
|
| 53 |
private Shell fShell; |
| 54 |
private String fPluginText; |
| 55 |
private IProject fProject; |
| 56 |
private String fResourceString; |
| 57 |
|
| 58 |
public RegisterCtxHelpOperation(Shell shell, IModel model, String pluginText) { |
| 59 |
fPluginText = pluginText; |
| 60 |
fProject = model.getUnderlyingResource().getProject(); |
| 61 |
fResourceString = model.getUnderlyingResource().getProjectRelativePath().toPortableString(); |
| 62 |
fShell = shell; |
| 63 |
} |
| 64 |
|
| 65 |
/* (non-Javadoc) |
| 66 |
* @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor) |
| 67 |
*/ |
| 68 |
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| 69 |
try { |
| 70 |
boolean fragment = PluginRegistry.findModel(fProject).isFragmentModel(); |
| 71 |
IFile file = fProject.getFile(fragment ? ICoreConstants.FRAGMENT_PATH : ICoreConstants.PLUGIN_PATH); |
| 72 |
// If the plug-in exists modify it accordingly; otherwise, create |
| 73 |
// a new plug-in file |
| 74 |
if (file.exists()) { |
| 75 |
modifyExistingPluginFile(file, monitor); |
| 76 |
} else { |
| 77 |
createNewPluginFile(file, monitor); |
| 78 |
} |
| 79 |
} catch (CoreException e) { |
| 80 |
throw new InvocationTargetException(e); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private void modifyExistingPluginFile(IFile file, IProgressMonitor monitor) throws CoreException { |
| 85 |
IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] {file}, fShell); |
| 86 |
if (status.getSeverity() != IStatus.OK) { |
| 87 |
throw new CoreException(new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, IStatus.ERROR, PDEUIMessages.RegisterCSOperation_errorManifestReadOnly, null)); |
| 88 |
} |
| 89 |
// Perform the modification of the plugin manifest file |
| 90 |
ModelModification mod = new ModelModification(fProject) { |
| 91 |
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
| 92 |
doModifyPluginModel(model, monitor); |
| 93 |
doModifyManifestModel(model); |
| 94 |
} |
| 95 |
}; |
| 96 |
PDEModelUtility.modifyModel(mod, monitor); |
| 97 |
} |
| 98 |
|
| 99 |
private void doModifyPluginModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
| 100 |
if ((model instanceof IPluginModelBase) == false) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
IPluginModelBase modelBase = (IPluginModelBase) model; |
| 105 |
IPluginExtension[] extensions = modelBase.getExtensions().getExtensions(); |
| 106 |
IPluginExtension existingExtension = null; |
| 107 |
for (int i = 0; i < extensions.length; i++) { |
| 108 |
String point = extensions[i].getPoint(); |
| 109 |
if (CTX_HELP_EXTENSION_POINT_ID.equals(point)) { |
| 110 |
if (checkExistingExtensionElement(extensions[i])) { |
| 111 |
// Exact match, no need to register anything |
| 112 |
return; |
| 113 |
} |
| 114 |
existingExtension = extensions[i]; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (existingExtension != null) { |
| 119 |
// No exact match, add a new entry to the existing extension |
| 120 |
addElementToExtension(existingExtension); |
| 121 |
} else { |
| 122 |
// No existing extension found, create one |
| 123 |
addExtensionToModel(modelBase); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
private void doModifyManifestModel(IBaseModel model) { |
| 128 |
// Make sure we have a base model |
| 129 |
if ((model instanceof IBundlePluginModelBase) == false) { |
| 130 |
return; |
| 131 |
} |
| 132 |
IBundlePluginModelBase modelBase = (IBundlePluginModelBase) model; |
| 133 |
IBundle bundle = modelBase.getBundleModel().getBundle(); |
| 134 |
// Get the heading specifying the singleton declaration |
| 135 |
IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME); |
| 136 |
if (header instanceof BundleSymbolicNameHeader) { |
| 137 |
BundleSymbolicNameHeader symbolic = (BundleSymbolicNameHeader) header; |
| 138 |
// If the singleton declaration is false, change it to true |
| 139 |
// This is required because plug-ins that specify extensions |
| 140 |
// must be singletons. |
| 141 |
if (symbolic.isSingleton() == false) { |
| 142 |
symbolic.setSingleton(true); |
| 143 |
} |
| 144 |
} |
| 145 |
// Add the context help plug-in to the list of required bundles |
| 146 |
header = bundle.getManifestHeader(Constants.REQUIRE_BUNDLE); |
| 147 |
if (header instanceof RequireBundleHeader) { |
| 148 |
RequireBundleHeader require = (RequireBundleHeader) header; |
| 149 |
if (require.hasElement(CTX_HELP_PLUGIN_ID) == false) { |
| 150 |
require.addBundle(CTX_HELP_PLUGIN_ID); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private void createNewPluginFile(IFile file, IProgressMonitor monitor) throws CoreException { |
| 156 |
monitor.beginTask(PDEUIMessages.RegisterCSOperation_addNewCSExtensionNewPlugin, 4); |
| 157 |
|
| 158 |
WorkspacePluginModelBase model; |
| 159 |
if (file.getProjectRelativePath().equals(ICoreConstants.FRAGMENT_PATH)) { |
| 160 |
model = new WorkspaceFragmentModel(file, false); |
| 161 |
} else { |
| 162 |
model = new WorkspacePluginModel(file, false); |
| 163 |
} |
| 164 |
monitor.worked(1); |
| 165 |
|
| 166 |
IPluginBase base = model.getPluginBase(); |
| 167 |
double targetVersion = TargetPlatformHelper.getTargetVersion(); |
| 168 |
String version = null; |
| 169 |
if (targetVersion < 3.2) { |
| 170 |
version = ICoreConstants.TARGET30; |
| 171 |
} else { |
| 172 |
version = ICoreConstants.TARGET32; |
| 173 |
} |
| 174 |
base.setSchemaVersion(version); |
| 175 |
|
| 176 |
addExtensionToModel(model); |
| 177 |
monitor.worked(1); |
| 178 |
|
| 179 |
model.save(); |
| 180 |
monitor.worked(1); |
| 181 |
// Update the MANIFEST.MF file to ensure the singleton directive is set |
| 182 |
// to true |
| 183 |
modifyExistingManifestFile(file); |
| 184 |
monitor.done(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @param model |
| 189 |
*/ |
| 190 |
private void modifyExistingManifestFile(IFile file) throws CoreException { |
| 191 |
// Validate the operation |
| 192 |
// Note: This is not accurate, we are validating the plugin.xml file rather |
| 193 |
// than the manifest file |
| 194 |
IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] {file}, fShell); |
| 195 |
if (status.getSeverity() != IStatus.OK) { |
| 196 |
throw new CoreException(new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, IStatus.ERROR, PDEUIMessages.RegisterCSOperation_errorManifestReadOnly, null)); |
| 197 |
} |
| 198 |
// Perform the modification of the manifest file |
| 199 |
ModelModification mod = new ModelModification(fProject) { |
| 200 |
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
| 201 |
doModifyManifestModel(model); |
| 202 |
doModifyBuildModel(model); |
| 203 |
} |
| 204 |
}; |
| 205 |
PDEModelUtility.modifyModel(mod, null); |
| 206 |
} |
| 207 |
|
| 208 |
private void doModifyBuildModel(IBaseModel model) throws CoreException { |
| 209 |
// Make sure we have a base model |
| 210 |
if ((model instanceof IPluginModelBase) == false) { |
| 211 |
return; |
| 212 |
} |
| 213 |
IPluginModelBase modelBase = (IPluginModelBase) model; |
| 214 |
IBuild build = ClasspathUtilCore.getBuild(modelBase); |
| 215 |
// Make sure we have a plugin.properties file |
| 216 |
if (build == null) { |
| 217 |
return; |
| 218 |
} |
| 219 |
// Get the entry for bin.includes |
| 220 |
IBuildEntry entry = build.getEntry(IBuildEntry.BIN_INCLUDES); |
| 221 |
if (entry == null) { |
| 222 |
// This should never happen since the manifest.mf file exists and |
| 223 |
// it has to be in the bin.includes |
| 224 |
return; |
| 225 |
} |
| 226 |
// Add the plugin.xml file to the bin.includes build entry if it does |
| 227 |
// not exist |
| 228 |
if (entry.contains(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR) == false) { |
| 229 |
entry.addToken(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR); |
| 230 |
} |
| 231 |
// There does not seem to be any support in PDEModelUtility or the |
| 232 |
// ModelModification framework to save build.properties modifications |
| 233 |
// As a result, explicitly do that here |
| 234 |
if (build instanceof BuildObject) { |
| 235 |
IBuildModel buildModel = ((BuildObject) build).getModel(); |
| 236 |
if (buildModel instanceof WorkspaceBuildModel) { |
| 237 |
((WorkspaceBuildModel) buildModel).save(); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
private boolean checkExistingExtensionElement(IPluginExtension extension) { |
| 243 |
IPluginObject[] pluginObjects = extension.getChildren(); |
| 244 |
for (int j = 0; j < pluginObjects.length; j++) { |
| 245 |
if (pluginObjects[j] instanceof IPluginElement) { |
| 246 |
IPluginElement element = (IPluginElement) pluginObjects[j]; |
| 247 |
if (element.getName().equals(ICtxHelpConstants.ELEMENT_ROOT)) { |
| 248 |
IPluginAttribute fileAttribute = element.getAttribute(CTX_HELP_ATTR_FILE); |
| 249 |
if ((fileAttribute != null) && PDETextHelper.isDefined(fileAttribute.getValue()) && fResourceString.equals(fileAttribute.getValue())) { |
| 250 |
IPluginAttribute pluginAttribute = element.getAttribute(CTX_HELP_ATTR_PLUGIN); |
| 251 |
if (pluginAttribute == null || !PDETextHelper.isDefined(pluginAttribute.getValue())) { |
| 252 |
if (fPluginText.length() == 0) { |
| 253 |
return true; |
| 254 |
} |
| 255 |
} else if (fPluginText.equals(pluginAttribute.getValue())) { |
| 256 |
return true; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
private void addExtensionToModel(IPluginModelBase model) throws CoreException { |
| 266 |
IPluginExtension extension = model.getFactory().createExtension(); |
| 267 |
extension.setPoint(CTX_HELP_EXTENSION_POINT_ID); |
| 268 |
addElementToExtension(extension); |
| 269 |
model.getPluginBase().add(extension); |
| 270 |
} |
| 271 |
|
| 272 |
private void addElementToExtension(IPluginExtension extension) throws CoreException { |
| 273 |
IPluginElement element = extension.getModel().getFactory().createElement(extension); |
| 274 |
element.setName(ICtxHelpConstants.ELEMENT_ROOT); |
| 275 |
element.setAttribute(CTX_HELP_ATTR_FILE, fResourceString); |
| 276 |
if (fPluginText.length() > 0) { |
| 277 |
element.setAttribute(CTX_HELP_ATTR_PLUGIN, fPluginText); |
| 278 |
} |
| 279 |
extension.add(element); |
| 280 |
} |
| 281 |
|
| 282 |
} |