aboutsummaryrefslogtreecommitdiffstats
blob: 59876aecc06be0680d01bad4e5a2ab0ec371d6cd (plain)
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 Atos.
 *
 *    
 * 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.resourceloading.util;

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.gmf.runtime.diagram.core.util.ViewUtil;
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.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;

/**
 * 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();
				if(obj instanceof IAdaptable) {
					View view = (View)((IAdaptable)obj).getAdapter(View.class);
					EObject eObject;
					if(view != null) {
						eObject = view.getElement();
					} else {
						eObject = (EObject)((IAdaptable)obj).getAdapter(EObject.class);
					}
					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) {

								//Bug 366709: Add tests to avoid NPEs
								NotationModel notationModel = NotationUtils.getNotationModel((ModelSet)containingResource.getResourceSet());
								if(notationModel != null) {
									URI notationModelURI = notationModel.getResourceURI();
									if(notationModelURI != null) {
										mainURI = notationModelURI.trimFileExtension();
									}
								}
							}
							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();
				if(obj instanceof IAdaptable) {
					View view = (View)((IAdaptable)obj).getAdapter(View.class);
					EObject eObject;
					if(view != null) {
						eObject = view.getElement();
					} else {
						eObject = (EObject)((IAdaptable)obj).getAdapter(EObject.class);
					}
					if(eObject != null && eObject.eIsProxy()) {
						continue;
					} else if(view instanceof Edge) {
						View target = ((Edge)view).getTarget();
						if(target != null && ViewUtil.resolveSemanticElement(target) == null) {
							// there is a backslash decorator
							continue;
						}
					}
				}
				// a step failed
				return false;
			}
			return true;
		}
		return false;
	}

}