Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9edbb8a8e13e28f65a6281625ecf4322842e77c0 (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
/*****************************************************************************
 * 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.infra.constraints.runtime;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

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.DisplayUnit;
import org.eclipse.papyrus.infra.constraints.constraints.Constraint;

/**
 * The default implementation for ConstraintEngine
 * 
 * @author Camille Letavernier
 */
public abstract class DefaultConstraintEngine<E extends DisplayUnit> implements ConstraintEngine<E> {

	protected final Set<Constraint> constraints = new LinkedHashSet<Constraint>();

	public abstract void refresh();

	public void addConstraint(ConstraintDescriptor descriptor) {
		Constraint constraint = ConstraintFactory.getInstance().createFromModel(descriptor);
		if(constraint != null) {
			constraints.add(constraint);
		}
	}

	public Set<E> getDisplayUnits(final ISelection forSelection) {
		Set<E> result = new HashSet<E>();

		IStructuredSelection selection;
		if(forSelection instanceof IStructuredSelection) {
			selection = (IStructuredSelection)forSelection;
		} else {
			return result;
		}

		Set<Constraint> matchedConstraints = match(selection);

		return getDisplayUnits(matchedConstraints);
	}

	private Set<Constraint> match(final IStructuredSelection selection) {
		Set<Constraint> matchedConstraints = new LinkedHashSet<Constraint>();

		for(Constraint c : constraints) {
			int elementMultiplicity = c.getDescriptor().getDisplay().getElementMultiplicity();
			int selectionSize = selection.size();
			if(elementMultiplicity == 1) {
				if(selectionSize == 1) {
					if(c.match(selection.getFirstElement())) {
						matchedConstraints.add(c);
					}
				}
			} else if(elementMultiplicity == selectionSize || elementMultiplicity < 0) {
				boolean allMatch = true;
				Iterator<?> selectionIterator = selection.iterator();
				while(selectionIterator.hasNext()) {
					Object selectedItem = selectionIterator.next();
					if(!c.match(selectedItem)) {
						allMatch = false;
						break;
					}
				}
				if(allMatch) {
					matchedConstraints.add(c);
				}
			}
		}

		//		String logValue;
		//
		//		logValue = "Filtered Constraints : "; //$NON-NLS-1$
		//		for(Constraint constraint : matchedConstraints) {
		//			logValue += constraint.getDescriptor().getName() + ", ";
		//		}
		//		Activator.log.warn(logValue);

		resolveConstraintConflicts(matchedConstraints);

		//		logValue = "Filtered Constraints : "; //$NON-NLS-1$
		//		for(Constraint constraint : matchedConstraints) {
		//			logValue += constraint.getDescriptor().getName() + ", ";
		//		}
		//
		//		Activator.log.warn(logValue);

		return matchedConstraints;
	}

	private void resolveConstraintConflicts(final Set<Constraint> matchedConstraints) {
		Set<Constraint> constraintsSet = new HashSet<Constraint>(matchedConstraints);
		for(Constraint c : constraintsSet) {
			for(Constraint c2 : constraintsSet) {
				if(c == c2) {
					continue;
				}

				if(c.getDescriptor().getOverriddenConstraints().contains(c2.getDescriptor())) {
					matchedConstraints.remove(c2);
					continue;
				}

				if(c2.getDescriptor().isOverrideable() && c.overrides(c2)) {
					matchedConstraints.remove(c2);
					continue;
				}
			}
		}
	}

	private Set<E> getDisplayUnits(final Set<Constraint> matchedConstraints) {
		Set<E> displayUnits = new LinkedHashSet<E>();
		for(Constraint c : matchedConstraints) {
			displayUnits.add((E)c.getDescriptor().getDisplay());
		}
		return displayUnits;
	}
}

Back to the top