Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c8227a6dc2246213e7d6ffb194960ed8c8648b2 (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *   Andreas Muelder - Initial contribution and API
 *   Fanch BONNABESSE (ALL4TEC) fanch.bonnabesse@all4tec.net - Bug 497289
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.modelexplorer;

import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.papyrus.extensionpoints.editors.Activator;
import org.eclipse.papyrus.extensionpoints.editors.configuration.ICustomDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.utils.DirectEditorsUtil;
import org.eclipse.papyrus.extensionpoints.editors.utils.IDirectEditorsIds;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.swt.widgets.Composite;

/**
 * Support for direct editors
 */
public class DirectEditorEditingSupport extends EditingSupport {

	public DirectEditorEditingSupport(ColumnViewer viewer) {
		super(viewer);
	}

	@Override
	protected CellEditor getCellEditor(final Object element) {
		ICustomDirectEditorConfiguration configuration = getConfigurationAE(element);
		EObject semanticObject = EMFHelper.getEObject(element);
		Composite parent = (Composite) getViewer().getControl();
		configuration.preEditAction(semanticObject);
		return configuration.createCellEditor(parent, semanticObject);
	}

	@Override
	protected boolean canEdit(Object element) {
		return getConfigurationAE(element) != null;
	}

	@Override
	protected Object getValue(Object element) {
		ICustomDirectEditorConfiguration configuration = getConfigurationAE(element);
		Object semanticObject = EMFHelper.getEObject(element);
		return configuration.createParser((EObject) semanticObject)
				.getEditString(new EObjectAdapter((EObject) semanticObject), 0);
	}

	@Override
	protected void setValue(Object element, Object value) {
		ICustomDirectEditorConfiguration configuration = getConfigurationAE(element);
		EObject semanticObject = EMFHelper.getEObject(element);
		IParser parser = configuration.createParser(semanticObject);

		ICommand command = parser.getParseCommand(new EObjectAdapter(
				semanticObject), (String) value, 0);
		TransactionalEditingDomain editingDomain = TransactionUtil
				.getEditingDomain(semanticObject);
		editingDomain.getCommandStack().execute(
				new GMFtoEMFCommandWrapper(command));
	}

	/**
	 * Obtain direct editor configuration for an element that can be adapted to
	 * an EObject
	 *
	 * @param element
	 *            an adaptable element
	 * @return The direct editor configuration, if it exists.
	 */
	public static ICustomDirectEditorConfiguration getConfigurationAE(final Object element) {
		EObject semanticObject = EMFHelper.getEObject(element);
		return getConfiguration(semanticObject);
	}

	/**
	 * Obtain direct editor configuration for a semantic element
	 *
	 * @param semanticElement
	 *            a semantic element
	 * @return The direct editor configuration, if it exists.
	 */
	public static ICustomDirectEditorConfiguration getConfiguration(final EObject semanticElement) {
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();

		if (null != semanticElement) {
			EClass eClass = semanticElement.eClass();
			String semanticClassName = eClass.getInstanceClassName();
			String key = IDirectEditorsIds.EDITOR_FOR_ELEMENT + semanticClassName;
			String languagePreferred = store.getString(key);

			IDirectEditorConfiguration configuration = null;

			if (null != languagePreferred && !languagePreferred.isEmpty()) {
				configuration = DirectEditorsUtil.findEditorConfiguration(languagePreferred, semanticElement, semanticElement);
			} else {
				configuration = getConfigurationSuperType(eClass, semanticElement);
			}

			if (configuration instanceof ICustomDirectEditorConfiguration) {
				return (ICustomDirectEditorConfiguration) configuration;
			}
		}

		return null;
	}

	/**
	 * Obtain an editor configuration compatible with a superType of an EClass.
	 * 
	 * @param basedEClass
	 *            The based EClass.
	 * @param semanticElement
	 *            The renamed semantic element.
	 * @return The editor configuration, if it exists.
	 */
	public static IDirectEditorConfiguration getConfigurationSuperType(final EClass basedEClass, final EObject semanticElement) {
		IDirectEditorConfiguration configuration = null;
		List<EClass> eClasses = basedEClass.getESuperTypes();

		if (null != eClasses && !eClasses.isEmpty()) {
			IPreferenceStore store = Activator.getDefault().getPreferenceStore();
			int i = 0;
			while (i < eClasses.size() && null == configuration) {
				EClass eClass = eClasses.get(i);
				String semanticClassName = eClass.getInstanceClassName();
				String key = IDirectEditorsIds.EDITOR_FOR_ELEMENT + semanticClassName;
				String languagePreferred = store.getString(key);

				// If language has found, get the corresponding configuration.
				if (languagePreferred != null && !languagePreferred.isEmpty()) {
					configuration = DirectEditorsUtil.findEditorConfiguration(languagePreferred, semanticElement, semanticElement);
					if (!configuration.isSuperType()) {
						configuration = getConfigurationSuperType(eClass, semanticElement);
					}
				} else {
					configuration = getConfigurationSuperType(eClass, semanticElement);
				}

				i++;
			}
		}

		return configuration;
	}
}

Back to the top