Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5451c3797400276fe3d491261871fa4ff9f6b74 (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
/*******************************************************************************
 * Copyright (c) 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.eclipselink.ui.internal.wizards;

import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jpt.common.core.internal.utility.PlatformTools;
import org.eclipse.jpt.common.core.resource.xml.JptXmlResource;
import org.eclipse.jpt.common.ui.internal.util.SWTUtil;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.context.JpaContextNode;
import org.eclipse.jpt.jpa.eclipselink.ui.internal.EclipseLinkUiMessages;
import org.eclipse.jpt.jpa.eclipselink.ui.internal.entity.data.model.DynamicEntityDataModelProvider;
import org.eclipse.jpt.jpa.eclipselink.ui.internal.plugin.JptJpaEclipseLinkUiPlugin;
import org.eclipse.jpt.jpa.ui.internal.JptUiIcons;
import org.eclipse.jpt.jpa.ui.internal.wizards.entity.data.model.IEntityDataModelProperties;
import org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.frameworks.datamodel.IDataModelProvider;
import org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizard;

public class EclipseLinkDynamicEntityWizard extends DataModelWizard implements INewWizard{

	private String initialProjectName;
	private DynamicEntityClassWizardPage page1;
	private DynamicEntityFieldsWizardPage page2;

	public EclipseLinkDynamicEntityWizard(IDataModel model) {
		super(model);
        setWindowTitle(EclipseLinkUiMessages.EclipseLinkDynamicEntityWizard_title);
        setDefaultPageImageDescriptor(JptJpaEclipseLinkUiPlugin.instance().buildImageDescriptor(JptUiIcons.ENTITY_WIZ_BANNER));
	}

	/**
	 * Empty constructor
	 */
	public EclipseLinkDynamicEntityWizard(){
    	this(null);
    }

	/* Adds the two pages of the new dynamic entity wizard 
	 * @see org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizard#doAddPages()
	 */
	@Override
	protected void doAddPages() {
		page1 = new DynamicEntityClassWizardPage(getDataModel(), "pageOne");
		page1.setProjectName(this.initialProjectName);
		addPage(page1);
		page2 = new DynamicEntityFieldsWizardPage(getDataModel(), "pageTwo");
		addPage(page2);
	}

	/*
	 * Wizard cannot be finished on the first page 
	 * Wizard can be finished on the second page since it's reasonable 
	 * for users to create a dynamic entity without any field(s) and then
	 * add virtual attributes manually or through JPA structure view
	 */
	@Override
	public boolean canFinish() {
		return this.getContainer().getCurrentPage() == page1 ? false : page2.getErrorMessage() == null;
	}

	/* Return the default data model provider (DynamicEntityDataModelProvider in our case)
	 * @see org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizard#getDefaultProvider()
	 */
	@Override
	protected IDataModelProvider getDefaultProvider() {
		return new DynamicEntityDataModelProvider();
	}

	/* 
	 * Override the parent method in order to open the generated dynamic entity in Java editor
	 * @see org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizard#postPerformFinish()
	 */
	@Override
    protected void postPerformFinish() throws InvocationTargetException {
        try {
        	IProject project = (IProject) this.getDataModel().getProperty(INewJavaClassDataModelProperties.PROJECT);
        	JpaProject jpaProject = (JpaProject)(project).getAdapter(JpaProject.class);
        	String xmlRuntimePath = this.getDataModel().getStringProperty(IEntityDataModelProperties.XML_NAME).trim();
        	JptXmlResource xmlResource = jpaProject.getMappingFileXmlResource(new Path(xmlRuntimePath));
            openEditor(xmlResource);
        } catch (Exception cantOpen) {
        	JptJpaEclipseLinkUiPlugin.instance().logError(cantOpen);
        }
    }

	/*
	 * Open the EclipseLink mapping file where the dynamic entity is created
	 */
	protected void openEditor(final JptXmlResource xmlResource) {
		SWTUtil.asyncExec(new Runnable() {
			public void run() {
				IFile file = xmlResource.getFile();
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				try {
					IDE.openEditor(page, file, true);
				} catch (PartInitException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void init(IWorkbench workbench, IStructuredSelection iSelection) {
    	this.initialProjectName = getProjectName(iSelection);
		this.getDataModel();
	}

	protected IProject extractProject(IStructuredSelection iSelection) {
		Object selectedObj = iSelection.getFirstElement();
		if (selectedObj == null) {
			return null;
		}

		IResource resource = PlatformTools.getAdapter(selectedObj, IResource.class);
		if (resource != null) {
			return resource.getProject();
		}
		IJavaElement javaElement = PlatformTools.getAdapter(selectedObj, IJavaElement.class);
		if (javaElement != null) {
			return javaElement.getJavaProject().getProject();
		}
		JpaContextNode node = PlatformTools.getAdapter(selectedObj, JpaContextNode.class);
		if (node != null) {
			return node.getJpaProject().getProject();
		}
		return null;
	}

    protected String getProjectName(IStructuredSelection iSelection) {
    	IProject iProject = this.extractProject(iSelection);
    	return iProject == null? 
    			EclipseLinkUiMessages.DynamicEntityClassWizardPage_emptyString : 
    			iProject.getName();
	}
}

Back to the top