Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9ef77ab65ef4d603d852c20bbdf8c342a95341aa (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
/*******************************************************************************
 * <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.modelintegration.ui;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.context.impl.CustomContext;
import org.eclipse.graphiti.features.custom.ICustomFeature;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jpt.jpa.core.JpaProject;
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.modelintegration.util.IModelIntegrationUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.util.ModelIntegrationUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPADiagramEditorInput;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorMatchingStrategy;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;


public class JPAEditorMatchingStrategy implements IEditorMatchingStrategy {
	
	private IJPAEditorFeatureProvider fp = null;
	
	private static final String CODE_GENERATED = "CODE_GENERATED"; 	//$NON-NLS-1$
	public static final String DOUBLE_CLICK = "DOUBLE_CLICK";		//$NON-NLS-1$
	
	
	public JPAEditorMatchingStrategy() {}
	
	/**
	 * This constructor is intended solely for the test purposes
	 *  
	 * @param fp the feature provider
	 */
	public JPAEditorMatchingStrategy(IJPAEditorFeatureProvider fp) {
		this.fp = fp;
	}
	
	public boolean matches(IEditorReference editorRef, IEditorInput input) {
		if (input instanceof IFileEditorInput) {
			IFileEditorInput fileInput = (IFileEditorInput) input;
			IFile entityFile = fileInput.getFile();
			if(!entityFile.getName().endsWith(".java")){ //$NON-NLS-1$
				return false;
			}
			try {
				QualifiedName qn = new QualifiedName(null, DOUBLE_CLICK);
				if ("true".equals(entityFile.getSessionProperty(qn))) { //$NON-NLS-1$
					entityFile.setSessionProperty(qn, null);
					return false;
				}
			} catch (CoreException e1) {
				System.err.println("Cannot get session property DOUBLE_CLICK");	//$NON-NLS-1$
				e1.printStackTrace();	
			}
			JavaPersistentType inputJptType = null; 
			
			if (fp == null) {
				inputJptType = JPAEditorUtil.getJPType(JavaCore.createCompilationUnitFrom(entityFile));
			} else {
				inputJptType = fp.getJPAEditorUtil().getJPType(JavaCore.createCompilationUnitFrom(entityFile));
			}
			if (inputJptType == null)
				return false;
			PersistenceUnit persistenceUnit = inputJptType.getPersistenceUnit();
			JpaProject jpaProject = persistenceUnit.getJpaProject();

			try {
				IEditorInput editorInput = editorRef.getEditorInput();
				if (editorInput instanceof IJPADiagramEditorInput) {
					IJPADiagramEditorInput jpInput = (IJPADiagramEditorInput) editorInput;
					Diagram diagram = jpInput.getDiagram();
					if (diagram != null) {
						
						IJPAEditorFeatureProvider featureProvider = null;
						IDiagramTypeProvider diagramProvider = null;
						if (fp != null) {
							featureProvider = this.fp;
							diagramProvider = featureProvider.getDiagramTypeProvider();
						} else {
							diagramProvider = ModelIntegrationUtil.getProviderByDiagram(diagram);
							featureProvider = (IJPAEditorFeatureProvider)diagramProvider.getFeatureProvider();
						}
						IModelIntegrationUtil moinIntegrationUtil = featureProvider.getMoinIntegrationUtil();
						JpaProject jpaProjectFromEditor = moinIntegrationUtil.getProjectByDiagram(diagram);
						if (jpaProject.equals(jpaProjectFromEditor)) {
							if (fileInput.getName() != CODE_GENERATED) 
								return false;
							PictogramElement pe = featureProvider.getPictogramElementForBusinessObject(inputJptType);
							if (pe != null) {
								diagramProvider.getDiagramEditor().setPictogramElementForSelection(pe);
								return true;
							}
							
							ICustomFeature feature = featureProvider.getAddAllEntitiesFeature();
							CustomContext context = new CustomContext();
							feature.execute(context);
							return true;
						}
					}
				}
			} catch (PartInitException e) {
				JPADiagramEditorPlugin.getDefault().getLog().log(e.getStatus());
			}
		} else if (input instanceof IJPADiagramEditorInput) {
			return editorRef.getPartName().equals(((IJPADiagramEditorInput) input).getProjectName());
		}
		return false;
	}

}

Back to the top