Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 381d21576426d47fef06754e330851ad93df540e (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
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST.
 * 
 * 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:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   
 *****************************************************************************/
package org.eclipse.papyrus.uml.nattable.xtext.valuespecification.manager.cell;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.nattable.manager.cell.EMFFeatureValueCellManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EObjectAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EStructuralFeatureAxis;
import org.eclipse.papyrus.infra.nattable.utils.AxisUtils;
import org.eclipse.papyrus.infra.tools.converter.AbstractStringValueConverter;
import org.eclipse.papyrus.uml.textedit.valuespecification.xtext.ui.internal.UmlValueSpecificationActivator;
import org.eclipse.papyrus.uml.textedit.valuespecification.xtext.utils.commands.ValueSpecificationSetCommand;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.ValueSpecification;

import com.google.inject.Injector;

/**
 * This class provides the method to get and set the values in the Cell
 * representing a ValueSpecification.
 */
public class ValueSpecificationCellManager extends EMFFeatureValueCellManager {

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.emf.nattable.manager.cell.EMFFeatureValueCellManager#getSetValueCommand(org.eclipse.emf.transaction.TransactionalEditingDomain,
	 *      java.lang.Object, java.lang.Object, java.lang.Object,
	 *      org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager)
	 */
	@Override
	public Command getSetValueCommand(final TransactionalEditingDomain domain,
			final Object columnElement, final Object rowElement,
			final Object newValue, final INattableModelManager tableManager) {
		if (newValue instanceof IAdaptable) {
			final ICommand cmd = (ICommand) ((IAdaptable) newValue)
					.getAdapter(ICommand.class);
			if (null != cmd && cmd.canExecute()) {
				return new GMFtoEMFCommandWrapper(cmd);
			} else {
				final String editedString = (String) ((IAdaptable) newValue)
						.getAdapter(String.class);
				return getSetStringValueCommand(domain, columnElement,
						rowElement, editedString, null, tableManager);
			}

		}
		return UnexecutableCommand.INSTANCE;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.emf.nattable.manager.cell.EMFFeatureValueCellManager#getSetStringValueCommand(org.eclipse.emf.transaction.TransactionalEditingDomain,
	 *      java.lang.Object, java.lang.Object, java.lang.String,
	 *      org.eclipse.papyrus.infra.tools.converter.AbstractStringValueConverter,
	 *      org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager)
	 */
	@Override
	public Command getSetStringValueCommand(
			final TransactionalEditingDomain domain, final Object columnElement,
			final Object rowElement, final String newValue,
			final AbstractStringValueConverter valueSolver,
			final INattableModelManager tableManager) {
		if (columnElement instanceof EStructuralFeatureAxis && rowElement instanceof EObjectAxis || columnElement instanceof EObjectAxis && rowElement instanceof EStructuralFeatureAxis) {

			EStructuralFeature structuralFeature = null;
			if (columnElement instanceof EStructuralFeatureAxis) {
				structuralFeature = (EStructuralFeature) ((EStructuralFeatureAxis) columnElement).getElement();
			} else {
				structuralFeature = (EStructuralFeature) ((EStructuralFeatureAxis) rowElement).getElement();
			}

			if (UMLPackage.Literals.VALUE_SPECIFICATION == structuralFeature
					.getEType()) {
				EObject eObject = null;
				if (rowElement instanceof EObjectAxis) {
					eObject = ((EObjectAxis) rowElement).getElement();
				} else {
					eObject = ((EObjectAxis) columnElement).getElement();
				}

				CompositeCommand result = ValueSpecificationSetCommand
						.getInstance().createSetCommand(getInjector(), eObject,
								structuralFeature, newValue, getDefaultLanguages());

				return result.isEmpty() ? null : new GMFtoEMFCommandWrapper(
						result);
			}
		}

		return null;
	}

	/**
	 * This allow to define the default languages for an opaque expression.
	 * 
	 * @return The list of default languages.
	 */
	protected Collection<String> getDefaultLanguages() {
		// This method will be redefined to define the default languages of an opaque expression
		return Collections.emptyList();
	}

	/**
	 * This allow to get the injector used for the xtext parser.
	 * 
	 * @return The injector corresponding to the xtext parser.
	 */
	public Injector getInjector() {
		return UmlValueSpecificationActivator
				.getInstance()
				.getInjector(
						UmlValueSpecificationActivator.ORG_ECLIPSE_PAPYRUS_UML_TEXTEDIT_VALUESPECIFICATION_XTEXT_UMLVALUESPECIFICATION);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.emf.nattable.manager.cell.EMFFeatureValueCellManager#handles(java.lang.Object, java.lang.Object)
	 */
	@Override
	public boolean handles(final Object columnElement, final Object rowElement) {
		boolean result = false;
		Object row = AxisUtils.getRepresentedElement(rowElement);
		Object column = AxisUtils.getRepresentedElement(columnElement);
		if (column instanceof EStructuralFeature) {
			result = UMLPackage.Literals.VALUE_SPECIFICATION == ((EStructuralFeature) column)
					.getEType();
		} else if (row instanceof EStructuralFeature) {
			result = UMLPackage.Literals.VALUE_SPECIFICATION == ((EStructuralFeature) row)
					.getEType();
		}
		return result;
	}

}

Back to the top