Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e495e6c49a66ff4cdf675a5ef139b1277fc85654 (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
/*****************************************************************************
 * 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.util.List;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TreeViewer;
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.FieldSelectionFactory;
import org.eclipse.papyrus.customization.properties.generation.fieldselection.FieldSelectionPackage;
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.messages.Messages;
import org.eclipse.papyrus.customization.properties.generation.providers.DataContextPropertiesContentProvider;
import org.eclipse.papyrus.customization.properties.generation.providers.DataContextPropertiesLabelProvider;
import org.eclipse.papyrus.customization.properties.generation.wizard.widget.TernaryButton;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.views.properties.contexts.Context;
import org.eclipse.papyrus.views.properties.contexts.DataContextElement;
import org.eclipse.papyrus.views.properties.contexts.DataContextPackage;
import org.eclipse.papyrus.views.properties.contexts.DataContextRoot;
import org.eclipse.papyrus.views.properties.contexts.Property;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

/**
 * A WizardPage to choose the fields that will be displayed in the Property view.
 * For each property, the choice can be either "True", "False" or "Default".
 * The visibility of properties marked as Default is decided by the Generator.
 * When the context is regenerated, the visibility of properties marked as default
 * may change (If the generator is changed)
 * 
 * @author Camille Letavernier
 */
public class SelectFieldsPage extends AbstractCreateContextPage {

	private List<URI> targetURI;

	private List<Context> contexts;

	private Composite root;

	private FieldSelection fieldSelection;

	public int fieldColumn = -1, selectionSingleColumn = -1, selectionMultipleColumn = -1, descriptionColumn = -1;

	private int columnId = 0;
	

	/**
	 * Constructor.
	 */
	protected SelectFieldsPage() {
		super(Messages.SelectFieldsPage_title);
	}

	public void createControl(Composite parent) {
		root = new Composite(parent, SWT.NONE);
		root.setLayout(new GridLayout(1, true));

		setControl(root);
		setPageComplete(false);

		setDescription(Messages.SelectFieldsPage_selectFields);
	}

	/**
	 * Sets the generated partial context
	 * 
	 * @param context
	 *        The partially generated context
	 */
	public void setContexts(List<Context> contexts) {
		if(contexts == null || contexts.isEmpty()) {
			Activator.log.warn("Generated context is null"); //$NON-NLS-1$
			return;
		}

		this.contexts = contexts;

		fieldSelection = createNewFieldSelection();

		getWizard().setContexts(contexts);

		Label label = new Label(root, SWT.NONE);
		label.setText(Messages.SelectFieldsPage_availableFields);

		TreeViewer viewer = new TreeViewer(root, SWT.BORDER);
		Tree tree = viewer.getTree();
		tree.setBackground(tree.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		tree.setLayout(new TableLayout());
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		fieldColumn = addColumn(tree, Messages.SelectFieldsPage_field, 40, 200);
		selectionSingleColumn = addColumn(tree, Messages.SelectFieldsPage_displaySingle, 25, 120);
		selectionMultipleColumn = addColumn(tree, Messages.SelectFieldsPage_displayMultiple, 25, 120);
		descriptionColumn = addColumn(tree, Messages.SelectFieldsPage_description, 60, 300);

		tree.setHeaderVisible(true);

		viewer.setContentProvider(new DataContextPropertiesContentProvider(contexts));
		viewer.setLabelProvider(new DataContextPropertiesLabelProvider(this));
		viewer.setInput(new Object());

		viewer.expandAll();

		installEditors(viewer);

		root.layout();
	}

	/**
	 * Creates a column in the treeViewer and returns its id
	 */
	private int addColumn(Tree tree, String label, int weight, int minimumWidth) {
		TreeColumn column = new TreeColumn(tree, SWT.NONE);

		column.setText(label);
		column.setResizable(true);
		((TableLayout)tree.getLayout()).addColumnData(new ColumnWeightData(weight, minimumWidth));

		return columnId++;
	}

	private void installEditors(TreeViewer viewer) {
		Tree tree = viewer.getTree();

		for(TreeItem treeItem : tree.getItems()) {
			installEditors(treeItem);
		}
	}

	private void installEditors(TreeItem treeItem) {

		EObject eObject = EMFHelper.getEObject(treeItem.getData());
		if(eObject instanceof Property) {
			Property property = (Property)eObject;

			boolean defaultSingleValue = getGenerator().isSelectedSingle(property);
			boolean defaultMultipleValue = getGenerator().isSelectedMultiple(property);

			PropertyDefinition propertyDefinition = getWizard().getPropertyDefinition(fieldSelection, property);

			IObservableValue singleValue = EMFProperties.value(FieldSelectionPackage.eINSTANCE.getPropertyDefinition_ValueSingle()).observe(propertyDefinition);
			IObservableValue multipleValue = EMFProperties.value(FieldSelectionPackage.eINSTANCE.getPropertyDefinition_ValueMultiple()).observe(propertyDefinition);

			createTreeEditor(treeItem, selectionSingleColumn, defaultSingleValue, singleValue);
			createTreeEditor(treeItem, selectionMultipleColumn, defaultMultipleValue, multipleValue);
		}

		for(TreeItem subItem : treeItem.getItems()) {
			installEditors(subItem);
		}

	}

	private void createTreeEditor(TreeItem treeItem, int columnIndex, boolean defaultValue, IObservableValue observable) {
		Tree tree = treeItem.getParent();

		TernaryButton button = new TernaryButton(tree, defaultValue);

		TreeEditor editor = new TreeEditor(tree);
		editor.horizontalAlignment = SWT.CENTER;
		editor.grabHorizontal = true;

		editor.setEditor(button, treeItem, columnIndex);

		button.setObservable(observable);
	}

	private FieldSelection createNewFieldSelection() {
		FieldSelection selection = FieldSelectionFactory.eINSTANCE.createFieldSelection();

		for(Context context : contexts) {
			for(DataContextRoot dataContextRoot : context.getDataContexts()) {
				ContextElement definition = createContextPackage(dataContextRoot);
				selection.getContextElements().add(definition);
			}
		}

		return selection;
	}

	private ContextElement createContextPackage(DataContextPackage sourcePackage) {
		ContextElement element = createContextElement(sourcePackage);

		for(DataContextElement sourceElement : sourcePackage.getElements()) {
			ContextElement subElement;
			if(sourceElement instanceof DataContextPackage) {
				subElement = createContextPackage((DataContextPackage)sourceElement);
			} else {
				subElement = createContextElement(sourceElement);
			}
			element.getElements().add(subElement);
		}
		return element;
	}

	private ContextElement createContextElement(DataContextElement sourceElement) {
		ContextElement element = FieldSelectionFactory.eINSTANCE.createContextElement();
		element.setName(sourceElement.getName());

		for(Property property : sourceElement.getProperties()) {
			PropertyDefinition propertyDefinition = FieldSelectionFactory.eINSTANCE.createPropertyDefinition();
			propertyDefinition.setName(property.getName());
			propertyDefinition.setValueSingle(TernaryButton.State.DEFAULT);
			propertyDefinition.setValueMultiple(TernaryButton.State.DEFAULT);
			element.getProperties().add(propertyDefinition);
		}

		return element;
	}

	private void displayFields(DataContextElement contextElement, TreeViewer treeViewer) {

		//		if(contextElement.getProperties().size() > 0) {
		//
		//			Label separator = new Label(fields, SWT.SEPARATOR | SWT.HORIZONTAL);
		//			GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
		//			separator.setLayoutData(data);
		//
		//			Label elementName = new Label(fields, SWT.NONE);
		//			elementName.setText(contextElement.getName());
		//			FontData[] fontDatas = elementName.getFont().getFontData();
		//			for(FontData fontData : fontDatas) {
		//				fontData.setStyle(SWT.BOLD);
		//				// fontData.setHeight(fontData.getHeight() + 2);
		//			}
		//			elementName.setFont(new Font(elementName.getDisplay(), fontDatas));
		//
		//			data = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
		//			elementName.setLayoutData(data);
		//
		//			Label separator2 = new Label(fields, SWT.SEPARATOR | SWT.HORIZONTAL);
		//			data = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
		//			separator2.setLayoutData(data);
		//
		//			for(Property property : contextElement.getProperties()) {
		//				Label label = new Label(fields, SWT.NONE);
		//				label.setText(property.getName());
		//
		//				TernaryButton showSingle = new TernaryButton(fields, getGenerator().isSelectedSingle(property));
		//				TernaryButton showMultiple = new TernaryButton(fields, getGenerator().isSelectedMultiple(property));
		//
		//				PropertyDefinition propertyDefinition = getWizard().getPropertyDefinition(fieldSelection, property);
		//
		//				IObservableValue singleValue = EMFProperties.value(FieldSelectionPackage.eINSTANCE.getPropertyDefinition_ValueSingle()).observe(propertyDefinition);
		//				IObservableValue multipleValue = EMFProperties.value(FieldSelectionPackage.eINSTANCE.getPropertyDefinition_ValueMultiple()).observe(propertyDefinition);
		//
		//				showSingle.setObservable(singleValue);
		//				showMultiple.setObservable(multipleValue);
		//
		//				Label description = new Label(fields, SWT.WRAP);
		//				String propertyDescription = "";// property.getTooltipText(); //$NON-NLS-1$
		//				if(propertyDescription == null || propertyDescription.trim().equals("")) { //$NON-NLS-1$
		//					propertyDescription = Messages.SelectFieldsPage_descriptionNotAvailable;
		//					description.setAlignment(SWT.CENTER);
		//				}
		//				description.setText(propertyDescription);
		//
		//				data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		//				label.setLayoutData(data);
		//				data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		//				showSingle.setLayoutData(data);
		//				data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		//				showMultiple.setLayoutData(data);
		//				data = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		//				data.widthHint = 500;
		//				description.setLayoutData(data);
		//			}
		//		}
		//
		//		if(contextElement instanceof DataContextPackage) {
		//			DataContextPackage contextPackage = (DataContextPackage)contextElement;
		//			for(DataContextElement element : contextPackage.getElements()) {
		//				displayFields(element, treeViewer);
		//			}
		//		}
	}

	private IGenerator getGenerator() {
		return getWizard().generator;
	}

	/**
	 * Sets the URI of the generated context
	 * 
	 * @param uri
	 *        The URI of the generated context
	 */
	public void setTargetURI(List<URI> uri) {
		this.targetURI = uri;
	}

	@Override
	public void setVisible(boolean visible) {
		super.setPageComplete(true);
		super.setVisible(visible);
		if(contexts == null && visible) {
			setContexts(getWizard().generator.generate(targetURI));
		}
	}

	/**
	 * @return the result of the user's field selection
	 */
	public FieldSelection getFieldSelection() {
		return fieldSelection;
	}

}

Back to the top