Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b90f50877f5e976de949d87562a3bbbe2913ee1 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *****************************************************************************/

package org.eclipse.papyrus.uml.table.widget.celleditors.composite;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.impl.EModelElementImpl;
import org.eclipse.emf.facet.widgets.celleditors.AbstractCellEditorComposite;
import org.eclipse.emf.facet.widgets.celleditors.internal.Messages;
import org.eclipse.emf.facet.widgets.internal.CustomizableLabelProvider;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.papyrus.infra.widgets.editors.ReferenceCombo;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
import org.eclipse.papyrus.uml.profilefacet.metamodel.profilefacet.StereotypePropertyElement;
import org.eclipse.papyrus.uml.profilefacet.utils.StereotypePropertyUtils;
import org.eclipse.papyrus.uml.table.widget.celleditors.utils.StereotypePropertyFacetElementUtils;
import org.eclipse.papyrus.uml.table.widget.celleditors.utils.NoReferencedElement;
import org.eclipse.papyrus.uml.tools.providers.UMLContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Stereotype;

public class UnaryReferencePapyrusCellEditorComposite extends AbstractCellEditorComposite<EObject> {

	private static final int GRID_DATA_HEIGHT = 50;

	private final Button button;

	private final Combo combo;

	private final Control parentControl;

	private final EObject source;

	private final Map<String, EObject> fElements = new TreeMap<String, EObject>();


	private final EStructuralFeature feature;

	/**
	 * @param parent
	 *        the parent composite
	 * @param eObjects
	 *        the possible choices
	 */
	public UnaryReferencePapyrusCellEditorComposite(final Composite parent, final List<EObject> eObjects, final EObject source1, final EStructuralFeature feature1) {
		super(parent);
		assert source1 instanceof Element;
		this.source = StereotypePropertyFacetElementUtils.getRealSource(source1, feature1);
		this.feature = StereotypePropertyFacetElementUtils.getRealFeature(source1, feature1);
		Stereotype ste = null;
		if(feature1 instanceof StereotypePropertyElement) {
			ste = StereotypePropertyUtils.getAppliedStereotype((Element)source1, (StereotypePropertyElement)feature1);
		}
		this.parentControl = parent;
		final ILabelProvider labelProvider = new UMLLabelProvider();
		final ILabelProvider customizableLabelProvider = new CustomizableLabelProvider();
		final UMLContentProvider provider;
		if(ste != null) {
			provider = new UMLContentProvider(source, feature, ste, source.eResource().getResourceSet());
		} else {
			provider = new UMLContentProvider(source1, feature1, null, source.eResource().getResourceSet());
		}

		EObject noReferencedElement = null;

		for(EObject eObject : eObjects) {
			String label;
			if(eObject instanceof NoReferencedElement) {
				label = eObject.toString();
				noReferencedElement = eObject;
			} else {
				label = customizableLabelProvider.getText(eObject);;
			}
			// find a unique label
			if(this.fElements.get(label) != null) {
				int suffix = 2;
				while(this.fElements.get(label + " (" + suffix + ")") != null) { //$NON-NLS-1$ //$NON-NLS-2$
					suffix++;
				}
				this.fElements.put(label + " (" + suffix + ")", eObject); //$NON-NLS-1$//$NON-NLS-2$
			} else {
				this.fElements.put(label, eObject);
			}
		}

		GridLayout compositeLayout = new GridLayout(2, false);
		compositeLayout.marginHeight = 0;
		compositeLayout.marginWidth = 0;
		compositeLayout.horizontalSpacing = 0;
		setLayout(compositeLayout);

		this.combo = new Combo(this, SWT.DROP_DOWN);

		// reduce the font so that the Combo fits in the cell
		FontData[] fontData = Display.getDefault().getSystemFont().getFontData();
		fontData[0].setHeight(fontData[0].getHeight()/* - 2 */);
		Font font = new Font(Display.getDefault(), fontData);
		this.combo.setFont(font);
		for(String label : this.fElements.keySet()) {
			this.combo.add(label);
		}

		//we force the null value to the first place
		if(noReferencedElement != null) {
			this.combo.remove(noReferencedElement.toString());
			this.combo.add(noReferencedElement.toString(), 0);
		}

		GridData comboGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		this.combo.setLayoutData(comboGridData);
		addCompletionHandler(this.combo, this.fElements.keySet());

		//we set the initial value in the combo : 
		Object currentValue = this.source.eGet(this.feature);
		if(currentValue == null) {
			if(noReferencedElement != null) {
				int index = this.combo.indexOf(noReferencedElement.toString());
				if(index > -1) {
					this.combo.select(index);
				}
			}
		} else {
			final String txt = customizableLabelProvider.getText(currentValue);
			int index = this.combo.indexOf(txt);
			if(index > -1) {
				this.combo.select(index);
			}
		}
		this.combo.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(final KeyEvent event) {
				// Enter key pressed
				if((event.keyCode == SWT.CR && event.stateMask == 0) || (event.keyCode == SWT.KEYPAD_CR && event.stateMask == 0)) {
					if(isValid()) {
						commit();
					} else {
						MessageDialog.openWarning(parent.getShell(), Messages.UnaryReferenceCellEditorComposite_0, Messages.UnaryReferenceCellEditorComposite_1);
					}
				} else if(event.keyCode == SWT.ESC && event.stateMask == 0) {
					// Escape key pressed
					close();
				}
			}
		});

		this.button = new Button(this, SWT.PUSH);
		this.button.setText("..."); //$NON-NLS-1$
		GridData buttonGridData = new GridData(SWT.FILL, SWT.FILL, false, true);
		buttonGridData.heightHint = UnaryReferencePapyrusCellEditorComposite.GRID_DATA_HEIGHT;
		this.button.setLayoutData(buttonGridData);
		this.button.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				TreeSelectorDialog dialog = new TreeSelectorDialog(Display.getCurrent().getActiveShell()) {

					@Override
					public void okPressed() {
						Object selectedElements[] = new Object[this.getResult().length];
						Object result[] = this.getResult();
						for(int i = 0; i < result.length; i++) {
							Object adapted = provider.getAdaptedValue(result[i]);
							selectedElements[i] = adapted;
						}
						String selectedElementLabel = null;

						if(selectedElements.length > 0) {
							Set<Entry<String, EObject>> entrySet = UnaryReferencePapyrusCellEditorComposite.this.getfElements().entrySet();
							for(Entry<String, EObject> entry : entrySet) {
								if(entry.getValue() == selectedElements[0]) {
									selectedElementLabel = entry.getKey();
									break;
								}
							}
						}
						UnaryReferencePapyrusCellEditorComposite.this.getCombo().setText(selectedElementLabel);
						commit();
						super.okPressed();
						super.close();
					}

				};


				dialog.setLabelProvider(labelProvider);
				dialog.setContentProvider(provider);
				if(source != null) {
					dialog.setInitialElementSelections(Collections.singletonList(source.eGet(feature)));
				}

				dialog.setTitle(feature.getEType().getName());

				dialog.open();

			}
		});

		// commit the cell editor when the mouse is clicked
		// anywhere outside the text field
		final Listener clickListener = new Listener() {

			public void handleEvent(final Event event) {
				if(event.widget instanceof Control) {
					Control control = (Control)event.widget;
					if(control.getShell() == UnaryReferencePapyrusCellEditorComposite.this.getParentControl().getShell() && event.widget != UnaryReferencePapyrusCellEditorComposite.this.getButton() && event.widget != UnaryReferencePapyrusCellEditorComposite.this.getCombo()) {
						if(isValid()) {
							commit();
						}
					}
				}

			}
		};
		Display.getDefault().addFilter(SWT.MouseDown, clickListener);

		// this listener is only here to remove
		// the other listener from the Display
		addDisposeListener(new DisposeListener() {

			public void widgetDisposed(final DisposeEvent e) {
				Display.getDefault().removeFilter(SWT.MouseDown, clickListener);
			}
		});

		this.combo.addModifyListener(new ModifyListener() {

			public void modifyText(final ModifyEvent e) {
				fireChanged();
			}
		});



		this.combo.forceFocus();
	}

	protected boolean isValid() {
		return this.fElements.get(this.combo.getText()) != null;
	}

	private static void addCompletionHandler(final Combo comboBox, final Collection<String> completions) {
		String[] completionsArray = completions.toArray(new String[completions.size()]);
		SimpleContentProposalProvider contentProposalProvider = new SimpleContentProposalProvider(completionsArray);
		contentProposalProvider.setFiltering(true);
		ContentProposalAdapter adapter = new ContentProposalAdapter(comboBox, new ComboContentAdapter(), contentProposalProvider, null, null);
		// adapter.setFilterStyle(ContentProposalAdapter.FILTER_CHARACTER);
		adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

	}

	/**
	 * 
	 * @see org.eclipse.emf.facet.widgets.celleditors.IWidget#getValue()
	 * 
	 * @return
	 */
	public EObject getValue() {
		return this.fElements.get(this.combo.getText());
	}

	protected void commit() {
		fireCommit();
	}


	protected Button getButton() {
		return this.button;
	}

	protected Combo getCombo() {
		return this.combo;
	}

	protected Control getParentControl() {
		return this.parentControl;
	}

	protected Map<String, EObject> getfElements() {
		return this.fElements;
	}

	public void setValue(EObject value) {
		//not used
	}
}

Back to the top