Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff5c8d7104d44bde18211b2417dd89a0bca1cc42 (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
/**
 * 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:
 *  CEA LIST - Initial API and implementation
 */
package org.eclipse.papyrus.uml.diagram.statemachine.custom.policies;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeCompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateUnspecifiedTypeRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.DropObjectsRequest;
import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.commands.SemanticAdapter;
import org.eclipse.papyrus.uml.diagram.statemachine.custom.commands.CustomFirstRegionInCompositeStateCreateElementCommand;
import org.eclipse.papyrus.uml.diagram.statemachine.custom.helpers.Zone;
import org.eclipse.papyrus.uml.diagram.statemachine.edit.parts.RegionEditPart;
import org.eclipse.papyrus.uml.diagram.statemachine.providers.UMLElementTypes;

public class CustomStateCompartmentCreationEditPolicy extends CreationEditPolicy {
	IFigure sizeOnDropFeedback = null;
	String dropLocation = Zone.NONE;

	@Override
	public Command getCommand(Request request) {
		// CHECK THIS
		TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
		CompositeTransactionalCommand cc = new CompositeTransactionalCommand(editingDomain, DiagramUIMessages.AddCommand_Label);
		if (understandsRequest(request)) {
			if (request instanceof CreateUnspecifiedTypeRequest) {
				CreateUnspecifiedTypeRequest unspecReq = (CreateUnspecifiedTypeRequest) request;
				for (Iterator<?> iter = unspecReq.getElementTypes().iterator(); iter.hasNext();) {
					IElementType elementType = (IElementType) iter.next();
					if (((IHintedType) elementType).getSemanticHint().equals(((IHintedType) UMLElementTypes.Region_3000).getSemanticHint())) {
						// starting point is the state compartment on
						// which mouse was moving
						View stateCompartmentView = (View) getHost().getModel();
						// get and adaptable for it, to pass on to commands
						IAdaptable adaptableForStateCompartmentView = new SemanticAdapter(null, stateCompartmentView);
						// do the whole job
						CustomFirstRegionInCompositeStateCreateElementCommand createNewRegion = new CustomFirstRegionInCompositeStateCreateElementCommand(adaptableForStateCompartmentView, null, ((IGraphicalEditPart) getHost()).getDiagramPreferencesHint(),
								editingDomain, DiagramUIMessages.CreateCommand_Label, dropLocation);
						cc.compose(createNewRegion);
						return new ICommandProxy(cc.reduce());
					}
				}
			} else if (request instanceof ChangeBoundsRequest) {
				ChangeBoundsRequest changeBoundsRequest = (ChangeBoundsRequest) request;
				Point mouseLocation = changeBoundsRequest.getLocation();
				DropObjectsRequest dropRequest = new DropObjectsRequest();
				dropRequest.setLocation(mouseLocation);
				List<View> list = new ArrayList<View>();
				Iterator<?> it = changeBoundsRequest.getEditParts().iterator();
				while (it.hasNext()) {
					Object next = it.next();
					if (next instanceof EditPart) {
						EditPart ep = (EditPart) next;
						if (ep instanceof RegionEditPart) {
							View regionToDrag = (View) ep.getModel();
							list.add(regionToDrag);
						}
					}
				}
				dropRequest.setObjects(list);
				return getHost().getCommand(dropRequest);
			}
			return super.getCommand(request);
		}
		return null;
	}

	@Override
	public EditPart getTargetEditPart(Request request) {
		if (request instanceof CreateUnspecifiedTypeRequest) {
			CreateUnspecifiedTypeRequest createUnspecifiedTypeRequest = (CreateUnspecifiedTypeRequest) request;
			if (understandsRequest(request)) {
				List<?> elementTypes = createUnspecifiedTypeRequest.getElementTypes();
				// Treat the case where only one element type is listed
				// Only take EntryPoint or ExitPoint element type into account
				if ((elementTypes.size() == 1) && (((IElementType) (elementTypes.get(0)) == UMLElementTypes.ConnectionPointReference_18000))) {
					// If the target is a compartment replace by its parent edit part
					if ((getHost() instanceof ShapeCompartmentEditPart)) {
						return getHost().getParent();
					}
				}
			}
		}
		return super.getTargetEditPart(request);
	}
	// @Override
	// public void eraseTargetFeedback(Request request) {
	// if(sizeOnDropFeedback != null) {
	// LayerManager.Helper.find(getHost()).getLayer(LayerConstants.FEEDBACK_LAYER).remove(sizeOnDropFeedback);
	// sizeOnDropFeedback = null;
	// }
	// }
	// protected IFigure getSizeOnDropFeedback() {
	// if(sizeOnDropFeedback == null) {
	// sizeOnDropFeedback = new RectangleFigure();
	// FigureUtilities.makeGhostShape((Shape)sizeOnDropFeedback);
	// ((Shape)sizeOnDropFeedback).setLineStyle(Graphics.LINE_DASHDOT);
	// sizeOnDropFeedback.setForegroundColor(ColorConstants.white);
	// LayerManager.Helper.find(getHost()).getLayer(LayerConstants.FEEDBACK_LAYER).add(sizeOnDropFeedback);
	// }
	// return sizeOnDropFeedback;
	// }
	// @Override
	// public EditPart getTargetEditPart(Request request) {
	//
	// if(request instanceof CreateUnspecifiedTypeRequest) {
	// CreateUnspecifiedTypeRequest createUnspecifiedTypeRequest = (CreateUnspecifiedTypeRequest)request;
	//
	// if(understandsRequest(request)) {
	// List<?> elementTypes = createUnspecifiedTypeRequest.getElementTypes();
	// // Treat the case where only one element type is listed
	// // Only take EntryPoint or ExitPoint element type into account
	// if((elementTypes.size() == 1) && (((IElementType)(elementTypes.get(0)) == UMLElementTypes.Pseudostate_16000) || ((IElementType)(elementTypes.get(0)) == UMLElementTypes.Pseudostate_17000))) {
	// // If the target is a compartment replace by its grand parent edit part
	// if((getHost() instanceof ShapeCompartmentEditPart)) {
	// return getHost().getParent().getParent().getParent();
	// }
	// }
	// }
	// }
	//
	// return super.getTargetEditPart(request);
	// }
	//
	// @Override
	// public void showTargetFeedback(Request request) {
	// if(request instanceof CreateUnspecifiedTypeRequest) {
	// CreateUnspecifiedTypeRequest unspecReq = (CreateUnspecifiedTypeRequest)request;
	// for(Iterator iter = unspecReq.getElementTypes().iterator(); iter.hasNext();) {
	// IElementType elementType = (IElementType)iter.next();
	// if(elementType.equals(UMLElementTypes.Region_3000)) {
	// RegionFigure targetFig = ((RegionEditPart)getHost().getParent()).getPrimaryShape();
	//
	// // make a local copy
	// Rectangle targetFigBounds = targetFig.getBounds().getCopy();
	// // transform the coordinates to absolute
	// targetFig.translateToAbsolute(targetFigBounds);
	// // retrieve mouse location
	// Point mouseLocation = unspecReq.getLocation();
	//
	// // get the drop location, i.e. RIGHT, LEFT, TOP, BOTTOM
	// dropLocation = Zone.getZoneFromLocationInRectangleWithAbsoluteCoordinates(mouseLocation, targetFigBounds);
	//
	// // perform corresponding change (scaling, translation) on
	// // targetFigBounds
	// // and updates the graph node drop location property
	// if(Zone.isTop(dropLocation)) {
	// targetFigBounds.setSize(targetFigBounds.getSize().scale(1.0, 0.5));
	// } else if(Zone.isLeft(dropLocation)) {
	// targetFigBounds.setSize(targetFigBounds.getSize().scale(0.5, 1.0));
	// } else if(Zone.isRight(dropLocation)) {
	// targetFigBounds.setSize(targetFigBounds.getSize().scale(0.5, 1.0));
	// targetFigBounds.translate(targetFigBounds.width, 0);
	// } else if(Zone.isBottom(dropLocation)) {
	// targetFigBounds.setSize(targetFigBounds.getSize().scale(1.0, 0.5));
	// targetFigBounds.translate(0, targetFigBounds.height);
	// }
	//
	// getSizeOnDropFeedback().setBounds(new PrecisionRectangle(targetFigBounds));
	// }
	// }
	// }
	// }
}

Back to the top