Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f85c0c1c9054b35eda4555ac9a8ee7f8a2b42ed6 (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
/*****************************************************************************
 * Copyright (c) 2011, 2016 Atos, 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 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:
 *  Vincent Hemery (Atos) vincent.hemery@atos.net - Initial API and implementation
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.resourceloading.internal.ui.expressions;

import java.util.Iterator;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;

/**
 * This class provides test to perform on resources to know their loading
 * status.
 */
public class LoadingTester extends PropertyTester {

	/**
	 * property to test if the selected elements are in loaded resources (at
	 * least one other than the opened one)
	 */
	public static final String IS_ALL_LOADED = "isAllLoaded"; //$NON-NLS-1$

	/** property to test if the selected elements are in not loaded resources */
	public static final String IS_ALL_NOTLOADED = "isAllNotLoaded"; //$NON-NLS-1$

	/**
	 * Test a property
	 *
	 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
	 *
	 * @param receiver
	 * @param property
	 * @param args
	 * @param expectedValue
	 * @return
	 */
	public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
		if (IS_ALL_LOADED.equals(property) && receiver instanceof IStructuredSelection) {
			boolean answer = isInLoadedResource((IStructuredSelection) receiver);
			return new Boolean(answer).equals(expectedValue);
		}
		if (IS_ALL_NOTLOADED.equals(property) && receiver instanceof IStructuredSelection) {
			boolean answer = isInNotLoadedResource((IStructuredSelection) receiver);
			return new Boolean(answer).equals(expectedValue);
		}
		return false;
	}

	/**
	 * Tests the selection in order to know if it is in a loaded resource
	 *
	 * @param selection
	 *            selected elements
	 * @return <code>true</code> if all selected elements are in loaded
	 *         resources ; <code>false</code otherwise or if empty selection
	 */
	private boolean isInLoadedResource(IStructuredSelection selection) {
		if (!selection.isEmpty()) {
			boolean atLeastOneInSubmodel = false;
			URI mainURI = null;
			Iterator<?> iter = selection.iterator();
			while (iter.hasNext()) {
				Object obj = iter.next();
				EObject eObject = EMFHelper.getEObject(obj);
				if (eObject != null && !eObject.eIsProxy()) {
					// test that there is at least one not loaded resource
					// object
					if (!atLeastOneInSubmodel) {
						Resource containingResource = eObject.eResource();
						if (mainURI == null && containingResource != null && containingResource.getResourceSet() instanceof ModelSet) {
							mainURI = ((ModelSet) containingResource.getResourceSet()).getURIWithoutExtension();
						}
						if (mainURI != null) {
							URI uriTrim = containingResource.getURI().trimFileExtension();
							atLeastOneInSubmodel = !uriTrim.equals(mainURI);
						}
					}
					continue;
				}

				// a step failed
				return false;
			}
			return atLeastOneInSubmodel;
		}
		return false;
	}

	/**
	 * Tests the selection in order to know if it is in a not loaded resource
	 *
	 * @param selection
	 *            selected elements
	 * @return <code>true</code> if all selected elements are in not loaded
	 *         resources ; <code>false</code otherwise or if empty selection
	 */
	private boolean isInNotLoadedResource(IStructuredSelection selection) {
		if (!selection.isEmpty()) {
			Iterator<?> iter = selection.iterator();
			while (iter.hasNext()) {
				Object obj = iter.next();
				EObject eObject = EMFHelper.getEObject(obj);
				if (eObject != null && eObject.eIsProxy()) {
					continue;
				} else if (obj instanceof IAdaptable) {
					View view = ((IAdaptable) obj).getAdapter(View.class);

					if (view instanceof Edge) {
						View target = ((Edge) view).getTarget();
						if (target != null && resolveSemanticElement(target) == null) {
							// there is a backslash decorator
							continue;
						}
					}
				}
				// a step failed
				return false;
			}
			return true;
		}
		return false;
	}

	private EObject resolveSemanticElement(View view) {
		EObject result = view.getElement();

		if ((result != null) && result.eIsProxy()) {
			// Try harder to resolve it
			result = EMFCoreUtil.resolve(TransactionUtil.getEditingDomain(view), result);
		}

		return result;
	}
}

Back to the top