Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3a9fdc516f2a6837c34c499b0cdcd1f15987f9f (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
/*******************************************************************************
 * Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others.
 * 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
 *******************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.xtext.glue.editingdomain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.xtext.resource.XtextResource;

import com.google.common.collect.Lists;

/**
 * @author Knut Wannheden - Initial contribution and API
 * @author Jan Koehnlein
 */
public class ChangeAggregatorAdapter extends EContentAdapter {

	private Collection<EObject> modifiedObjects = new LinkedHashSet<EObject>();

	private boolean isRecording = false;

	private boolean isSuspended = false;

	@Override
	public void notifyChanged(Notification notification) {
		super.notifyChanged(notification);

		if (!doRecord(notification))
			return;

		if (notification.getNotifier() instanceof EObject) {
			recordObjectModification((EObject) notification.getNotifier());
		}
	}

	protected void recordObjectModification(EObject obj) {
		if (obj.eResource() == null || false == obj.eResource() instanceof XtextResource)
			modifiedObjects.remove(obj);
		else
			modifiedObjects.add(obj);
	}

	protected boolean doRecord(Notification notification) {
		if (!isRecording || isSuspended || notification.isTouch())
			return false;

		switch (notification.getEventType()) {
			case Notification.ADD:
			case Notification.ADD_MANY:
			case Notification.MOVE:
			case Notification.REMOVE:
			case Notification.REMOVE_MANY:
			case Notification.SET:
			case Notification.UNSET:
				return true;
			default:
				return false;
		}
	}

	private void reset() {
		modifiedObjects.clear();
	}

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 */
	public void beginRecording() {
		reset();
		isRecording = true;
	}

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 */
	public void endRecording() {
		isRecording = false;
	}

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 * @param isSuspended 
	 */
	public void setSuspended(boolean isSuspended) {
		this.isSuspended = isSuspended;
	}

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 * @return List<EOject>
	 */
	public List<EObject> getModificationRoots() {
		Map<Resource, List<EObject>> resource2ChangePathMap = new HashMap<Resource, List<EObject>>();
		for (EObject eObject : modifiedObjects) {
			if (!eObject.eIsProxy()) {
				Resource resource = eObject.eResource();
				List<EObject> resourceChangePath = resource2ChangePathMap.get(resource);
				if (resourceChangePath == null) {
					resourceChangePath = allContainers(eObject);
					resource2ChangePathMap.put(resource, resourceChangePath);
				} else {
					resourceChangePath.retainAll(allContainers(eObject));
				}
			}
		}
		List<EObject> modificationRoots = new ArrayList<EObject>(resource2ChangePathMap.size());
		for (List<EObject> changePath : resource2ChangePathMap.values()) {
			if (!changePath.isEmpty()) {
				modificationRoots.add(changePath.get(changePath.size() - 1));
			}
		}
		return modificationRoots;
	}

	private LinkedList<EObject> allContainers(EObject eObject) {
		final LinkedList<EObject> allContainers = Lists.newLinkedList();
		allContainers.add(eObject);
		EObject currentContainer = eObject.eContainer();
		final Resource resource = eObject.eResource();
		while (currentContainer != null && resource == currentContainer.eResource()) {
			allContainers.addFirst(currentContainer);
			currentContainer = currentContainer.eContainer();
		}
		return allContainers;
	}

	/**
	 * Only attach to XtextResources
	 */
	@Override
	protected void setTarget(Resource target) {
		if (target instanceof XtextResource) {
			super.setTarget(target);
		}
	}

	@Override
	protected void setTarget(EObject target) {
		if (target.eResource() instanceof XtextResource) {
			super.setTarget(target);
		}
	}

	@Override
	public boolean isAdapterForType(Object type) {
		return type == ChangeAggregatorAdapter.class;
	}

}

Back to the top