Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29973c9bf1228a09a665a9b7a98769dda629337b (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gmf.runtime.notation.View;

/**
 * @since 3.0
 */
public class EditPartUtils {


	public static EditPart findFirstChildEditPartWithId(final EditPart editPart, final String visualId) {
		final List<? extends EditPart> result = findChildEditPartsWithId(editPart, visualId);
		if (result.isEmpty()) {
			return null;
		}
		return result.get(0);
	}

	public static List<? extends EditPart> findChildEditPartsWithId(final EditPart editPart, final String visualId) {
		final List<EditPart> editParts = new ArrayList<>();
		internalFindChildEditPartsWithId(editPart, visualId, editParts);
		return editParts;
	}

	private static void internalFindChildEditPartsWithId(final EditPart editPart, final String visualId, final List<EditPart> result) {
		final Object model = editPart.getModel();
		if (model instanceof View) {
			final View view = (View) model;
			if (view.getType() != null) {
				if (view.getType().equals(visualId)) {
					result.add(editPart);
				}
			}
		}
		@SuppressWarnings("unchecked")
		final List<EditPart> children = editPart.getChildren();
		for (final EditPart child : children) {
			internalFindChildEditPartsWithId(child, visualId, result);
		}
	}

	public static EditPart findParentEditPartWithId(final EditPart editPart, final String visualId) {
		EditPart parent = editPart;
		while (parent != null) {
			final Object model = parent.getModel();
			if (model instanceof View) {
				final View parentView = (View) model;
				if (parentView.getType() != null) {
					if (parentView.getType().equals(visualId)) {
						return parent;
					}
				}
				parent = parent.getParent();
			} else {
				break;
			}
		}
		return null;
	}

	/**
	 * Find the EditPart whose Figure is closest to the given y-coordinate.
	 *
	 * @param ordinate
	 *            the y-coordinate
	 * @param editParts
	 *            the EditParts among which to choose
	 * @return the EditPart closest to the given vertical coordinate
	 */
	public static GraphicalEditPart findEditPartClosestToOrdinate(final int ordinate, final List<? extends GraphicalEditPart> editParts) {
		if (editParts.isEmpty()) {
			return null;
		}

		final TreeMap<Integer, GraphicalEditPart> distanceMap = new TreeMap<>();
		for (final GraphicalEditPart editPart : editParts) {
			final IFigure figure = editPart.getFigure();
			final Rectangle bounds = new Rectangle(figure.getBounds());
			figure.getParent().translateToAbsolute(bounds);
			final int posY = bounds.y + bounds.height / 2;
			final int distance = Math.abs(posY - ordinate);
			distanceMap.put(Integer.valueOf(distance), editPart);
		}
		return distanceMap.values().iterator().next();
	}


	/** Reveals the given EditPart in its viewer */
	public static void revealEditPart(final EditPart editPart) {
		if (editPart != null && editPart.getViewer() != null) {
			editPart.getViewer().reveal(editPart);
		}
	}
}

Back to the top