Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0113bc847a2bd46b553029255360ac85097bf0a5 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST, 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - Use URIs to support non-URL-compatible storage (CDO)
 *  Christian W. Damus (CEA) - bug 417409
 *  Christian W. Damus (CEA) - bug 444227
 *  Christian W. Damus - bug 450478
 *  Christian W. Damus - bug 454536
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.runtime;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.views.properties.Activator;
import org.eclipse.papyrus.views.properties.catalog.PropertiesURIHandler;
import org.eclipse.papyrus.views.properties.contexts.Context;
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.modelelement.DataSource;
import org.eclipse.papyrus.views.properties.util.EMFURLStreamHandler;
import org.eclipse.papyrus.views.properties.xwt.XWTTabDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.views.properties.tabbed.ITabDescriptor;
import org.eclipse.xwt.DefaultLoadingContext;
import org.eclipse.xwt.ILoadingContext;
import org.eclipse.xwt.IXWTLoader;
import org.eclipse.xwt.XWT;

/**
 * A default implementation for {@link DisplayEngine}
 *
 * @author Camille Letavernier
 */
public class DefaultDisplayEngine implements DisplayEngine {

	private ILoadingContext loadingContext = new DefaultLoadingContext(getClass().getClassLoader());

	private Map<String, XWTTabDescriptor> currentTabs = new HashMap<String, XWTTabDescriptor>();

	private TabModel<DataSource> displayedSections = new TabModel<DataSource>();

	private TabModel<Control> controls = new TabModel<Control>();

	private boolean allowDuplicate;

	private Object xmlCache;

	/**
	 * Constructs a new DisplayEnginet that doesn't allow the duplication of sections
	 */
	public DefaultDisplayEngine() {
		this(false);
	}

	/**
	 * Constructor.
	 *
	 * @param allowDuplicate
	 *            If false, two calls of {@link #createSection(Composite, Section, DataSource)} with the same
	 *            section will display the section only once : only the first call is taken into account
	 *            The main property view doesn't allow duplication, to avoid redundancy when two views link to
	 *            the same section.
	 */
	public DefaultDisplayEngine(boolean allowDuplicate) {
		this.allowDuplicate = allowDuplicate;
	}

	public List<ITabDescriptor> getTabDescriptors(Set<View> views) {
		Map<String, XWTTabDescriptor> result = new LinkedHashMap<String, XWTTabDescriptor>();

		Set<String> selectedSections = new HashSet<String>();

		for (View view : views) {
			for (Section section : view.getSections()) {
				if (selectedSections.contains(section.getName())) {
					continue;
				}

				Tab tab = section.getTab();

				if (tab == null) {
					Activator.log.warn("Null tab for " + section); //$NON-NLS-1$
					continue;
				}

				XWTTabDescriptor descriptor;

				if (result.containsKey(tab.getId())) {
					descriptor = result.get(tab.getId());
				} else {
					descriptor = new XWTTabDescriptor(tab);
					result.put(tab.getId(), descriptor);
				}

				descriptor.addSection(section, view, this);
				selectedSections.add(section.getName());
			}
		}

		for (Map.Entry<String, XWTTabDescriptor> next : result.entrySet()) {
			XWTTabDescriptor existing = currentTabs.get(next.getKey());
			if ((existing != null) && !existing.equals(next.getValue())) {
				// Will have to rebuild this tab
				disposeControls(next.getKey());
			}
		}

		currentTabs = result;

		return new ArrayList<ITabDescriptor>(result.values());
	}

	/**
	 * Disposes the controls created by this DisplayEngine for the specified section.
	 * This should not dispose the engine itself, which can be reused.
	 */
	protected void disposeControls(Section section) {
		Control control = controls.remove(section);
		if (control != null) {
			control.dispose();
		}

		DataSource dataSource = displayedSections.remove(section);
		if (dataSource != null) {
			dataSource.dispose();
		}
	}

	/**
	 * Disposes the controls created by this DisplayEngine for the specified tab ID.
	 * This should not dispose the engine itself, which can be reused.
	 */
	protected void disposeControls(String tabID) {
		for (Control control : this.controls.remove(tabID)) {
			control.dispose();
		}

		for (DataSource dataSource : displayedSections.remove(tabID)) {
			dataSource.dispose();
		}
	}

	protected void disposeControls() {
		for (String next : new ArrayList<String>(controls.tabIDs())) {
			disposeControls(next);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void dispose() {
		disposeControls();
	}

	/**
	 * Invalidates any caches that I may have because the displayed property UI contexts, constraints, or views have
	 * changed in some way.
	 */
	public void invalidate() {
		// Forget the cached XML property definitions because they may have changed
		xmlCache = null;
	}

	public Control createSection(Composite parent, Section section, DataSource source) {
		if (source == null) {
			return null;
		}

		DataSource existing = getDataSource(section);
		if (!allowDuplicate && (existing != null)) {
			if (isUnloaded(existing) || conflictingArity(existing.getSelection(), source.getSelection())) {
				// If it's a left-over from an unloaded resource, then rebuild the properties UI because
				// element-browser widgets and other things may remember the previous (now invalid)
				// resource-set context. Also, cannot reuse a multiple-selection data source for
				// single-selection and vice-versa
				disposeControls(section);
			} else {
				// Update the data source and fire the bindings
				existing.setSelection(source.getSelection());

				return null;
			}
		}

		Control control = createSection(parent, section, loadXWTFile(section), source);

		addDataSource(section, source);

		if (control != null) {
			addControl(section, control);
		}

		return control;
	}

	protected DataSource getDataSource(Section section) {
		return displayedSections.get(section);
	}

	/**
	 * Queries whether any object selected in a data source is unloaded (now an EMF proxy object).
	 *
	 * @param dataSource
	 *            a data source
	 * @return whether it contains an unloaded model element
	 */
	protected boolean isUnloaded(DataSource dataSource) {
		boolean result = false;

		for (Iterator<?> iter = dataSource.getSelection().iterator(); !result && iter.hasNext();) {
			EObject next = EMFHelper.getEObject(iter.next());
			result = (next != null) && next.eIsProxy();
		}

		return result;
	}

	protected boolean conflictingArity(IStructuredSelection selection1, IStructuredSelection selection2) {
		return (selection1.size() <= 1) != (selection2.size() <= 1);
	}

	/**
	 * Adds a new {@code dataSource} for a property {@code section}.
	 *
	 * @return the previously-recorded data source, if any, for this {@code section} which has now been displaced
	 */
	protected DataSource addDataSource(Section section, DataSource dataSource) {
		return displayedSections.put(section, dataSource);
	}

	protected void addControl(final Section section, Control control) {
		controls.put(section, control);

		control.addDisposeListener(new DisposeListener() {

			public void widgetDisposed(DisposeEvent e) {
				// Perhaps the tabbed properties view is disposing a tab that is not shown by the new selection
				displayedSections.remove(section);
				controls.remove(section);
			}
		});
	}

	public void refreshSection(Composite parent, Section section, DataSource source) {
		for (Control control : parent.getChildren()) {
			control.dispose();
		}

		Control control = createSection(parent, section, loadXWTFile(section), source);

		addDataSource(section, source);

		if (control != null) {
			addControl(section, control);
		}
	}

	public Control createSection(Composite parent, Section section, URI sectionFile, DataSource source) {
		if (sectionFile == null) {
			sectionFile = loadXWTFile(section);
			if (sectionFile == null) {
				return null;
			}
		}

		ILoadingContext xwtContext = XWT.getLoadingContext();
		XWT.setLoadingContext(loadingContext);

		Control control = null;

		try {
			ResourceSet rset = section.eResource().getResourceSet();
			URL url = new URL(null, sectionFile.toString(), new EMFURLStreamHandler(rset.getURIConverter()));

			Map<String, Object> options = new HashMap<String, Object>();
			options.put(IXWTLoader.CONTAINER_PROPERTY, parent);
			options.put(IXWTLoader.DATACONTEXT_PROPERTY, source);
			options.put(IXWTLoader.XML_CACHE_PROPERTY, (xmlCache != null) ? xmlCache : Boolean.TRUE);
			control = (Control) XWT.loadWithOptions(url, options);
			xmlCache = options.get(IXWTLoader.XML_CACHE_PROPERTY);

			if (control != null) {
				control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				addControl(section, control);
			}
		} catch (Exception ex) {
			Activator.log.error("Error while loading " + section.getSectionFile(), ex); //$NON-NLS-1$
			disposeControls(section.getTab().getId());
			Label label = new Label(parent, SWT.NONE);
			label.setText("An error occured in the property view. The file " + section.getSectionFile() + " could not be loaded"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		layout(parent);

		XWT.setLoadingContext(xwtContext);

		return control;
	}

	private URI loadXWTFile(Section section) {
		EObject tab = section.eContainer();
		Context context = (Context) tab.eContainer();
		if (context.eResource() == null) {
			context = ConfigurationManager.getInstance().getContext(context.getName());
			Activator.log.warn("No resource for Context : " + context + " ; refreshing the model"); //$NON-NLS-1$ //$NON-NLS-2$
		}

		URI sectionURI = URI.createURI(section.getSectionFile());
		URI baseURI = context.eResource().getURI();
		if (PropertiesURIHandler.PROPERTIES_SCHEME.equals(baseURI.scheme())) {
			PropertiesURIHandler handler = new PropertiesURIHandler();
			baseURI = handler.getConvertedURI(baseURI);
		}
		sectionURI = sectionURI.resolve(baseURI);

		return sectionURI;
	}

	private void layout(Composite parent) {
		parent.getParent().getParent().layout();
		parent.getParent().layout();
		parent.layout();
	}

	public void removeSection(Composite parent) {
		for (Control control : parent.getChildren()) {
			control.dispose();
		}
		layout(parent);
	}

	/**
	 * Creates a proxy for a {@code section} that makes it distinct from other occurrences of the same section, according to some
	 * arbitrary {@code disciminator}.
	 *
	 * @param section
	 *            a section to be repeated with unique discriminators
	 * @param discriminator
	 *            this {@code section}'s discriminator value
	 *
	 * @return the proxy instance combining the identity of the {@code section} with its unique {@code discriminator}
	 */
	public static Section discriminate(Section section, Object discriminator) {
		if (section == null) {
			throw new IllegalArgumentException("null section");
		}
		if (discriminator == null) {
			throw new IllegalArgumentException("null discriminator");
		}
		if (SectionDiscriminator.isDiscriminated(section)) {
			throw new IllegalArgumentException("section already has a discriminator");
		}

		return SectionDiscriminator.discriminate(section, discriminator);
	}

	/**
	 * Obtains the discriminator for a {@code section} proxy, if it is a proxy.
	 *
	 * @param section
	 *            a section that is repeated with unique discriminators
	 * @return this {@code section}'s discriminator value, or {@code null} if it is a singleton (non-proxy) section
	 */
	public static Object getDiscriminator(Section section) {
		if (section == null) {
			throw new IllegalArgumentException("null section");
		}

		return SectionDiscriminator.getDiscriminator(section);
	}
}

Back to the top