Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpfullbright2008-04-30 14:39:33 +0000
committerpfullbright2008-04-30 14:39:33 +0000
commit89827373e905be99a479dd59ec72b9dd34abe9fb (patch)
tree886ca183faea4702a10341bb6f2414b3bb028e26
parent81195677a39b588b5a4715effc746b6ae203ea3b (diff)
downloadwebtools.dali-89827373e905be99a479dd59ec72b9dd34abe9fb.tar.gz
webtools.dali-89827373e905be99a479dd59ec72b9dd34abe9fb.tar.xz
webtools.dali-89827373e905be99a479dd59ec72b9dd34abe9fb.zip
[Bug 228777] - Mapping File wizard: no defaults based on selection selected
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/operations/OrmFileCreationDataModelProvider.java17
-rw-r--r--jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizard.java169
-rw-r--r--jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizardPage.java41
3 files changed, 184 insertions, 43 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/operations/OrmFileCreationDataModelProvider.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/operations/OrmFileCreationDataModelProvider.java
index 9a761673b7..1dfeb70547 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/operations/OrmFileCreationDataModelProvider.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/operations/OrmFileCreationDataModelProvider.java
@@ -122,7 +122,17 @@ public class OrmFileCreationDataModelProvider extends AbstractDataModelProvider
@Override
public DataModelPropertyDescriptor[] getValidPropertyDescriptors(String propertyName) {
- if (propertyName.equals(DEFAULT_ACCESS)) {
+ if (propertyName.equals(PROJECT_NAME)) {
+ return CollectionTools.array(
+ new TransformationIterator<IProject, DataModelPropertyDescriptor>(jpaIProjects()) {
+ @Override
+ protected DataModelPropertyDescriptor transform(IProject next) {
+ return new DataModelPropertyDescriptor(next.getName());
+ }
+ },
+ new DataModelPropertyDescriptor[0]);
+ }
+ else if (propertyName.equals(DEFAULT_ACCESS)) {
DataModelPropertyDescriptor[] accessTypes = new DataModelPropertyDescriptor[3];
accessTypes[0] = accessPropertyDescriptor(null);
accessTypes[1] = accessPropertyDescriptor(AccessType.FIELD);
@@ -145,7 +155,10 @@ public class OrmFileCreationDataModelProvider extends AbstractDataModelProvider
@Override
public DataModelPropertyDescriptor getPropertyDescriptor(String propertyName) {
- if (propertyName.equals(DEFAULT_ACCESS)) {
+ if (propertyName.equals(PROJECT_NAME)) {
+ return new DataModelPropertyDescriptor(getStringProperty(PROJECT_NAME));
+ }
+ else if (propertyName.equals(DEFAULT_ACCESS)) {
return accessPropertyDescriptor((AccessType) getProperty(DEFAULT_ACCESS));
}
else if (propertyName.equals(PERSISTENCE_UNIT)) {
diff --git a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizard.java b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizard.java
index a5677600e6..04e198336d 100644
--- a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizard.java
+++ b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizard.java
@@ -12,10 +12,22 @@ package org.eclipse.jpt.ui.internal.wizards.orm;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jpt.core.JptCorePlugin;
+import org.eclipse.jpt.core.context.JpaContextNode;
+import org.eclipse.jpt.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.core.internal.operations.OrmFileCreationDataModelProperties;
import org.eclipse.jpt.core.internal.operations.OrmFileCreationDataModelProvider;
import org.eclipse.jpt.ui.JptUiPlugin;
@@ -61,7 +73,164 @@ public class MappingFileWizard extends DataModelWizard
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
+ // check for project, source folder, persistence unit?
+ if (selection == null || selection.isEmpty()) {
+ return;
+ }
+ Object firstSelection = selection.getFirstElement();
+
+ PersistenceUnit pUnit = extractPersistenceUnit(firstSelection);
+ IFolder sourceFolder = extractSourceFolder(pUnit, firstSelection);
+ IProject project = extractProject(pUnit, sourceFolder, firstSelection);
+
+ if (project != null) {
+ getDataModel().setStringProperty(OrmFileCreationDataModelProperties.PROJECT_NAME, project.getName());
+ }
+ if (sourceFolder != null) {
+ getDataModel().setStringProperty(OrmFileCreationDataModelProperties.SOURCE_FOLDER, sourceFolder.getFullPath().toPortableString());
+ }
+ if (pUnit != null) {
+ getDataModel().setBooleanProperty(OrmFileCreationDataModelProperties.ADD_TO_PERSISTENCE_UNIT, true);
+ getDataModel().setStringProperty(OrmFileCreationDataModelProperties.PERSISTENCE_UNIT, pUnit.getName());
+ }
+ }
+
+ private PersistenceUnit extractPersistenceUnit(Object selection) {
+ if (selection instanceof PersistenceUnit) {
+ return (PersistenceUnit) selection;
+ }
+ PersistenceUnit pUnit = null;
+ if (selection instanceof JpaContextNode) {
+ try {
+ pUnit = ((JpaContextNode) selection).getPersistenceUnit();
+ }
+ catch (Exception e) { /* do nothing, just continue */ }
+ }
+ if (pUnit == null && selection instanceof IAdaptable) {
+ pUnit = (PersistenceUnit) ((IAdaptable) selection).getAdapter(PersistenceUnit.class);
+
+ }
+ return pUnit;
+ }
+
+ private IFolder extractSourceFolder(PersistenceUnit pUnit, Object selection) {
+ IJavaProject javaProject = null;
+ IFolder srcFolder = null;
+ if (pUnit != null) {
+ javaProject = pUnit.getJpaProject().getJavaProject();
+ srcFolder = findSourceFolder(javaProject, pUnit.getResource());
+ if (srcFolder != null) {
+ return srcFolder;
+ }
+
+ }
+ if (selection instanceof IResource) {
+ javaProject = JavaCore.create(((IResource) selection).getProject());
+ if (javaProject.exists()) {
+ srcFolder = findSourceFolder(javaProject, (IResource) selection);
+ if (srcFolder != null) {
+ return srcFolder;
+ }
+ }
+ }
+
+ if (selection instanceof IAdaptable) {
+ IResource resource = (IResource) ((IAdaptable) selection).getAdapter(IResource.class);
+ if (resource != null) {
+ javaProject = JavaCore.create((resource).getProject());
+ if (javaProject.exists()) {
+ srcFolder = findSourceFolder(javaProject, resource);
+ if (srcFolder != null) {
+ return srcFolder;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ private IFolder findSourceFolder(IJavaProject javaProject, IResource resource) {
+ if (JptCorePlugin.getJpaProject(javaProject.getProject()) == null) {
+ // not even a jpa project
+ return null;
+ }
+ while (resource != null && ! (resource instanceof IFolder)) {
+ resource = resource.getParent();
+ }
+ if (resource == null) {
+ return null;
+ }
+ IFolder folder = (IFolder) resource;
+ try {
+ IPackageFragmentRoot packageFragmentRoot = null;
+ while (packageFragmentRoot == null && folder != null) {
+ packageFragmentRoot = javaProject.findPackageFragmentRoot(folder.getFullPath());
+ if (packageFragmentRoot == null) {
+ IPackageFragment packageFragment = javaProject.findPackageFragment(folder.getFullPath());
+ if (packageFragment != null) {
+ packageFragmentRoot = (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
+ }
+ }
+ if (packageFragmentRoot == null) {
+ try {
+ folder = (IFolder) folder.getParent();
+ }
+ catch (ClassCastException cce) {
+ folder = null;
+ }
+ }
+ }
+ if (packageFragmentRoot == null) {
+ return null;
+ }
+ if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
+ return (IFolder) packageFragmentRoot.getResource();
+ }
+ }
+ catch (JavaModelException jme) { /* do nothing, return null */ }
+ return null;
+ }
+
+ private IProject extractProject(PersistenceUnit pUnit, IFolder sourceFolder, Object selection) {
+ if (pUnit != null) {
+ return pUnit.getJpaProject().getProject();
+ }
+ if (sourceFolder != null) {
+ return sourceFolder.getProject();
+ }
+
+ IProject project = null;
+ if (selection instanceof IResource) {
+ project = ((IResource) selection).getProject();
+ }
+ if (project == null && selection instanceof IJavaElement) {
+ project = ((IJavaElement) selection).getJavaProject().getProject();
+ }
+ if (project == null && selection instanceof JpaContextNode) {
+ project = ((JpaContextNode) selection).getJpaProject().getProject();
+ }
+ if (project == null && selection instanceof IAdaptable) {
+ project = (IProject) ((IAdaptable) selection).getAdapter(IProject.class);
+ if (project == null) {
+ IResource resource = (IResource) ((IAdaptable) selection).getAdapter(IResource.class);
+ if (resource != null) {
+ project = resource.getProject();
+ }
+ }
+ if (project == null) {
+ IJavaElement javaElement = (IJavaElement) ((IAdaptable) selection).getAdapter(IJavaElement.class);
+ if (javaElement != null) {
+ project = javaElement.getJavaProject().getProject();
+ }
+ }
+ }
+
+ if (project != null && JptCorePlugin.getJpaProject(project) != null) {
+ return project;
+ }
+
+ return null;
}
protected void postPerformFinish() throws InvocationTargetException {
diff --git a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizardPage.java b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizardPage.java
index a8f1b4b876..787762c167 100644
--- a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizardPage.java
+++ b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/orm/MappingFileWizardPage.java
@@ -10,12 +10,10 @@
*******************************************************************************/
package org.eclipse.jpt.ui.internal.wizards.orm;
-import java.util.Iterator;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IPackageFragmentRoot;
@@ -28,15 +26,11 @@ import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.window.Window;
-import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.core.internal.operations.OrmFileCreationDataModelProperties;
import org.eclipse.jpt.ui.JptUiPlugin;
import org.eclipse.jpt.ui.internal.JptUiMessages;
-import org.eclipse.jpt.utility.internal.CollectionTools;
-import org.eclipse.jpt.utility.internal.iterators.FilteringIterator;
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
@@ -54,7 +48,6 @@ import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizardPage;
-import org.eclipse.wst.common.project.facet.core.FacetedProjectFramework;
public class MappingFileWizardPage extends DataModelWizardPage
implements OrmFileCreationDataModelProperties
@@ -124,20 +117,7 @@ public class MappingFileWizardPage extends DataModelWizardPage
data.widthHint = 300;
data.horizontalSpan = 1;
projectNameCombo.setLayoutData(data);
- projectNameCombo.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- super.widgetSelected(e);
- // update source folder
-// if (folderText != null) {
-// String sourceFolder = getDefaultJavaSourceFolder(ProjectUtilities.getProject(projectNameCombo.getText())).getFullPath().toOSString();
-// if (sourceFolder != null)
-// folderText.setText(sourceFolder);
-// }
- }
- });
synchHelper.synchCombo(projectNameCombo, PROJECT_NAME, null);
- fillProjects();
new Label(composite, SWT.NONE);
sourceFolderLabel = new Label(composite, SWT.NONE);
@@ -232,27 +212,6 @@ public class MappingFileWizardPage extends DataModelWizardPage
return composite;
}
- private void fillProjects() {
- projectNameCombo.setItems(new String[0]);
- for (Iterator<IProject> stream = jpaProjects(); stream.hasNext(); ) {
- projectNameCombo.add(stream.next().getName());
- }
- }
-
- private Iterator<IProject> jpaProjects() {
- return new FilteringIterator<IProject, IProject>(CollectionTools.iterator(ProjectUtilities.getAllProjects())) {
- @Override
- protected boolean accept(IProject project) {
- try {
- return FacetedProjectFramework.hasProjectFacet(project, JptCorePlugin.FACET_ID);
- }
- catch (CoreException ce) {
- return false;
- }
- }
- };
- }
-
private void handleSourceFolderButtonPressed() {
ISelectionStatusValidator validator = getSourceFolderDialogSelectionValidator();
ViewerFilter filter = getSourceFolderDialogViewerFilter();

Back to the top