Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 061e5619d5409f1b31f9d75c77966ff139bd83da (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*****************************************************************************
 * Copyright (c) 2009-2011 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:
 *  Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.composite.custom.edit.policies;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.InternalTransaction;
import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
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.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeCompartmentEditPart;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.composite.custom.edit.command.CreateBehaviorPortCommand;
import org.eclipse.papyrus.uml.diagram.composite.custom.locators.BehaviorPortLocator;
import org.eclipse.papyrus.uml.diagram.composite.edit.parts.BehaviorPortLinkEditPart;
import org.eclipse.papyrus.uml.diagram.composite.part.UMLDiagramEditorPlugin;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.UMLPackage;

/**
 *This editpolicy is a listener that listen the feature is_behavior of the port. It launches a synchronous thread to create a notation node and the link.
 * This editpolicy calls explicitly BehaviorPortLocator  in order to place the symbol behavior at the good place (inside the composite).
 */
public class BehaviorPortEditPolicy extends GraphicalEditPolicy implements NotificationListener, IPapyrusListener {
	public static String BEHAVIOR_PORT="BehaviorPortPolicy";

	private Port hostSemanticElement;

	public void notifyChanged(Notification notification) {

		if( UMLPackage.eINSTANCE.getPort_IsBehavior().equals(notification.getFeature())){
			udaptePortBehavior();
		}
	}

	protected void udaptePortBehavior() {
		ShapeCompartmentEditPart targetEditPart=getPossibleCompartment(((GraphicalEditPart) getHost()).getParent());
		if(targetEditPart!=null){
			View behaviorPort=getBehaviorPortNode();
			if(hostSemanticElement.isBehavior()&& behaviorPort==null){
				BehaviorPortLocator locator = new BehaviorPortLocator(((GraphicalEditPart) getHost().getParent()).getFigure(), PositionConstants.NONE);
				locator.setBorderItemOffset(60);
				Rectangle rectBehavior =locator.getPreferredLocation(((GraphicalEditPart)getHost()).getFigure().getBounds());
				//obtain coordinate to put the behavior
				Rectangle rectContainer=(targetEditPart).getContentPane().getParent().getBounds();
				rectBehavior=rectBehavior.getTranslated(-rectContainer.x, -rectContainer.y);
				executeBehaviorPortCreation(targetEditPart, ((GraphicalEditPart) getHost()),((GraphicalEditPart) getHost()).getEditingDomain(), rectBehavior);
			}
			else{
				if(!hostSemanticElement.isBehavior()){
					//remove behaviorPort
					executeBehaviorPortDeletion(((GraphicalEditPart) getHost()).getEditingDomain(), behaviorPort);}
			}
		}
	}

	protected void executeBehaviorPortDeletion( final TransactionalEditingDomain domain, final View behaviorNode) {
		try {
			domain.runExclusive(new Runnable() {

				public void run() {
					Display.getCurrent().syncExec(new Runnable() {

						public void run() {
							//because it is asynchrone the comment node maybe become s null
							if( behaviorNode!= null&& TransactionUtil.getEditingDomain(behaviorNode)!=null){
								DeleteCommand command= new DeleteCommand(behaviorNode);
								//use to avoid to put it in the command stack
								Map<String,Boolean> options = new HashMap<String,Boolean>();  
								options.put(Transaction.OPTION_UNPROTECTED, Boolean.TRUE);
								try{
									InternalTransaction it=((InternalTransactionalEditingDomain)  TransactionUtil.getEditingDomain(behaviorNode)).startTransaction(false, options);
									GMFtoEMFCommandWrapper warpperCmd= new GMFtoEMFCommandWrapper (command);
									warpperCmd.execute();
									it.commit();
								}catch(Exception e){
									Activator.log.error(e);
								}
							}
						}
					});
				}
			});
		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	/**
	 * return the comment nodes that represent stereotype properties
	 * @return may be null if nothing is founded
	 */
	protected Node getBehaviorPortNode(){
		View SemanticView=(View) getHost().getModel();

		Edge behaviorPortLink= null;
		@SuppressWarnings("unchecked")
		Iterator<Edge>edgeIterator=SemanticView.getSourceEdges().iterator();
		while(edgeIterator.hasNext()) {
			Edge edge = (Edge)edgeIterator.next();
			if (edge.getType().equals(""+BehaviorPortLinkEditPart.VISUAL_ID)){
				behaviorPortLink=edge;
			}

		}
		if(behaviorPortLink== null){
			return null;
		}
		return (Node)behaviorPortLink.getTarget();

	}

	protected ShapeCompartmentEditPart getPossibleCompartment(EditPart parent){
		ShapeCompartmentEditPart found=null;
		int i=0;
		while(found==null && i < parent.getChildren().size()){
			if( parent.getChildren().get(i) instanceof ShapeCompartmentEditPart){
				found=(ShapeCompartmentEditPart)parent.getChildren().get(i);
			}
			i++;
		}
		return found;
	}

	protected  void executeBehaviorPortCreation(final EditPart editPart, final EditPart port,final TransactionalEditingDomain domain, final Rectangle position){
		try {
			domain.runExclusive(new Runnable() {

				public void run() {
					Display.getCurrent().syncExec(new Runnable() {

						public void run() {
							CreateBehaviorPortCommand command = new CreateBehaviorPortCommand(domain, (View)editPart.getModel(), (View)port.getModel(), position);
							//use to avoid to put it in the command stack
							Map<String,Boolean> options = new HashMap<String,Boolean>();  
							options.put(Transaction.OPTION_UNPROTECTED, Boolean.TRUE);
							try{
								InternalTransaction it=((InternalTransactionalEditingDomain)  domain).startTransaction(false, options);
								command.execute();
								it.commit();
							}catch(Exception e){
								Activator.log.error(e);
							}
						}
					}
						);
				}
			}
				);
		}
		catch(Exception e){

		}
	}
	/**
	 * 
	 * {@inheritDoc}
	 */
	public void activate() {
		// retrieve the view and the element managed by the edit part
		View view = getView();
		if(view == null) {
			return;
		}
		hostSemanticElement = getUMLElement();
		// adds a listener on the view and the element controlled by the
		// editpart
		getDiagramEventBroker().addNotificationListener(view, this);
		if(hostSemanticElement == null) {
			return;
		}
		getDiagramEventBroker().addNotificationListener(hostSemanticElement, this);
		udaptePortBehavior();
	}
	@Override
	public void deactivate() {
		// TODO Auto-generated method stub
		super.deactivate();
		// remove notification on element
		getDiagramEventBroker().removeNotificationListener(hostSemanticElement, this);

	}

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

	/**
	 * Returns the uml element controlled by the host edit part
	 * 
	 * @return the uml element controlled by the host edit part
	 */
	protected Port getUMLElement() {
		EObject element = getView().getElement();
		if(element instanceof Port) {
			return (Port)element;
		}
		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();
	}
}

Back to the top