Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb4eae528497ee272ffe5b3764d8e636216278d7 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*****************************************************************************
 * Copyright (c) 2011, 2016 Atos Origin, 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:
 *  Vincent Hemery (Atos) vincent.hemery@atos.net - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 415639
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.resourceloading.util;

import static org.eclipse.papyrus.infra.core.utils.TransactionHelper.createPrivilegedRunnable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.infra.core.language.ILanguageService;
import org.eclipse.papyrus.infra.core.resource.AbstractBaseModel;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.IPapyrusRunnable;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceSet;
import org.eclipse.papyrus.infra.services.resourceloading.Activator;
import org.eclipse.papyrus.infra.services.resourceloading.Messages;
import org.eclipse.papyrus.infra.tools.notify.NotificationBuilder;
import org.eclipse.papyrus.infra.tools.notify.Type;
import org.eclipse.papyrus.infra.tools.util.CoreExecutors;
import org.eclipse.papyrus.infra.tools.util.IProgressRunnable;

/**
 * This class provides utility methods for model loading
 *
 * @author vhemery
 */
public class LoadingUtils {

	/**
	 * Load corresponding resources in model set for all its existing models.
	 *
	 * @param modelSet
	 *            the model set
	 * @param uriWithoutFileExtension
	 *            path of resources to load without file extension
	 */
	public static void loadResourcesInModelSet(final ModelSet modelSet, final URI uriWithoutFileExtension) {
		// This must be created on the UI thread
		final NotificationBuilder error = NotificationBuilder
				.createAsyncPopup(Messages.LoadingUtils_ErrorTitle, String.format(Messages.LoadingUtils_ErrorMessage, uriWithoutFileExtension.toString()))
				.setType(Type.ERROR).setDelay(2000);

		runInEditingDomain(modelSet, monitor -> {
			try {
				try {
					IPageManager pageMngr = ServiceUtilsForResourceSet.getInstance().getIPageManager(modelSet);
					List<Object> allPages = pageMngr.allPages();
					// mark progress
					monitor.beginTask(Messages.LoadingUtils_RefreshPagesTask, allPages.size());
					// the uri is added after getting all the pages. If it is done before, the eobjects are resolved
					for (Object o : allPages) {
						// refresh pages to display proxy diagrams
						if (o instanceof EObject) {
							EObject eobject = (EObject) o;
							if (eobject.eIsProxy()) {
								InternalEObject internal = (InternalEObject) eobject;
								URI uriProxy = internal.eProxyURI();
								URI trimFragment = uriProxy.trimFragment();
								if (uriWithoutFileExtension.equals(trimFragment.trimFileExtension())) {
									try {
										Resource r = modelSet.getResource(trimFragment, true);
										if (r != null) {
											EObject newEObject = r.getEObject(uriProxy.fragment());
											if (pageMngr.isOpen(newEObject)) {
												pageMngr.selectPage(newEObject);
											}
										} else {
											error.run();
										}
									} catch (Exception e) {
										error.run();
										Activator.logError(e);
									}
								}
							}
						}
						// mark progress
						monitor.worked(1);
					}
					Set<String> extensions = getExtensions(modelSet);
					// mark progress
					monitor.beginTask(Messages.LoadingUtils_LoadModelsTask, extensions.size());
					for (String s : extensions) {
						try {
							URI uriToLoad = uriWithoutFileExtension.appendFileExtension(s);
							Resource r = modelSet.getResource(uriToLoad, true);
							if (r == null) {
								error.run();
							}
						} catch (Exception re) {
							error.run();
							Activator.logError(re);
						}
						// mark progress
						monitor.worked(1);
					}
				} catch (ServiceException e) {
					Activator.logError(e);
				}
			} finally {
				// mark progress
				monitor.done();
			}
		});
	}

	/**
	 * Unload corresponding resources from model set for all its existing models.
	 *
	 * @param modelSet
	 *            the model set
	 * @param uriWithoutFileExtension
	 *            path of resources to unload without file extension
	 */
	public static void unloadResourcesFromModelSet(ModelSet modelSet, URI uriWithoutFileExtension) {
		runInEditingDomain(modelSet, monitor -> {

			try {
				// mark progress
				monitor.beginTask(Messages.LoadingUtils_UnloadModelsTask, modelSet.getResources().size());

				// Use the platform string of a normalized URI for comparison below, see bug 372326
				// (registered libraries in the model set have different URIs - e.g. due to a pathmap -
				// although they point to the same location).
				// TODO: Use a single detection mechanism in ResourceUpdateService and here
				String unloadPlatformString;
				if (uriWithoutFileExtension.isPlatform()) {
					unloadPlatformString = uriWithoutFileExtension.toPlatformString(true);
				} else {
					unloadPlatformString = URI.decode(uriWithoutFileExtension.toString());
				}
				// URIConverter uriConverter = modelSet.getURIConverter();
				// unload resource
				for (Resource res : new ArrayList<Resource>(modelSet.getResources())) {
					URI normalizedURI = res.getURI();
					String platformString;
					if (normalizedURI.isPlatform()) {
						platformString = normalizedURI.trimFileExtension().toPlatformString(true);
					} else {
						platformString = URI.decode(normalizedURI.trimFileExtension().toString());
					}

					if ((platformString != null) && platformString.equals(unloadPlatformString)) {
						// unload this resource
						res.unload();
						// there is no need to remove it from the resource set (which inevitably
						// causes ConcurrentModificationExceptions!), especially as we may be
						// loading it again (the editor is still open)
					}
					// mark progress
					monitor.worked(1);
				}
				// // mark progress
				// monitor.beginTask("Resolve", 1);
				// EcoreUtil.resolveAll(modelSet);
				// monitor.worked(1);
			} finally {
				// mark progress
				monitor.done();
			}
		});
	}

	static void runInEditingDomain(ModelSet modelSet, IPapyrusRunnable runnable) {
		try {
			// Created the privileged progress-runnable that borrows the editing domain
			IProgressRunnable privileged = createPrivilegedRunnable(
					modelSet.getTransactionalEditingDomain(),
					runnable);

			// And wrap it in the service-registry context for best possible UI feed-back
			privileged = ServiceUtilsForResourceSet.getInstance().runnable(privileged, modelSet);

			// Go
			CoreExecutors.getUIExecutorService().syncExec(privileged);
		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	/**
	 * Get list of file extensions existing for this model set
	 *
	 * @param modelSet
	 *            model set to find common extensions for
	 * @return extensions list to explore
	 */
	private static Set<String> getExtensions(ModelSet modelSet) {
		Set<String> result = new HashSet<String>();
		// FIXME: Also need to generalize the notation and DI models
		result.add("notation"); //$NON-NLS-1$
		result.add(DiModel.DI_FILE_EXTENSION);

		// Get the semantic model extensions
		Collection<IModel> languageModels = ILanguageService.getLanguageModels(modelSet);
		if (languageModels.isEmpty()) {
			// No semantic models? Force UML for compatibility
			Activator.log.warn("Semantic service unavailable. Assuming a UML model."); //$NON-NLS-1$
			result.add("uml"); //$NON-NLS-1$
		} else {
			for (IModel model : languageModels) {
				if (model instanceof AbstractBaseModel) {
					URI uri = ((AbstractBaseModel) model).getResourceURI();
					if ((uri != null) && (uri.fileExtension() != null)) {
						result.add(uri.fileExtension());
					}
				}
			}
		}

		return result;
	}

	/**
	 * Get File from a URI
	 *
	 * @param uri
	 *            the URI to transform
	 * @return the corresponding file
	 */
	public static IFile getFile(URI uri) {
		IPath path = getPath(uri);
		if (path != null) {
			return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		}
		return null;
	}

	/**
	 * Get Path from a URI
	 *
	 * @param uri
	 *            the URI to transform
	 * @return the corresponding path
	 */
	public static IPath getPath(URI uri) {
		String scheme = uri.scheme();
		IPath path = null;
		if ("platform".equals(scheme)) { //$NON-NLS-1$
			path = Path.fromPortableString(uri.toPlatformString(true));
		} else if ("file".equals(scheme)) { //$NON-NLS-1$
			path = Path.fromPortableString(uri.toFileString());
		}
		return path;
	}
}

Back to the top