Skip to main content
summaryrefslogtreecommitdiffstats
blob: fc9908083fbc3bfd3f41df401cf341866685a4ac (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
/*******************************************************************************
 * Copyright (c) 2011, 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.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.ui.JpaPlatformUi;
import org.eclipse.jpt.jpa.ui.internal.platform.JpaPlatformUiRegistry;
import org.eclipse.ui.handlers.HandlerUtil;

public abstract class AbstractJpaJavaMetadataConversionHandler extends AbstractHandler {

	public AbstractJpaJavaMetadataConversionHandler() {
		super();
	}


	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection selections = (IStructuredSelection)
				HandlerUtil.getCurrentSelectionChecked(event);
		
		Object selection = selections.iterator().next();
		JpaProject jpaProject = getJpaProjectFromSelection(selection);
		if (jpaProject != null) {
			converterJavaGlobalMetadata(jpaProject);
		}
		return null;
	}


	abstract protected void converterJavaGlobalMetadata(JpaProject jpaProject);

	protected JpaPlatformUi getJpaPlatformUi(JpaProject project) {
		String coreJpaPlatformId = project.getJpaPlatform().getId();
		return JpaPlatformUiRegistry.instance().getJpaPlatformUi(coreJpaPlatformId);
	}

	protected JpaProject getJpaProjectFromSelection(Object selection) {
		IProject project = getIProjectFromSelection(selection);
		if (project != null) {
			return (JpaProject) project.getAdapter(JpaProject.class);
		}
		return null;
	}

	protected IProject getIProjectFromSelection(Object selection) {
		if (selection instanceof IProject) {
			return (IProject) selection;
		}
		if (selection instanceof IJavaProject) {
			return ((IJavaProject) selection).getProject();
		}
		return null;
	}
}

Back to the top