Skip to main content
summaryrefslogtreecommitdiffstats
blob: dfdc14db294170fe9fcef6e7c3d1608ad986c313 (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
package org.eclipse.papyrus.infra.gmfdiag.common.editpolicies;

/*****************************************************************************
 * 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:
 *  Céline Janssens (ALL4TEC) celine.janssens@all4tec.net - Initial API and implementation
 *
 *****************************************************************************/


import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gef.requests.AlignmentRequest;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;

/**
 * This EditPolicy provides the command for the alignment. In the case of
 * Label.
 */
public class LabelAlignmentEditPolicy extends AbstractEditPolicy{

	/** 
	 * the key for the Port Alignment EditPolicy 
	 */
	public static final String LABEL_ALIGNMENT_KEY = "Label Alignment"; //$NON-NLS-1$


	/** Integer corresponding to the alignment type ( p.e. Left = 1, Bottom = 32) */
	private int alignment ;

	/** Current edit Part */
	private GraphicalEditPart label ;

	/** Reference rectangle on which the Label is aligned */
	private Rectangle refBounds;

	/** Bounds of the current label */
	private Rectangle bounds;

	/**
	 * Get Command associate to the corresponding Request
	 * Only REQ_ALIGN request return a Command
	 * @return Command to move the label in case of REQ_ALIGN, null if not.
	 * @param request Request for which a command is required. 
	 */
	public Command getCommand(Request request) {
		Command cmd = null;
		if (REQ_ALIGN.equals(request.getType())){
			 cmd = getAlignLabelCommand ((AlignmentRequest) request);
		}
		return cmd;
	}

	/**
	 * Get The alignment Command for the corresponding Alignment Request.
	 * This method transforms the alignment Request into a move request and return the move command associated to this new Request.
	 * 
	 * @param request Alignment Request for which the Alignment Command is returned 
	 * @return Alignment command 
	 */
	private Command getAlignLabelCommand(AlignmentRequest request) {

		alignment = request.getAlignment();		
		// get the Alignment rectangle on which the selected edit part should be aligned.
		refBounds = request.getAlignmentRectangle().getCopy();

		label = (GraphicalEditPart) getHost(); 
		bounds = getAbsolutePosition(label);

		Point moveDelta = new Point(0, 0);

		if (isAlignmentAllowed()){
			switch (alignment ){
			case PositionConstants.LEFT:
				moveDelta.setX(refBounds.x - bounds.x );
				break;
			case PositionConstants.RIGHT:
				moveDelta.setX(refBounds.getRight().x - bounds.getRight().x );
				break;
			case PositionConstants.TOP:
				moveDelta.setY(refBounds.y - bounds.y );
				break;
			case PositionConstants.BOTTOM:
				moveDelta.setY(refBounds.getBottom().y - bounds.getBottom().y );
				break;
			case PositionConstants.CENTER:
				moveDelta.setX(refBounds.getTop().x - bounds.getTop().x );
				break;
			case PositionConstants.MIDDLE:
				moveDelta.setY(refBounds.getLeft().y - bounds.getLeft().y );
				break;
			}


			//build the Move request
			ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_MOVE);

			req.setEditParts(((ChangeBoundsRequest)request).getEditParts());
			req.setMoveDelta(moveDelta);
			req.setExtendedData(request.getExtendedData());

			return getHost().getCommand(req);

		} 
		else {
			// if alignment not allowed return no command
			return null;

		}

	}

	/**
	 * Determine if the current edit part can be aligned.
	 * 
	 * The label cannot be aligned if its connector target or source moves as well
	 * Because if the source/target moves, the connector moves as well and the label linked to the connector translates with the Edge
	 * Same if the Affixed node is selected. 
	 */
	private boolean isAlignmentAllowed() {
		EditPart parent = label.getParent();
		boolean isAllow = true;
		if (parent instanceof AbstractConnectionEditPart){
			// Label alignment is not allowed if it should moves at the same time as the connector
			// That means if the Source or target of the connector is part of the selection
			int sourceSelectionType = ((ConnectionEditPart)label.getParent()).getSource().getSelected();
			int targetSelectionType = ((ConnectionEditPart)label.getParent()).getTarget().getSelected();

			isAllow = !((sourceSelectionType == EditPart.SELECTED)||(targetSelectionType == EditPart.SELECTED)) ;

		} else if (parent instanceof AbstractBorderItemEditPart) {
			// if the label is an affixed label and if the affixed node is part of the selection, the label is not aligned
			isAllow =  !(parent.getSelected() == EditPart.SELECTED);
		}

		return isAllow;
	}


	/**
	 * 
	 * A {@link Rectangle} with the absolute position from the {@link EditPart}
	 * 
	 * @param ep
	 *        the {@link EditPart} that we want the position
	 * @return a {@link Rectangle} with the absolute position from the {@link EditPart}
	 * 
	 */
	public static Rectangle getAbsolutePosition(EditPart ep) {
		GraphicalEditPart part = (GraphicalEditPart)ep;
		Rectangle rect = part.getFigure().getBounds().getCopy();
		part.getFigure().translateToAbsolute(rect);
		return rect;
	}



}

Back to the top