Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a140931f8d22b1955a472e20b9c3473d7f199f0 (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
/*****************************************************************************
 * Copyright (c) 2014, 2015 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.profile.elementtypesconfigurations.generator.ui.internal.wizards;

import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jface.util.Policy;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.nebula.jface.tablecomboviewer.TableComboViewer;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.elementtypesconfigurations.ElementTypeSetConfiguration;
import org.eclipse.papyrus.infra.elementtypesconfigurations.registries.ElementTypeSetConfigurationRegistry;
import org.eclipse.papyrus.uml.profile.elementtypesconfigurations.generator.ui.internal.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Ordering;

/**
 * @author damus
 *
 */
class BaseElementTypeSetBlock {

	private static final String DIAGRAM = "diagram"; //$NON-NLS-1$

	private static final String SUPPRESS_SEMANTIC_SUPERTYPES = "suppressSemanticSupertypes"; //$NON-NLS-1$

	private static final String UML_ELEMENT_TYPE_SET = "org.eclipse.papyrus.uml.service.types.UMLElementTypeSet"; //$NON-NLS-1$

	private static final int COLUMN_NAME = 0;
	private static final int COLUMN_LOCATION = 1;

	private final GeneratorWizardModel model;

	private final BiMap<String, ElementTypeSetConfiguration> elementTypeSets;

	public BaseElementTypeSetBlock(GeneratorWizardModel model) {
		super();

		this.model = model;
		this.elementTypeSets = HashBiMap.create(ElementTypeSetConfigurationRegistry.getInstance().getElementTypeSetConfigurations());
	}

	public void createControl(Composite parent) {
		new Label(parent, SWT.NONE).setText("Base element types set:");

		// Use a two-column table because styled label providers don't work in the TableComboViewer
		TableComboViewer combo = new TableComboViewer(parent, SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL);
		combo.getTableCombo().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		combo.getTableCombo().defineColumns(2);
		combo.getTableCombo().setDisplayColumnIndex(0);

		combo.setContentProvider(ArrayContentProvider.getInstance());
		combo.setLabelProvider(new DiagramLabelProvider(parent));

		// TableCombo doesn't use a ViewerComparator even when one is set
		combo.setInput(getOrdering().sortedCopy(elementTypeSets.values()));

		final Button suppressSemanticSuperElementTypes = new Button(parent, SWT.CHECK);
		suppressSemanticSuperElementTypes.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, false, false, 2, 1));
		suppressSemanticSuperElementTypes.setText("Suppress semantic parent in diagram-specific element types");
		suppressSemanticSuperElementTypes.setSelection(model.getDialogSettings().getBoolean(SUPPRESS_SEMANTIC_SUPERTYPES));

		ElementTypeSetConfiguration initialSelection = getInitialSelection();
		if (initialSelection != null) {
			combo.setSelection(new StructuredSelection(initialSelection));
			elementTypeSetSelectionChanged((IStructuredSelection) combo.getSelection());
		}
		combo.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				elementTypeSetSelectionChanged((IStructuredSelection) event.getSelection());
				suppressSemanticSuperElementTypes.setEnabled(!UML_ELEMENT_TYPE_SET.equals(model.getSelectedElementTypeSet()));
				model.validatePage();
			}
		});

		setSuppressSemanticSupertypes(suppressSemanticSuperElementTypes.getSelection());
		suppressSemanticSuperElementTypes.setEnabled(!UML_ELEMENT_TYPE_SET.equals(model.getSelectedElementTypeSet()));
		suppressSemanticSuperElementTypes.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setSuppressSemanticSupertypes(suppressSemanticSuperElementTypes.getSelection());
			}
		});
	}

	void elementTypeSetSelectionChanged(IStructuredSelection selection) {
		if (selection.isEmpty()) {
			setElementTypeSet(null);
		} else {
			setElementTypeSet((ElementTypeSetConfiguration) selection.getFirstElement());
		}
	}

	void setElementTypeSet(ElementTypeSetConfiguration elementTypeSet) {
		model.setSelectedElementTypeSet((elementTypeSet == null) ? null : elementTypeSets.inverse().get(elementTypeSet));
	}

	void setSuppressSemanticSupertypes(boolean suppress) {
		model.setSuppressSemanticSuperElementTypes(suppress);
	}

	void save() {
		model.getDialogSettings().put(DIAGRAM, model.getSelectedElementTypeSet());
		model.getDialogSettings().put(SUPPRESS_SEMANTIC_SUPERTYPES, model.isSuppressSemanticSuperElementTypes());
	}

	private ElementTypeSetConfiguration getInitialSelection() {
		ElementTypeSetConfiguration result = null;

		String id = model.getDialogSettings().get(DIAGRAM);
		if (id != null) {
			result = elementTypeSets.get(id);
		}

		if (result == null) {
			result = elementTypeSets.get(UML_ELEMENT_TYPE_SET);
		}

		return result;
	}

	static String getName(ElementTypeSetConfiguration elementTypeSet) {
		String result = elementTypeSet.getName();

		if (Strings.isNullOrEmpty(result)) {
			// Infer a name from the URI
			URI uri = elementTypeSet.eResource().getURI();
			result = uri.trimFileExtension().lastSegment();
		}

		return result;
	}

	private static Function<ElementTypeSetConfiguration, String> getNameFunction() {
		return new Function<ElementTypeSetConfiguration, String>() {
			@Override
			public String apply(ElementTypeSetConfiguration input) {
				return getName(input);
			}
		};
	}

	static String getLocation(ElementTypeSetConfiguration elementTypeSet) {
		URI uri = elementTypeSet.eResource().getURI();
		String result;

		if (uri.isPlatformResource()) {
			result = uri.trimSegments(1).toPlatformString(true);
		} else if (uri.isPlatformPlugin()) {
			result = uri.segment(1);
		} else {
			result = uri.toString();
		}

		return result;
	}

	private static Function<ElementTypeSetConfiguration, String> getLocationFunction() {
		return new Function<ElementTypeSetConfiguration, String>() {
			@Override
			public String apply(ElementTypeSetConfiguration input) {
				return getLocation(input);
			}
		};
	}

	static Ordering<ElementTypeSetConfiguration> getOrdering() {
		Ordering<Object> strings = Ordering.from(Policy.getComparator());

		return strings.onResultOf(getNameFunction()).compound(strings.onResultOf(getLocationFunction()));
	}

	//
	// Nested types
	//

	private static class DiagramLabelProvider extends LabelProvider implements IStyledLabelProvider, ITableLabelProvider, ITableColorProvider {
		private ResourceManager images;

		DiagramLabelProvider(Control owner) {
			super();

			// Because we specify an owner, the owner will take care of clean-up, so we
			// don't need a dispose() method of our own
			images = new LocalResourceManager(JFaceResources.getResources(), owner);
		}

		@Override
		public StyledString getStyledText(Object element) {
			return new StyledString(getColumnText(element, COLUMN_NAME)) //
					.append(NLS.bind(" - {0}", getColumnText(element, COLUMN_LOCATION)), StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
		}

		@Override
		public String getColumnText(Object element, int column) {
			ElementTypeSetConfiguration elementTypeSet = (ElementTypeSetConfiguration) element;
			String result;

			switch (column) {
			case COLUMN_NAME:
				result = getName(elementTypeSet);
				break;
			case COLUMN_LOCATION:
				result = getLocation(elementTypeSet);
				break;
			default:
				throw new IllegalArgumentException("no such column: " + column); //$NON-NLS-1$
			}

			return result;
		}

		@Override
		public Image getColumnImage(Object element, int column) {
			Image result = null;

			if (column == COLUMN_NAME) {
				ElementTypeSetConfiguration set = (ElementTypeSetConfiguration) element;
				URI uri = set.eResource().getURI();

				if (uri.isPlatformPlugin()) {
					result = (Image) images.get(Activator.getInstance().getIcon("obj16/plugin.gif")); //$NON-NLS-1$
				} else if (uri.isPlatformResource()) {
					result = (Image) images.get(Activator.getInstance().getIcon("obj16/project.png")); //$NON-NLS-1$
				}
			}

			return result;
		}

		@Override
		public Color getForeground(Object element, int column) {
			Color result = null;

			if (column == COLUMN_LOCATION) {
				result = JFaceResources.getColorRegistry().get(JFacePreferences.QUALIFIER_COLOR);
			}

			return result;
		}

		@Override
		public Color getBackground(Object element, int columnIndex) {
			return null;
		}
	}
}

Back to the top