Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c5239271e0c939396afb308822d031f8c5945787 (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
/*****************************************************************************
 * Copyright (c) 2014 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
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.editpolicies;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.gmf.runtime.gef.ui.internal.editpolicies.GraphicalEditPolicyEx;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.gmfdiag.common.Activator;
import org.eclipse.papyrus.infra.gmfdiag.common.decoration.ExternalReferenceMarker;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.ServiceUtilsForEditPart;
import org.eclipse.papyrus.infra.services.decoration.DecorationService;
import org.eclipse.papyrus.infra.services.markerlistener.IPapyrusMarker;

/**
 * A generic EditPolicy which applies {@link ExternalReferenceMarker} to EditParts when they have been imported
 * (According to {@link NotationHelper#isExternalRef(View)}
 *
 * @author Camille Letavernier
 *
 */
// Rely on the GraphicalEditPolicyEx, which is less expensive than adding a listener to detect changes in the containment tree
@SuppressWarnings("restriction")
public class ExternalReferenceEditPolicy extends GraphicalEditPolicyEx {

	/**
	 * The Edit Policy Role/ID
	 */
	public static final Object EDIT_POLICY_ROLE = Activator.ID + ".externalReferenceDecorator"; //$NON-NLS-1$

	/**
	 * Last known value for isExternalReference
	 */
	protected boolean isExternalReference = false;

	protected DecorationService decorationService;

	protected IPapyrusMarker marker;

	protected Adapter listener;

	@Override
	public void activate() {
		super.activate();

		try {
			decorationService = ServiceUtilsForEditPart.getInstance().getService(DecorationService.class, getHost());
			refresh();
		} catch (ServiceException ex) {
			// Ignored; do nothing
		}
	}

	@Override
	public void refresh() {
		if (getView() == null) {
			return;
		}
		if (decorationService == null) {
			return;
		}

		// Add or remove the decoration when the current value is different from the last known value
		if (isExternalRef(getView()) != isExternalReference) {
			isExternalReference = !isExternalReference;
			if (isExternalReference) {
				decorationService.addDecoration(getMarker(), getView());
			} else {
				decorationService.removeDecoration(getMarker().toString());
			}
			// We need to call refresh again, so that the decorator gets display.
			// We shouldn't end up with a StackOverFlow, because we arrive here only when the "isExternalRef" value changes
			getHost().refresh();
		}
	}

	protected boolean isExternalRef(View view) {
		return NotationHelper.isExternalRef(view);
	}

	/**
	 * Returns the marker instance. It is never null
	 *
	 * @return
	 */
	protected IPapyrusMarker getMarker() {
		if (marker == null) {
			marker = new ExternalReferenceMarker(getView());
		}
		return marker;
	}

	/**
	 * Return the view associated to this edit policy
	 *
	 * @return
	 */
	protected View getView() {
		return (View) getHost().getModel();
	}

	@Override
	public void deactivate() {
		super.deactivate();

		if (marker != null && decorationService != null) {
			// Remove the marker from the View to avoid duplication
			decorationService.removeDecoration(getMarker().toString());
			isExternalReference = false;
		}

		marker = null;
		decorationService = null;
	}
}

Back to the top