Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4fd388b48728dbd7434f5dc6cd2be58696b4e718 (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
/*******************************************************************************
 * <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.feature;

import org.eclipse.graphiti.features.context.IContext;
import org.eclipse.graphiti.features.context.IDeleteContext;
import org.eclipse.graphiti.features.context.impl.DeleteContext;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.ui.features.DefaultDeleteFeature;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.i18n.JPAEditorMessages;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.AbstractRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.HasReferanceRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IBidirectionalRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IUnidirectionalRelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.relations.IsARelation;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;


public class DeleteRelationFeature extends DefaultDeleteFeature{
	
	private static IJPAEditorUtil ut = null;
		
	public DeleteRelationFeature(IJPAEditorFeatureProvider fp) {
		super(fp);
		ut = fp.getJPAEditorUtil();
	}
	
	@Override
	public boolean canDelete(IDeleteContext context) {
		return true;
	}
	
	@Override
	public boolean isAvailable(IContext context) {
		return true;
	}
	
	@Override
	public boolean canExecute(IContext context) {
		return true;
	}	
	
    protected String getQuestionToUser() {
    	return JPAEditorMessages.DeleteRelationFeature_deleteRelationQuestion;
    }
	
    @Override
	public void delete(IDeleteContext context) {
        PictogramElement pe = context.getPictogramElement();
        Object businessObjectForPictogramElement = getBusinessObjectForPictogramElement(pe);
        if (businessObjectForPictogramElement != null) {
            if (!getUserDecision(context)) {
                return;
            }
        }

        preDelete(context);
        
        if(businessObjectForPictogramElement instanceof AbstractRelation) {
        	deleteAbstractRelation(businessObjectForPictogramElement);   
        } else if (businessObjectForPictogramElement instanceof HasReferanceRelation){
        	deleteEmbeddedRelation(businessObjectForPictogramElement);
        } else if (businessObjectForPictogramElement instanceof IsARelation) {
        	deleteInheritanceRelation(businessObjectForPictogramElement);
        }

        postDelete(context);
    }

    private void deleteInheritanceRelation(Object businessObjectForPictogramElement) {
		IsARelation rel = (IsARelation)businessObjectForPictogramElement;
		PersistentType superclass = rel.getSuperclass();
		PersistentType subclass = rel.getSubclass();
		JpaArtifactFactory.instance().buildHierarchy(superclass, subclass, false);
		
		JPAEditorUtil.getCompilationUnit(subclass);
//		subclass.getJavaResourceType().getJavaResourceCompilationUnit().synchronizeWithJavaSource();
		
		getFeatureProvider().addJPTForUpdate(subclass.getName());
	}
    
	private void deleteEmbeddedRelation(Object businessObjectForPictogramElement) {
		HasReferanceRelation rel = (HasReferanceRelation)businessObjectForPictogramElement;
		PersistentAttribute attribute = rel.getEmbeddedAnnotatedAttribute();
		PictogramElement textShape = getFeatureProvider().getPictogramElementForBusinessObject(attribute);
		if(textShape == null)
			return;
		ClickRemoveAttributeButtonFeature feat = new ClickRemoveAttributeButtonFeature(getFeatureProvider());
		IDeleteContext delCtx = new DeleteContext(textShape);    		
		feat.delete(delCtx, false);
	}

	private void deleteAbstractRelation(Object businessObjectForPictogramElement) {
		AbstractRelation rel = (AbstractRelation)businessObjectForPictogramElement;
		
        if (rel instanceof IUnidirectionalRelation) {
        	IUnidirectionalRelation relation = (IUnidirectionalRelation)rel;
    		PersistentAttribute attribute = relation.getAnnotatedAttribute();
    		PictogramElement textShape = getFeatureProvider().getPictogramElementForBusinessObject(attribute);
    		if(textShape == null)
    			return;
        	ClickRemoveAttributeButtonFeature feat = new ClickRemoveAttributeButtonFeature(getFeatureProvider());
    		IDeleteContext delCtx = new DeleteContext(textShape);    		
    		feat.delete(delCtx, false);
    	}    	

        if (rel instanceof IBidirectionalRelation) { 			
        	IBidirectionalRelation relation = (IBidirectionalRelation)(rel);
        	ClickRemoveAttributeButtonFeature feat = new ClickRemoveAttributeButtonFeature(getFeatureProvider());
    		PersistentAttribute ownerAttribute = relation.getOwnerAnnotatedAttribute();
    		PictogramElement ownerAttributeTextShape = getFeatureProvider().getPictogramElementForBusinessObject(ownerAttribute);
    		if(ownerAttributeTextShape == null)
    			return;
    		IDeleteContext deleteOwnerAttributeContext = new DeleteContext(ownerAttributeTextShape);
    		feat.delete(deleteOwnerAttributeContext, false);

    		PersistentAttribute inverseAttribute = relation.getInverseAnnotatedAttribute();
    		PictogramElement inverseAttributeTextShape = getFeatureProvider().getPictogramElementForBusinessObject(inverseAttribute);
    		if(inverseAttributeTextShape == null)
    			return;
    		IDeleteContext deleteInverseAttributeContext = new DeleteContext(inverseAttributeTextShape);
    		feat.delete(deleteInverseAttributeContext, false);
    	}
	}	
    
	@Override
	public void postDelete(IDeleteContext context) {
        PictogramElement pe = context.getPictogramElement();
        Object businessObjectForPictogramElement = getBusinessObjectForPictogramElement(pe);
		IWorkbenchSite ws = ((IEditorPart) getDiagramEditor()).getSite();
		if (businessObjectForPictogramElement instanceof IRelation) {
			IRelation rel = (IRelation) businessObjectForPictogramElement;
			ICompilationUnit cu = getFeatureProvider().getCompilationUnit(rel.getOwner());
			ut.organizeImports(cu, ws);
			if (rel instanceof IBidirectionalRelation) {
				cu = getFeatureProvider().getCompilationUnit(rel.getInverse());
				ut.organizeImports(cu, ws);
			}
		} else if(businessObjectForPictogramElement instanceof HasReferanceRelation){
			HasReferanceRelation rel = (HasReferanceRelation) businessObjectForPictogramElement;
			ICompilationUnit cu = getFeatureProvider().getCompilationUnit(rel.getEmbeddingEntity());
			ut.organizeImports(cu, ws); 
		} else if (businessObjectForPictogramElement instanceof IsARelation){
			IsARelation rel = (IsARelation) businessObjectForPictogramElement;
			ICompilationUnit cu = getFeatureProvider().getCompilationUnit(rel.getSubclass());
			ut.organizeImports(cu, ws); 
		}
	}
    
    
	@Override
	public IJPAEditorFeatureProvider getFeatureProvider() {
		return  (IJPAEditorFeatureProvider)super.getFeatureProvider();
	}    
	
	@Override
	protected boolean getUserDecision(IDeleteContext context) {
		return MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
				JPAEditorMessages.DeleteFeature_deleteConfirm, JPAEditorMessages.DeleteRelationFeature_deleteRelationQuestion);
	}
	
}

Back to the top