Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91b7d9a565663df3bb24c5fd27f0ef4676dc844c (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2014 CEA, Christian W. Damus, and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Christian W. Damus (CEA) - Initial API and implementation
 *   Christian W. Damus - bug 399859
 *
 */
package org.eclipse.papyrus.uml.modelrepair.internal.stereotypes;

import java.util.Collection;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.uml.modelrepair.internal.participants.StereotypeApplicationRepairParticipant;
import org.eclipse.papyrus.uml.tools.helper.IProfileApplicationDelegate;
import org.eclipse.papyrus.uml.tools.helper.ProfileApplicationDelegateRegistry;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;


/**
 * This is the ApplyProfileAction type. Enjoy.
 */
public class ApplyProfileAction extends AbstractRepairAction {

	private final Set<Package> packages;

	private Supplier<Profile> profileSupplier;

	private String label;

	public ApplyProfileAction(Iterable<? extends Package> packages, Profile profile, LabelProviderService labelProviderService) {
		this(packages, Suppliers.ofInstance(profile));

		// Don't have to worry about the profile not having any definition because if it didn't, we couldn't be
		// looking at a previous version that traced to it to let us know that we need to apply this profile
		this.label = NLS.bind("Migrate to {0}", labelProviderService.getLabelProvider().getText(profile.getDefinition()));
	}

	public ApplyProfileAction(Iterable<? extends Package> packages, Supplier<Profile> profileSupplier) {
		super(Kind.APPLY_LATEST_PROFILE_DEFINITION);

		if (Iterables.isEmpty(packages)) {
			throw new IllegalArgumentException("no packages"); //$NON-NLS-1$
		}

		this.packages = Sets.newLinkedHashSet(packages);
		this.profileSupplier = profileSupplier;
	}

	@Override
	public String getLabel() {
		return (label != null) ? label : super.getLabel();
	}

	public void addPackage(Package package_) {
		this.packages.add(package_);
	}

	public boolean repair(Resource resource, EPackage profileDefinition, Collection<? extends EObject> stereotypeApplications, DiagnosticChain diagnostics, IProgressMonitor monitor) {
		// We can be assured that there is at least one stereotype application, otherwise we wouldn't be here
		boolean result = false;

		Profile profile = profileSupplier.get();
		if (profile != null) {
			String taskName = NLS.bind("Migrating stereotypes to current version of profile \"{0}\"...", profile.getName());
			SubMonitor sub = SubMonitor.convert(monitor, taskName, stereotypeApplications.size() * 3 / 2);

			// Apply the profile
			StereotypeApplicationRepairParticipant.createStereotypeApplicationMigrator(profile, diagnostics).migrate(stereotypeApplications, sub.newChild(stereotypeApplications.size()));
			for (Package next : packages) {
				applyProfile(next, profile);
			}

			result = true;

			sub.done();
		}

		return result;
	}

	protected ProfileApplication applyProfile(Package package_, Profile profile) {
		ProfileApplication result;

		IProfileApplicationDelegate delegate = ProfileApplicationDelegateRegistry.INSTANCE.getDelegate(package_);

		// Is this a re-application?
		ProfileApplication existing = delegate.getProfileApplication(package_, profile);
		if (existing != null) {
			delegate = ProfileApplicationDelegateRegistry.INSTANCE.getDelegate(existing);
			delegate.reapplyProfile(package_, profile);
			result = existing;
		} else {
			package_.applyProfile(profile);
			result = delegate.getProfileApplication(package_, profile);
		}

		return result;
	}
}

Back to the top