Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 38d71b647c21c0ff8d12ffedf3473d599c084811 (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
/*****************************************************************************
 * 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:
 *  Tatiana Fesenko (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.wizards.kind;

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

import org.eclipse.emf.ecore.EClass;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.infra.viewpoints.configuration.Category;
import org.eclipse.papyrus.infra.viewpoints.configuration.ModelRule;
import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView;
import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * The ContentProvider for DiagramCategory table.
 * Returns available diagram kinds for the given diagram category(ies).
 */
public class DiagramKindContentProvider implements IStructuredContentProvider {

	private final Map<String, Collection<ViewPrototype>> prototypes;
	
	/**
	 * Instantiates a new diagram kind content provider.
	 *
	 */
	public DiagramKindContentProvider() {
		Collection<ViewPrototype> vps = PolicyChecker.getCurrent().getAllPrototypes();
		this.prototypes = new HashMap<String, Collection<ViewPrototype>>();
		for (ViewPrototype vp : vps)
			for (Category category : vp.getCategories())
				cache(category.getName(), vp);
	}
	
	/**
	 * Stores the given diagram prototype in the cache
	 * @param prototype The prototype to cache
	 */
	private void cache(String category, ViewPrototype prototype) {
		if (!prototypes.containsKey(category))
			prototypes.put(category, new ArrayList<ViewPrototype>());
		prototypes.get(category).add(prototype);
	}


	/**
	 * Dispose.
	 *
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	public void dispose() {
		prototypes.clear();
	}

	/**
	 * Input changed.
	 *
	 * @param viewer the viewer
	 * @param oldInput the old input
	 * @param newInput the new input
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

	/**
	 * Gets the elements.
	 *
	 * @param inputElement the input element
	 * @return the elements
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
	 */
	public Object[] getElements(Object inputElement) {
		if(inputElement instanceof Object[]) {
			List<ViewPrototype> result = new ArrayList<ViewPrototype>();
			for (Object next: (Object[])inputElement) {
				if (next instanceof String) {
					String diagramCategory = (String)next;
					result.addAll(getPrototypes(diagramCategory));
				}
			}
			Collections.sort(result, new Comparator<ViewPrototype>() {

				public int compare(ViewPrototype o1, ViewPrototype o2) {
					return o1.getLabel().compareTo(o2.getLabel());
				}
			});
			return result.toArray(new Object[result.size()]);
		}
		if(inputElement instanceof String) {
			String diagramCategory = (String)inputElement;
			List<ViewPrototype> result = getPrototypes(diagramCategory);
			return result.toArray(new Object[result.size()]);
		}
		return null;
	}

	/**
	 * Gets the creation commands.
	 *
	 * @param diagramCategory the diagram category
	 * @return the creation commands
	 */
	protected List<ViewPrototype> getPrototypes(String diagramCategory) {
		List<ViewPrototype> result = new ArrayList<ViewPrototype>();
		if (!prototypes.containsKey(diagramCategory))
			return result;
		EClass rootType = getExpectedRootType(diagramCategory);
		for(ViewPrototype proto : prototypes.get(diagramCategory))
			if (isAllowedOn(proto, rootType))
				result.add(proto);
		return result;
	}
	
	private EClass getExpectedRootType(String category) {
		if ("profile".equals(category))
			return UMLPackage.eINSTANCE.getProfile();
		return UMLPackage.eINSTANCE.getModel();
	}
	
	private boolean isAllowedOn(ViewPrototype prototype, EClass clazz)  {
		PapyrusView current = prototype.getConfiguration();
		while (current != null) {
			for (ModelRule rule : current.getModelRules()) {
				int result = allows(rule, clazz);
				if (result == -1)
					return false;
				if (result == 1)
					return true;
			}
			current = current.getParent();
		}
		return false;
	}
	
	private int allows(ModelRule rule, EClass owner) {
		EClass c = rule.getElement();
		if (c == null || c.isSuperTypeOf(owner)) {
			// matching type => check the application of the required stereotypes
			return rule.isPermit() ? 1 : -1;
		} else {
			// type is not matching => unknown
			return 0;
		}
	}
}

Back to the top