Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94ae1e05df9efa242d6f296baf15f12934a1ffc3 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*****************************************************************************
 * Copyright (c) 2008, 2014, 2018 CEA LIST 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
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *  Christian W. Damus (CEA) - Refactoring package/profile import/apply UI for CDO
 *  Christian W. Damus (CEA) - bug 422257
 *  Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Bug 538193
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.properties.profile.ui.dialogs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.extensionpoints.Registry;
import org.eclipse.papyrus.uml.extensionpoints.profile.IRegisteredProfile;
import org.eclipse.papyrus.uml.extensionpoints.standard.FilteredRegisteredElementsSelectionDialog;
import org.eclipse.papyrus.uml.extensionpoints.utils.Util;
import org.eclipse.papyrus.uml.profile.ui.dialogs.ElementImportTreeSelectionDialog.ImportSpec;
import org.eclipse.papyrus.uml.profile.ui.dialogs.ProfileTreeSelectionDialog;
import org.eclipse.papyrus.uml.tools.utils.ProfileUtil;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;

/**
 *
 */
public class RegisteredProfileSelectionDialog extends FilteredRegisteredElementsSelectionDialog {

	/**
	 *
	 */
	private Package currentPackage;

	/**
	 *
	 *
	 * @param umlPackage
	 * @param parent
	 */
	public RegisteredProfileSelectionDialog(Composite parent, Package umlPackage) {
		super(parent.getShell(), true, Registry.getRegisteredProfiles().toArray(new IRegisteredProfile[0]), new ArrayList<>(), "Apply profiles from Papyrus repository :", "");
		currentPackage = umlPackage;
	}

	/**
	 *
	 *
	 * @return
	 */
	public List<Profile> run() {
		// /*String message= "List of profiles\n";
		// for(int i = 0; i < regProfiles.length ; i++) {
		// message+= "|"+regProfiles[i].name+": "+regProfiles[i].qualifiednames+"|";
		// }
		// MessageDialog dialog = new MessageDialog(new Shell(),
		// "Profiles available",
		// null,
		// message,
		// MessageDialog.INFORMATION,
		// new String[] {"OK"},
		// 0);
		// dialog.open();
		this.open();

		List<Profile> result = new LinkedList<>();
		ResourceSet resourceSet = Util.createTemporaryResourceSet();

		try {
			List<Profile> profilesToApply = this.treatSelection(resourceSet);

			for (Profile profile : profilesToApply) {
				result.add(EMFHelper.reloadIntoContext(profile, currentPackage));
			}
		} finally {
			EMFHelper.unload(resourceSet);
		}

		return result;
	}

	/**
	 *
	 *
	 * @return
	 */
	private List<Profile> treatSelection(ResourceSet resourceSet) {

		// User selection
		Object[] selection = this.getResult();

		if (selection == null) { // Cancel was selected
			return new ArrayList<>();
		}

		// This first list (listOfProfileToApply) contain every selected profile
		// which owns sub-profiles (it is possible to select a set of sub-profiles)
		// The list is used to build a profile selection tree
		List<Package> listOfProfileToApply = new ArrayList<>();
		// try to parse the qualified names

		List<String> subprofilesList = new ArrayList<>();
		for (int i = 0; i < selection.length; i++) {

			IRegisteredProfile currentProfile = (IRegisteredProfile) (selection[i]);
			URI modelUri = currentProfile.getUri();
			Resource modelResource = resourceSet.getResource(modelUri, true);

			// retrieve registered sub-profiles to be selected
			String qualifiedNames = currentProfile.getQualifiedNames();

			// try to parse the qualified names
			String[] profiles = qualifiedNames.split(",");

			// make a collection with String with no space
			for (int j = 0; j < profiles.length; j++) {
				String string = profiles[j].trim();
				subprofilesList.add(string);
			}

			if ((!modelResource.getContents().isEmpty()) && modelResource.getContents().get(0) instanceof Profile) {
				Message processMsg = new Message("Profile application", "Loading profiles...");
				processMsg.open();
				Profile profileToApply = (Profile) (modelResource.getContents().get(0));
				processMsg.close();
				// if (PackageUtil.getSubProfiles(profileToApply).isEmpty()) {
				// No sub-profile -> apply profile directly
				// PackageUtil.applyProfile(currentPackage, profileToApply, false);
				// } else {

				listOfProfileToApply.add(profileToApply);
				// }
			}
		}

		if (!listOfProfileToApply.isEmpty()) {
			final Profile onlyOneProfile = ProfileUtil.getTheOnlyOneProfile(listOfProfileToApply);
			if (null == onlyOneProfile) {

				// Open package/profile selection tree selection
				ProfileTreeSelectionDialog profileDialog = new ProfileTreeSelectionDialog(getShell(), listOfProfileToApply, subprofilesList);
				int returnValue = profileDialog.open();

				// Apply selected profile if ok was selected
				if (Window.OK == returnValue) {
					Collection<ImportSpec<Profile>> dlgResult = profileDialog.getResult();
					List<Profile> result = new java.util.ArrayList<>(dlgResult.size());
					for (ImportSpec<Profile> next : dlgResult) {
						result.add(next.getElement());
					}
					return result;
				} else {
					return new ArrayList<Profile>();
				}
			} else {
				return Collections.singletonList(onlyOneProfile);
			}
		}
		return new ArrayList<>();
	}
}

Back to the top