Skip to main content
summaryrefslogtreecommitdiffstats
blob: eaae23e06e75fcc0de2a71520f2b8d8c3b9faa77 (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
/**
 *  Copyright (c) 2011-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:
 *  	Gregoire Dupe (Mia-Software) - Bug 361794 - [Restructuring] EMF Facet customization meta-model
 *      Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 *      Nicolas Bros (Mia-Software) - Bug 372626 - Aggregates
 *      Gregoire Dupe (Mia-Software) - Bug 373078 - API Cleaning
 *      Nicolas Bros (Mia-Software) - Bug 378271 - [Table] Select Columns To Hide is broken
 */
package org.eclipse.emf.facet.custom.core.internal;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.facet.custom.core.ICustomizationManager;
import org.eclipse.emf.facet.custom.core.exception.CustomizationException;
import org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.Customization;
import org.eclipse.emf.facet.efacet.core.IFacetManager;
import org.eclipse.emf.facet.efacet.core.IFacetManagerFactory;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetOperation;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetSet;

public class CustomizationManager implements ICustomizationManager {

	private final HashSet<FacetOperation> customProperties = new HashSet<FacetOperation>();
	private final IFacetManager facetManager;

	public CustomizationManager(final ResourceSet resourceSet) {
		this.facetManager = IFacetManagerFactory.DEFAULT.getOrCreateFacetManager(resourceSet);
	}
	
	public CustomizationManager(final IFacetManager facetManager) {
		this.facetManager = facetManager;
	}

	public void addFrontManagedCustomization(final Customization customization) {
		this.facetManager.getManagedFacetSets().add(0, customization);
	}

	public void removeCustomization(final Customization customization) {
		this.facetManager.getManagedFacetSets().remove(customization);
	}

	public List<Customization> getManagedCustomizations() {
		return new CustomizationsDelegatingList(this.facetManager.getManagedFacetSets());
	}

	public <T> T getCustomValueOf(final EObject eObject,
			final FacetOperation customizationProperty,
			final Class<T> classs)
			throws CustomizationException {
		// Begin precondition checking section
		/* The scope of a customization property is not available yet in new customization meta-model
		if (!(customizationProperty.getScope().contains(
				CustomizationPropertyScope.ECLASS) || (customizationProperty
				.getScope().contains(CustomizationPropertyScope.FACET)))) {
			throw new RuntimeException(
					"The customization property is expected to be applicable on an EClass or on a Facet"); //$NON-NLS-1$
		}
		*/
		// End precondition checking section
		T result = null;
		try {
			// the structural feature : null
			final Object[] args = new Object[] { null };
			result = this.facetManager.invoke(eObject, customizationProperty,
					classs, null, args);
		} catch (final Exception e) {
			throw new CustomizationException(e);
		} 
		return result;
	}

	public <T> T getCustomValueOf(final EObject eObject,
			final ETypedElement eTypedElement,
			final FacetOperation customizationProperty,
			final Class<T> classs)
			throws CustomizationException {
		// Begin precondition checking section
		if (eObject == null) {
			throw new IllegalArgumentException(
					"The parameter 'eObject' must not be null."); //$NON-NLS-1$
		}
		if (customizationProperty == null) {
			throw new IllegalArgumentException(
					"The parameter 'customizationProperty' must not be null."); //$NON-NLS-1$
		}
		/* The scope of a customization property is not available yet in new customization meta-model
		if (!(eStructuralFeature instanceof EReference)) {
			if (customizationProperty.getScope().equals(
					CustomizationPropertyScope.EREFERENCE)) {
				throw new CustomizationException(
						"The eStructuralFeature is an instance of " //$NON-NLS-1$
								+ eStructuralFeature.getClass().getName()
								+ " but EReference is expected."); //$NON-NLS-1$
			}
		}

		if (!(eStructuralFeature instanceof EAttribute)) {
			if (customizationProperty.getScope().equals(
					CustomizationPropertyScope.EATTRIBUTE)) {
				throw new CustomizationException(
						"The eStructuralFeature is an instance of " //$NON-NLS-1$
								+ eStructuralFeature.getClass().getName()
								+ " but EAttribute is expected."); //$NON-NLS-1$
			}
		}
		*/
		// End precondition checking section
		try {
			return this.facetManager.invoke(eObject, customizationProperty, classs, null, new Object[] { eTypedElement });
		} catch (Exception e) {
			throw new CustomizationException(e);
		}
	}

	public List<FacetOperation> getCustomizationPropertiesByName(
			final String name) {
		if (name == null) {
			throw new IllegalArgumentException(
					"The parameter 'name' must not be null."); //$NON-NLS-1$
		}
		List<FacetOperation> results = new ArrayList<FacetOperation>();
		for (FacetOperation customizationProperty : this.customProperties) {
			if (name.equals(customizationProperty.getName())) {
				results.add(customizationProperty);
			}
		}
		return results;
	}

	public void addCustomization(final URI uri) {
		if (uri == null) {
			throw new IllegalArgumentException(
					"The parameter 'uri' must not be null."); //$NON-NLS-1$
		}
		Resource resource = this.facetManager.getResourceSet().getResource(uri, true);
		Iterator<EObject> iterator = resource.getAllContents();
		while (iterator.hasNext()) {
			EObject eObject = iterator.next();
			if (eObject instanceof FacetOperation) {
				FacetOperation customizationProperty = (FacetOperation) eObject;
				this.customProperties.add(customizationProperty);
			}
		}
	}

	public ResourceSet getResourceSet() {
		return this.facetManager.getResourceSet();
	}

	public void removeAllCustomizations() {
		List<Customization> customizationsToRemove = new LinkedList<Customization>();
		for (FacetSet facetSet : this.facetManager.getManagedFacetSets()) {
			if (facetSet instanceof Customization) {
				Customization customization = (Customization) facetSet;
				customizationsToRemove.add(customization);
			}
		}
		for (Customization customization : customizationsToRemove) {
			this.facetManager.getManagedFacetSets().remove(customization);
		}
	}

	public IFacetManager getFacetManager() {
		return this.facetManager;
	}
}

Back to the top