Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af91a4680a49319dc7352d8d0a448d7987fa861c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*****************************************************************************
 * Copyright (c) 2020 CEA LIST, EclipseSource and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Alexandra Buzila (EclipseSource) - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.toolsmiths.plugin.builder.quickfix;

import java.util.Optional;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.infra.emf.utils.ResourceUtils;
import org.eclipse.papyrus.toolsmiths.plugin.builder.Activator;
import org.eclipse.papyrus.toolsmiths.plugin.builder.Messages;
import org.eclipse.papyrus.toolsmiths.validation.profile.constants.ProfilePluginValidationConstants;
import org.eclipse.pde.core.IBaseModel;
import org.eclipse.pde.core.plugin.IPluginElement;
import org.eclipse.pde.core.plugin.IPluginExtension;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.ui.util.ModelModification;
import org.eclipse.pde.internal.ui.util.PDEModelUtility;
import org.eclipse.uml2.uml.Profile;

/**
 * Resolution that adds an entry for the 'org.eclipse.emf.ecore.generated_package' extension and configures it.
 */
@SuppressWarnings("restriction")
public class NoUMLGenPackageMarkerResolution extends AbstractPapyrusMarkerResolution {

	private IMarker marker;

	@Override
	public String getDescription() {
		return Messages.NoUMLGenPackageMarkerResolution_description;
	}

	@Override
	public String getLabel() {
		return Messages.NoUMLGenPackageMarkerResolution_label;
	}

	@Override
	public void run(IMarker marker) {
		this.marker = marker;
		if (!(marker.getResource() instanceof IFile)) {
			return;
		}
		ModelModification modification = new ModelModification((IFile) marker.getResource()) {
			@Override
			protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
				if (model instanceof IPluginModelBase) {
					addExtension((IPluginModelBase) model);
				}
			}
		};
		PDEModelUtility.modifyModel(modification, null);
	}

	private void addExtension(IPluginModelBase model) {
		try {
			IPluginExtension extension = model.getFactory().createExtension();
			extension.setPoint(ProfilePluginValidationConstants.UML_GENERATED_PACKAGE_EXTENSION_POINT);
			model.getPluginBase().add(extension);
			IPluginElement packageElement = model.getFactory().createElement(extension);
			packageElement.setName("profile"); //$NON-NLS-1$
			extension.add(packageElement);

			Optional<Profile> profileOptional = MarkerResolutionUtils.getProfile(marker);
			if (profileOptional.isEmpty()) {
				return;
			}
			Profile profile = profileOptional.get();
			String uri = MarkerResolutionUtils.getStereotypeUri(profile);
			if (uri != null) {
				packageElement.setAttribute("uri", uri); //$NON-NLS-1$
			}
			Resource resource = profile.eResource();
			String uriFragment = resource.getURIFragment(profile);
			IFile umlModelFile = MarkerResolutionUtils.getUMLModelFile(marker);
			if (umlModelFile != null) {
				String location = ResourceUtils.mapAndEncodePath(umlModelFile) + "#" + uriFragment; //$NON-NLS-1$
				packageElement.setAttribute("location", location); //$NON-NLS-1$
			}
		} catch (CoreException e) {
			Activator.log.error(e);
		}
	}

}

Back to the top