Skip to main content
summaryrefslogtreecommitdiffstats
blob: d33ae49753898fed13ba8512c4dc2fb6e6583153 (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
/*******************************************************************************
 * Copyright (c) 2012 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:
 *     Nicolas Bros (Mia-Software) - Bug 372865 - FacetSet selection dialog
 *     Gregoire Dupe (Mia-Software) - Bug 372626 - Aggregates
 *     Nicolas Bros (Mia-Software) - Bug 372626 - Aggregates
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.efacet.ui.internal.widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.papyrus.emf.facet.efacet.core.internal.exported.IResolverManager;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetSet;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.Activator;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.dialogs.FacetSetTreeContentProvider;
import org.eclipse.papyrus.emf.facet.efacet.ui.internal.exported.widget.IFacetSetSelectionWidget;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.utils.ImageUtils;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/** A control that displays a tree of FacetSets with checkboxes to select them. */
public class FacetSetSelectionControl implements IFacetSetSelectionWidget {

	/** An OK status, but without the "OK" message, because we don't want "OK" to appear in the status bar */
	protected static final IStatus OK_STATUS = new Status(IStatus.OK, Activator.PLUGIN_ID, ""); //$NON-NLS-1$
	protected static final String INVALID_ELEMENTS = Messages.FacetSetSelectionControl_selectionContainsInvalidElements;
	protected static final String ONLY_FACET_SETS = Messages.FacetSetSelectionControl_onlyFacetSetsAllowedInSelection;

	private final Composite cParent;
	private CheckboxTreeViewer treeViewer;
	private Collection<? extends FacetSet> available;

	private final int selectionMaxSize;
	private final boolean allowEmpty;
	private IStatus validationStatus;
	private final Runnable onChange;
	private final Set<FacetSet> selected = new HashSet<FacetSet>();

	/** Create a checkbox tree to select FacetSets. */
	public FacetSetSelectionControl(final Composite parentComposite, final int selectionMaxSize, final boolean allowEmpty, final Runnable onChange) {
		this.cParent = parentComposite;
		this.selectionMaxSize = selectionMaxSize;
		this.allowEmpty = allowEmpty;
		this.onChange = onChange;
	}

	public void createContents() {
		final Composite composite = new Composite(this.cParent, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		createCheckboxTreeViewer(composite);
		updateValidationStatus();
	}

	protected void notifyChanged() {
		if (this.onChange != null) {
			this.onChange.run();
		}
	}

	public void setAvailableFacetSets(final Collection<? extends FacetSet> newAvailable) {
		this.available = newAvailable;
		this.treeViewer.setInput(newAvailable.toArray());
		updateValidationStatus();
		notifyChanged();
	}

	public Collection<? extends FacetSet> getAvailableFacetSets() {
		return Collections.unmodifiableCollection(this.available);
	}

	public List<FacetSet> getSelectedFacetSets() {
		return Collections.unmodifiableList(new ArrayList<FacetSet>(this.selected));
	}

	public void setSelectedFacetSets(final Collection<? extends FacetSet> facetSetsToSelect) {
		if (facetSetsToSelect == null) {
			throw new IllegalArgumentException();
		}
		this.selected.clear();
		this.selected.addAll(facetSetsToSelect);
		updateValidationStatus();
		notifyChanged();
		this.treeViewer.refresh();
	}

	public final void addCheckStateListener(final ICheckStateListener listener) {
		this.treeViewer.addCheckStateListener(listener);
	}

	protected void createCheckboxTreeViewer(final Composite parentComposite) {
		this.treeViewer = new CheckboxTreeViewer(parentComposite, SWT.BORDER);
		this.treeViewer.setContentProvider(createContentProvider());
		this.treeViewer.setLabelProvider(createLabelProvider());
		this.treeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		getTreeViewer().setCheckStateProvider(createCheckStateProvider());
		addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(final CheckStateChangedEvent event) {
				FacetSetSelectionControl.this.checkStateChanged(event);
			}
		});
		getTreeViewer().refresh();
	}
	
	protected void checkStateChanged(final CheckStateChangedEvent event) {
		handleCheckStateChange(event);
		getTreeViewer().refresh();
		updateValidationStatus();
		notifyChanged();
	}

	protected void handleCheckStateChange(final CheckStateChangedEvent event) {
		final Object element = event.getElement();
		if (event.getElement() instanceof FacetSet) {
			final FacetSet facetSet = (FacetSet) event.getElement();
			FacetSet propagation = IResolverManager.DEFAULT
					.selectionRoot(element, FacetSet.class);
			if (propagation == null) {
				propagation = facetSet;
			}
			if (event.getChecked()) {
				this.selected.add(propagation);
			} else {
				this.selected.remove(propagation);
			}
		}
		this.treeViewer.refresh();
	}
	
	protected boolean isUnderCheckedSuperPackage(final Object element) {
		boolean result = false;
		if (element instanceof FacetSet) {
			final FacetSet facetSet = (FacetSet) element;
			EObject eSuperPackage = facetSet.eContainer();
			while (eSuperPackage != null) {
				if (this.selected.contains(eSuperPackage)) {
					result = true;
					break;
				}
				eSuperPackage = eSuperPackage.eContainer();
			}
		}
		return result;
	}

	protected ICheckStateProvider createCheckStateProvider() {
		return new ICheckStateProvider() {
			public boolean isGrayed(final Object element) {
				return FacetSetSelectionControl.this.isGrayed(element);
			}

			public boolean isChecked(final Object element) {
				return FacetSetSelectionControl.this.isChecked(element);
			}
		};
	}

	protected boolean isGrayed(final Object element) {
		final List<FacetSet> propagation = IResolverManager.DEFAULT
				.selectionPropagation(element, FacetSet.class);
		boolean contains = false;
		if (element instanceof EObject) {
			final EObject eObject = (EObject) element;
			contains = containsSelectedElement(eObject);
		}
		return (contains || isUnderCheckedSuperPackage(element) || propagation
				.contains(element)) && !this.selected.contains(element);
	}

	protected boolean containsSelectedElement(final EObject eObject) {
		boolean contains = false;
		final Iterator<EObject> iterator = eObject.eAllContents();
		while (iterator.hasNext()) {
			final EObject subObject = iterator.next();
			contains = this.selected.contains(subObject);
			if (contains) {
				break;
			}
		}
		return contains;
	}

	protected boolean isChecked(final Object element) {
		// This "|| isGrayed(element)" has been added avoid problems with
		// Windows 7.
		return this.selected.contains(element) || isGrayed(element);
	}

	protected void updateValidationStatus() {
		if (this.treeViewer == null) {
			return;
		}
		final Object[] checkedElements = this.treeViewer.getCheckedElements();
		IStatus newStatus = FacetSetSelectionControl.OK_STATUS;
		if (!this.allowEmpty && checkedElements.length == 0) {
			newStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(Messages.FacetSetSelectionControl_mustSelectAtLeastOneElement,
					Integer.valueOf(this.selectionMaxSize)));
		} else if (checkedElements.length > this.selectionMaxSize) {
			newStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(Messages.FacetSetSelectionControl_mustSelectAtMostNElements,
					Integer.valueOf(this.selectionMaxSize)));
		} else {
			for (final Object checkedElement : checkedElements) {
				final IStatus elementStatus = validateElement(checkedElement);
				if (elementStatus.getSeverity() >= IStatus.ERROR) {
					newStatus = elementStatus;
					break;
				}
			}
		}
		setValidationStatus(newStatus);
	}
	
	protected static IStatus validateElement(final Object checkedElement) {
		IStatus status = FacetSetSelectionControl.OK_STATUS;
		if (!(checkedElement instanceof FacetSet)) {
			status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, FacetSetSelectionControl.ONLY_FACET_SETS);
		}
		return status;
	}

	protected boolean isAvailable(final FacetSet facetSet) {
		boolean bAvailable = false;
		if (this.available.contains(facetSet)) {
			bAvailable = true;
		} else {
			EPackage parent = facetSet.getESuperPackage();
			while (parent != null) {
				if (this.available.contains(parent)) {
					bAvailable = true;
					break;
				}
				parent = parent.getESuperPackage();
			}
		}
		return bAvailable;
	}

	protected void setValidationStatus(final IStatus status) {
		this.validationStatus = status;
	}

	public IStatus getValidationStatus() {
		return this.validationStatus;
	}

	public boolean isErrorStatus() {
		return this.validationStatus == null || this.validationStatus.getSeverity() >= IStatus.ERROR;
	}

	protected ILabelProvider createLabelProvider() {
		return new LabelProvider() {
			@Override
			public String getText(final Object element) {
				String text;
				if (element instanceof ENamedElement) {
					final ENamedElement namedElement = (ENamedElement) element;
					text = namedElement.getName();
				} else {
					text = super.getText(element);
				}
				return text;
			}

			@Override
			public Image getImage(final Object element) {
				return ImageUtils.getImage(element);
			}
		};
	}

	// @SuppressWarnings("static-method") : meant to be overridden
	@SuppressWarnings("static-method")
	protected IContentProvider createContentProvider() {
		return new FacetSetTreeContentProvider(false, true, true);
	}

	public CheckboxTreeViewer getTreeViewer() {
		return this.treeViewer;
	}

	public boolean isDisposed() {
		return this.treeViewer.getTree().isDisposed();
	}

	public void selectAll() {
		this.selected.clear();
		this.selected.addAll(this.available);
		this.treeViewer.refresh();
	}

	public void deselectAll() {
		this.selected.clear();
		this.treeViewer.refresh();
	}

	public Control getControl() {
		Control control = null;
		if (this.treeViewer != null) {
			control = this.treeViewer.getTree();
		}
		return control;
	}
}

Back to the top