Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8eb616a8e9a683f1a02488ecfef4d9a3b6a616e6 (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.referencialgrilling;

import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
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.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.DecorationNode;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.AutomaticNotationEditPolicy;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
import org.eclipse.uml2.uml.Element;

/**
 * this class is used to connect a node to the grid
 * X, Y, X+Width, Y+HEIGHT
 *
 */
public class ConnectNodeToGridEditPolicy extends ConnectToGridEditPolicy implements AutomaticNotationEditPolicy, NotificationListener, IGrillingEditpolicy {

	protected GrillingEditpart grillingCompartment=null;

	public static String CONNECT_TO_GRILLING_MANAGEMENT="CONNECT_TO_GRILLING_MANAGEMENT";

	protected DecorationNode rowStart=null;
	protected DecorationNode rowFinish=null;
	protected DecorationNode columnStart=null;
	protected DecorationNode columnFinish=null;

	/**
	 * Constructor.
	 *
	 */
	public ConnectNodeToGridEditPolicy() {
	}

	/**
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#activate()
	 *
	 */
	@Override
	public void activate() {
		super.activate();
		getDiagramEventBroker().addNotificationListener(((EObject)getHost().getModel()), this);
		DiagramEditPart diagramEditPart=getDiagramEditPart(getHost());
		Node node=((Node)((GraphicalEditPart)getHost()).getNotationView());
		try{
			GrillingManagementEditPolicy grilling=(GrillingManagementEditPolicy)diagramEditPart.getEditPolicy(GrillingManagementEditPolicy.GRILLING_MANAGEMENT);
			grilling.cleanUnusedRowAndColumn();
			Node nodeContainer=(Node)(((GraphicalEditPart)getHost()).getNotationView()).eContainer();
			PrecisionRectangle originPosition= NotationHelper.getAbsoluteBounds(nodeContainer);
			Element element=(Element) ((GraphicalEditPart)getHost()).resolveSemanticElement();
			if (grilling!=null){
				PrecisionRectangle p=NotationHelper.getAbsoluteBounds((Node)((GraphicalEditPart)getHost()).getNotationView());
				rowStart=grilling.getRowTolisten(originPosition.y+p.y(), element);
				columnStart=grilling.getColumnTolisten(originPosition.x+p.x(), element);
				getDiagramEventBroker().addNotificationListener(rowStart, this);
				getDiagramEventBroker().addNotificationListener(columnStart, this);
				rowFinish=grilling.getRowTolisten(originPosition.y+p.y()+BoundForEditPart.getHeightFromView(node), element);
				getDiagramEventBroker().addNotificationListener(rowFinish, this);
				columnFinish=grilling.getColumnTolisten(originPosition.x+p.x()+BoundForEditPart.getWidthFromView(node), element);
				getDiagramEventBroker().addNotificationListener(columnFinish, this);



			}
		}catch (NoGrillElementFound e) {
			UMLDiagramEditorPlugin.log.error(e);
		}

	}

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


	/**
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#deactivate()
	 *
	 */
	@Override
	public void deactivate() {
		getDiagramEventBroker().removeNotificationListener(((EObject)getHost().getModel()), this);
		super.deactivate();
	}

	/**
	 * @see org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void notifyChanged(Notification notification) {
		//Display imprecision
		Node nodeContainer=(Node)(((GraphicalEditPart)getHost()).getNotationView()).eContainer();
		PrecisionRectangle originPosition= NotationHelper.getAbsoluteBounds(nodeContainer);
		Bounds currentBounds=(Bounds)	((Node)((GraphicalEditPart)getHost()).getNotationView()).getLayoutConstraint();

		//the ROW has changed so we must update the position of the combined fragment
		if( notification.getEventType()==Notification.SET && notification.getNotifier() instanceof Location && (((EObject)notification.getNotifier()).eContainer().equals(rowStart))){
			//The grill is in absolute
			//compute next position for View.bound.y	
			Location boundsRow=(Location)	((Node)rowStart).getLayoutConstraint();
			int newY=boundsRow.getY()-originPosition.y();
			updateNodePositionOfControler(currentBounds.getX(), newY);
		}
		//the COLUMN has changed so we must update the postion of the combined fragment
		if( notification.getEventType()==Notification.SET && notification.getNotifier() instanceof Location && (((EObject)notification.getNotifier()).eContainer().equals(columnStart))){
			//The grill is in absolute
			Location boundsColumn=(Location)	((Node)columnStart).getLayoutConstraint();
			int newX=boundsColumn.getX()-originPosition.x();
			updateNodePositionOfControler(newX, currentBounds.getY());
			
		}
		if( notification.getEventType()==Notification.SET && notification.getNotifier() instanceof Location && (((EObject)notification.getNotifier()).eContainer().equals(rowFinish))){
			//The grill is in absolute
			Location boundsColumn=(Location)	((Node)rowFinish).getLayoutConstraint();
			int newHeight=boundsColumn.getY()-originPosition.y()-currentBounds.getY();
			updateSizeOfControler(currentBounds.getWidth(), newHeight);
			
		}
		if( notification.getEventType()==Notification.SET && notification.getNotifier() instanceof Location && (((EObject)notification.getNotifier()).eContainer().equals(columnFinish))){
			//The grill is in absolute
			Location boundsColumn=(Location)	((Node)columnFinish).getLayoutConstraint();
			int newX=boundsColumn.getX()-originPosition.x()-currentBounds.getX();
			updateSizeOfControler(newX, currentBounds.getHeight());
			
		}
		
		//UPDATE COLUM AND ROW of THE GRID
		if( notification.getEventType()==Notification.SET && notification.getNotifier() instanceof Bounds && (((EObject)notification.getNotifier()).eContainer().equals(((EObject)getHost().getModel())))){
			PrecisionRectangle p=NotationHelper.getAbsoluteBounds((Node)((GraphicalEditPart)getHost()).getNotationView());

			if( notification.getFeature().equals(NotationPackage.eINSTANCE.getSize_Height())){
				int newY=p.y+p.height;
				updatePositionGridAxis(rowFinish, 0, newY);

			}
			if( notification.getFeature().equals(NotationPackage.eINSTANCE.getSize_Width())){
				int newX=p.x+p.width;
				updatePositionGridAxis(columnFinish, newX,0);

			}
			if( notification.getFeature().equals(NotationPackage.eINSTANCE.getLocation_Y())){
				//compute next position for RowStart
				int newY=notification.getNewIntValue()+originPosition.y();
				updatePositionGridAxis(rowStart, 0, newY);
					
			}
			if( notification.getFeature().equals(NotationPackage.eINSTANCE.getLocation_X())){
				//compute next position for RowStart
				int newX=notification.getNewIntValue()+originPosition.x();
				updatePositionGridAxis(columnStart, newX, 0);
					
			}
		}
	}
}

Back to the top