/
GenModelUpdateGenFeatureContainment.java
111 lines (94 loc) · 3.38 KB
/
GenModelUpdateGenFeatureContainment.java
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
/*******************************************************************************
* Copyright (c) 2014, 2023 Obeo
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.ecoretools.design.service;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.emf.codegen.ecore.genmodel.GenFeature;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.sirius.business.api.session.ModelChangeTrigger;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ext.base.Options;
/**
* a {@link ModelChangeTrigger} which update GenFeature children/notify/create
* child based on the EReference.containment change. context.
*
* @author Cedric Brun <cedric.brun@obeo.fr>
*
*/
public class GenModelUpdateGenFeatureContainment implements ModelChangeTrigger {
private Session session;
public GenModelUpdateGenFeatureContainment(Session set) {
this.session = set;
}
public static final NotificationFilter IS_EREFENCE_CONTAINMENT = new NotificationFilter.Custom() {
public boolean matches(Notification notification) {
return (notification.getFeature() == EcorePackage.eINSTANCE
.getEReference_Containment());
}
};
//
public static final NotificationFilter SHOULD_UPDATE = GenModelAutoReload.IS_TOUCH
.negated().and(
GenModelAutoReload.IS_ECORE.and(IS_EREFENCE_CONTAINMENT));
public Option<Command> localChangesAboutToCommit(
Collection<Notification> notifications) {
final Collection<GenFeature> toBeUpdated = new ArrayList<>();
for (Notification notif : notifications) {
if (SHOULD_UPDATE.matches(notif)) {
/*
* this is an EReference now being containment or no more.
*/
EReference updatedRef = (EReference) notif.getNotifier();
for (Setting xRef : session.getSemanticCrossReferencer()
.getInverseReferences(updatedRef)) {
if (xRef.getEObject() instanceof GenFeature) {
toBeUpdated.add((GenFeature) xRef.getEObject());
}
}
}
}
if (toBeUpdated.size() > 0) {
Command result = new RecordingCommand(
session.getTransactionalEditingDomain()) {
@Override
protected void doExecute() {
for (GenFeature genFeature : toBeUpdated) {
if (genFeature.getEcoreFeature() instanceof EReference) {
if (((EReference) genFeature.getEcoreFeature())
.isContainment()) {
genFeature.setChildren(true);
genFeature.setNotify(true);
genFeature.setCreateChild(true);
} else {
genFeature.setChildren(false);
genFeature.setNotify(false);
genFeature.setCreateChild(false);
}
}
}
}
};
return Options.newSome(result);
}
return Options.newNone();
}
public int priority() {
return GenModelAutoReload.PRIORITY + 1;
}
}