Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 448fe8bb45bc461b4bb4c1080ccb58c134f1ab38 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*****************************************************************************
 * Copyright (c) 2009 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.diagram.clazz.custom.policies;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gef.requests.ReconnectRequest;
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.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramGraphicalViewer;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.diagram.clazz.edit.parts.GeneralizationEditPart;
import org.eclipse.papyrus.diagram.clazz.edit.parts.GeneralizationSetEditPart;
import org.eclipse.papyrus.diagram.common.Activator;
import org.eclipse.swt.widgets.Display;

/**
 * this policy allows displaying generalizationSet without label if it references the same semantic
 * element. It allows placing correctly anchors in this case
 */
public class GeneralizationSetLabelDisplayEditPolicy extends AbstractEditPolicy implements NotificationListener, IPapyrusListener {

	/** stores the host associated semantic element */
	protected EObject hostSemanticElement;

	/**
	 * 
	 * {@inheritDoc}
	 */
	public void activate() {
		View view = (View)getHost().getModel();
		hostSemanticElement = view.getElement();
		getDiagramEventBroker().addNotificationListener(view, this);
		getDiagramEventBroker().addNotificationListener(hostSemanticElement, this);
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	public void deactivate() {
		View view = (View)getHost().getModel();
		getDiagramEventBroker().removeNotificationListener(view, this);
		getDiagramEventBroker().removeNotificationListener(hostSemanticElement, this);

		// removes the reference to the semantic element
		hostSemanticElement = null;
	}

	/**
	 * 
	 * @param editPart
	 * @return
	 */
	public ArrayList<GeneralizationSetEditPart> getAllSameSemanticGeneralizationSet(GeneralizationEditPart editPart) {
		// look for all Generalization set connected to the source location that reference the same
		// generalizationSet
		ArrayList<GeneralizationSetEditPart> result = new ArrayList<GeneralizationSetEditPart>();
		ArrayList linkList = new ArrayList();
		linkList.addAll(editPart.getSourceConnections());
		linkList.addAll(editPart.getTargetConnections());
		// remove reconnected link
		linkList.remove(getHost());
		// get the link that refer the same model element
		GeneralizationSetEditPart edgeToMove = null;
		Iterator iterator = linkList.iterator();
		while(iterator.hasNext()) {
			Object currentObject = iterator.next();
			if(currentObject instanceof GeneralizationSetEditPart) {
				if(((GeneralizationSetEditPart)getHost()).resolveSemanticElement().equals(((GeneralizationSetEditPart)currentObject).resolveSemanticElement())) {
					result.add((GeneralizationSetEditPart)currentObject);
				}
			}
		}
		return result;
	}

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

	/**
	 * 
	 * {@inheritedDoc}
	 */
	public void notifyChanged(Notification notification) {
		if(getHost().getViewer() instanceof DiagramGraphicalViewer) {
			if(getHost() instanceof GeneralizationSetEditPart) {
				String elementID = EMFCoreUtil.getProxyID(((GeneralizationSetEditPart)getHost()).resolveSemanticElement());
				List<EditPart> editpartList = ((DiagramGraphicalViewer)(getHost().getViewer())).findEditPartsForElement(elementID, GeneralizationSetEditPart.class);
				if(editpartList.size() > 1 && editpartList.indexOf(getHost()) > 0) {

					try {

						((IGraphicalEditPart)getHost()).getEditingDomain().runExclusive(new Runnable() {

							public void run() {

								Display.getCurrent().asyncExec(new Runnable() {

									public void run() {
										for(int i = 0; i < getHost().getChildren().size(); i++) {
											SetRequest setRequest = new SetRequest((View)((EditPart)getHost().getChildren().get(i)).getModel(), NotationPackage.eINSTANCE.getView_Visible(), false);
											SetValueCommand setValueCommand = new SetValueCommand(setRequest);
											((IGraphicalEditPart)getHost()).getDiagramEditDomain().getDiagramCommandStack().execute(new ICommandProxy(setValueCommand));
											// refresh();
										}
									}
								});
							}
						});

					} catch (Exception e) {
						Activator.log.error(e);
					}
				}
			}
		}
		if(notification.getNotifier() instanceof RelativeBendpoints) {

			try {

				((IGraphicalEditPart)getHost()).getEditingDomain().runExclusive(new Runnable() {

					public void run() {

						Display.getCurrent().asyncExec(new Runnable() {

							public void run() {
								if(getAllSameSemanticGeneralizationSet(((GeneralizationEditPart)((GeneralizationSetEditPart)getHost()).getTarget())).size() != 0) {
									ReconnectRequest reconnectRequest = new ReconnectRequest();
									reconnectRequest.setType(GraphicalNodeEditPolicy.REQ_RECONNECT_TARGET);
									reconnectRequest.setConnectionEditPart(((GeneralizationSetEditPart)getHost()));
									reconnectRequest.setTargetEditPart(((GeneralizationSetEditPart)getHost()).getTarget());
									Command command = ((GeneralizationSetEditPart)getHost()).getTarget().getCommand(reconnectRequest);
									((GeneralizationSetEditPart)getHost()).getDiagramEditDomain().getDiagramCommandStack().execute(command);
								}
								if(getAllSameSemanticGeneralizationSet(((GeneralizationEditPart)((GeneralizationSetEditPart)getHost()).getSource())).size() != 0) {
									ReconnectRequest reconnectRequest = new ReconnectRequest();
									reconnectRequest.setType(GraphicalNodeEditPolicy.REQ_RECONNECT_SOURCE);
									reconnectRequest.setConnectionEditPart(((GeneralizationSetEditPart)getHost()));
									reconnectRequest.setTargetEditPart(((GeneralizationSetEditPart)getHost()).getSource());
									Command command = ((GeneralizationSetEditPart)getHost()).getSource().getCommand(reconnectRequest);
									((GeneralizationSetEditPart)getHost()).getDiagramEditDomain().getDiagramCommandStack().execute(command);
								}
							}
						});
					}
				});

			} catch (Exception e) {
				Activator.log.error(e);
			}

		}
	}

}

Back to the top