Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2ffd90c0820bab5f87a9f98b63658e9cefee4ce (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.views.config.manager.cell;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource;
import org.eclipse.papyrus.infra.nattable.manager.cell.AbstractCellManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.utils.AxisUtils;
import org.eclipse.papyrus.infra.nattable.views.config.utils.Utils;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;

/**
 * The Cell manager for the ModelViews table
 * 
 * @author Vincent Lorenzo
 * 
 */
public class ModelViewsCellManager extends AbstractCellManager {



	/**
	 * 
	 * @param columnElement
	 *        the column element
	 * @param rowElement
	 *        the row element
	 * @return <code>null</code> or a list of 2 objects.
	 *         <ul>
	 *         <li>the first element is the edited EObject</li>
	 *         <li>the second one is the edited feature</li>
	 *         </ul>
	 * 
	 */
	protected List<Object> organizeAndResolvedObjects(final Object columnElement, final Object rowElement) {
		final List<Object> objects = new ArrayList<Object>();
		Object row = AxisUtils.getRepresentedElement(rowElement);
		Object column = AxisUtils.getRepresentedElement(columnElement);
		if(row instanceof String && ((String)row).startsWith(Utils.NATTABLE_EDITOR_PAGE_ID) && column instanceof EObject) {
			objects.add(column);
			objects.add(row);
		} else if(column instanceof String && ((String)column).startsWith(Utils.NATTABLE_EDITOR_PAGE_ID) && row instanceof EObject) {
			objects.add(row);
			objects.add(column);
		}

		if(objects.size() == 2) {
			final EObject first = (EObject)objects.get(0);
			final IPageManager mngr = Utils.getIPagneManager(first);
			if((mngr != null && !mngr.allPages().contains(first)) || mngr == null) {
				return null;
			}
		}

		if(objects.size() == 2) {
			return objects;
		}
		return null;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.manager.cell.ICellManager#handles(java.lang.Object, java.lang.Object)
	 * 
	 * @param rowElement
	 * @param columnElement
	 * @return
	 */
	public boolean handles(Object rowElement, Object columnElement) {
		return organizeAndResolvedObjects(columnElement, rowElement) != null;
	}


	/**
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.manager.cell.AbstractCellManager#doGetValue(java.lang.Object, java.lang.Object,
	 *      org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager)
	 * 
	 * @param columnElement
	 * @param rowElement
	 * @param tableManager
	 * @return
	 */
	@Override
	protected Object doGetValue(Object columnElement, Object rowElement, INattableModelManager tableManager) {
		final List<Object> objects = organizeAndResolvedObjects(columnElement, rowElement);
		final String featureName = ((String)objects.get(1)).replace(Utils.NATTABLE_EDITOR_PAGE_ID, ""); //$NON-NLS-1$
		final Object editor = objects.get(0);
		if(Utils.VIEW_NAME.equals(featureName)) {
			return getEditorName(editor);
		}
		if(Utils.VIEW_CONTEXT.equals(featureName)) {
			return getEditorContext(editor);
		}
		if(Utils.VIEW_IS_OPEN.equals(featureName)) {
			return getEditorIsOpen(editor);
		}
		if(Utils.VIEW_EDITOR_TYPE.equals(featureName)) {
			return getEditorType(editor);
		}
		return NOT_AVALAIBLE;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.manager.cell.ICellManager#isCellEditable(java.lang.Object, java.lang.Object)
	 * 
	 * @param rowElement
	 * @param columnElement
	 * @return
	 *         <code>true</code> excepted if the edited feature is isOpen
	 */
	public boolean isCellEditable(Object rowElement, Object columnElement) {
		final List<Object> objects = organizeAndResolvedObjects(columnElement, rowElement);
		final String featureName = ((String)objects.get(1)).replace(Utils.NATTABLE_EDITOR_PAGE_ID, ""); //$NON-NLS-1$
		return featureName.equals(Utils.VIEW_NAME);
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.manager.cell.AbstractCellManager#getSetValueCommand(org.eclipse.emf.transaction.TransactionalEditingDomain,
	 *      java.lang.Object, java.lang.Object, java.lang.Object, org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager)
	 * 
	 * @param domain
	 * @param columnElement
	 * @param rowElement
	 * @param newValue
	 * @param manager
	 * @return
	 */
	@Override
	public Command getSetValueCommand(final TransactionalEditingDomain domain, final Object columnElement, final Object rowElement, final Object newValue, final INattableModelManager manager) {
		final List<Object> objects = organizeAndResolvedObjects(columnElement, rowElement);
		final EObject editor = (EObject)objects.get(0);
		final String featureName = ((String)objects.get(1)).replace(Utils.NATTABLE_EDITOR_PAGE_ID, ""); //$NON-NLS-1$
		if(Utils.VIEW_NAME.equals(featureName)) {
			final EStructuralFeature feature = editor.eClass().getEStructuralFeature(Utils.VIEW_NAME);
			final AbstractEditCommandRequest request = new SetRequest((TransactionalEditingDomain)domain, editor, feature, newValue);
			final IElementEditService provider = ElementEditServiceUtils.getCommandProvider(editor);
			return new GMFtoEMFCommandWrapper(provider.getEditCommand(request));
		}
		return null;
	}

	/**
	 * 
	 * @param editor
	 *        an editor
	 * @return
	 *         the type of the editor
	 */
	protected Object getEditorType(final Object editor) {
		if(editor instanceof EObject) {
			if(editor instanceof Table) {
				return ((Table)editor).getTableConfiguration().getType();
			} else {
				EStructuralFeature feature = ((EObject)editor).eClass().getEStructuralFeature(Utils.VIEW_EDITOR_TYPE);
				if(feature != null) {
					return ((EObject)editor).eGet(feature);
				}
			}
		}
		return NOT_AVALAIBLE;
	}

	/**
	 * 
	 * @param editor
	 *        the editor
	 * @return
	 *         <code>true</code> if the current editor is open
	 */
	protected Object getEditorIsOpen(final Object editor) {
		if(editor instanceof EObject) {
			IPageManager mngr = null;
			try {
				mngr = ServiceUtilsForResource.getInstance().getIPageManager(((EObject)editor).eResource());
				return mngr.isOpen(editor);
			} catch (ServiceException e) {
				//				Activator.log.error(e);
			}
		}

		return NOT_AVALAIBLE;
	}

	/**
	 * 
	 * @param editor
	 *        an editor
	 * @return
	 *         the name of the editor
	 */
	protected Object getEditorName(final Object editor) {
		if(editor instanceof EObject) {
			if(editor instanceof EObject) {
				final EObject eobject = (EObject)editor;
				EStructuralFeature feature = eobject.eClass().getEStructuralFeature(Utils.VIEW_NAME);
				if(feature != null) {
					return eobject.eGet(feature);
				}
			}
		}
		return NOT_AVALAIBLE;
	}

	/**
	 * 
	 * @param editor
	 *        the editor
	 * @return
	 *         the context of this editor
	 */
	protected Object getEditorContext(final Object editor) {
		final Object result = Utils.getEditorContext(editor);
		if(result == null) {
			return NOT_AVALAIBLE;
		}
		return result;
	}

}

Back to the top