Skip to main content
summaryrefslogtreecommitdiffstats
blob: a6155e53649912a6355da6f7d12f08a20d2dadd5 (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
/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * 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:
 *         Florian Pirchner - Initial implementation
 */

package org.eclipse.osbp.ecview.dsl.scope;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.AbstractScope;
import org.eclipse.osbp.ecview.semantic.uimodel.UiRawBindable;
import org.eclipse.osbp.ecview.semantic.uimodel.UiRawBindablePathSegment;
import org.eclipse.osbp.ecview.semantic.uimodel.UiRawBindableProvider;
import org.eclipse.osbp.ecview.semantic.uimodel.UiTypedBindableDef;
import org.eclipse.osbp.ecview.semantic.uimodel.UiTypedBindableRawType;
import org.eclipse.osbp.ecview.semantic.uimodel.UiView;

// TODO: Auto-generated Javadoc
/**
 * The Class RawBindablePathRawBindableScope.
 */
public class RawBindablePathRawBindableScope extends AbstractScope {

	/** The context. */
	private EObject context;
	
	/** The name provider. */
	@SuppressWarnings("unused")
	private IQualifiedNameProvider nameProvider;

	/**
	 * Instantiates a new raw bindable path raw bindable scope.
	 *
	 * @param context
	 *            the context
	 * @param nameProvider
	 *            the name provider
	 */
	protected RawBindablePathRawBindableScope(EObject context,
			IQualifiedNameProvider nameProvider) {
		super(IScope.NULLSCOPE, true);
		this.context = context;
		this.nameProvider = nameProvider;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.xtext.scoping.impl.AbstractScope#getAllLocalElements()
	 */
	@Override
	protected Iterable<IEObjectDescription> getAllLocalElements() {
		if (context instanceof UiRawBindablePathSegment) {
			UiRawBindablePathSegment pathSegment = (UiRawBindablePathSegment) context;

			EObject expected = null;
			EObject parent = context.eContainer();
			if (parent instanceof UiTypedBindableDef) {
				expected = ((UiTypedBindableDef) parent).getRawBindable();
			} else if (parent instanceof UiRawBindablePathSegment) {
				expected = ((UiRawBindablePathSegment) parent).getRawBindable();
			} else if (parent instanceof UiTypedBindableRawType) {
				expected = ((UiTypedBindableRawType) parent).getRawBindable();
			}

			List<IEObjectDescription> result = null;
			if (!pathSegment.isToParent()) {
				result = collectRawBindables(expected);
			} else if (expected != null) {
				result = new ArrayList<IEObjectDescription>(1);
				IEObjectDescription bindableResult = findValidRawBindableInParent(expected.eContainer());
				if (bindableResult != null) {
					result.add(bindableResult);
				}
			}
			return result;
		}

		return Collections.emptyList();
	}

	/**
	 * Iterate the eContainers() up to root to find a raw bindable.
	 *
	 * @param expected
	 *            the expected
	 * @return the IE object description
	 */
	private IEObjectDescription findValidRawBindableInParent(EObject expected) {

		UiRawBindable result = findInParent(expected);
		if (result != null) {
			return EObjectDescription.create(
					((UiRawBindable) result).getName(), result);
		} else {
			return null;
		}

	}

	/**
	 * Find in parent.
	 *
	 * @param expected
	 *            the expected
	 * @return the ui raw bindable
	 */
	private UiRawBindable findInParent(EObject expected) {
		if (expected == null) {
			return null;
		}

		if (expected instanceof UiRawBindable
				&& isValid(((UiRawBindable) expected).getName())) {
			return (UiRawBindable) expected;
		}

		return findInParent(expected.eContainer());
	}

	/**
	 * Iterate the structure down to find raw bindables.
	 *
	 * @param container
	 *            the container
	 * @return the list
	 */
	protected List<IEObjectDescription> collectRawBindables(EObject container) {
		List<IEObjectDescription> result = new ArrayList<IEObjectDescription>();
		for (EObject object : container.eContents()) {
			if (object instanceof UiRawBindable) {
				if (isValid(((UiRawBindable) object).getName())) {
					result.add(EObjectDescription.create(
							((UiRawBindable) object).getName(), object));
				} else {
					// direct children may be of type UiRawBindable
					result.addAll(collectRawBindables(object));
				}
			} else if (object instanceof UiRawBindableProvider) {
				// direct children may be of type UiRawBindable
				result.addAll(collectRawBindables(object));
			}
		}
		return result;
	}

	/**
	 * Collect raw bindables.
	 *
	 * @param def
	 *            the def
	 * @return the list
	 */
	protected List<IEObjectDescription> collectRawBindables(
			UiTypedBindableDef def) {
		EObject rawBindableParent = findRawBindableParent(def);
		if (rawBindableParent == null) {
			rawBindableParent = findView(def);
		}
		List<IEObjectDescription> result = collectRawBindables(rawBindableParent);
		return result;
	}

	/**
	 * Checks if is valid.
	 *
	 * @param value
	 *            the value
	 * @return true, if is valid
	 */
	private boolean isValid(String value) {
		return value != null && !value.equals("");
	}

	/**
	 * Find raw bindable parent.
	 *
	 * @param eObject
	 *            the e object
	 * @return the ui raw bindable
	 */
	private UiRawBindable findRawBindableParent(EObject eObject) {
		UiRawBindable result = null;
		while (eObject != null && eObject.eContainer() != null) {
			if (eObject instanceof UiRawBindable) {
				result = (UiRawBindable) eObject;
				break;
			}
			eObject = eObject.eContainer();
		}
		return result;
	}

	/**
	 * Find view.
	 *
	 * @param def
	 *            the def
	 * @return the ui view
	 */
	private UiView findView(EObject def) {
		UiView result = null;
		while (def.eContainer() != null) {
			def = def.eContainer();
			if (def instanceof UiView) {
				result = (UiView) def;
				break;
			}
		}
		return result;
	}
}

Back to the top