Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2ee5926fb06ecc5ccfd73662929bc73572f629c1 (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
/**
 * Copyright (c) 2012 Mia-Software.
 *  
 * 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:
 *  	Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 *  	Grégoire Dupé (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.emf.facet.efacet.sdk.ui.internal.widget.creation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.Facet;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetAttribute;
import org.eclipse.emf.facet.util.ui.internal.exported.dialog.IDialog;
import org.eclipse.emf.facet.util.ui.internal.exported.util.widget.command.AbstractGetOrCreateFilteredElementCommandWidget;
import org.eclipse.swt.widgets.Composite;

/**
 * Widget for the selection and/or creation of an element in a list.
 */
public class GetFilteredSubTypingTypeWidget extends
		AbstractGetOrCreateFilteredElementCommandWidget<ETypedElement, Object> {

	private final EClass context;
	private final Facet facet;

	/**
	 * Constructor.
	 * 
	 * @param parent
	 *            the parent of this widget.
	 * @param properties
	 *            the properties.
	 */
	public GetFilteredSubTypingTypeWidget(final Composite parent, final EClass context,
			final Facet facet) {
		super(parent);
		this.context = context;
		this.facet = facet;
	}

	@Override
	protected Map<String, ETypedElement> getElements() {
		final Map<String, ETypedElement> allElements = new HashMap<String, ETypedElement>();
		if (this.context != null) {
			allElements.putAll(getAttributes(this.context));
			allElements.putAll(getOperations(this.context.getEAllOperations()));
		}
		if (this.facet != null) {
			allElements.putAll(getFacetAttributes(this.facet));
			allElements.putAll(getOperations(this.facet.getFacetOperations()));
		}
		return allElements;
	}

	/**
	 * @param facet
	 * @return
	 */
	private static Map<String, ETypedElement> getFacetAttributes(
			final Facet facet) {
		final Map<String, ETypedElement> allElements = new HashMap<String, ETypedElement>();
		for (final EStructuralFeature element : facet.getFacetElements()) {
			final EClassifier eType = element.getEType();
			if ((element instanceof FacetAttribute)
					&& eType.equals(
							EcorePackage.eINSTANCE.getEBoolean())) {
				allElements.put(element.getName(), element);
			}
		}
		return allElements;
	}

	/**
	 * @param context
	 * @return
	 */
	private static Map<String, ETypedElement> getOperations(
			final List<? extends EOperation> elements) {
		final Map<String, ETypedElement> allElements = new HashMap<String, ETypedElement>();
		for (final EOperation operation : elements) {
			// Get all the operations returning a boolean and without any
			// parameter.
			if (operation.getEType().equals(
					EcorePackage.eINSTANCE.getEBoolean())
					&& (operation.getEParameters().size() == 0)) {
				allElements.put(operation.getName(), operation);
			}
		}
		return allElements;
	}

	/**
	 * @param context
	 * @return
	 */
	private static Map<String, ETypedElement> getAttributes(
			final EClass context) {
		final Map<String, ETypedElement> allElements = new HashMap<String, ETypedElement>();
		for (final EAttribute attribute : context.getEAllAttributes()) {
			if (attribute.getEType().equals(
					EcorePackage.eINSTANCE.getEBoolean())) {
				allElements.put(attribute.getName(), attribute);
			}
		}
		return allElements;
	}

	@Override
	protected IDialog<Object> createDialog() {
		// No element can be created.
		return null;
	}

	/**
	 * @return the selected element in the filtredList.
	 */
	public ETypedElement getSubTypeSelected() {
		return getElementSelected();
	}

	@Override
	public Command getCommand() {
		// Here, this widget only return a selected element so, no command is
		// returned.
		return null;
	}

	@Override
	public void notifyChanged() {
		// No action has to be done if a change appends.
	}

	@Override
	public void onDialogValidation() {
		// Nothing.
	}
}

Back to the top