Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48f6a4786a59308f00bbbd8b0e1bbe85e22f3eb1 (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
/*****************************************************************************
 * 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.layout;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.customization.properties.generation.Activator;
import org.eclipse.papyrus.customization.properties.generation.generators.IGenerator;
import org.eclipse.papyrus.customization.properties.generation.messages.Messages;
import org.eclipse.papyrus.views.properties.contexts.ContextsFactory;
import org.eclipse.papyrus.views.properties.contexts.Property;
import org.eclipse.papyrus.views.properties.contexts.Section;
import org.eclipse.papyrus.views.properties.contexts.View;
import org.eclipse.papyrus.views.properties.environment.CompositeWidgetType;
import org.eclipse.papyrus.views.properties.environment.LayoutType;
import org.eclipse.papyrus.views.properties.environment.Namespace;
import org.eclipse.papyrus.views.properties.environment.Type;
import org.eclipse.papyrus.views.properties.runtime.ConfigurationManager;
import org.eclipse.papyrus.views.properties.ui.CompositeWidget;
import org.eclipse.papyrus.views.properties.ui.Layout;
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.papyrus.views.properties.util.PropertiesUtil;

/**
 * Default implementation for ILayoutGenerator
 * PropertyEditors are grouped by their property type (Strings, booleans, ...)
 * Boolean and integer sections have two columns, while the other ones have only one columns
 * 
 * All multiple value editors are displayed after all the single value editors.
 * 
 * @author Camille Letavernier
 */
public class StandardLayoutGenerator implements ILayoutGenerator {

	protected final TreeMap<Category, List<PropertyEditor>> editorsByCategory = new TreeMap<Category, List<PropertyEditor>>();

	protected final Set<Namespace> namespaces = new HashSet<Namespace>();

	public synchronized List<Section> layoutElements(List<PropertyEditor> editors, View parent) {

		editorsByCategory.clear();
		namespaces.clear();

		namespaces.addAll(ConfigurationManager.instance.getBaseNamespaces());

		sortEditors(editors);

		Section section = createSection(parent);

		for(Map.Entry<Category, List<PropertyEditor>> mapping : editorsByCategory.entrySet()) {
			Category category = mapping.getKey();
			List<PropertyEditor> categorizedEditors = mapping.getValue();

			CompositeWidget container = layoutCategorizedEditors(category, categorizedEditors);

			section.getWidget().getWidgets().add(container);
		}

		return Collections.singletonList(section);
	}

	protected CompositeWidget layoutCategorizedEditors(Category category, List<PropertyEditor> editors) {
		CompositeWidgetType compositeType = ConfigurationManager.instance.getDefaultCompositeType();
		LayoutType propertiesLayoutType = ConfigurationManager.instance.getDefaultLayoutType();

		CompositeWidget container = UiFactory.eINSTANCE.createCompositeWidget();
		container.setWidgetType(compositeType);
		Layout layout = UiFactory.eINSTANCE.createLayout();
		container.setLayout(layout);
		ValueAttribute numColumns = UiFactory.eINSTANCE.createValueAttribute();
		numColumns.setName("numColumns"); //$NON-NLS-1$
		numColumns.setValue(category.getNumColumns().toString());
		layout.getAttributes().add(numColumns);
		layout.setLayoutType(propertiesLayoutType);
		container.getWidgets().addAll(editors);

		return container;
	}

	protected void sortEditors(List<PropertyEditor> editors) {
		for(PropertyEditor editor : editors) {
			Category category = new Category(editor.getProperty());
			getByCategory(category).add(editor);

			if(editor.getWidgetType() == null) {
				Activator.log.warn(String.format("Editor for property %s doesn't have a WidgetType", editor.getProperty().getName())); //$NON-NLS-1$
				continue;
			}

			namespaces.add(editor.getWidgetType().getNamespace());
		}
	}

	protected Section createSection(View parent) {
		CompositeWidget sectionRoot = createSectionRoot();

		Section section = ContextsFactory.eINSTANCE.createSection();
		section.setName(parent.getName());
		section.setSectionFile(String.format("ui/%s.xwt", section.getName().replaceAll(" ", ""))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 

		URI compositeURI = URI.createURI(section.getSectionFile());
		compositeURI = compositeURI.resolve(parent.eResource().getURI());
		Resource resource = parent.eResource().getResourceSet().createResource(compositeURI);

		section.setWidget(sectionRoot);

		resource.getContents().add(sectionRoot);

		return section;
	}

	protected CompositeWidget createSectionRoot() {
		CompositeWidgetType compositeType = ConfigurationManager.instance.getDefaultCompositeType();
		namespaces.add(compositeType.getNamespace());
		LayoutType propertiesLayoutType = ConfigurationManager.instance.getDefaultLayoutType();
		namespaces.add(propertiesLayoutType.getNamespace());

		CompositeWidget sectionRoot = UiFactory.eINSTANCE.createCompositeWidget();
		sectionRoot.setWidgetType(compositeType);
		Layout layout = UiFactory.eINSTANCE.createLayout();
		layout.setLayoutType(propertiesLayoutType);
		sectionRoot.setLayout(layout);
		sectionRoot.getAttributes().addAll(createNamespaces(namespaces));

		return sectionRoot;
	}

	protected List<ValueAttribute> createNamespaces(Collection<Namespace> namespaces) {
		List<ValueAttribute> xmlNamespaces = new LinkedList<ValueAttribute>();
		for(Namespace namespace : namespaces) {
			if(namespace == null) {
				continue;
			}

			ValueAttribute attribute = UiFactory.eINSTANCE.createValueAttribute();
			attribute.setName(PropertiesUtil.getQualifiedName(namespace));
			attribute.setValue(PropertiesUtil.getPrefixedValue(namespace));
			xmlNamespaces.add(attribute);
		}
		return xmlNamespaces;
	}

	protected List<PropertyEditor> getByCategory(Category category) {
		if(!editorsByCategory.containsKey(category)) {
			editorsByCategory.put(category, new LinkedList<PropertyEditor>());
		}
		return editorsByCategory.get(category);
	}

	protected class Category implements Comparable<Category> {

		public Type editorType;

		public int multiplicity;

		public Integer getNumColumns() {
			switch(editorType) {
			case BOOLEAN:
				return 2;
			case ENUMERATION:
				return 1;
			case INTEGER:
				return 2;
			case REFERENCE:
				return 1;
			case STRING:
				return 1;
			}
			return 1; //Cannot happen
		}

		public Category(Property property) {
			this.editorType = property.getType();
			this.multiplicity = property.getMultiplicity();
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + ((editorType == null) ? 0 : editorType.hashCode());
			result = prime * result + multiplicity;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			if(!(obj instanceof Category)) {
				return false;
			}
			Category other = (Category)obj;
			if(!getOuterType().equals(other.getOuterType())) {
				return false;
			}
			if(editorType != other.editorType) {
				return false;
			}
			if(multiplicity != other.multiplicity) {
				return false;
			}
			return true;
		}

		public int compareTo(Category category) {
			if(category == null) {
				return -1;
			}

			if(category.multiplicity != multiplicity) {
				return multiplicity == 1 ? -1 : 1;
			}

			int result = getTypeIndex().compareTo(category.getTypeIndex());
			return result;
		}

		public Integer getTypeIndex() {
			int i = 0;
			for(Type type : orderedTypes) {
				if(type == editorType) {
					return i;
				}
				i++;
			}
			return orderedTypes.length;
		}

		@Override
		public String toString() {
			return (multiplicity == 1 ? "Single" : "Multiple") + editorType.toString(); //$NON-NLS-1$ //$NON-NLS-2$
		}

		private StandardLayoutGenerator getOuterType() {
			return StandardLayoutGenerator.this;
		}
	}

	/**
	 * The order in which the types are displayed
	 */
	public static Type[] orderedTypes = new Type[]{ Type.STRING, Type.BOOLEAN, Type.INTEGER, Type.ENUMERATION, Type.REFERENCE };

	public String getName() {
		return Messages.StandardLayoutGenerator_name;
	}

	public void setGenerator(IGenerator generator) {
		//Ignored
	}
}

Back to the top