Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f451e3c1e4691de9bcc0bcb6f6e88a550343dbb (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 422257
 *
 *****************************************************************************/
package org.eclipse.papyrus.customization.properties.generation.wizard;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.papyrus.customization.properties.generation.Activator;
import org.eclipse.papyrus.customization.properties.generation.fieldselection.ContextElement;
import org.eclipse.papyrus.customization.properties.generation.fieldselection.FieldSelection;
import org.eclipse.papyrus.customization.properties.generation.fieldselection.PropertyDefinition;
import org.eclipse.papyrus.customization.properties.generation.generators.IGenerator;
import org.eclipse.papyrus.customization.properties.generation.layout.ILayoutGenerator;
import org.eclipse.papyrus.customization.properties.generation.messages.Messages;
import org.eclipse.papyrus.customization.properties.generation.wizard.widget.TernaryButton;
import org.eclipse.papyrus.views.properties.contexts.Context;
import org.eclipse.papyrus.views.properties.contexts.ContextsFactory;
import org.eclipse.papyrus.views.properties.contexts.DataContextElement;
import org.eclipse.papyrus.views.properties.contexts.Property;
import org.eclipse.papyrus.views.properties.contexts.Section;
import org.eclipse.papyrus.views.properties.contexts.Tab;
import org.eclipse.papyrus.views.properties.contexts.View;
import org.eclipse.papyrus.views.properties.runtime.ConfigurationManager;
import org.eclipse.papyrus.views.properties.ui.PropertyEditor;
import org.eclipse.papyrus.views.properties.ui.UiFactory;
import org.eclipse.papyrus.views.properties.ui.ValueAttribute;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

/**
 * A Wizard for generating Property view contexts
 *
 * @author Camille Letavernier
 */
public class CreateContextWizard extends Wizard implements INewWizard {

	protected CreateContextMainPage mainPage;

	protected GeneratorPage generatorPage;

	protected SelectOutputPage selectOutputPage;

	// protected LayoutPage layout;

	protected SelectFieldsPage selectFieldsPage;

	/**
	 * The generated contexts
	 */
	protected List<Context> contexts;

	/**
	 * The IGenerator used to generate the context
	 */
	protected IGenerator generator;

	/**
	 * The ILayoutGenerator used to layout the context's sections
	 */
	protected ILayoutGenerator layoutGenerator;

	public CreateContextWizard() {
		setDefaultPageImageDescriptor(Activator.getDefault().getImageDescriptor("/icons/wizban-custom.png")); //$NON-NLS-1$
	}

	@Override
	public boolean performFinish() {
		if (generator == null || contexts == null || contexts.isEmpty() || layoutGenerator == null) {
			return false;
		}

		ConfigurationManager configManager = ConfigurationManager.getInstance();

		for (Context context : contexts) {
			Tab defaultTab = ContextsFactory.eINSTANCE.createTab();
			defaultTab.setId(context.getName().toLowerCase());
			defaultTab.setLabel(context.getName());
			defaultTab.setPriority(100);
			context.getTabs().add(defaultTab);

			FieldSelection fieldSelection = selectFieldsPage.getFieldSelection();

			// URI contextURI = context.eResource().getURI();
			//		Resource selectionResource = context.eResource().getResourceSet().createResource(URI.createURI(context.getName() + "FieldSelection.xmi").resolve(contextURI)); //$NON-NLS-1$
			// selectionResource.getContents().add(fieldSelection);
			// try {
			// selectionResource.save(null);
			// } catch (IOException ex) {
			//			Activator.log.error("Couldn't persist the field selection model", ex); //$NON-NLS-1$
			// }

			layoutGenerator.setGenerator(generator);

			for (View view : context.getViews()) {
				if (view.getConstraints().size() == 0) {
					continue;
				}

				List<PropertyEditor> editors = new LinkedList<PropertyEditor>();

				for (DataContextElement element : getAllContextElements(view.getDatacontexts())) {
					for (Property property : element.getProperties()) {
						if (isSelected(fieldSelection, property, view.getElementMultiplicity() != 1)) {
							PropertyEditor editor = UiFactory.eINSTANCE.createPropertyEditor();
							editor.setProperty(property);
							editor.setWidgetType(configManager.getDefaultEditorType(property));
							editors.add(editor);
							ValueAttribute input = UiFactory.eINSTANCE.createValueAttribute();
							input.setName("input"); //$NON-NLS-1$
							input.setValue("{Binding}"); //$NON-NLS-1$
							editor.getAttributes().add(input);
						}
					}
				}

				List<Section> generatedSections = layoutGenerator.layoutElements(editors, view);
				defaultTab.getSections().addAll(generatedSections);
				view.getSections().addAll(generatedSections);
				context.getViews().add(view);
			}

			int i = 1;
			for (Tab tab : context.getTabs()) {
				i += tab.getSections().size();
			}
			final int numberOfSections = i;
			try {
				setNeedsProgressMonitor(true);
				final Context currentContext = context;

				final Map<String, Object> saveOptions = new HashMap<String, Object>();
				saveOptions.put(XMLResource.OPTION_PROCESS_DANGLING_HREF, XMLResource.OPTION_PROCESS_DANGLING_HREF_RECORD);

				getContainer().run(true, true, new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
						monitor.beginTask(Messages.CreateContextWizard_propertyViewGenerationJobName + currentContext.getName(), numberOfSections + 1);
						monitor.worked(1);

						try {
							currentContext.eResource().save(saveOptions);
							monitor.worked(1);
							for (Tab tab : currentContext.getTabs()) {
								for (Section section : tab.getSections()) {
									if (monitor.isCanceled()) {
										return;
									}
									section.getWidget().eResource().save(saveOptions);
									monitor.worked(1);
								}
							}
						} catch (IOException ex) {
							Activator.log.error(ex);
							return;
						}
						monitor.done();
					}

				});
			} catch (InvocationTargetException ex) {
				Activator.log.error(ex);
			} catch (InterruptedException ex) {
				Activator.log.error(ex);
			}

		}

		return true;
	}

	private boolean isSelected(FieldSelection fieldSelection, Property property, boolean multiple) {
		PropertyDefinition definition = getPropertyDefinition(fieldSelection, property);
		if (definition == null) {
			return false;
		}
		TernaryButton.State value = multiple ? definition.getValueMultiple() : definition.getValueSingle();
		switch (value) {
		case TRUE:
			return true;
		case FALSE:
			return false;
		case DEFAULT:
			return multiple ? generator.isSelectedMultiple(property) : generator.isSelectedSingle(property);
		}

		return false;
	}

	protected PropertyDefinition getPropertyDefinition(FieldSelection fieldSelection, Property property) {
		List<String> propertyPath = getPropertyPath(property.getContextElement());
		if (propertyPath.isEmpty()) {
			return null;
		}

		ContextElement currentElement = null;
		for (ContextElement contextRoot : fieldSelection.getContextElements()) {
			if (contextRoot.getName().equals(propertyPath.get(0))) {
				currentElement = contextRoot;
			}
		}

		propertyPath.remove(0);
		if (currentElement == null) {
			return null;
		}

		while (propertyPath.size() > 0) {
			String name = propertyPath.get(0);
			propertyPath.remove(0);
			currentElement = findByName(currentElement, name);
		}

		if (currentElement == null) {
			return null;
		}

		for (PropertyDefinition definition : currentElement.getProperties()) {
			if (definition.getName().equals(property.getName())) {
				return definition;
			}
		}

		return null;
	}

	protected ContextElement findByName(ContextElement source, String name) {
		for (ContextElement element : source.getElements()) {
			if (element.getName().equals(name)) {
				return element;
			}
		}
		return null;
	}

	protected List<String> getPropertyPath(DataContextElement element) {
		List<String> result;
		if (element.getPackage() == null) {
			result = new LinkedList<String>();
		} else {
			result = getPropertyPath(element.getPackage());
		}
		result.add(element.getName());
		return result;
	}

	private Set<DataContextElement> getAllContextElements(Collection<DataContextElement> source) {
		Set<DataContextElement> result = new HashSet<DataContextElement>();
		for (DataContextElement element : source) {
			getAllContextElements(element, result);
		}
		return result;
	}

	private void getAllContextElements(DataContextElement source, Set<DataContextElement> result) {
		if (result.contains(source)) {
			return;
		}

		result.add(source);
		for (DataContextElement element : source.getSupertypes()) {
			getAllContextElements(element, result);
		}
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		addPage(mainPage = new CreateContextMainPage());
		addPage(generatorPage = new GeneratorPage());
		addPage(selectOutputPage = new SelectOutputPage());
		addPage(selectFieldsPage = new SelectFieldsPage());
		// addPage(layout = new LayoutPage());

		setWindowTitle(Messages.CreateContextWizard_pageTitle);

	}

	protected void setGenerator(IGenerator generator) {
		this.generator = generator;
		generatorPage.setGenerator(generator);
		generatorPage.doBinding();
	}

	protected void setContexts(List<Context> contexts) {
		if (!contexts.isEmpty()) {
			this.contexts = contexts;
		}
	}

}

Back to the top