Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d540944d813cafc3b0df72480c2b7795a01efc9e (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
/*******************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 * 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.uml.diagram.timing.custom.edit.policies;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.EditPartUtils;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.Utils;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * This EditPolicy adds a notification listener on the UML element of its EditPart in order to refresh the notation
 * model in response to changes in the semantic model.
 */
public class FirstEventRefreshEditPolicy extends GraphicalEditPolicy implements NotificationListener, IPapyrusListener {

	public static final String VIEW_REFRESH_ROLE = "ViewRefreshRole"; //$NON-NLS-1$

	private Element umlElement;

	public FirstEventRefreshEditPolicy() {
		super();
	}

	@Override
	public void activate() {
		this.umlElement = getUMLElement();
		if (this.umlElement == null) {
			return;
		}
		// adds a listener on the UML element
		getDiagramEventBroker().addNotificationListener(this.umlElement, this);
		// initial refresh
		refreshView();
	}

	@Override
	public void deactivate() {
		// retrieve the UML element managed by the edit part
		if (this.umlElement == null) {
			return;
		}
		// remove the listener from the UML element
		getDiagramEventBroker().removeNotificationListener(this.umlElement, this);
	}

	/**
	 * Gets the diagram event broker from the editing domain.
	 * 
	 * @return the diagram event broker
	 */
	protected DiagramEventBroker getDiagramEventBroker() {
		final TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
		if (editingDomain != null) {
			return DiagramEventBroker.getInstance(editingDomain);
		}
		return null;
	}

	/**
	 * Returns the uml element controlled by the host edit part
	 * 
	 * @return the uml element controlled by the host edit part
	 */
	protected Element getUMLElement() {
		final View view = getView();
		if (view != null) {
			return (Element) view.getElement();
		}
		return null;
	}

	/**
	 * Returns the view controlled by the host edit part
	 * 
	 * @return the view controlled by the host edit part
	 */
	protected View getView() {
		return (View) getHost().getModel();
	}

	public void notifyChanged(final Notification notification) {
		if (notification.getEventType() == Notification.SET) {
			final Object feature = notification.getFeature();
			if (feature == UMLPackage.eINSTANCE.getTimeObservation_FirstEvent() || feature == UMLPackage.eINSTANCE.getTimeObservation_Event()
					|| feature == UMLPackage.eINSTANCE.getTimeConstraint_FirstEvent() || feature == UMLPackage.eINSTANCE.getConstraint_ConstrainedElement()) {
				refreshView();
			}
		}
	}

	/** Refreshes the View for the element controlled by the edit part with this edit policy */
	public void refreshView() {
		final EditPart compartmentEditPart = EditPartUtils.findParentTimelineCompartment(getHost());
		final Command command = compartmentEditPart.getCommand(AbstractTimelineLayoutPolicy.UPDATE_LAYOUT_REQUEST);
		final TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(getView());
		Utils.executeLaterUnprotected(command, domain);
	}
}

Back to the top