Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 65bd1ba114c607b359f49f62ef4a00b8104506ef (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.customization.properties.generation.wizard;

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

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
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 {

	CreateContextMainPage mainPage;

	GeneratorPage generatorPage;

	//protected LayoutPage layout;

	SelectFieldsPage selectFieldsPage;

	/**
	 * All available context generators
	 */
	protected static List<IGenerator> contextGenerators = new LinkedList<IGenerator>();

	/**
	 * All available layout generators
	 */
	protected static List<ILayoutGenerator> layoutGenerators = new LinkedList<ILayoutGenerator>();

	/**
	 * The generated context
	 */
	protected Context context;

	/**
	 * 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 || context == null) {
			return false;
		}

		ConfigurationManager configManager = ConfigurationManager.instance;

		Tab defaultTab = ContextsFactory.eINSTANCE.createTab();
		defaultTab.setCategory("default"); //$NON-NLS-1$
		defaultTab.setId("default"); //$NON-NLS-1$
		defaultTab.setLabel("Default"); //$NON-NLS-1$
		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 = layoutGenerators.get(0); //TODO : Use the layoutGenerator combo

		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);
			getContainer().run(true, true, new IRunnableWithProgress() {

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

					try {
						context.eResource().save(Collections.EMPTY_MAP);

						monitor.worked(1);
						for(Tab tab : context.getTabs()) {
							for(Section section : tab.getSections()) {
								if(monitor.isCanceled()) {
									return;
								}
								section.getWidget().eResource().save(Collections.EMPTY_MAP);
								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);
		}
		//		Job job = new Job(Messages.CreateContextWizard_propertyViewGenerationJobName + context.getName()) {
		//
		//			@Override
		//			protected IStatus run(IProgressMonitor monitor) {
		//				monitor.beginTask(getName(), numberOfSections);
		//
		//				try {
		//					context.eResource().save(Collections.EMPTY_MAP);
		//
		//					monitor.worked(1);
		//					for(Tab tab : context.getTabs()) {
		//						for(Section section : tab.getSections()) {
		//							if(monitor.isCanceled()) {
		//								return Status.CANCEL_STATUS;
		//							}
		//							section.getWidget().eResource().save(Collections.EMPTY_MAP);
		//							monitor.worked(1);
		//						}
		//					}
		//				} catch (IOException ex) {
		//					Activator.log.error(ex);
		//					return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CreateContextWizard_propertyViewGenerationError + context.getName(), ex);
		//				}
		//				return Status.OK_STATUS;
		//			}
		//		};
		//		job.setPriority(Job.INTERACTIVE);
		//		job.setUser(true);
		//		job.schedule();

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

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

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

		return null;
	}

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

	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(selectFieldsPage = new SelectFieldsPage());
		//addPage(layout = new LayoutPage());

		setWindowTitle(Messages.CreateContextWizard_pageTitle);

	}

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

	void setContext(Context context) {
		this.context = context;
	}

	/**
	 * Registers a new context Generator for the CreateContextWizard
	 * 
	 * @param generator
	 *        The IGenerator to register
	 */
	public static void addGenerator(IGenerator generator) {
		contextGenerators.add(generator);
	}

	/**
	 * Registers a new Layout Generator for the CreateContextWizard
	 * 
	 * @param generator
	 *        The ILayoutGenerator to register
	 */
	public static void addLayoutGenerator(ILayoutGenerator generator) {
		layoutGenerators.add(generator);
	}

}

Back to the top