Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6fb03239ea9d08af5b6f0171f46869fa7e9cb548 (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
/*******************************************************************************
 * 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 org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.ResourceSetListener;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.xtext.resource.XtextResource;

/**
 * Reloads the semantic root element (the element of the diagram) on changes and refreshes the diagram. GMF does by
 * default not listen to such events which can occur in an {@link XtextResource}, if the document changes close to the
 * root element.
 * 
 * Activate an instance of this in the {@link EditPart#activate()} method of the {@link DiagramEditPart}.
 * 
 * @author koehnlein
 */
public class SemanticRootUnloadListener implements ResourceSetListener {

	private DiagramEditPart rootEditPart;
	private EObject semanticRootElement;

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 * @param rootEditPart 
	 *
	 */
	public SemanticRootUnloadListener(DiagramEditPart rootEditPart) {
		this.rootEditPart = rootEditPart;
		this.semanticRootElement = rootEditPart.resolveSemanticElement();
	}

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 */
	public void activate() {
		rootEditPart.getEditingDomain().addResourceSetListener(this);
	}

	/**
	 * This element comes from the XText/GMF integration example, and was not originally documented.
	 */
	public void deactivate() {
		rootEditPart.getEditingDomain().removeResourceSetListener(this);
	}

	public NotificationFilter getFilter() {
		return new NotificationFilter.Custom() {
			@Override
			public boolean matches(Notification notification) {
				int featureID = notification.getFeatureID(Resource.class);
				Object notifier = notification.getNotifier();
				int eventType = notification.getEventType();
				return notification.getOldValue() == semanticRootElement && featureID == Resource.RESOURCE__CONTENTS
						&& (eventType == Notification.REMOVE || eventType == Notification.SET)
						&& notifier instanceof Resource;
			}
		};
	}

	public boolean isAggregatePrecommitListener() {
		return false;
	}

	public boolean isPostcommitOnly() {
		return true;
	}

	public boolean isPrecommitOnly() {
		return false;
	}

	public void resourceSetChanged(ResourceSetChangeEvent event) {
		semanticRootElement = rootEditPart.resolveSemanticElement();
		rootEditPart.refresh();
	}

	public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException {
		return null;
	}

}

Back to the top