Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3449be405a7243af62cd00500f2c2246f8885a35 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*******************************************************************************
 * <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:
 *    Stefan Dimov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.internal.provider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.ICreateConnectionFeature;
import org.eclipse.graphiti.features.ICreateFeature;
import org.eclipse.graphiti.features.IDeleteFeature;
import org.eclipse.graphiti.features.IDirectEditingFeature;
import org.eclipse.graphiti.features.ILayoutFeature;
import org.eclipse.graphiti.features.IMoveConnectionDecoratorFeature;
import org.eclipse.graphiti.features.IMoveShapeFeature;
import org.eclipse.graphiti.features.IReason;
import org.eclipse.graphiti.features.IRemoveFeature;
import org.eclipse.graphiti.features.IResizeShapeFeature;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.context.ICustomContext;
import org.eclipse.graphiti.features.context.IDeleteContext;
import org.eclipse.graphiti.features.context.IDirectEditingContext;
import org.eclipse.graphiti.features.context.ILayoutContext;
import org.eclipse.graphiti.features.context.IMoveConnectionDecoratorContext;
import org.eclipse.graphiti.features.context.IMoveShapeContext;
import org.eclipse.graphiti.features.context.IRemoveContext;
import org.eclipse.graphiti.features.context.IResizeShapeContext;
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
import org.eclipse.graphiti.features.custom.ICustomFeature;
import org.eclipse.graphiti.features.impl.Reason;
import org.eclipse.graphiti.internal.services.GraphitiInternal;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IPeService;
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jdt.internal.core.SourceType;
import org.eclipse.jpt.jpa.core.JpaNode;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.java.JavaEntity;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.AddAllEntitiesFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.AddAttributeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.AddJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.AddRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.ClickAddAttributeButtonFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.ClickRemoveAttributeButtonFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CollapseAllEntitiesFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CollapseCompartmentShapeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CollapseEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateManyToManyBiDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateManyToManyUniDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateManyToOneBiDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateManyToOneUniDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateMappedSuperclassFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateOneToManyUniDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateOneToOneBiDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.CreateOneToOneUniDirRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.DeleteJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.DeleteRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.DirectEditAttributeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.DirectEditJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.ExpandCompartmentShapeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.ExpandEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.JPAMoveConnectionDecoratorFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.LayoutJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.MoveAttributeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.MoveEntityShapeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.RemoveAttributeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.RemoveJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.RemoveRelationFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.ResizeAttributeFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.feature.ResizeJPAEntityFeature;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.util.IModelIntegrationUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.util.ModelIntegrationUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.util.ModelIntegrationUtilImpl;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.propertypage.JPADiagramPropertyPage;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.AbstractRelation;
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.relations.IsARelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.GraphicsUpdaterImpl;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IGraphicsUpdater;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJpaSolver;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IPeServiceUtil;
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.JPASolver;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.PeServiceUtilImpl;
import org.eclipse.ui.PlatformUI;


@SuppressWarnings("restriction")
public class JPAEditorFeatureProvider extends DefaultFeatureProvider implements IJPAEditorFeatureProvider {
	
	private ClickAddAttributeButtonFeature clickAddAttBtnFeat = null;
	private ClickRemoveAttributeButtonFeature clickRemoveAttBtnFeat = null;
	private IPeServiceUtil peServiceUtil = new PeServiceUtilImpl();
	private IPeService peService = Graphiti.getPeService();
	private IJPAEditorUtil jpaEditorUtil = new JPAEditorUtilImpl();
	private IModelIntegrationUtil moinIntegrationUtil = new ModelIntegrationUtilImpl(); 
	private IGraphicsUpdater graphicsUpdater = new GraphicsUpdaterImpl(); 
	
    public JPAEditorFeatureProvider(IDiagramTypeProvider dtp, IJpaSolver is) {
        super(dtp);
        is.setFeatureProvider(this);
        this.setIndependenceSolver(is);
    }
    
    public void stopThread() {
    	if (getIndependenceSolver() != null)
    		((JPASolver)getIndependenceSolver()).stopThread();
    }
    
	public Diagram getDiagram() {
		return getDiagramTypeProvider().getDiagram();
	}

	public HashSet<IsARelation> getAllExistingIsARelations() {
		EList<Connection> allCons = getDiagram().getConnections();
		HashSet<IsARelation> res = new HashSet<IsARelation>();
		HashSet<Connection> tbd = new HashSet<Connection>();
		for (Connection conn : allCons) {
			if (IsARelation.isIsAConnection(conn)) 
				try { 
					res.add(new IsARelation(this, conn));
				} catch (NullPointerException e) {
					tbd.add(conn);
				}
		}
		Iterator<Connection> it = tbd.iterator();
		while (it.hasNext()) {
			Graphiti.getPeService().deletePictogramElement(it.next());
		}
		return res;
	}
	
	public HashSet<IsARelation> getAllRedundantIsARelations() {
		EList<Connection> allCons = getDiagram().getConnections();
		HashSet<IsARelation> res = new HashSet<IsARelation>();
		for (Connection conn : allCons) {
			if (IsARelation.isIsAConnection(conn)) 
				res.add(new IsARelation(this, conn));
		}
		return res;
	}
	
	public void removeAllRedundantIsARelations() {
		EList<Connection> allCons = getDiagram().getConnections();
		Collection<Connection> redundantConnections = new LinkedList<Connection>();
		for (Connection conn : allCons) {
			if (IsARelation.isIsAConnection(conn)) {
				IsARelation rel = null;
				try {
					rel = new IsARelation(this, conn);	
				} catch (NullPointerException e) {
					redundantConnections.add(conn);
					continue;
				}
				if (!rel.getSuperclass().getName().equals(getFirstSuperclassBelongingToTheDiagram(rel.getSubclass()).getName())) {
					redundantConnections.add(conn);
				}
			}
		}
		Iterator<Connection> it = redundantConnections.iterator();
		while (it.hasNext()) {
			Connection conn = it.next();
			peService.deletePictogramElement(conn);
		}	
	}
	
	public boolean existRedundantIsARelations() {
		EList<Connection> allCons = getDiagram().getConnections();
		for (Connection conn : allCons) {
			if (IsARelation.isIsAConnection(conn)) {
				IsARelation rel = null;
				try {
					rel = new IsARelation(this, conn);	
				} catch (NullPointerException e) {
					return true;
				}
				if (!rel.getSuperclass().equals(getFirstSuperclassBelongingToTheDiagram(rel.getSubclass()))) {
					return true;
				}
			}
		}
		return false;
	}
		
	public JavaPersistentType getFirstSuperclassBelongingToTheDiagram(JavaPersistentType subclass) {
		Iterable<PersistentType> h = subclass.getInheritanceHierarchy();
		Iterator<PersistentType> iter = h.iterator();
		if (!iter.hasNext())
			return null;
		HashSet<PersistentType> cycleChecker = new HashSet<PersistentType>();
		cycleChecker.add(iter.next());
		while (iter.hasNext()) {
			PersistentType superclass = iter.next();
			if (hasObjectWithName(getKeyForBusinessObject(superclass))) {
				return (JavaPersistentType) superclass;
			}
			if (!cycleChecker.add(superclass))
				return null;
		}
		return null;
	}
	
    
	public void addJPTForUpdate(String jptName) {
		((JPASolver)getIndependenceSolver()).addJPTForUpdate(jptName);
	}

    	
	public void addRemoveIgnore(JavaPersistentType jpt, String atName) {
		((JPASolver)getIndependenceSolver()).addRemoveIgnore(jpt.getName() + "." + atName);  //$NON-NLS-1$
	}
	
	public void addAddIgnore(JavaPersistentType jpt, String atName) {
		((JPASolver)getIndependenceSolver()).addAddIgnore(jpt.getName() + "." + atName); //$NON-NLS-1$
	}
	
	public HashSet<String> getAddIgnore() {
		return ((JPASolver)getIndependenceSolver()).getAddIgnore(); 
	}

	
	public void addAttribForUpdate(PersistenceUnit pu, String entAtMappedBy) {
		((JPASolver)getIndependenceSolver()).addAttribForUpdate(pu, entAtMappedBy);
	}

	public boolean existsRelation(JavaPersistentType jpt1, JavaPersistentType jpt2) {
		return ((JPASolver)getIndependenceSolver()).existsRelation(jpt1, jpt2);
	}
    
    @Override
    public IAddFeature getAddFeature(IAddContext context) {
    	Object newObj = context.getNewObject(); 
        if (newObj instanceof JavaPersistentType) {
            return new AddJPAEntityFeature(this, true);
        } else if (newObj instanceof AbstractRelation) {
        	 return new AddRelationFeature(this);
        } else if (newObj instanceof PersistentAttribute) { 
        	if (Diagram.class.isInstance(context.getTargetContainer())) {     			
        		return null;
        	} else {
        		return new AddAttributeFeature(this);
        	}
        } else if (newObj instanceof CompilationUnit) {
        	CompilationUnit cu = (CompilationUnit)newObj;
        	JavaPersistentType jpt = JPAEditorUtil.getJPType(cu);
        	if (jpt != null) return new AddJPAEntityFeature(this, true);
        } else if ((newObj instanceof JpaNode)) {
        	return new AddAllEntitiesFeature(this);
        } else if (newObj instanceof SourceType) {
        	return new AddJPAEntityFeature(this, true);
        }
        return super.getAddFeature(context);
    }
    
    
    @Override
    public ICreateFeature[] getCreateFeatures() {
    	return new ICreateFeature[] {new CreateJPAEntityFeature(this), 
    								 new CreateMappedSuperclassFeature(this)};
    }
    
    @Override
	public IDeleteFeature getDeleteFeature(IDeleteContext context) {
    	PictogramElement pe = context.getPictogramElement();
    	Object bo = getBusinessObjectForPictogramElement(pe);
    	if (bo instanceof JavaPersistentType) {
    		return new DeleteJPAEntityFeature(this);	
    	} else if (bo instanceof AbstractRelation) {
    		return new DeleteRelationFeature(this);
    	} else if (bo instanceof JavaPersistentAttribute) {
    		return new ClickRemoveAttributeButtonFeature(this);
    	}
		return null;
	}    

	@Override
	public ICustomFeature[] getCustomFeatures(ICustomContext context) {
		ICustomFeature[] ret = super.getCustomFeatures(context);
		List<ICustomFeature> retList = new ArrayList<ICustomFeature>();
		for (int i = 0; i < ret.length; i++) {
			retList.add(ret[i]);
		}
		
		retList.add(new CollapseAllEntitiesFeature(this));

		retList.add(new CollapseEntityFeature(this));
		retList.add(new ExpandEntityFeature(this));
	
		retList.add(new CollapseCompartmentShapeFeature(this));
		retList.add(new ExpandCompartmentShapeFeature(this));
		
		ret = retList.toArray(ret);
		return ret;
	}
    
    
    @Override
    public IResizeShapeFeature getResizeShapeFeature(IResizeShapeContext context) {
		PictogramElement pe = context.getPictogramElement();
		if (getBusinessObjectForPictogramElement(pe) instanceof JavaPersistentType) {
			return new ResizeJPAEntityFeature(this);
		}
    	GraphicsAlgorithm ga = pe.getGraphicsAlgorithm();
    	if (ga == null) 
    		return super.getResizeShapeFeature(context);
    	List<GraphicsAlgorithm> ch = ga.getGraphicsAlgorithmChildren();
    	if ((ch != null) && (ch.size() > 0) && (ch.get(0) instanceof Text)) {
    		return new ResizeAttributeFeature(this);
    	}
		
		return super.getResizeShapeFeature(context);
    }
      
    
   
    
    @Override
    public ILayoutFeature getLayoutFeature(ILayoutContext context) {
        PictogramElement pictogramElement = context.getPictogramElement();
        Object bo = getBusinessObjectForPictogramElement(pictogramElement);
        if (bo instanceof JavaPersistentType) {
            return new LayoutJPAEntityFeature(this);
        }
        return super.getLayoutFeature(context);
    }
    
    
    @Override
    public IRemoveFeature getRemoveFeature(IRemoveContext context) {
        PictogramElement pe = context.getPictogramElement();
        Object bo = getBusinessObjectForPictogramElement(pe);   
        if (bo == null)
        	super.getRemoveFeature(context);
    	if (bo instanceof JavaPersistentType) {
    		return new RemoveJPAEntityFeature(this, true);	
    	} else if (bo instanceof AbstractRelation) { 
    		return new RemoveRelationFeature(this);
    	}
    	GraphicsAlgorithm ga = pe.getGraphicsAlgorithm();
    	if (ga == null) 
    		return super.getRemoveFeature(context);
    	List<GraphicsAlgorithm> ch = ga.getGraphicsAlgorithmChildren();
    	if ((ch != null) && (ch.size() > 0) && (ch.get(0) instanceof Text)) {
    		return new RemoveAttributeFeature(this);
    	}
    	return super.getRemoveFeature(context);
    }
        
    @Override
    public ICreateConnectionFeature[] getCreateConnectionFeatures() {
        return new ICreateConnectionFeature[] {
        	new CreateOneToOneUniDirRelationFeature(this),
            new CreateOneToOneBiDirRelationFeature(this),           
        	new CreateOneToManyUniDirRelationFeature(this),                    
        	new CreateManyToOneUniDirRelationFeature(this),
            new CreateManyToOneBiDirRelationFeature(this),            
        	new CreateManyToManyUniDirRelationFeature(this),
            new CreateManyToManyBiDirRelationFeature(this)
        };
    }
    
    @Override
    public IReason canAdd(IAddContext context) {
    	return Reason.createTrueReason();
    }
    
    public ICustomFeature getAddAllEntitiesFeature() {
    	return new AddAllEntitiesFeature(this); 
    }
    
    @Override
    public IMoveShapeFeature getMoveShapeFeature(IMoveShapeContext context) {
        PictogramElement pe = context.getPictogramElement();
        Object bo = getBusinessObjectForPictogramElement(pe);
        if (JavaPersistentType.class.isInstance(bo)) {
        	return new MoveEntityShapeFeature(this);
        }
        if(bo == null){
    		return new MoveAttributeFeature(this);
        } 
    	GraphicsAlgorithm ga = pe.getGraphicsAlgorithm();
    	if (ga == null) 
    		return super.getMoveShapeFeature(context);
    	List<GraphicsAlgorithm> ch = ga.getGraphicsAlgorithmChildren();
    	if ((ch != null) && (ch.size() > 0) && (ch.get(0) instanceof Text)) {
    		return new MoveAttributeFeature(this);
    	}
        return super.getMoveShapeFeature(context);
    }
    
    
    @Override
    public IDirectEditingFeature getDirectEditingFeature(IDirectEditingContext context) {
        PictogramElement pe = context.getPictogramElement();
        GraphicsAlgorithm ga = pe.getGraphicsAlgorithm();
        if (!(ga instanceof Rectangle)) 
        	return super.getDirectEditingFeature(context);
        List<GraphicsAlgorithm> ch = ga.getGraphicsAlgorithmChildren();
        if ((ch == null) || (ch.size() == 0)) 
        	return super.getDirectEditingFeature(context);
        ga = ch.get(0);
        if (!(ga instanceof Text)) 
        	return super.getDirectEditingFeature(context);
        if(((Text)ga).getFont().isItalic())
    		return super.getDirectEditingFeature(context);
        Object bo = getBusinessObjectForPictogramElement(pe);
        if (bo != null) {
        	if (bo instanceof JavaPersistentAttribute) {
        		return new DirectEditAttributeFeature(this);
        	}
        }
        if (!(pe instanceof Shape)) 
        	return super.getDirectEditingFeature(context);
        Shape sh = (Shape)pe;
        ContainerShape csh = sh.getContainer();
        if (csh == null) 
        	return super.getDirectEditingFeature(context); 
        bo = getBusinessObjectForPictogramElement(csh);
        if (bo instanceof JavaPersistentType){ 
            return new DirectEditJPAEntityFeature(this);
        }
        return super.getDirectEditingFeature(context);
    }
    
    public void addMissingRelations(JavaPersistentType newEntity, ContainerShape newShape) {
        Collection<PictogramElement> allContainedPictogramElements = Graphiti.getPeService()
        .getAllContainedPictogramElements(getDiagramTypeProvider().getDiagram());
        
        for (PictogramElement pe : allContainedPictogramElements) {
        	if ((pe != newShape) && (pe instanceof ContainerShape)) {
        		JavaPersistentType entity = (JavaPersistentType)getBusinessObjectForPictogramElement(pe);
        		AbstractRelation newRel = produceNecessaryRelation(newEntity, entity);
        		if (newRel != null) {
        			JavaPersistentType owner = newRel.getOwner();
        			JavaPersistentType inverse = newRel.getInverse();
        			ContainerShape ownerShape = (ContainerShape)getPictogramElementForBusinessObject(owner);
        			ContainerShape inverseShape = (ContainerShape)getPictogramElementForBusinessObject(inverse);
         			AddRelationFeature f = new AddRelationFeature(this);
        			AddConnectionContext ctx = 
        				new AddConnectionContext(JPAEditorUtil.getAnchor(ownerShape), JPAEditorUtil.getAnchor(inverseShape));
        			f.add(ctx);
        		}
        	}
        }
    }
    
	public void renewAttributeJoiningStrategyPropertyListener(JavaPersistentAttribute jpa) {
		((JPASolver)getIndependenceSolver()).renewAttributeJoiningStrategyPropertyListener(jpa);
	}
        
    private AbstractRelation produceNecessaryRelation(JavaPersistentType ent1, JavaPersistentType ent2) {
    	return null;
    }
    
	public String produceKeyForRel(JavaPersistentType jpt, String attributeName) {
		return ((JPASolver)getIndependenceSolver()).produceKeyForRel(jpt, attributeName);
	}

    
    public ClickAddAttributeButtonFeature getClickAddAttributeButtonFeature() {
    	if (clickAddAttBtnFeat == null) {
    		clickAddAttBtnFeat = new ClickAddAttributeButtonFeature(this);
    	}
    	return clickAddAttBtnFeat;
    }
    
    public ClickRemoveAttributeButtonFeature getClickRemoveAttributeButtonFeature() {
    	if (clickRemoveAttBtnFeat == null) {
    		clickRemoveAttBtnFeat = new ClickRemoveAttributeButtonFeature(this);
    	}
    	return clickRemoveAttBtnFeat;
    }    
    
    @Override
    public IMoveConnectionDecoratorFeature getMoveConnectionDecoratorFeature(
    		IMoveConnectionDecoratorContext context) {
      	 return new JPAMoveConnectionDecoratorFeature(this);
    }

    public Object getBusinessObjectForKey(String key) {
    	return getIndependenceSolver().getBusinessObjectForKey(key);
    }
    
    public String getKeyForBusinessObject(Object bo) {
    	return getIndependenceSolver().getKeyForBusinessObject(bo);
    }
        
    public void putKeyToBusinessObject(String key, Object bo) {
    	((JPASolver)getIndependenceSolver()).addKeyBusinessObject(key, bo);
    }  
    
    public Object remove(String key) {
    	return remove(key, false);
    }
    
    public Object remove(String key, boolean save) {
    	Object res = getBusinessObjectForKey(key);
    	if (res instanceof JavaPersistentType) {
    		final JavaPersistentType jpt = (JavaPersistentType)res;
    		if (save)
    			JpaArtifactFactory.instance().forceSaveEntityClass(jpt, this);
    		if(!(jpt.getMapping() instanceof JavaEntity)){
    			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
    				public void run() {
    			          removeFromDiagramIfNotEntity(jpt);
    				}});
    		}

    	}
    	return ((JPASolver)getIndependenceSolver()).remove(key);
    }    
    
    public Collection<JavaPersistentType> getPersistentTypes() {
    	return ((JPASolver)getIndependenceSolver()).getPersistentTypes();
    }
    
    protected void removeAllConnections(Shape shape) {
		for (Iterator<Anchor> iter = shape.getAnchors().iterator(); iter.hasNext();) {
			Anchor anchor = iter.next();
			for (Iterator<Connection> iterator = Graphiti.getPeService().getAllConnections(anchor).iterator(); iterator.hasNext();) {
				Connection connection = iterator.next();
				if (GraphitiInternal.getEmfService().isObjectAlive(connection)) {
 					Object ob = getBusinessObjectForPictogramElement(connection);
						if(ob instanceof AbstractRelation){
							String key = getKeyForBusinessObject(ob);
							((JPASolver)getIndependenceSolver()).remove(key);
						}
					Graphiti.getPeService().deletePictogramElement(connection);
				}
			}
		}
	}

    
    private void removeFromDiagramIfNotEntity(final JavaPersistentType jpt) {
		final PictogramElement cs = this.getPictogramElementForBusinessObject(jpt);
		if (cs != null) {
			final Shape shape = (Shape) cs;
			TransactionalEditingDomain ted = ModelIntegrationUtil.getTransactionalEditingDomain(this
							                                           .getDiagramTypeProvider().getDiagram());
			ted.getCommandStack().execute(new RecordingCommand(ted) {
				@Override
				protected void doExecute() {
					removeAllConnections(shape);
					Graphiti.getPeService().deletePictogramElement(cs);
				}
			});
		}
	}


    
    public boolean hasObjectWithName(String name) {
    	return ((JPASolver)getIndependenceSolver()).containsKey(name);
    }
    
    public void replaceAttribute(final JavaPersistentAttribute oldAt, final JavaPersistentAttribute newAt) {
    	final PictogramElement pe = this.getPictogramElementForBusinessObject(oldAt);
    	if (pe == null) {
    		//System.err.println("PictogramElement is null\n");
    		throw new RuntimeException();    		
    		//return;
    	}
    	TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(pe);
    	ted.getCommandStack().execute(new RecordingCommand(ted) {
			protected void doExecute() {
		    	JPASolver solver = (JPASolver)getIndependenceSolver(); 
		    	solver.remove(solver.getKeyForBusinessObject(oldAt));
		    	solver.addKeyBusinessObject(solver.getKeyForBusinessObject(newAt), newAt);
		    	if (newAt == null) {
					JPADiagramEditorPlugin.logError("New attribute is null\n", new Exception()); //$NON-NLS-1$
		    		return;
		    	}
		    	link(pe, newAt);
		    	GraphicsAlgorithm ga = pe.getGraphicsAlgorithm();
		    	if (ga == null)
		    		return;
		    	List<GraphicsAlgorithm> ch = ga.getGraphicsAlgorithmChildren();
		    	((Text)ch.get(0)).setValue(newAt.getName());
			}
    	});
    }
    
    public boolean doesRelationExist(JavaPersistentType owner, 
			JavaPersistentType inverse, 
			String ownerAttributeName,
			RelType relType, 
			RelDir relDir) {
    	
    	String id = AbstractRelation.generateId(owner, inverse, ownerAttributeName, relType, relDir);
    	return (getBusinessObjectForKey(id) != null);
    }   
    
	public boolean isRelationRelatedToAttribute(JavaPersistentAttribute jpa) {
		return ((JPASolver)getIndependenceSolver()).isRelationRelatedToAttribute(jpa);
	}
	
	public IRelation getRelationRelatedToAttribute(JavaPersistentAttribute jpa) {
		return ((JPASolver)getIndependenceSolver()).getRelationRelatedToAttribute(jpa);
	}    
		
	public ICompilationUnit getCompilationUnit(JavaPersistentType jpt) {
		return ((JPASolver)getIndependenceSolver()).getCompilationUnit(jpt);
	}

	public void restoreEntity(JavaPersistentType jpt) {
		((JPASolver)getIndependenceSolver()).restoreEntity(jpt);
	}
	
	public int getAttribsNum(Shape sh) {
		String s = Graphiti.getPeService().getPropertyValue(sh, JPAEditorConstants.PROP_ATTRIBS_NUM);
		int res = 0;
		if (s == null) {
			setAttribsNum(0, sh);
		} else {
			res = Integer.parseInt(s);
		}
		return res;
	}
	
	public int increaseAttribsNum(Shape sh) {
		int attribsNum = getAttribsNum(sh);
		attribsNum++;
		setAttribsNum(attribsNum, sh);
		return attribsNum;
	}
	
	public int decreaseAttribsNum(Shape sh) {
		int attribsNum = getAttribsNum(sh);
		attribsNum--;
		setAttribsNum(attribsNum, sh);
		return attribsNum;
	}	
	
	public void setAttribsNum(int newAttribsNum, Shape sh) {
		Graphiti.getPeService().setPropertyValue(sh, JPAEditorConstants.PROP_ATTRIBS_NUM, "" + newAttribsNum); //$NON-NLS-1$
	}
	
	/*
	public Collection<ResourceOperation> getAffectedPartitionsForModification() {
		return new HashSet<ResourceOperation>();
	}
	*/

	public void dispose() {
		stopThread();
		((JPASolver)getIndependenceSolver()).dispose();
		setIndependenceSolver(null);
	}
	
	public IPeService getPeService() {
		return peService;
	}

	public IPeServiceUtil getPeServiceUtil() {
		return peServiceUtil;
	}
	
	public IJPAEditorUtil getJPAEditorUtil() {
		return this.jpaEditorUtil;
	}
	
	public IModelIntegrationUtil getMoinIntegrationUtil() {
		return moinIntegrationUtil;
	}
	
	public IGraphicsUpdater getGraphicsUpdater() {
		return graphicsUpdater;
	}

	public TransactionalEditingDomain getTransactionalEditingDomain() {
		Diagram diagram = getDiagramTypeProvider().getDiagram();
		return TransactionUtil.getEditingDomain(diagram);
	}
	
	public PictogramElement getPictogramElementForBusinessObject(Object businessObject) {
		PictogramElement pe = super.getPictogramElementForBusinessObject(businessObject);
		/*
		int cnt = 0;
		while ((pe == null) && (cnt < 20)) {
			//System.out.println("loop");
			try {
				Thread.sleep(250);
			} catch (InterruptedException e) {
			}
			System.out.println("loop; hc:" + businessObject.hashCode());
			pe = super.getPictogramElementForBusinessObject(businessObject);
			cnt++;
		}
		if (pe == null) {
			System.out.println("FP - PE is null; hc:" + businessObject.hashCode());
		}
		*/
		return pe;
	}
	
	public Properties loadProperties(IProject project) {
		return JPADiagramPropertyPage.loadProperties(project);
	}

}

Back to the top