Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7a50137519ccd744e58686e5d164b75d2a97d3f3 (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
/*****************************************************************************
 * Copyright (c) 2015, 2017, 2018 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
 *   Ansgar Radermacher (CEA) ansgar.radermacher@cea.fr - bug 528199
 *   
 *****************************************************************************/
package org.eclipse.papyrus.uml.nattable.xtext.valuespecification.celleditor;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.utils.DirectEditorsUtil;
import org.eclipse.papyrus.infra.nattable.manager.table.ITableAxisElementProvider;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.utils.AxisUtils;
import org.eclipse.papyrus.uml.nattable.xtext.integration.celleditor.AbstractXtextCellEditor;
import org.eclipse.papyrus.uml.xtext.integration.DefaultXtextDirectEditorConfiguration;
import org.eclipse.swt.widgets.Text;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * {@link ICellEditor} implementation that wraps a SWT {@link Text} control to
 * support text editing. This is also the default editor in NatTable if you
 * didn't configure something else.
 */
public class ValueSpecificationCellEditor extends AbstractXtextCellEditor {

	/**
	 * Constructor.
	 *
	 * @param table
	 *            The table.
	 * @param axisElement
	 *            The axis element object.
	 * @param elementProvider
	 *            The element provider.
	 */
	public ValueSpecificationCellEditor(final Table table,
			final Object axisElement,
			final ITableAxisElementProvider elementProvider) {
		super(table, axisElement, elementProvider);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.celleditor.AbstractPapyrusStyledTextCellEditor#getEditedEObject()
	 */
	@Override
	protected EObject getEditedEObject() {
		int columnIndex = this.layerCell.getColumnIndex();
		int rowIndex = this.layerCell.getRowIndex();
		Object row = this.elementProvider.getRowElement(rowIndex);
		Object column = this.elementProvider.getColumnElement(columnIndex);
		row = AxisUtils.getRepresentedElement(row);
		column = AxisUtils.getRepresentedElement(column);
		EObject editedEObject = null;
		if (row instanceof EObject && column instanceof EStructuralFeature) {
			if (UMLPackage.Literals.VALUE_SPECIFICATION == ((EStructuralFeature) column)
					.getEType()) {
				editedEObject = (EObject) row;
			}
		} else if (row instanceof EStructuralFeature && column instanceof EObject) {
			if (UMLPackage.Literals.VALUE_SPECIFICATION == ((EStructuralFeature) row)
					.getEType()) {
				editedEObject = (EObject) column;
			}
		}
		return editedEObject;
	}

	/**
	 * This allow to get the configuration for edited object.
	 * 
	 * @param row
	 *            The row element.
	 * @param column
	 *            The column element.
	 * @return The {@link DefaultXtextDirectEditorConfiguration} corresponding
	 *         the the edited object.
	 */
	protected DefaultXtextDirectEditorConfiguration getConfigurationFromEditedEObject(
			final Object row, final Object column) {
		if (row instanceof EObject && column instanceof EStructuralFeature || row instanceof EStructuralFeature && column instanceof EObject) {
			final EStructuralFeature feature = (EStructuralFeature) (column instanceof EStructuralFeature ? column : row);
			final EObject contextElement = (EObject) (row instanceof EObject ? row : column);
			final Object featureValue = contextElement.eGet(feature);
			
			if (featureValue instanceof EObject) {
				IDirectEditorConfiguration directEditorConfiguration = DirectEditorsUtil.findEditorConfiguration(null, (EObject) featureValue);
				if (directEditorConfiguration instanceof DefaultXtextDirectEditorConfiguration) {
					DefaultXtextDirectEditorConfiguration xtextConfiguration = (DefaultXtextDirectEditorConfiguration) directEditorConfiguration;
					xtextConfiguration.preEditAction(featureValue);
					return xtextConfiguration;
				}
			}
		}
		return null;
	}
}

Back to the top