Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b3c49759cbb772c6897bc5d15ec6afd4d1990ed (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
/*****************************************************************************
 * Copyright (c) 2010 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 Bros (Mia-Software) - initial API and implementation
 *   Nicolas Guyomar (Mia-Software) - initial API and implementation
 *   Nicolas Bros (Mia-Software) - Bug 339664 - org.eclipse.papyrus.emf.facet.widgets.celleditors API cleaning
 *   Nicolas Bros (Mia-Software) - Bug 334539 - [celleditors] change listener
 *   Grégoire Dupé (Mia-Software) - Bug 424122 - [Table] Images, fonts and colors are not shared between the instances of table
 *****************************************************************************/
package org.eclipse.papyrus.emf.facet.widgets.celleditors.internal.core.composite;

import java.util.Collection;
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.jface.dialogs.MessageDialog;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.papyrus.emf.facet.util.swt.fontprovider.IFontProvider;
import org.eclipse.papyrus.emf.facet.util.swt.fontprovider.IFontProviderFactory;
import org.eclipse.papyrus.emf.facet.widgets.celleditors.AbstractCellEditorComposite;
import org.eclipse.papyrus.emf.facet.widgets.celleditors.internal.Messages;
import org.eclipse.papyrus.emf.facet.widgets.internal.CustomizableLabelProvider;
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.ui.dialogs.ElementListSelectionDialog;

/** A cell editor for unary references */
public class UnaryReferenceCellEditorComposite 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 Map<String, EObject> fElements = new TreeMap<String, EObject>();

	/**
	 * @param parent
	 *            the parent composite
	 * @param eObjects
	 *            the possible choices
	 */
	public UnaryReferenceCellEditorComposite(final Composite parent, final List<EObject> eObjects) {
		super(parent);
		this.parentControl = parent;

		CustomizableLabelProvider customizableLabelProvider = new CustomizableLabelProvider();
		for (EObject eObject : eObjects) {
			String 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);
		final IFontProvider fontProvider = IFontProviderFactory.DEFAULT
				.getOrCreateIFontProvider(Display.getDefault());
		final Font font = fontProvider.getFont(fontData[0]);
		this.combo.setFont(font);
		for (String label : this.fElements.keySet()) {
			this.combo.add(label);
		}
		GridData comboGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		this.combo.setLayoutData(comboGridData);
		addCompletionHandler(this.combo, this.fElements.keySet());

		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 = UnaryReferenceCellEditorComposite.GRID_DATA_HEIGHT;
		this.button.setLayoutData(buttonGridData);
		this.button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				ElementListSelectionDialog dialog = new ElementListSelectionDialog(
						UnaryReferenceCellEditorComposite.this.getParentControl().getShell(),
						new CustomizableLabelProvider()) {
					@Override
					protected void okPressed() {
						Object[] selectedElements = getSelectedElements();
						String selectedElementLabel = null;
						if (selectedElements.length > 0) {
							Set<Entry<String, EObject>> entrySet = UnaryReferenceCellEditorComposite.this
									.getfElements().entrySet();
							for (Entry<String, EObject> entry : entrySet) {
								if (entry.getValue() == selectedElements[0]) {
									selectedElementLabel = entry.getKey();
									break;
								}
							}
						}
						UnaryReferenceCellEditorComposite.this.getCombo().setText(
								selectedElementLabel);
						commit();
						super.okPressed();
					}
				};
				dialog.setTitle(Messages.UnaryReferenceCellEditorComposite_2);
				dialog.setElements(eObjects.toArray());
				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() == UnaryReferenceCellEditorComposite.this
							.getParentControl().getShell()
							&& event.widget != UnaryReferenceCellEditorComposite.this.getButton()
							&& event.widget != UnaryReferenceCellEditorComposite.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);

	}

	public EObject getValue() {
		EObject eObject = this.fElements.get(this.combo.getText());
		return eObject;
	}

	protected void commit() {
		fireCommit();
	}

	public void setValue(final EObject value) {
		//
	}

	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;
	}

}

Back to the top