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
|
/*****************************************************************************
* Copyright (c) 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:
*
* CEA LIST - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.sysml.diagram.blockdefinition.utils;
import java.util.Iterator;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PreferencesConstantsHelper;
import org.eclipse.papyrus.sysml.diagram.blockdefinition.Activator;
import org.eclipse.papyrus.sysml.diagram.blockdefinition.provider.ElementTypes;
import org.eclipse.papyrus.sysml.diagram.common.utils.SysMLGraphicalTypes;
import org.eclipse.papyrus.uml.diagram.common.locator.PortPositionLocatorUtils;
import org.eclipse.papyrus.uml.diagram.common.utils.UMLGraphicalTypes;
/**
* This class fix Port and FlowPort location during diagram opening (no edit part exists yet).
*/
public class FixPortsLocationOnOpening {
/**
* This method fix the diagram Port and FlowPort locations.
* @param diagram the diagram
*/
public void fix(Diagram diagram) {
// Parse diagram content
Iterator<EObject> it = diagram.eAllContents();
while(it.hasNext()) {
EObject current = it.next();
// Select Port and FlowPorts
if (! (current instanceof Shape)) {
continue;
}
Shape portView = (Shape)current;
String currentType = (portView).getType();
if (SysMLGraphicalTypes.SHAPE_SYSML_FLOWPORT_AS_AFFIXED_ID.equals(currentType) || UMLGraphicalTypes.SHAPE_UML_PORT_AS_AFFIXED_ID.equals(currentType)) {
int borderItemOffset = 10;
View parentView = ViewUtil.getViewContainer(portView);
if (parentView instanceof Shape){
Shape parentShape = (Shape)parentView;
Bounds portViewBounds = (Bounds) portView.getLayoutConstraint();
Bounds parentViewBounds = (Bounds) parentShape.getLayoutConstraint();
final Rectangle portBounds = new Rectangle(portViewBounds.getX(), portViewBounds.getY(), portViewBounds.getWidth(), portViewBounds.getHeight());
int parentWidth = parentViewBounds.getWidth();
int parentHeight = parentViewBounds.getHeight();
if ((parentWidth == -1) && (parentHeight == -1)) {
// warning the size may not be set in notation (default size), in such a case get default size from preferences.
String parentPrefKey = ElementTypes.DIAGRAM_ID + "_" + parentShape.getType();
parentWidth = Activator.getInstance().getPreferenceStore().getInt(PreferencesConstantsHelper.getElementConstant(parentPrefKey, PreferencesConstantsHelper.WIDTH));
parentHeight = Activator.getInstance().getPreferenceStore().getInt(PreferencesConstantsHelper.getElementConstant(parentPrefKey, PreferencesConstantsHelper.HEIGHT));
}
final Rectangle parentBounds = new Rectangle(parentViewBounds.getX(), parentViewBounds.getY(), parentWidth, parentHeight);
// Calculate the valid location based on currently stored location and parent bounds
final Rectangle validLocation = PortPositionLocatorUtils.getBorderLocation(parentBounds, portBounds, borderItemOffset);
// Fix when current location is not the valid location (only possible if parent size is set)
if (! portBounds.equals(validLocation)) {
TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(diagram);
Command fixCommand = new FixLocationCommand(editingDomain, "Fix Port location on opening", portViewBounds, validLocation);
editingDomain.getCommandStack().execute(fixCommand);
}
}
}
}
}
/**
* This command set the Port view with valid locations.
*/
public class FixLocationCommand extends RecordingCommand {
/** Port view bounds. */
private Bounds portViewBounds;
/** Port valid bounds (only the location is used here). */
private Rectangle portValidBounds;
/** Constructor. */
public FixLocationCommand(TransactionalEditingDomain domain,String label, Bounds portViewBounds, Rectangle portValidBounds) {
super(domain, label);
this.portViewBounds = portViewBounds;
this.portValidBounds = portValidBounds;
}
@Override
protected void doExecute() {
portViewBounds.setX(portValidBounds.x());
portViewBounds.setY(portValidBounds.y());
}
@Override
public boolean canUndo() {
return false;
}
}
}
|