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
|
/*****************************************************************************
* Copyright (c) 2015 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.common.editpolicies;
import java.util.Collections;
import java.util.Map;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.DefaultCreationEditPolicy;
import org.eclipse.papyrus.infra.gmfdiag.common.snap.NodeSnapHelper;
import org.eclipse.papyrus.uml.diagram.common.locator.PortPositionLocator;
import org.eclipse.papyrus.uml.diagram.common.service.AspectUnspecifiedTypeCreationTool;
public class SideAffixedNodesCreationEditPolicy extends DefaultCreationEditPolicy {
/**
* {@inheritDoc}
*/
@Override
protected ICommand getSetBoundsCommand(CreateViewRequest request, CreateViewRequest.ViewDescriptor descriptor) {
ICommand setBoundsCommand = null;
TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
// Retrieve parent location
Point parentLoc = getHostFigure().getBounds().getLocation().getCopy();
final Point realWantedLocation;
Map<?, ?> params = request.getExtendedData();
Point realLocation = (Point) params.get(AspectUnspecifiedTypeCreationTool.INITIAL_MOUSE_LOCATION_FOR_CREATION);
if (realLocation != null) {
realWantedLocation = realLocation.getCopy();
} else {
// we use this location to be able to create Port in the corners of the figure
realWantedLocation = request.getLocation().getCopy();
}
// Compute relative creation location
Point requestedLocation = realWantedLocation.getCopy();
getHostFigure().translateToRelative(requestedLocation);
// Create proposed creation bounds and use the locator to find the expected position
PortPositionLocator locator = getPositionLocator();
if (locator == null) {
return null;
}
final Rectangle preferredBounds = locator.getPreferredLocation(new Rectangle(requestedLocation, new Dimension(20, 20)));
Rectangle retainedBounds = preferredBounds.getCopy();
// find the current side of the wanted position
final Rectangle parentBounds = getHostFigure().getBounds().getCopy();
// break all!!! getHostFigure().translateToAbsolute(parentBounds);
locator.setConstraint(preferredBounds.getCopy().translate(parentBounds.getLocation().getNegated()));
int currentSide = locator.getCurrentSideOfParent();
if (request.isSnapToEnabled() && currentSide != PositionConstants.NORTH_EAST && currentSide != PositionConstants.NORTH_WEST && currentSide != PositionConstants.SOUTH_EAST && currentSide != PositionConstants.SOUTH_WEST) { // request for snap port at the
// // creation
// we find the best location with snap
Point wantedPoint = preferredBounds.getLocation();
getHostFigure().translateToAbsolute(wantedPoint);
Rectangle portBounds = new Rectangle(wantedPoint, new Dimension(20, 20));
NodeSnapHelper helper = new NodeSnapHelper((SnapToHelper) getHost().getAdapter(SnapToHelper.class), portBounds, false, false, true);
final ChangeBoundsRequest tmpRequest = new ChangeBoundsRequest("move"); //$NON-NLS-1$
tmpRequest.setEditParts(Collections.emptyList());
tmpRequest.setSnapToEnabled(true);
tmpRequest.setLocation(portBounds.getLocation());
helper.snapPoint(tmpRequest);
preferredBounds.translate(tmpRequest.getMoveDelta());
switch (currentSide) {
case PositionConstants.NORTH:
case PositionConstants.SOUTH:
preferredBounds.y = retainedBounds.y;
break;
case PositionConstants.EAST:
case PositionConstants.WEST:
preferredBounds.x = retainedBounds.x;
break;
default:
break;
}
}
// Convert the calculated preferred bounds as relative to parent location
Rectangle creationBounds = preferredBounds.getTranslated(parentLoc.getNegated());
setBoundsCommand = new SetBoundsCommand(editingDomain, DiagramUIMessages.SetLocationCommand_Label_Resize, descriptor, creationBounds);
return setBoundsCommand;
}
protected PortPositionLocator getPositionLocator() {
return new PortPositionLocator(getHostFigure(), PositionConstants.NONE);
}
/**
* {@inheritDoc}
*/
private IFigure getHostFigure() {
return ((GraphicalEditPart) getHost()).getFigure();
}
}
|