Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98454ba31914bed749b2a5b2bde69f67c1c2b40c (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
/**
 * 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.custom.sdk.core.internal;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
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.ETypedElementCase;
import org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.FacetCustomization;
import org.eclipse.emf.facet.custom.sdk.core.ICustomizationCommandFactory;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.extensible.Query;

/**
 * Concrete implementation of {@link ICustomizationCommandFactory}.
 */
public class CustomizationCommandFactory implements
		ICustomizationCommandFactory {

	private final EditingDomain editingDomain;

	public CustomizationCommandFactory(final EditingDomain editingDomain) {
		if (editingDomain == null) {
			throw new IllegalArgumentException(
					"The given EditingDomain cannot be null"); //$NON-NLS-1$
		}
		this.editingDomain = editingDomain;
	}

	/**
	 * Create an {@link AddCommand} to add the element to the owner.
	 * 
	 * @param owner
	 *            the owner.
	 * @param element
	 *            the element to add.
	 * @return the command.
	 */
	private Command createAddCommand(final Object owner, final Object element) {
		if (owner == null) {
			throw new IllegalArgumentException("The given owner cannot be null"); //$NON-NLS-1$
		}
		if (element == null) {
			throw new IllegalArgumentException(
					"The given element cannot be null"); //$NON-NLS-1$
		}
		return AddCommand.create(this.editingDomain, owner,
				getEStructuralFeature(element), element);
	}

	/**
	 * Return the {@link EStructuralFeature} for a given element.
	 * 
	 * @param element
	 *            the element.
	 * @return the eStructuralFeature for the element.
	 */
	private static EStructuralFeature getEStructuralFeature(final Object element) {
		EStructuralFeature result = null;
		if (element instanceof EClassCustomization) {
			result = EcorePackage.eINSTANCE.getEPackage_EClassifiers();
		}
		return result;
	}

	public Command createEClassCustomization(final Customization parent,
			final EClassCustomization eClassCustom) {
		return createAddCommand(parent, eClassCustom);
	}

	public Command createFacetCustomization(final Customization parent,
			final FacetCustomization facetCustom) {
		return createAddCommand(parent, facetCustom);
	}

	public Command addValueInCase(final ETypedElementCase elementCase,
			final Query value) {
		return createAddCommand(elementCase, value);
	}
}

Back to the top