Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 032c7cfa743f273e88d1074b4ac0217b6530c11e (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * 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 FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.customization.nattableconfiguration.edition;

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

import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.customization.nattableconfiguration.utils.NattableConfigurationConstants;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.PasteEObjectConfiguration;
import org.eclipse.papyrus.infra.nattable.utils.StringComparator;
import org.eclipse.papyrus.infra.services.edit.utils.ElementTypeUtils;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.editors.ITreeSelectorDialog;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
import org.eclipse.papyrus.infra.widgets.providers.CompoundFilteredRestrictedContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.FlattenableRestrictedFilteredContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IRestrictedContentProvider;
import org.eclipse.papyrus.infra.widgets.selectors.ReferenceSelector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
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.Point;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

/**
 * The dialog which allow to edit the paste configuration features.
 */
public class PasteEObjectConfigurationDialog extends SelectionDialog {

	/**
	 * A copy of initial paste configuration to modify with this dialog.
	 */
	protected final PasteEObjectConfiguration modifiedPasteConfiguration;

	/**
	 * The label provider for the UML elements.
	 */
	protected final ILabelProvider labelProvider;

	/**
	 * The CLabel composite for the containment feature.
	 */
	protected CLabel containmentFeatureText;

	/**
	 * The browse button which allow to edit the containment feature.
	 */
	protected Button browseValuesButton;

	/**
	 * The delete button which allow to unset the containment feature value.
	 */
	protected Button deleteValuesButton;

	/**
	 * Constructor.
	 *
	 * @param parentShell
	 *            The parent shell of the dialog.
	 * @param pasteConfiguration
	 *            The initial paste configuration to edit.
	 */
	public PasteEObjectConfigurationDialog(final Shell parentShell, final PasteEObjectConfiguration pasteConfiguration) {
		super(parentShell);
		this.modifiedPasteConfiguration = EcoreUtil.copy(pasteConfiguration);
		setTitle(org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages.PasteEObjectConfigurationDialog_pasteEObjectConfigurationDialogName);
		
		LabelProviderService labelProviderService = new LabelProviderServiceImpl();
		try {
			labelProviderService.startService();
		} catch (ServiceException ex) {
			Activator.log.error(ex);
		}

		labelProvider = labelProviderService.getLabelProvider();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#create()
	 */
	@Override
	public void create() {
		super.create();
		getShell().setImage(Activator.getDefault().getImage(NattableConfigurationConstants.PAPYRUS_ICON_PATH));

		final Composite parent = new Composite((Composite) getDialogArea(), SWT.NONE);
		parent.setLayout(new GridLayout(4, false));
		parent.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		getShell().setSize(600, 400);

		// Create the fields
		createFields(parent);

		getShell().pack();
		Point size = getShell().getSize();
		int minWidth = 600;
		if (size.x < minWidth) {
			size.x = minWidth;
		}
		getShell().setSize(size);
	}

	/**
	 * This allows to create the fields to edit the paste configuration.
	 * 
	 * @param parent
	 *            The parent composite.
	 */
	protected void createFields(final Composite parent) {
		// Create the detached mode checkbox
		final Label detachedModeLabel = new Label(parent, SWT.NONE);
		detachedModeLabel.setText(org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages.PasteEObjectConfigurationDialog_detachedModeLabel);
		detachedModeLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		final Button detachedModeButton = new Button(parent, SWT.CHECK);
		detachedModeButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 3, 1));
		detachedModeButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				modifiedPasteConfiguration.setDetachedMode(detachedModeButton.getSelection());
			}
		});
		detachedModeButton.setSelection(modifiedPasteConfiguration.isDetachedMode());

		// Create the pasted element id combo box
		final Label pastedElementIdLabel = new Label(parent, SWT.NONE);
		pastedElementIdLabel.setText(org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages.PasteEObjectConfigurationDialog_pastedElementIdLabel);
		pastedElementIdLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		final Combo pastedElementIdCombo = new Combo(parent, SWT.READ_ONLY);
		pastedElementIdCombo.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 3, 1));
		pastedElementIdCombo.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				modifiedPasteConfiguration.setPastedElementId(pastedElementIdCombo.getText());
			}
		});

		final Collection<IElementType> possibleValues = ElementTypeUtils.getAllExistingElementTypes();
		final List<String> possibleValuesAsString = new ArrayList<String>(possibleValues.size());

		int indexToSelect = -1;
		int index = 0;

		for (final IElementType elementType : possibleValues) {
			possibleValuesAsString.add(elementType.getDisplayName());
			if (elementType.getDisplayName().equals(modifiedPasteConfiguration.getPastedElementId())) {
				indexToSelect = index;
			}
			index++;
		}

		pastedElementIdCombo.setItems((String[]) possibleValuesAsString.toArray(new String[possibleValuesAsString.size()]));
		if (-1 != indexToSelect) {
			pastedElementIdCombo.select(indexToSelect);
		}

		// Create the containment feature labels and dialog
		final Label containmentFeatureLabel = new Label(parent, SWT.NONE);
		containmentFeatureLabel.setText(org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages.PasteEObjectConfigurationDialog_containmentFeatureLabel);
		containmentFeatureLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		containmentFeatureText = new CLabel(parent, SWT.BORDER);
		containmentFeatureText.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		browseValuesButton = new Button(parent, SWT.PUSH);
		browseValuesButton.setImage(Activator.getDefault().getImage(NattableConfigurationConstants.BROWSE_ICON_PATH));
		browseValuesButton.setToolTipText(org.eclipse.papyrus.infra.widgets.messages.Messages.ReferenceDialog_EditValue);
		browseValuesButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				browseAction(modifiedPasteConfiguration.getPasteElementContainementFeature());
			}
		});

		deleteValuesButton = new Button(parent, SWT.PUSH);
		deleteValuesButton.setImage(Activator.getDefault().getImage(NattableConfigurationConstants.DELETE_ICON_PATH));
		deleteValuesButton.setToolTipText(org.eclipse.papyrus.infra.widgets.messages.Messages.ReferenceDialog_UnsetValue);
		deleteValuesButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				setPasteElementContainementFeature(null);
			}
		});

		setPasteElementContainementFeature(modifiedPasteConfiguration.getPasteElementContainementFeature());

		// TODO : Post actions
	}

	/**
	 * The action executed when the "browse" button is selected Choose a value
	 * from a selection of already created objects.
	 * 
	 * @param value
	 *            The initial value of the containment feature.
	 */
	protected void browseAction(final Object value) {
		ITreeSelectorDialog dialog = new TreeSelectorDialog(getShell()) {
			@Override
			protected void initViewerAndProvider() {
				super.initViewerAndProvider();
				// Set a comparator to sort the tree viewer
				getViewer().setComparator(new ViewerComparator(new StringComparator()));// should always be string element
			}
		};
		dialog.setTitle(org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages.PasteEObjectConfigurationDialog_containmentFeatureDialogName);

		final CompoundFilteredRestrictedContentProvider contentProvider = new CompoundFilteredRestrictedContentProvider();
		final ReferenceSelector selector = new ReferenceSelector(false) {

			@Override
			public void createControls(final Composite parent) {
				super.createControls(parent);
				this.treeViewer.setComparator(new ViewerComparator(new StringComparator()));// should always be string element
			}
		};

		selector.setLabelProvider(labelProvider);
		selector.setContentProvider(contentProvider);
		final ITreeContentProvider treeContentProvider = new FlattenableRestrictedFilteredContentProvider((IRestrictedContentProvider) contentProvider, selector) {

			@Override
			public boolean isValidValue(final Object element) {
				// EMF dependency, must not be done here, it should be better with a new content provider service
				return element instanceof EReference && ((EReference) element).isMany() && element != EcorePackage.eINSTANCE.getEModelElement_EAnnotations();
			}
		};

		dialog.setContentProvider(treeContentProvider);
		dialog.setLabelProvider(labelProvider);
		if (null != value) {
			dialog.setInitialElementSelections(Collections.singletonList(value));
		}
		int result = dialog.open();
		if (Window.OK == result) {
			Object[] newValue = dialog.getResult();
			if (null == newValue) {
				return;
			}

			if (0 == newValue.length) {
				setPasteElementContainementFeature(null);
			} else {
				Object selectedValue = newValue[0];
				setPasteElementContainementFeature((EStructuralFeature) selectedValue);
			}
		}
	}

	/**
	 * This allows to set the containment feature and refresh the button corresponding to the containment feature value.
	 * 
	 * @param structuralFeature
	 *            The structural feature to set.
	 */
	protected void setPasteElementContainementFeature(final EStructuralFeature structuralFeature) {
		if (null == modifiedPasteConfiguration.getPasteElementContainementFeature() || !structuralFeature.equals(modifiedPasteConfiguration.getPasteElementContainementFeature())) {
			modifiedPasteConfiguration.setPasteElementContainementFeature(structuralFeature);
		}
		containmentFeatureText.setImage(labelProvider.getImage(modifiedPasteConfiguration.getPasteElementContainementFeature()));
		containmentFeatureText.setText(labelProvider.getText(modifiedPasteConfiguration.getPasteElementContainementFeature()));

		deleteValuesButton.setEnabled(null != structuralFeature);
	}

	/**
	 * Returns the modified paste configuration.
	 * 
	 * @return The modified paste configuration.
	 */
	public PasteEObjectConfiguration getModifiedPasteEObjectConfiguration() {
		return modifiedPasteConfiguration;
	}

}

Back to the top