Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36291e9dd59114d661f1a94dff69e8aae0c159e9 (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
/**
 *  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 373078 - API Cleaning
 *      Gregoire Dupe (Mia-Software) - Bug 376576 - [EFacet] Change the multiplicity of Facet::extendedFacet
 *      David Couvrand (Soft-Maint) - Bug 418418 - [Customization] Overlay icons not implemented
 */
package org.eclipse.emf.facet.custom.core.internal.exported;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.facet.custom.core.ICustomizationManager;
import org.eclipse.emf.facet.custom.core.exception.CustomizationException;
import org.eclipse.emf.facet.custom.core.internal.Activator;
import org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.Customization;
import org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.EClassCustomization;
import org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.FacetCustomization;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetOperation;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetSet;
import org.eclipse.emf.facet.util.core.Logger;

/**
 * @since 0.2
 */
public final class CustomizationUtils {

	private static final String FILE_EXTENSION = "custom"; //$NON-NLS-1$

	private CustomizationUtils() {
		// Must not be used
	}

	public static EPackage getCustomizedEPackage(
			final Customization customization) {
		EPackage result = null;
		for (EClassifier eClassifier : customization.getEClassifiers()) {
			for (EPackage ePackage : getExtendedEPackage(eClassifier)) {
				if (ePackage != null && !(ePackage instanceof FacetSet)) {
					result = ePackage;
					break;
				}
			}
		}
		return result;
	}

	private static List<EPackage> getExtendedEPackage(
			final EClassifier eClassifier) {
		final List<EClassifier> extended = new ArrayList<EClassifier>();
		if (eClassifier instanceof FacetCustomization) {
			final FacetCustomization facetCustom = (FacetCustomization) eClassifier;
			extended.addAll(facetCustom.getExtendedFacets());
		} else if (eClassifier instanceof EClassCustomization) {
			final EClassCustomization eClassCustom = (EClassCustomization) eClassifier;
			extended.add(eClassCustom.getExtendedMetaclass());
		}
		final List<EPackage> ePackages = new LinkedList<EPackage>();
		for (EClassifier extCassifier : extended) {
			if (extCassifier != null) {
				ePackages.add(extCassifier.getEPackage());
			}
		}
		return ePackages;
	}

	/**
	 * Return the default file extension of a customization file.
	 * 
	 * @return the file extension without the dot.
	 */
	public static String getDefaultFileExtension() {
		return CustomizationUtils.FILE_EXTENSION;
	}
	
	/**
	 * Find a Customization with the given name among the given list of Customizations. If several Customizations have the same name,
	 * then return the first one.
	 * 
	 * @param customizations
	 *            where to look for
	 * @param name
	 *            the name of the Customization to find
	 * @return the Customization, or <code>null</code> if not found in the given list
	 */
	public static Customization getCustomization(final Collection<Customization> customizations, final String name) {
		Customization result = null;
		for (Customization customization : customizations) {
			if (name.equals(customization.getName())) {
				result = customization;
			}
		}
		return result;
	}

	public static <T> T getPropertyValue(
			final ICustomizationManager customManager, final Object element,
			final FacetOperation property, final ETypedElement eTypedElement,
			final Class<T> classs) {
		T result = null;
		if (element instanceof EObject) {
			final EObject eObject = (EObject) element;
			try {
				if (eTypedElement == null) {
					result = customManager.getCustomValueOf(eObject, property,
							classs);
				} else {
					result = customManager.getCustomValueOf(eObject,
							eTypedElement, property, classs);
				}
			} catch (final CustomizationException e) {
				Logger.logError(
						e,
						"Failed to get the value of '" + property.getName() + "' for " + element, //$NON-NLS-1$ //$NON-NLS-2$
						Activator.getDefault());
			}
		}
		return result;
	}
}

Back to the top