This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
DefaultAttributeMonitor.java
86 lines (74 loc) · 2.93 KB
/
DefaultAttributeMonitor.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
package org.eclipse.incquery.runtime.triggerengine.specific;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.incquery.runtime.api.IPatternMatch;
import org.eclipse.incquery.runtime.triggerengine.notification.AttributeMonitor;
public class DefaultAttributeMonitor<MatchType extends IPatternMatch> extends AttributeMonitor<MatchType> {
private ChangeListener changeListener;
private Map<IObservableValue, MatchType> observableMap;
private Map<MatchType, List<IObservableValue>> observableMapReversed;
public DefaultAttributeMonitor() {
super();
this.changeListener = new ChangeListener();
this.observableMap = new HashMap<IObservableValue, MatchType>();
this.observableMapReversed = new HashMap<MatchType, List<IObservableValue>>();
}
private class ChangeListener implements IValueChangeListener {
@Override
public void handleValueChange(ValueChangeEvent event) {
IObservableValue val = event.getObservableValue();
if (val != null) {
notifyListeners(observableMap.get(val));
}
}
}
@Override
public void registerFor(MatchType match) {
List<IObservableValue> values = new ArrayList<IObservableValue>();
for (String param : match.parameterNames()) {
Object location = match.get(param);
List<IObservableValue> observableValues = observeAllAttributes(changeListener, location);
values.addAll(observableValues);
}
//inserting {observable,match} pairs
for (IObservableValue val : values) {
observableMap.put(val, match);
}
//inserting {match, list(observable)} pairs
observableMapReversed.put(match, values);
}
private List<IObservableValue> observeAllAttributes(IValueChangeListener changeListener, Object object) {
List<IObservableValue> affectedValues = new ArrayList<IObservableValue>();
if (object instanceof EObject) {
for (EStructuralFeature feature : ((EObject) object).eClass().getEAllStructuralFeatures()) {
IObservableValue val = EMFProperties.value(feature).observe(object);
affectedValues.add(val);
val.addValueChangeListener(changeListener);
}
}
return affectedValues;
}
@Override
public void unregisterForAll() {
for (MatchType match : observableMapReversed.keySet()) {
unregisterFor(match);
}
}
@Override
public void unregisterFor(MatchType match) {
List<IObservableValue> observables = observableMapReversed.get(match);
if (observables != null) {
for (IObservableValue val : observables) {
val.removeValueChangeListener(changeListener);
}
}
}
}