Skip to main content
summaryrefslogtreecommitdiffstats
blob: 08a2d76ecac1b96a1a634c5ea6c99264af0f5565 (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
/*******************************************************************************
 * 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 370806 - [table] rewrite the "allowed contents" query selection dialog for v0.2
 *    Gregoire Dupe (Mia-Software) - Bug 372626 - Aggregates
 *******************************************************************************/
package org.eclipse.emf.facet.efacet.ui.internal.widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.facet.util.core.internal.exported.IFilter;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class ETypedElementSelectionControlContentProvider implements ITreeContentProvider {

	private final IFilter<EObject> childrenFilter;

	public ETypedElementSelectionControlContentProvider(final IFilter<EObject> childrenFilter) {
		this.childrenFilter = childrenFilter;
	}

	private Object input;

	public Object[] getElements(final Object inputElement) {
		// @SuppressWarnings("unchecked") : this is the type setInput accepts
		@SuppressWarnings("unchecked")
		final Collection<? extends EObject> containers = (Collection<? extends EObject>) inputElement;
		return containers.toArray();
	}

	public Object[] getChildren(final Object parentElement) {
		Object[] children;
		if (parentElement instanceof EObject) {
			final EObject container = (EObject) parentElement;
			final List<EObject> filteredChildren = computeFilteredChildren(container);
			children = filteredChildren.toArray();
		} else {
			children = new Object[0];
		}
		return children;
	}

	public Object getParent(final Object element) {
		Object parent = null;
		if (element instanceof EObject) {
			parent = ((EObject) element).eContainer();
		}
		return parent;
	}

	// @SuppressWarnings("unchecked") : the input type is checked by setInput
	@SuppressWarnings("unchecked")
	public boolean hasChildren(final Object element) {
		boolean result;
		final Collection<? extends EObject> rootElements = (Collection<? extends EObject>) this.input;
		if (rootElements.contains(element)) {
			// avoid computing all the children of all the top-level elements
			// since this could be very expensive
			result = true;
		} else if (element instanceof EObject) {
			result = getChildren(element).length > 0;
		} else {
			result = false;
		}
		return result;
	}

	public void inputChanged(final Viewer viewer, final Object oldInput,
			final Object newInput) {
		this.input = newInput;
	}

	public void dispose() {
		// nothing
	}

	protected List<EObject> computeFilteredChildren(final EObject parentEObject) {
		final List<EObject> children = new ArrayList<EObject>();
		final EList<EObject> eContents = parentEObject.eContents();
		children.addAll(eContents);
		final List<EObject> filteredChildren = new ArrayList<EObject>();
		for (final EObject eObject : children) {
			if (this.childrenFilter.filter(eObject)) {
				filteredChildren.add(eObject);
			}
		}
		return filteredChildren;
	}
}

Back to the top