Skip to main content
summaryrefslogtreecommitdiffstats
blob: 50d2ce4e933bd88529d8ed4775c83f29f6474010 (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
/*******************************************************************************
* Copyright (c) 2010, 2012 Oracle. 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:
*     Oracle - initial API and implementation
*******************************************************************************/
package org.eclipse.jpt.jpa.ui.internal.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.ui.JptJpaUiPlugin;
import org.eclipse.jpt.jpa.ui.internal.wizards.JpaMakePersistentWizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;


public class MakePersistentAction implements IObjectActionDelegate {

	
	private Map<IProject, List<IType>> selectedTypes;
	
	public MakePersistentAction() {
		super();
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// do nothing		
	}
	
	public void selectionChanged(IAction action, ISelection selection) {
		// Action is contributed for IType, ICompilationUnit, IPackageFragment, and IPackageFragmentRoot that is a source folder
		this.selectedTypes = this.buildSelectedTypes((StructuredSelection) selection);
	}

	protected Map<IProject, List<IType>> buildSelectedTypes(StructuredSelection structuredSelection) {
		Map<IProject, List<IType>> types = new HashMap<IProject, List<IType>>();
		
		for (Object selection : structuredSelection.toList()) {
			switch (((IJavaElement) selection).getElementType()) {
				case IJavaElement.TYPE :
					addSelectedType((IType) selection, types);
					break;
				case IJavaElement.COMPILATION_UNIT :
					addSelectedType((ICompilationUnit) selection, types);
					break;
				case IJavaElement.PACKAGE_FRAGMENT :
					addSelectedType((IPackageFragment) selection, types);
					break;
				case IJavaElement.PACKAGE_FRAGMENT_ROOT :
					addSelectedType((IPackageFragmentRoot) selection, types);
					break;
				default :
					break;
			}
		}
		return types;
	}

	private void addSelectedType(IPackageFragmentRoot packageFragmentRoot, Map<IProject, List<IType>> types) {
		for (IJavaElement pkgFragment : getPackageFragments(packageFragmentRoot)) {
			addSelectedType((IPackageFragment) pkgFragment, types);
		}
	}

	private void addSelectedType(IPackageFragment packageFragment, Map<IProject, List<IType>> types) {
		for (ICompilationUnit compUnit : getCompilationUnits(packageFragment)) {
			addSelectedType(compUnit, types);
		}
	}
	
	private void addSelectedType(ICompilationUnit compilationUnit, Map<IProject, List<IType>> types) {
		IType primaryType = this.getPrimaryType(compilationUnit);
		if (primaryType != null) {
			this.addSelectedType(primaryType, types);
		}
	}
	
	private void addSelectedType(IType primaryType, Map<IProject, List<IType>> typesMap) {
		IProject project = primaryType.getJavaProject().getProject();
		List<IType> types = typesMap.get(project);
		if (types == null) {
			types = new ArrayList<IType>();
			typesMap.put(project, types);
		}
		if (!types.contains(primaryType)) {
			types.add(primaryType);
		}
	}

	private ICompilationUnit[] getCompilationUnits(IPackageFragment packageFragment) {
		try {
			return packageFragment.getCompilationUnits();
		}
		catch (JavaModelException e) {
			JptJpaUiPlugin.log(e);
		}
		return new ICompilationUnit[0];
	}
	
	private IJavaElement[] getPackageFragments(IPackageFragmentRoot packageFragmentRoot) {
		try {
			return packageFragmentRoot.getChildren();
		}
		catch (JavaModelException e) {
			JptJpaUiPlugin.log(e);
		}
		return new IJavaElement[0];
	}

	private IType getPrimaryType(ICompilationUnit compilationUnit) {
		return compilationUnit.findPrimaryType();
	}

	public void run(IAction action) {
		for (List<IType> types : this.selectedTypes.values()) {
			IProject project = types.get(0).getResource().getProject();
			JpaProject jpaProject = (JpaProject) project.getAdapter(JpaProject.class);
			if (jpaProject != null) {
				//open the wizard once for each selected project
				JpaMakePersistentWizard wizard = new JpaMakePersistentWizard(jpaProject, types);
				WizardDialog dialog = new WizardDialog(this.getCurrentShell(), wizard);
				dialog.create();
				dialog.open();
			}
		}
	}
	
	private Shell getCurrentShell() {
	    return Display.getCurrent().getActiveShell();
	}
}

Back to the top