Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7070f44813efb40de8019cd40525caed866d03e7 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Red Hat, Inc.
 *  All rights reserved.
 * This program is 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:
 * Red Hat, Inc. - initial API and implementation
 *
 * @author Ivar Meikas
 ******************************************************************************/
package org.eclipse.bpmn2.modeler.core.features;

import org.eclipse.bpmn2.BaseElement;
import org.eclipse.bpmn2.DataAssociation;
import org.eclipse.bpmn2.di.BPMNEdge;
import org.eclipse.bpmn2.modeler.core.di.DIUtils;
import org.eclipse.bpmn2.modeler.core.utils.AnchorUtil;
import org.eclipse.bpmn2.modeler.core.utils.BusinessObjectUtil;
import org.eclipse.bpmn2.modeler.core.utils.FeatureSupport;
import org.eclipse.dd.di.DiagramElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.graphiti.datatypes.ILocation;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IReconnectionContext;
import org.eclipse.graphiti.features.context.impl.ReconnectionContext;
import org.eclipse.graphiti.features.impl.DefaultReconnectionFeature;
import org.eclipse.graphiti.mm.pictograms.AnchorContainer;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.FixPointAnchor;

// TODO: Auto-generated Javadoc
/**
 * The Class ReconnectBaseElementFeature.
 */
public class ReconnectBaseElementFeature extends DefaultReconnectionFeature {

	/** The changes done. */
	protected boolean changesDone = false;
	
	/**
	 * Instantiates a new reconnect base element feature.
	 *
	 * @param fp the fp
	 */
	public ReconnectBaseElementFeature(IFeatureProvider fp) {
		super(fp);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.features.impl.DefaultReconnectionFeature#canReconnect(org.eclipse.graphiti.features.context.IReconnectionContext)
	 */
	@Override
	public boolean canReconnect(IReconnectionContext context) {
		AnchorContainer sourceContainer = null;
		AnchorContainer targetContainer = null;
		EObject businessObject = BusinessObjectUtil.getBusinessObjectForPictogramElement(context.getConnection());
		if (context.getReconnectType().equals(ReconnectionContext.RECONNECT_TARGET)) {
			sourceContainer = context.getConnection().getStart().getParent();
			if (context.getTargetPictogramElement() instanceof AnchorContainer)
				targetContainer = (AnchorContainer) context.getTargetPictogramElement();
		}
		else {
			targetContainer = context.getConnection().getEnd().getParent();
			if (context.getTargetPictogramElement() instanceof AnchorContainer)
				sourceContainer = (AnchorContainer) context.getTargetPictogramElement();
		}
		if (!AbstractBpmn2CreateConnectionFeature.canCreateConnection(sourceContainer, targetContainer, businessObject.eClass(), context.getReconnectType()))
			return false;

		return super.canReconnect(context);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.features.impl.DefaultReconnectionFeature#preReconnect(org.eclipse.graphiti.features.context.IReconnectionContext)
	 */
	@Override
	public void preReconnect(IReconnectionContext context) {
		Connection connection = context.getConnection();
		FixPointAnchor newAnchor = null;

		AnchorContainer source = connection.getStart().getParent();
		AnchorContainer target = connection.getEnd().getParent();
		if (context.getReconnectType().equals(ReconnectionContext.RECONNECT_TARGET)) {
			target = (AnchorContainer) context.getTargetPictogramElement();
			ILocation targetLoc = context.getTargetLocation();
			newAnchor = AnchorUtil.createAnchor(target, targetLoc.getX(), targetLoc.getY());
		}
		else {
			source = (AnchorContainer) context.getTargetPictogramElement();
			ILocation targetLoc = context.getTargetLocation();
			newAnchor = AnchorUtil.createAnchor(source, targetLoc.getX(), targetLoc.getY());
		}

		if (newAnchor!=null)
			((ReconnectionContext)context).setNewAnchor(newAnchor);
		
		super.preReconnect(context);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.features.impl.DefaultReconnectionFeature#postReconnect(org.eclipse.graphiti.features.context.IReconnectionContext)
	 */
	@Override
	public void postReconnect(IReconnectionContext context) {
		super.postReconnect(context);

		BPMNEdge edge = BusinessObjectUtil.getFirstElementOfType(context.getConnection(), BPMNEdge.class);
		DiagramElement de = BusinessObjectUtil.getFirstElementOfType(context.getTargetPictogramElement(), DiagramElement.class);
		if (context.getReconnectType().equals(ReconnectionContext.RECONNECT_TARGET)) {
			edge.setTargetElement(de);
		}
		else {
			edge.setSourceElement(de);
		}
		
		Connection connection = context.getConnection();
		BaseElement flow = BusinessObjectUtil.getFirstElementOfType(connection, BaseElement.class);
		if (!(flow instanceof DataAssociation)) {
			BaseElement be = BusinessObjectUtil.getFirstElementOfType(context.getTargetPictogramElement(), BaseElement.class);
			if (context.getReconnectType().equals(ReconnectionContext.RECONNECT_TARGET)) {
				EStructuralFeature feature = flow.eClass().getEStructuralFeature("targetRef"); //$NON-NLS-1$
				if (feature!=null)
					flow.eSet(feature, be);
			}
			else {
				EStructuralFeature feature = flow.eClass().getEStructuralFeature("sourceRef"); //$NON-NLS-1$
				if (feature!=null && !feature.isMany())
					flow.eSet(feature, be);
			}
		}
		
		FeatureSupport.updateConnection(getFeatureProvider(), connection, true);

		DIUtils.updateDIEdge(context.getConnection());
		
		changesDone = true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.features.impl.AbstractFeature#hasDoneChanges()
	 */
	@Override
	public boolean hasDoneChanges() {
		return changesDone;
	}
}

Back to the top