Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 984594e82d44ecaed8a051ab87a7d4f1e180a0fb (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
/*****************************************************************************
 * Copyright (c) 2014 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.dev.types.providers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.MetamodelType;
import org.eclipse.gmf.runtime.emf.type.core.SpecializationType;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.dev.types.utils.IElementTypeComparator;

public class ElementTypesContentProvider implements ITreeContentProvider {

	private Map<IElementType, List<SpecializationType>> elementTypesHierarchy = new HashMap<IElementType, List<SpecializationType>>();

	@Override
	public void dispose() {
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if (newInput instanceof Object[]) {
			elementTypesHierarchy.clear();
			for (Object elementType : ((Object[]) newInput)) {
				if (elementType instanceof MetamodelType) {
					if (!elementTypesHierarchy.containsKey(elementType)) {
						elementTypesHierarchy.put((MetamodelType) elementType, new ArrayList<SpecializationType>());
					}
				} else if (elementType instanceof SpecializationType) {
					for (IElementType superType : ((SpecializationType) elementType).getSpecializedTypes()) {
						if (!elementTypesHierarchy.containsKey(superType)) {
							elementTypesHierarchy.put(superType, new ArrayList<SpecializationType>());
						}
						elementTypesHierarchy.get(superType).add((SpecializationType) elementType);
					}
				}
			}
		}
	}

	@Override
	public Object[] getElements(Object inputElement) {
		inputChanged(null, null, inputElement);
		ArrayList<IElementType> result = new ArrayList<IElementType>();
		for (IElementType iElementType : elementTypesHierarchy.keySet()) {
			if (iElementType instanceof MetamodelType) {
				result.add(iElementType);
			}
		}
		Collections.sort(result, new IElementTypeComparator());

		return result.toArray();
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		return elementTypesHierarchy.containsKey(parentElement) ? elementTypesHierarchy.get(parentElement).toArray() : Collections.emptyList().toArray();
	}

	@Override
	public Object getParent(Object element) {
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		return getChildren(element).length == 0 ? false : true;
	}
}

Back to the top