Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3abf27806fae9cb5be748a9f835310d418119c4 (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
/*****************************************************************************
 * 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.properties.runtime;

import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.e4.xwt.DefaultLoadingContext;
import org.eclipse.e4.xwt.ILoadingContext;
import org.eclipse.e4.xwt.XWT;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.properties.Activator;
import org.eclipse.papyrus.properties.catalog.PropertiesURIHandler;
import org.eclipse.papyrus.properties.contexts.Context;
import org.eclipse.papyrus.properties.contexts.Section;
import org.eclipse.papyrus.properties.contexts.Tab;
import org.eclipse.papyrus.properties.contexts.View;
import org.eclipse.papyrus.properties.modelelement.DataSource;
import org.eclipse.papyrus.properties.xwt.XWTTabDescriptor;
import org.eclipse.swt.SWT;
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;

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

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

	private Set<String> displayedSections = new HashSet<String>();

	private Set<Control> controls = new HashSet<Control>();

	private boolean allowDuplicate;

	/**
	 * 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());
			}
		}

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

	private void dispose() {
		for(Control control : controls) {
			control.dispose();
		}
		displayedSections.clear();
		controls.clear();
	}

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

		if(!allowDuplicate && displayedSections.contains(section.getName())) {
			return null;
		}

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

		displayedSections.add(section.getName());

		if(control != null) {
			controls.add(control);
		}

		return control;
	}

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

		createSection(parent, section, loadXWTFile(section), source);
	}

	public Control createSection(Composite parent, Section section, URL 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 {
			control = (Control)XWT.load(parent, sectionFile, source);

			if(control != null) {
				control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				controls.add(control);
			}
		} catch (Exception ex) {
			Activator.log.error("Error while loading " + section.getSectionFile(), ex); //$NON-NLS-1$
			dispose();
			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 URL loadXWTFile(Section section) {
		EObject tab = section.eContainer();
		Context context = (Context)tab.eContainer();
		if(context.eResource() == null) {
			context = ConfigurationManager.instance.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);

		try {
			URL url = new URL(sectionURI.toString());
			return url;
		} catch (IOException ex) {
			Activator.log.error(ex);
		}

		return null;
	}

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

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

Back to the top