Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7bdc59d017eabba46775dcab5745d51f64e63e9e (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*****************************************************************************
 * Copyright (c) 2011, 2014 Atos, CEA, 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:
 *   Arthur Daussy (Atos) - Initial API and implementation
 *   Arthur Daussy - 371712 : 372745: [ActivityDiagram] Major refactoring group framework
 *   Christian W. Damus (CEA) - bug 410346
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.activitygroup.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.papyrus.uml.diagram.activity.activitygroup.predicates.DescendantsFilter;
import org.eclipse.papyrus.uml.diagram.activity.activitygroup.request.IGroupRequest;
import org.eclipse.papyrus.uml.diagram.activity.part.UMLDiagramEditorPlugin;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

public class Utils {

	/**
	 * Debug message
	 */
	private static final String OLD_PARENT = "Old parent :";

	/**
	 * Return a iterable of each targeted edit part
	 *
	 * @param req
	 * @return
	 */
	public static Iterable<IGraphicalEditPart> getTargetedEditPart(ChangeBoundsRequest req) {
		if (req != null && req.getEditParts() != null) {
			return Iterables.transform(Iterables.filter((Iterable<Object>) req.getEditParts(), new Predicate<Object>() {

				@Override
				public boolean apply(Object input) {
					return input instanceof IGraphicalEditPart;
				}
			}), new Function<Object, IGraphicalEditPart>() {

				@Override
				public IGraphicalEditPart apply(Object from) {
					return (IGraphicalEditPart) from;
				}
			});
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * Get the abslotue bounds of a figure after a change bounds request
	 *
	 * @param request
	 * @param host
	 * @return
	 */
	public static Rectangle getAbslotueRequestBounds(ChangeBoundsRequest request, IGraphicalEditPart host) {
		Rectangle bounds = getAbsoluteBounds(host);
		return request.getTransformedRectangle(bounds);
	}

	/**
	 * Get the bounds of an edit part
	 *
	 * @param part
	 *            edit part to find bounds
	 * @return part's bounds in absolute coordinates
	 */
	public static Rectangle getAbsoluteBounds(IGraphicalEditPart part) {
		// take bounds from figure
		part.refresh();
		part.getFigure().invalidate();
		part.getFigure().validate();
		Rectangle bounds = part.getFigure().getBounds().getCopy();
		if (part.getNotationView() instanceof Node) {
			// rather update with up to date model bounds
			Node node = (Node) part.getNotationView();
			LayoutConstraint cst = node.getLayoutConstraint();
			if (cst instanceof Bounds) {
				Bounds b = (Bounds) cst;
				Point parentLoc = part.getFigure().getParent().getBounds().getLocation();
				if (b.getX() > 0) {
					bounds.x = b.getX() + parentLoc.x;
				}
				if (b.getY() > 0) {
					bounds.y = b.getY() + parentLoc.y;
				}
				if (b.getHeight() != -1) {
					bounds.height = b.getHeight();
				}
				if (b.getWidth() != -1) {
					bounds.width = b.getWidth();
				}
			}
		}
		part.getFigure().getParent().translateToAbsolute(bounds);
		return bounds;
	}

	// /**
	// * Get the bounds of an edit part
	// *
	// * @param part
	// * edit part to find bounds
	// * @return part's bounds in absolute coordinates
	// */
	// public static Rectangle getAbsoluteBounds(Shape s) {
	// s.
	// // take bounds from figure
	// part.getTopGraphicEditPart().refresh();
	// Rectangle bounds = part.getFigure().getBounds().getCopy();
	//
	// if(part.getNotationView() instanceof Node) {
	// // rather update with up to date model bounds
	// Node node = (Node)part.getNotationView();
	// LayoutConstraint cst = node.getLayoutConstraint();
	// if(cst instanceof Bounds) {
	// Bounds b = (Bounds)cst;
	// Point parentLoc = part.getFigure().getParent().getBounds().getLocation();
	// if(b.getX() > 0) {
	// bounds.x = b.getX() + parentLoc.x;
	// }
	// if(b.getY() > 0) {
	// bounds.y = b.getY() + parentLoc.y;
	// }
	// if(b.getHeight() != -1) {
	// bounds.height = b.getHeight();
	// }
	// if(b.getWidth() != -1) {
	// bounds.width = b.getWidth();
	// }
	// }
	// }
	//
	// part.getFigure().getParent().translateToAbsolute(bounds);
	//
	// return bounds;
	// }
	/**
	 * Same as {@link EcoreUtil#filterDescendants(Collection)}
	 *
	 * @param all
	 * @return
	 */
	public static Set<EObject> filterDescendants(Set<EObject> all) {
		return Sets.filter(all, new DescendantsFilter(all));
	}

	static AdapterFactoryLabelProvider factory = new AdapterFactoryLabelProvider(UMLDiagramEditorPlugin.getInstance().getItemProvidersAdapterFactory());

	public static String getCorrectLabel(Object object) {
		if (object instanceof EObject) {
			return factory.getText(object);
		} else if (object instanceof EReference) {
			return ((EReference) object).getName();
		} else {
			return String.valueOf(object); // null safe
		}
	}

	public static String getCorrectLabel(IAdaptable object) {
		Object view = object.getAdapter(EObject.class);
		if (view instanceof EObject) {
			return getCorrectLabel(view);
		} else {
			return object.toString();
		}
	}

	/**
	 * Get old parent references {@link EObject} sorted with {@link EReference} as keys
	 *
	 * @param request
	 * @return
	 */
	public static Multimap<EReference, EObject> getOldParents(IGroupRequest request) {
		if (request.getNodeDescpitor() != null) {
			return getReferenceMultimap(request, request.getNodeDescpitor().getParentReferences());
		}
		return ArrayListMultimap.create();
	}

	/**
	 * Get old getOldChildren references {@link EObject} sorted with {@link EReference} as keys
	 *
	 * @param request
	 * @return
	 */
	public static Multimap<EReference, EObject> getOldChildren(IGroupRequest request) {
		if (request.getNodeDescpitor() != null) {
			return getReferenceMultimap(request, request.getNodeDescpitor().getChildrenReferences());
		}
		return ArrayListMultimap.create();
	}

	/**
	 * Get the the multimap relative a request and a {@link Iterable} of {@link EReference}
	 *
	 * @param request
	 * @param references
	 * @return
	 */
	protected static Multimap<EReference, EObject> getReferenceMultimap(IGroupRequest request, Iterable<EReference> references) {
		Object elementAdapter = request.getTargetElement().getAdapter(EObject.class);
		if (elementAdapter instanceof EObject && references != null) {
			Multimap<EReference, EObject> result = ArrayListMultimap.create();
			EObject targetElement = (EObject) elementAdapter;
			if (targetElement != null) {
				for (EReference ref : references) {
					if (ref.isMany()) {
						Collection<EObject> values = (Collection<EObject>) targetElement.eGet(ref);
						for (EObject v : values) {
							result.put(ref, v);
						}
					} else {
						Object value = targetElement.eGet(ref);
						;
						if (value instanceof EObject) {
							result.put(ref, (EObject) value);
						}
					}
				}
			}
			return result;
		}
		return ArrayListMultimap.create();
	}

	/**
	 * Copy a ChangeBoundsRequest.
	 * Do not copy metadata
	 *
	 * @param req
	 * @param part
	 * @return
	 */
	public static ChangeBoundsRequest getChangeBoundsRequestCopy(final ChangeBoundsRequest req, final EditPart part) {
		ChangeBoundsRequest auxRequest = new ChangeBoundsRequest(req.getType());
		auxRequest.setLocation(req.getLocation());
		auxRequest.setEditParts(Collections.singletonList(part));
		auxRequest.setMoveDelta(req.getMoveDelta());
		auxRequest.setResizeDirection(req.getResizeDirection());
		auxRequest.setSizeDelta(req.getSizeDelta());
		return auxRequest;
	}

	public static Rectangle getAbslotueRequestBounds(CreateViewRequest initialRequest) {
		Point location = initialRequest.getLocation();
		Dimension size = initialRequest.getSize();
		/**
		 * TODO get default size
		 */
		Rectangle creationBounds = new Rectangle(location != null ? location : new Point(0, 0), size != null ? size : new Dimension(20, 20));
		return creationBounds;
	}
}

Back to the top