Skip to main content
summaryrefslogtreecommitdiffstats
blob: 67db74e1e3bd0bc5544b7a9c760a84cf6c0cf537 (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
/*******************************************************************************
 * Copyright (c) 2013 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.diagram.internal.factories.extensions;

import com.google.common.base.Predicate;

import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.ResourceAttachmentChange;
import org.eclipse.emf.compare.diagram.internal.extensions.DiagramChange;
import org.eclipse.emf.compare.diagram.internal.extensions.DiagramDiff;
import org.eclipse.emf.compare.diagram.internal.extensions.ExtensionsFactory;
import org.eclipse.emf.compare.diagram.internal.factories.AbstractDiagramChangeFactory;
import org.eclipse.emf.compare.utils.MatchUtil;
import org.eclipse.emf.compare.utils.ReferenceUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationPackage;

/**
 * Factory of diagram changes.
 * 
 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
 */
public class DiagramChangeFactory extends AbstractDiagramChangeFactory {

	/**
	 * Constructor.
	 */
	public DiagramChangeFactory() {
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#getExtensionKind()
	 */
	@Override
	public Class<? extends Diff> getExtensionKind() {
		return DiagramChange.class;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#createExtension()
	 */
	@Override
	public DiagramDiff createExtension() {
		return ExtensionsFactory.eINSTANCE.createDiagramChange();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#setRefiningChanges(org.eclipse.emf.compare.diagram.internal.extensions.DiagramDiff,
	 *      org.eclipse.emf.compare.DifferenceKind, org.eclipse.emf.compare.Diff)
	 */
	@Override
	public void setRefiningChanges(Diff extension, DifferenceKind extensionKind, Diff refiningDiff) {
		// Macroscopic change on a diagram is refined by the unit main change and all unit children related
		// changes.
		extension.getRefinedBy().add(refiningDiff);
		extension.getRefinedBy().addAll(getAllContainedDifferences(refiningDiff));
	}

	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#fillRequiredDifferences(org.eclipse.emf.compare.Comparison, org.eclipse.emf.compare.Diff)
	 */
	@Override
	public void fillRequiredDifferences(Comparison comparison, Diff extension) {
		// nothing
	}

	/**
	 * Predicate to check that the given difference is the main unit difference for this macroscopic add or
	 * delete of diagram.
	 * 
	 * @return The predicate.
	 */
	public static Predicate<Diff> isMainDiffForAddOrDeleteDiagram() {
		return new Predicate<Diff>() {
			public boolean apply(Diff difference) {
				return difference instanceof ResourceAttachmentChange
						&& (isRelatedToAnAddDiagram((ResourceAttachmentChange)difference) || isRelatedToADeleteDiagram((ResourceAttachmentChange)difference));
			}
		};
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#isRelatedToAnExtensionAdd(org.eclipse.emf.compare.ResourceAttachmentChange)
	 */
	@Override
	protected boolean isRelatedToAnExtensionAdd(ResourceAttachmentChange input) {
		return isRelatedToAnAddDiagram(input);
	}

	/**
	 * It checks that the given resource attachment change concerns the add of a diagram.
	 * 
	 * @param input
	 *            The reference change.
	 * @return True if it concerns the add of a node, False otherwise.
	 */
	protected static boolean isRelatedToAnAddDiagram(ResourceAttachmentChange input) {
		return isContainmentOnSemanticDiagram(input) && input.getKind() == DifferenceKind.ADD;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#isRelatedToAnExtensionDelete(org.eclipse.emf.compare.ResourceAttachmentChange)
	 */
	@Override
	protected boolean isRelatedToAnExtensionDelete(ResourceAttachmentChange input) {
		return isRelatedToADeleteDiagram(input);
	}

	/**
	 * It checks that the given resource attachment change concerns the delete of a diagram.
	 * 
	 * @param input
	 *            The reference change.
	 * @return True if it concerns the delete of a node, False otherwise.
	 */
	protected static boolean isRelatedToADeleteDiagram(ResourceAttachmentChange input) {
		return isContainmentOnSemanticDiagram(input) && input.getKind() == DifferenceKind.DELETE;
	}

	/**
	 * It checks that the given difference is on a containment link to a Diagram attached to a semantic
	 * object.
	 * 
	 * @param input
	 *            The difference.
	 * @return True if the difference matches with the predicate.
	 */
	private static boolean isContainmentOnSemanticDiagram(ResourceAttachmentChange input) {
		EObject value = MatchUtil.getContainer(input.getMatch().getComparison(), input);
		return value instanceof Diagram
				&& ReferenceUtil.safeEGet(value, NotationPackage.Literals.VIEW__ELEMENT) != null;
	}

}

Back to the top