Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 82b70c722b1a0ab6cdb07b06a646f7718945cba0 (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
287
288
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2010 SAP AG.
 * 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:
 *    Kiril Mitov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.internal.feature;

import static org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelDir.UNI;
import static org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelType.MANY_TO_MANY;
import static org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelType.MANY_TO_ONE;
import static org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelType.ONE_TO_MANY;
import static org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelType.ONE_TO_ONE;

import java.util.List;

import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.graphiti.features.IAddBendpointFeature;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddConnectionContext;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.context.impl.AddBendpointContext;
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.ManyToOneMapping;
import org.eclipse.jpt.jpa.core.context.OneToOneMapping;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.jpa2.context.DerivedIdentity2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.SingleRelationshipMapping2_0;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorImageCreator;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.JPAEditorImageCreator;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelDir;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation.RelType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtilImpl;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.Wrp;
import org.eclipse.swt.graphics.Point;


public class AddRelationFeature extends AbstractAddFeature {
	
	private static final double START_COEFFICIENT = 0.1;

	private IJPAEditorImageCreator imageCreator;
	private IJPAEditorUtil jpaEditorUtil; 

    public AddRelationFeature(IFeatureProvider fp) {
		this(fp, new JPAEditorImageCreator(), new JPAEditorUtilImpl());
	}

	public AddRelationFeature(IFeatureProvider fp, 
							  IJPAEditorImageCreator imageCreator, 
							  IJPAEditorUtil jpaEditorUtil) {
        super(fp);
		this.imageCreator = imageCreator;
		this.jpaEditorUtil = jpaEditorUtil;
    }
 
    public PictogramElement add(IAddContext context) {
        final IAddConnectionContext addConContext = (IAddConnectionContext) context;
		final IRelation relation = (IRelation) context.getNewObject();
		final Diagram diagram = getDiagram();
		final Wrp wrp = new Wrp();
		TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(diagram);
		RecordingCommand rc = new RecordingCommand(ted) {
			@Override
			protected void doExecute() {
				FreeFormConnection connection = createConnection(addConContext, relation, diagram);
				imageCreator.createConnectionLine(diagram, connection);
				getFeatureProvider().putKeyToBusinessObject(relation.getId(), relation);
				link(connection, relation);
				layoutPictogramElement(connection);
				wrp.setObj(connection);
			}			
		};
		try {
		ted.getCommandStack().execute(rc);
		} catch (Exception e){
		
		}
		
		return (PictogramElement)wrp.getObj();
    }
        
	private FreeFormConnection createConnection(IAddConnectionContext addConContext, IRelation relation,
			final Diagram diagram) {
        FreeFormConnection connection = getFeatureProvider().getPeServiceUtil().createFreeFormConnection(diagram);
        connection.setStart(addConContext.getSourceAnchor());
        connection.setEnd(addConContext.getTargetAnchor());
        connection.setVisible(true);
        connection.setActive(true);        
        List<Point> points = jpaEditorUtil.createBendPointList(connection, relation.getOwner() == relation.getInverse());
        for (int i = 0; i < points.size(); i++) {
        	Point p = points.get(i);
        	AddBendpointContext ctx = new AddBendpointContext(connection, p.x, p.y, i);
            IAddBendpointFeature ft =getFeatureProvider().getAddBendpointFeature(ctx);
            ft.addBendpoint(ctx);
        }
        addDecorators(connection, relation); 
        addTextDecorators(connection, relation);
		return connection;
	}

	private void addDecorators(FreeFormConnection connection, IRelation relation) {
		RelDir direction = relation.getRelDir();
		RelType type = relation.getRelType();
		if (ONE_TO_ONE.equals(type)) {
			addOneToOneDecorator(connection, direction, relation);
		} else if (ONE_TO_MANY.equals(type) && UNI.equals(direction)) {
			addOneToManyDecorator(connection, relation);
		} else if (MANY_TO_ONE.equals(type)) {
			addManyToOneDecorator(connection, direction, relation);
		} else if (MANY_TO_MANY.equals(type)) {
			addManyToManyDecorator(connection, direction, relation);
		}
	}
	
	private void addTextDecorators(FreeFormConnection connection, IRelation relation) {
		RelDir direction = relation.getRelDir();
		RelType type = relation.getRelType();
		if (ONE_TO_ONE.equals(type)) {
			addOneToOneTextDecorator(connection, direction, relation);
		} else if (ONE_TO_MANY.equals(type) && UNI.equals(direction)) {
			addOneToManyTextDecorator(connection, relation);
		} else if (MANY_TO_ONE.equals(type)) {
			addManyToOneTextDecorator(connection, direction, relation);
		} else if (MANY_TO_MANY.equals(type)) {
			addManyToManyTextDecorator(connection, direction, relation);
		}
	}

	private void addOneToOneDecorator(FreeFormConnection c, RelDir direction, IRelation rel) {
		double startCoefficient = START_COEFFICIENT;
		double endCoefficient = 1.0 - startCoefficient;	
		int len = JPAEditorUtil.calcConnectionLength(c);		
		if (UNI.equals(direction)) {
			ConnectionDecorator d = imageCreator.createArrowConnectionDecorator(c, endCoefficient);
			Graphiti.getGaService().setLocation(d.getGraphicsAlgorithm(),Math.round(-len/10), 0);				
		} 
	}
        
	private void addOneToManyDecorator(FreeFormConnection c, IRelation rel) {
		double startCoefficient = START_COEFFICIENT;
		double endCoefficient = 1.0 - startCoefficient;
		int len = JPAEditorUtil.calcConnectionLength(c);
		ConnectionDecorator d = imageCreator.createManyEndWithArrowDecorator(c, endCoefficient);
		Graphiti.getGaService().setLocation(d.getGraphicsAlgorithm(),Math.round(-len/10), 0);
	}
                
	private void addManyToOneDecorator(FreeFormConnection c, RelDir direction, IRelation rel) {
		double startCoefficient = START_COEFFICIENT;
		double endCoefficient = 1.0 - startCoefficient;
		int len = JPAEditorUtil.calcConnectionLength(c);		
		ConnectionDecorator d1 = imageCreator.createManyStartDecorator(c, startCoefficient);
		Graphiti.getGaService().setLocation(d1.getGraphicsAlgorithm(),Math.round(len/10), 0);
		if (UNI.equals(direction)) {
			ConnectionDecorator d2 = imageCreator.createArrowConnectionDecorator(c, endCoefficient);
			Graphiti.getGaService().setLocation(d2.getGraphicsAlgorithm(),Math.round(-len/10), 0);
		} 
    }
    
	private void addManyToManyDecorator(FreeFormConnection c, RelDir direction, IRelation rel) {
		double startCoefficient = START_COEFFICIENT;
		double endCoefficient = 1.0 - startCoefficient;		
		int len = JPAEditorUtil.calcConnectionLength(c);		
		ConnectionDecorator d1 = imageCreator.createManyStartDecorator(c, startCoefficient);
		Graphiti.getGaService().setLocation(d1.getGraphicsAlgorithm(),Math.round(len/10), 0);
		if (UNI.equals(direction)) {
			ConnectionDecorator d = imageCreator.createManyEndWithArrowDecorator(c, endCoefficient);
			Graphiti.getGaService().setLocation(d.getGraphicsAlgorithm(),Math.round(-len/10), 0);
		} else {
			ConnectionDecorator d = imageCreator.createManyEndDecorator(c, endCoefficient, false);
			Graphiti.getGaService().setLocation(d.getGraphicsAlgorithm(),Math.round(-len/10), 0);
		}
    }

	private void addOneToOneTextDecorator(FreeFormConnection c, RelDir direction, IRelation rel) {
		boolean isOptional = false;
		if (RelDir.BI.equals(direction)) {
			PersistentAttribute inverse = rel.getInverse().getAttributeNamed(rel.getInverseAttributeName());
			AttributeMapping mapping = JpaArtifactFactory.instance().getAttributeMapping(inverse);
			if(mapping instanceof OneToOneMapping){
				isOptional = ((OneToOneMapping)mapping).isOptional();
			}
			imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, isOptional ? JPAEditorConstants.CARDINALITY_ZERO_ONE : JPAEditorConstants.CARDINALITY_ONE, 0.0);				
		}
		PersistentAttribute owner = rel.getOwner().getAttributeNamed(rel.getOwnerAttributeName());
		owner.update();
		if(isDerivedId(owner)){
			isOptional = false;
		} else {
			AttributeMapping mapping = JpaArtifactFactory.instance().getAttributeMapping(owner);
			if(mapping instanceof OneToOneMapping){
				isOptional = ((OneToOneMapping)mapping).isOptional();
			}
		}
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, isOptional ? JPAEditorConstants.CARDINALITY_ZERO_ONE : JPAEditorConstants.CARDINALITY_ONE, 1.0);
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getOwnerAttributeName(), 0.0);
		if (!UNI.equals(direction)) {
			imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getInverseAttributeName(), 1.0);
		}
	}
	
	private boolean isDerivedId(PersistentAttribute attr){
		AttributeMapping attributeMapping = JpaArtifactFactory.instance().getAttributeMapping(attr);
		if(attributeMapping instanceof SingleRelationshipMapping2_0){
			DerivedIdentity2_0 derivedIdentity = ((SingleRelationshipMapping2_0)attributeMapping).getDerivedIdentity();
			if(derivedIdentity.usesIdDerivedIdentityStrategy() || derivedIdentity.usesMapsIdDerivedIdentityStrategy()){
				return true;
			}
		}		
		return false;
	}
        
	private void addOneToManyTextDecorator(FreeFormConnection c, IRelation rel) {
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, JPAEditorConstants.CARDINALITY_ZERO_ONE, 0.0);
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, JPAEditorConstants.CARDINALITY_ZERO_N, 1.0);	
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getOwnerAttributeName(), 0.0);						
	}
                
	private void addManyToOneTextDecorator(FreeFormConnection c, RelDir direction, IRelation rel) {
		boolean isOptional = false;
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, JPAEditorConstants.CARDINALITY_ZERO_N, 0.0);
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getOwnerAttributeName(), 0.0);				

		PersistentAttribute owner = rel.getOwner().getAttributeNamed(rel.getOwnerAttributeName());

		if(isDerivedId(owner)){
			isOptional = false;
		} else {
			AttributeMapping mapping = JpaArtifactFactory.instance().getAttributeMapping(owner);
			if(mapping instanceof ManyToOneMapping) {
				isOptional = ((ManyToOneMapping)mapping).isOptional();
			}
		}
		
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, isOptional ?
																	JPAEditorConstants.CARDINALITY_ZERO_ONE :
																	JPAEditorConstants.CARDINALITY_ONE, 1.0);
		if (!UNI.equals(direction)) {
			imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getInverseAttributeName(), 1.0);
		}
    }
    
	private void addManyToManyTextDecorator(FreeFormConnection c, RelDir direction, IRelation rel) {
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, JPAEditorConstants.CARDINALITY_ZERO_N, 0.0);		
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, JPAEditorConstants.CARDINALITY_ZERO_N, 1.0);	
		imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getOwnerAttributeName(), 0.0);
		if (!UNI.equals(direction)) {
			imageCreator.createCardinalityConnectionDecorator(getDiagram(), c, rel.getInverseAttributeName(), 1.0);	
		}
    }
	
    public boolean canAdd(IAddContext context) {
		if (context instanceof IAddConnectionContext && context.getNewObject() instanceof IRelation) {
            return true;
        }
        return false;
    }
    
	@Override
	public IJPAEditorFeatureProvider getFeatureProvider() {
		return (IJPAEditorFeatureProvider)super.getFeatureProvider();
	}
    
}

Back to the top