Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7d2905fb203e4c3c241fd03f3b8576e5f43a589 (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
/*****************************************************************************
 * 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.views.properties.xwt;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.constraints.ConstraintDescriptor;
import org.eclipse.papyrus.infra.constraints.constraints.Constraint;
import org.eclipse.papyrus.infra.constraints.runtime.ConstraintFactory;
import org.eclipse.papyrus.views.properties.Activator;
import org.eclipse.papyrus.views.properties.contexts.Section;
import org.eclipse.papyrus.views.properties.contexts.View;
import org.eclipse.papyrus.views.properties.modelelement.DataSource;
import org.eclipse.papyrus.views.properties.modelelement.DataSourceFactory;
import org.eclipse.papyrus.views.properties.runtime.DisplayEngine;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

/**
 * An Implementation of ISection for the TabbedPropertyView framework.
 * The XWTSection uses an XWT File to display the SWT Controls, and
 * a DataSource for DataBinding
 * 
 * @author Camille Letavernier
 */
public class XWTSection extends AbstractPropertySection implements IChangeListener {

	private Section section;

	private DataSource source;

	private View view;

	private Composite self;

	private DisplayEngine display;

	private Set<Constraint> constraints;

	/**
	 * Constructor.
	 * 
	 * @param section
	 *        The Section object containing the Metadata for the XWTSection
	 * @param view
	 *        The view this section belongs to
	 * @param display
	 *        The display engine that will generate the SWT Controls
	 */
	public XWTSection(Section section, View view, DisplayEngine display) {
		this.section = section;
		this.view = view;
		this.display = display;
	}

	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		self = new Composite(parent, SWT.NONE);

		GridLayout layout = new GridLayout(1, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		self.setLayout(layout);

		layout = new GridLayout(1, false);
		layout.verticalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 5;
		parent.setLayout(layout);

		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		self.setLayoutData(data);

		self.setBackground(parent.getBackground());
		self.setBackgroundMode(SWT.INHERIT_DEFAULT);
	}

	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		if(selection == getSelection()) {
			return;
		}

		//Sets the initial input, *or* changes the input for the same view : we need to clean the cache
		DataSourceFactory.instance.removeFromCache(getSelection(), view);

		super.setInput(part, selection);

		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;

			setSource(DataSourceFactory.instance.createDataSourceFromSelection(structuredSelection, view));
		}
	}

	private void setSource(DataSource source) {
		if(this.source != null) {
			this.source.removeChangeListener(this);
			this.source.dispose();
		}

		this.source = source;

		if(section.getConstraints().size() > 0) {
			source.addChangeListener(this);
		}
	}

	/**
	 * Displays the section
	 * 
	 * @param refresh
	 *        If true, and the section has already been displayed, the controls will be
	 *        regenerated. If false, the section will only be displayed if it hasn't been
	 *        displayed yet, or if the display engine allows duplication of sections
	 */
	public void display(boolean refresh) {
		if(self.isDisposed()) {
			Activator.log.debug("Error : widget is disposed"); //$NON-NLS-1$
			dispose();
			return;
		}

		if(!isApplied()) {
			hide();
			return;
		}

		self.setRedraw(false);
		if(refresh) {
			display.refreshSection(self, section, source);
		} else {
			display.createSection(self, section, source);
		}
		self.setRedraw(true);
	}

	private void hide() {
		display.removeSection(self);
	}

	@Override
	public void refresh() {
		display(false);
	}

	/**
	 * Tests if this section is applied. A section is applied if it doesn't have
	 * any constraint, or if at least one of its constraints match the current selection
	 * 
	 * @return
	 *         True if the section should be displayed
	 */
	protected boolean isApplied() {
		if(getConstraints().isEmpty()) {
			return true;
		}

		ISelection selection = getSelection();

		//Return true only if at least one constraint matches the selection

		for(Constraint constraint : getConstraints()) {
			if(constraint.match((IStructuredSelection)selection)) {
				return true;
			}
		}

		return false;
	}

	protected Set<Constraint> getConstraints() {
		if(constraints == null) {
			constraints = new HashSet<Constraint>();
			for(ConstraintDescriptor constraintDescriptor : section.getConstraints()) {
				Constraint constraint = ConstraintFactory.getInstance().createFromModel(constraintDescriptor);
				if(constraint != null) {
					constraints.add(constraint);
				}
			}
		}

		return constraints;
	}

	@Override
	public void dispose() {
		//Dispose the DataSource
		if(source != null) {
			source.removeChangeListener(this);
			source.dispose();
		}

		//Dispose the SWT Composite
		if(self != null) {
			self.dispose();
		}

		//Clean the DataSource cache
		DataSourceFactory.instance.removeFromCache(getSelection(), view);
		super.dispose();
	}

	@Override
	public IStructuredSelection getSelection() {
		return (IStructuredSelection)super.getSelection();
	}

	@Override
	public String toString() {
		return "XWTSection : " + section.getName(); //$NON-NLS-1$
	}

	public void handleChange(ChangeEvent event) {
		display(true);
	}

}

Back to the top