Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ebdaa5f3da53e6e2a871c9ee4e5a9493e84fee3 (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
248
249
250
251
252
253
/*****************************************************************************
 * 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.uml.nattable.editor;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;
import org.eclipse.papyrus.infra.nattable.manager.table.ITableAxisElementProvider;
import org.eclipse.papyrus.infra.nattable.model.nattable.IdAxis;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.editors.IElementSelector;
import org.eclipse.papyrus.infra.widgets.editors.MultipleValueSelectorDialog;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.uml.nattable.utils.UMLTableUtils;
import org.eclipse.papyrus.uml.tools.providers.UMLContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLLabelProvider;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;

/**
 * 
 * Abstract Cell Editor for MultiValued properties
 * 
 * @author Vincent Lorenzo
 * 
 */
public abstract class AbstractUMLMultiValueCellEditor extends AbstractDialogCellEditor {

	private Collection<Object> returnedValue;

	/**
	 * the table axis element provider
	 */
	protected ITableAxisElementProvider manager;

	/**
	 * the axis element
	 */
	private Object axisElement;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param axisElement
	 *        the axis element
	 * @param elementProvider
	 *        the table axis element provider
	 */
	protected AbstractUMLMultiValueCellEditor(final Object axisElement, final ITableAxisElementProvider elementProvider) {
		this.manager = elementProvider;
		this.axisElement = axisElement;
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor#open()
	 * 
	 * @return
	 */
	@Override
	public int open() {
		return ((MultipleValueSelectorDialog)dialog).open();
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor#createDialogInstance()
	 * 
	 * @return
	 */
	@Override
	public Object createDialogInstance() {
		int columnIndex = layerCell.getColumnIndex();
		int rowIndex = layerCell.getRowIndex();
		final Object row = this.manager.getRowElement(rowIndex);
		final Object column = this.manager.getColumnElement(columnIndex);
		Element editedElement = null;
		Object feature = null;
		if(row instanceof EObject && column == axisElement) {
			editedElement = (Element)row;
			feature = column;
		} else {
			editedElement = (Element)column;
			feature = row;
		}

		EStructuralFeature realFeature = null;
		EObject realEditedObject = null;
		Stereotype stereotype = null;
		List<Stereotype> stereotypesWithEditedFeatureAppliedOnElement = null;
		if(feature instanceof EStructuralFeature) {
			realFeature = (EStructuralFeature)feature;
			realEditedObject = editedElement;
		} else {
			String id = null;
			if(feature instanceof IdAxis) {
				id = ((IdAxis)feature).getElement();
			} else if(feature instanceof String) {
				id = (String)feature;
			}
			stereotypesWithEditedFeatureAppliedOnElement = UMLTableUtils.getAppliedSteretoypesWithThisProperty(editedElement, id);
			stereotype = stereotypesWithEditedFeatureAppliedOnElement.get(0);
			realEditedObject = editedElement.getStereotypeApplication(stereotypesWithEditedFeatureAppliedOnElement.get(0));
			Property prop = UMLTableUtils.getRealStereotypeProperty(editedElement, id);
			realFeature = realEditedObject.eClass().getEStructuralFeature(prop.getName());
		}
		if(stereotypesWithEditedFeatureAppliedOnElement != null && stereotypesWithEditedFeatureAppliedOnElement.size() > 1) {
			//FIXME : not yet managed
		} else {
			dialog = createDialog(realEditedObject, realFeature, stereotype, editedElement.eResource().getResourceSet());
		}
		return dialog;
	}

	/**
	 * 
	 * @param realEditedObject
	 *        the real edited object : the stereotype application or the edited Element
	 * @param realFeature
	 *        the real edited feature : the feature of the stereotype application or the the feature of the edited Element
	 * @param stereotype
	 *        the stereotype if we are are editing a stereotype application
	 * @param resourceSet
	 *        the resourceset
	 * @return
	 *         the dialog to edit the property
	 */
	protected Object createDialog(EObject realEditedObject, EStructuralFeature realFeature, Stereotype stereotype, ResourceSet resourceSet) {
		final UMLContentProvider p = new UMLContentProvider(realEditedObject, realFeature, stereotype, resourceSet);
		final String title = realFeature.getName();
		final boolean unique = realFeature.isUnique();
		final boolean ordered = realFeature.isOrdered();
		final int upperBound = realFeature.getUpperBound();
		final Object value = realEditedObject.eGet(realFeature);
		IElementSelector selector = getElementSelector(unique, new UMLLabelProvider(), p);
		final MultipleValueSelectorDialog dialog = new MultipleValueSelectorDialog(Display.getCurrent().getActiveShell(), selector, title, unique, ordered, upperBound) {

			@Override
			protected void okPressed() {
				super.okPressed();
				Collection<Object> newValue = new ArrayList<Object>();
				Object[] result = this.getResult();
				for(Object object : result) {
					newValue.add(object);

				}
				returnedValue = newValue;
				editHandler.commit(newValue, MoveDirectionEnum.NONE);
			}

		};
		dialog.setLabelProvider(new UMLLabelProvider());
		if(value != null && value instanceof Collection) {
			Collection<?> coll = (Collection<?>)value;
			if(!coll.isEmpty()) {
				dialog.setInitialSelections(coll.toArray());
			}
		}

		ReferenceValueFactory factory = getFactory();
		if(factory != null) {
			dialog.setFactory(factory);
		}

		return dialog;
	}

	/**
	 * 
	 * @return
	 *         the reference factory used to create to element
	 */
	protected abstract ReferenceValueFactory getFactory();

	/**
	 * 
	 * @param isUnique
	 * @param labelProvider
	 * @param contentProvider
	 * @return
	 *         the element selector to use in the dialog
	 */
	//FIXME : remove the label provider?!
	protected abstract IElementSelector getElementSelector(final boolean isUnique, final ILabelProvider labelProvider, final IStaticContentProvider contentProvider);

	@Override
	public Object getDialogInstance() {
		return dialog;
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor#getEditorValue()
	 * 
	 * @return
	 */
	@Override
	public Object getEditorValue() {
		return returnedValue;
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor#setEditorValue(java.lang.Object)
	 * 
	 * @param value
	 */
	@Override
	public void setEditorValue(Object value) {
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor#close()
	 * 
	 */
	@Override
	public void close() {
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor#isClosed()
	 * 
	 * @return
	 */
	@Override
	public boolean isClosed() {
		return false;
	}

}

Back to the top