Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7bc3a167fb8e937a806a56edb61d4245d702c508 (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
/*******************************************************************************
 * 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.draw2d.IFigure;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
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.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.uml.diagram.timing.custom.figures.LifelineFigure;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.FigureUtils;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.ViewUtils;

/**
 * This EditPolicy adds a notification listener on the time ruler View that listens for its visibility and updates the
 * {@link LifelineFigure#setDisplayTimeRuler(boolean) LifelineFigure's time ruler visibility}
 */
public class TimeRulerVisibilityRefreshEditPolicy extends GraphicalEditPolicy implements NotificationListener, IPapyrusListener {

	public static final String ROLE = "TimeRulerVisibilityRefreshRole"; //$NON-NLS-1$

	private View timeRulerCompartmentView;

	public TimeRulerVisibilityRefreshEditPolicy() {
		super();
	}

	@Override
	public void activate() {

		this.timeRulerCompartmentView = getListenedView();
		if (this.timeRulerCompartmentView == null) {
			return;
		}
		// adds a listener on the View
		getDiagramEventBroker().addNotificationListener(this.timeRulerCompartmentView, this);
		// initial refresh (so that the figure has the right visibility when the diagram is first opened)
		refreshFigure();
	}

	@Override
	public void deactivate() {
		if (this.timeRulerCompartmentView == null) {
			return;
		}
		// remove the listener from the View
		getDiagramEventBroker().removeNotificationListener(this.timeRulerCompartmentView, 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;
	}

	protected View getListenedView() {
		final View view = getView();
		if (view != null) {
			return ViewUtils.findTimeRulerCompartmentView(view);
		}
		return null;
	}

	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 == NotationPackage.eINSTANCE.getView_Visible()) {
				refreshFigure();
			}
		}
	}

	/**
	 * Refreshes the "displayTimeRuler" attribute of the LifelineFigure so that it matches the visibility of the
	 * TimeRulerCompartment View.
	 */
	public void refreshFigure() {
		final boolean visible = getListenedView().isVisible();
		final IFigure figure = ((IGraphicalEditPart) getHost()).getFigure();
		final LifelineFigure lifelineFigure = FigureUtils.findChildFigureInstance(figure, LifelineFigure.class);
		lifelineFigure.setDisplayTimeRuler(visible);
	}
}

Back to the top