Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3c6590568c257dbf13a3465f63d19db8ad7774cb (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
package org.eclipse.papyrus.uml.diagram.activity.edit.part;

import java.util.List;

import org.eclipse.draw2d.AbstractPointListShape;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.BorderedBorderItemEditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.activity.edit.parts.ControlFlowEditPart;
import org.eclipse.papyrus.uml.diagram.activity.edit.parts.ObjectFlowEditPart;
import org.eclipse.papyrus.uml.diagram.activity.figures.PinFigure;
import org.eclipse.papyrus.uml.diagram.activity.helper.ActivityFigureDrawer;


public abstract class AbstractPinEditPart extends BorderedBorderItemEditPart {

	public AbstractPinEditPart(View view) {
		super(view);
	}

	/**
	 * Notifies listeners that a target connection has been added.
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 * @param index
	 *            Position child is being added into.
	 */
	@Override
	protected void fireTargetConnectionAdded(ConnectionEditPart connection, int index) {
		super.fireTargetConnectionAdded(connection, index);
		undrawPinArrow(connection);
	}

	/**
	 * Notifies listeners that a source connection has been added.
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 * @param index
	 *            Position child is being added into.
	 */
	@Override
	protected void fireSourceConnectionAdded(ConnectionEditPart connection, int index) {
		super.fireSourceConnectionAdded(connection, index);
		undrawPinArrow(connection);
	}

	/**
	 * Undraw the pin arrow
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 */
	private void undrawPinArrow(ConnectionEditPart connection) {
		if (isConnectionSupported(connection)) {
			PinFigure pinFigure = getPrimaryShape();
			AbstractPointListShape arrow = pinFigure.getOptionalArrowFigure();
			ActivityFigureDrawer.undrawFigure(arrow);
		}
	}

	/**
	 * Draw the pin arrow
	 *
	 */
	private void drawPinArrow() {
		PinFigure pinFigure = getPrimaryShape();
		AbstractPointListShape arrow = pinFigure.getOptionalArrowFigure();
		int side = getBorderItemLocator().getCurrentSideOfParent();
		int direction = ActivityFigureDrawer.getOppositeDirection(side);
		ActivityFigureDrawer.redrawPinArrow(arrow, getMapMode(), getSize(), direction);
	}

	/**
	 * redraw the pin arrow if current connection doesnt set
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 */
	private void redrawPinArrow(List<?> connections) {
		this.redrawPinArrow(null, connections);
	}

	/**
	 * redraw the pin arrow if no other target connection left
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 */
	private void redrawPinArrow(ConnectionEditPart connection, List<?> connections) {
		if (isHasActiveEdge(connection, connections)) {
			undrawPinArrow(connection);
			return;
		}
		drawPinArrow();
	}

	private boolean isHasActiveEdge(ConnectionEditPart connection, List<?> connections) {
		if (connection == null && (connections == null || connections.isEmpty())) {
			return false;
		}
		if (connection == null) {
			for (Object next : connections) {
				if (isConnectionSupported((ConnectionEditPart) next)) {
					return true;
				}
			}
			return false;
		}
		for (Object next : connections) {
			if (!connection.equals(next) && isConnectionSupported((ConnectionEditPart) next)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * is connection supported for the pin arrow redraw
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 */
	private boolean isConnectionSupported(ConnectionEditPart connection) {
		return (connection instanceof ObjectFlowEditPart || connection instanceof ControlFlowEditPart);
	}

	/**
	 * Notifies listeners that a target connection has been removed.
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 * @param index
	 *            Position child is being added into.
	 */
	@Override
	protected void fireRemovingTargetConnection(ConnectionEditPart connection, int index) {
		super.fireRemovingTargetConnection(connection, index);
		redrawPinArrow(connection, getTargetConnections());
	}

	/**
	 * Notifies listeners that a source connection has been removed.
	 *
	 * @param connection
	 *            <code>ConnectionEditPart</code> being added as child.
	 * @param index
	 *            Position child is being added into.
	 */
	@Override
	protected void fireRemovingSourceConnection(ConnectionEditPart connection, int index) {
		super.fireRemovingTargetConnection(connection, index);
		redrawPinArrow(connection, getSourceConnections());
	}

	public abstract PinFigure getPrimaryShape();

	/**
	 * Registers this editpart to recieve notation and semantic events.
	 *
	 */
	@Override
	public void activate() {
		super.activate();
		// redraw the pin arrow if no connection
		redrawPinArrow(getTargetConnections().isEmpty() ? getSourceConnections() : getTargetConnections());
	}
}

Back to the top