diff options
author | kmoore | 2009-04-08 21:14:43 +0000 |
---|---|---|
committer | kmoore | 2009-04-08 21:14:43 +0000 |
commit | 42930aa8f26f77f3ab22ef27fbc4c425045dfec6 (patch) | |
tree | 07a533f4f74589d04a207267343cf0fd47c05d28 /jpa | |
parent | 8185ab4ca8d58705d1cf6b099fce0905987ebbf8 (diff) | |
download | webtools.dali-42930aa8f26f77f3ab22ef27fbc4c425045dfec6.tar.gz webtools.dali-42930aa8f26f77f3ab22ef27fbc4c425045dfec6.tar.xz webtools.dali-42930aa8f26f77f3ab22ef27fbc4c425045dfec6.zip |
250802 - Missing Restrictions when creating an Entity Field in the new Entity wizard
Diffstat (limited to 'jpa')
4 files changed, 166 insertions, 15 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.ui/property_files/jpt_ui_entity_wizard.properties b/jpa/plugins/org.eclipse.jpt.ui/property_files/jpt_ui_entity_wizard.properties index efdf650706..8ec07e7a43 100644 --- a/jpa/plugins/org.eclipse.jpt.ui/property_files/jpt_ui_entity_wizard.properties +++ b/jpa/plugins/org.eclipse.jpt.ui/property_files/jpt_ui_entity_wizard.properties @@ -52,3 +52,5 @@ NO_JPA_PROJECTS=No JPA projects in the workspace APPLY_CHANGES_TO_PERSISTENCE_XML=Apply changes to persistence XML ADD_MAPPED_SUPERCLASS_TO_XML=Add mapped superclass to XML ADD_ENTITY_TO_XML=Add entity to XML +EntityDataModelProvider_entityNotInProjectClasspath=''{0}'' does not exist on the project classpath +EntityDataModelProvider_invalidArgument=Invalid argument ''{0}'' diff --git a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityRowTableWizardSection.java b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityRowTableWizardSection.java index 9228e1200f..d8aa93d21d 100644 --- a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityRowTableWizardSection.java +++ b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityRowTableWizardSection.java @@ -15,11 +15,15 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.eclipse.jdt.ui.IJavaElementSearchConstants; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.core.runtime.IStatus; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.search.IJavaSearchConstants; +import org.eclipse.jdt.core.JavaConventions; +import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.search.IJavaSearchScope; -import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.viewers.CheckStateChangedEvent; @@ -37,6 +41,7 @@ import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.window.Window; +import org.eclipse.jpt.ui.JptUiPlugin; import org.eclipse.jpt.ui.internal.wizards.entity.data.model.EntityRow; import org.eclipse.jpt.ui.internal.wizards.entity.data.model.IEntityDataModelProperties; import org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties; @@ -62,6 +67,7 @@ import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.dialogs.SelectionDialog; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; /** @@ -506,7 +512,16 @@ public class EntityRowTableWizardSection extends Composite { */ public boolean validate(Combo combo, Text[] texts) { if (texts.length > 0) { - return texts[0].getText().trim().length() > 0; + IStatus validateFieldNameStatus = JavaConventions + .validateFieldName(texts[0].getText(), + JavaCore.VERSION_1_5, + JavaCore.VERSION_1_5); + if (!validateFieldNameStatus.isOK()) { + return false; + } + } + if (combo.getText().equals("")) { + return false; } return true; } @@ -574,7 +589,7 @@ public class EntityRowTableWizardSection extends Composite { label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); - combo = new Combo(composite, SWT.SINGLE | SWT.BORDER);// | SWT.READ_ONLY); + combo = new Combo(composite, SWT.SINGLE | SWT.BORDER);// | SWT.READ_ONLY); combo.setItems(labelsForCombo); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; @@ -615,7 +630,6 @@ public class EntityRowTableWizardSection extends Composite { /** * Process browsing when the Browse... button have been pressed. Allow adding of entity field * with arbitrary type. - * @see org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog */ private void handleChooseEntityTypeButtonPressed() { //getControl().setCursor(new Cursor(getShell().getDisplay(), SWT.CURSOR_WAIT)); @@ -625,15 +639,33 @@ public class EntityRowTableWizardSection extends Composite { } // this eliminates the non-exported classpath entries - final IJavaSearchScope scope = TypeSearchEngine.createJavaSearchScopeForAProject(packRoot.getJavaProject(), true, true); - + final IJavaSearchScope scope = TypeSearchEngine.createJavaSearchScopeForAProject(packRoot.getJavaProject(), true, true); + // This includes all entries on the classpath. - FilteredTypesSelectionDialog dialog = new FilteredTypesSelectionDialog(getShell(), false, null/*getWizard().getContainer()*/, scope, IJavaSearchConstants.TYPE); + SelectionDialog dialog=null; + try{ + dialog = JavaUI + .createTypeDialog( + getShell(), + null, + scope, + IJavaElementSearchConstants.CONSIDER_ALL_TYPES, + false); + } catch (JavaModelException e) { + JptUiPlugin.instance().getLog().log(e.getStatus()); + return; + } + dialog.setTitle(EntityWizardMsg.TYPE_DIALOG_TITLE); dialog.setMessage(EntityWizardMsg.TYPE_DIALOG_DESCRIPTION); if (dialog.open() == Window.OK) { - IType type = (IType) dialog.getFirstResult(); + IType type; + Object[] result = dialog.getResult(); + if (result == null || result.length == 0) { + type = null; + } + else type = (IType) result[0]; String superclassFullPath = IEntityDataModelProperties.EMPTY_STRING; if (type != null) { superclassFullPath = type.getFullyQualifiedName(); @@ -654,6 +686,7 @@ public class EntityRowTableWizardSection extends Composite { Composite composite = (Composite) super.createContents(parent); combo.addSelectionListener(this); + combo.addModifyListener(this); for (int i = 0; i < texts.length; i++) { texts[i].addModifyListener(this); } diff --git a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityWizardMsg.java b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityWizardMsg.java index a393ecaba3..6509ac6f01 100644 --- a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityWizardMsg.java +++ b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/EntityWizardMsg.java @@ -54,7 +54,9 @@ public class EntityWizardMsg extends NLS { public static String BROWSE_BUTTON_LABEL; public static String ADD_BUTTON_LABEL; public static String EDIT_BUTTON_LABEL; - public static String REMOVE_BUTTON_LABEL; + public static String EntityDataModelProvider_entityNotInProjectClasspath; + public static String EntityDataModelProvider_invalidArgument; + public static String REMOVE_BUTTON_LABEL; public static String DUPLICATED_ENTITY_NAMES_MESSAGE; public static String ACCESS_TYPE; public static String FIELD_BASED; diff --git a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/data/model/EntityDataModelProvider.java b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/data/model/EntityDataModelProvider.java index fa7714b06f..b7eaf21557 100644 --- a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/data/model/EntityDataModelProvider.java +++ b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/wizards/entity/data/model/EntityDataModelProvider.java @@ -11,12 +11,20 @@ ***********************************************************************/ package org.eclipse.jpt.ui.internal.wizards.entity.data.model; +import java.text.MessageFormat; import java.util.ArrayList; +import java.util.List; import java.util.Set; + import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaConventions; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.Signature; import org.eclipse.jem.util.emf.workbench.ProjectUtilities; import org.eclipse.jpt.core.JpaFile; import org.eclipse.jpt.core.JptCorePlugin; @@ -24,9 +32,11 @@ import org.eclipse.jpt.ui.JptUiPlugin; import org.eclipse.jpt.ui.internal.wizards.entity.EntityWizardMsg; import org.eclipse.jpt.ui.internal.wizards.entity.data.operation.NewEntityClassOperation; import org.eclipse.jst.j2ee.internal.common.J2EECommonMessages; +import org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties; import org.eclipse.jst.j2ee.internal.common.operations.NewJavaClassDataModelProvider; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation; +import org.eclipse.wst.common.frameworks.datamodel.IDataModelProvider; import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonPlugin; public class EntityDataModelProvider extends NewJavaClassDataModelProvider implements IEntityDataModelProperties{ @@ -151,7 +161,7 @@ public class EntityDataModelProvider extends NewJavaClassDataModelProvider imple return WTPCommonPlugin.createWarningStatus(EntityWizardMsg.DEFAULT_PACKAGE_WARNING); } // Use standard java conventions to validate the package name - IStatus javaStatus = JavaConventions.validatePackageName(packName); + IStatus javaStatus = JavaConventions.validatePackageName(packName, JavaCore.VERSION_1_5, JavaCore.VERSION_1_5); if (javaStatus.getSeverity() == IStatus.ERROR) { String msg = J2EECommonMessages.ERR_JAVA_PACAKGE_NAME_INVALID + javaStatus.getMessage(); return WTPCommonPlugin.createErrorStatus(msg); @@ -175,7 +185,7 @@ public class EntityDataModelProvider extends NewJavaClassDataModelProvider imple */ private IStatus validateXmlName(String xmlName) { if (getBooleanProperty(XML_SUPPORT)) { - String projectName = model.getStringProperty(PROJECT_NAME); + String projectName = this.model.getStringProperty(PROJECT_NAME); IProject project = ProjectUtilities.getProject(projectName); if (project != null) { //TODO need to check content type as well since user can type in a file name, should have a different error message for invalid content type @@ -184,7 +194,6 @@ public class EntityDataModelProvider extends NewJavaClassDataModelProvider imple return new Status( IStatus.ERROR, JptUiPlugin.PLUGIN_ID, EntityWizardMsg.INVALID_XML_NAME); - } } } @@ -209,11 +218,117 @@ public class EntityDataModelProvider extends NewJavaClassDataModelProvider imple String msg = EntityWizardMsg.DUPLICATED_ENTITY_NAMES_MESSAGE; return WTPCommonPlugin.createErrorStatus(msg); } + // Ensure that the entries in the list are valid + String errorMsg = checkInputElementsTypeValidation(entities); + if (errorMsg != null) { + return WTPCommonPlugin.createErrorStatus(errorMsg); + } + String warningMsg = checkInputElementsTypeExistence(entities); + if (warningMsg != null) { + return WTPCommonPlugin.createWarningStatus(warningMsg); + } } - // Return OK return WTPCommonPlugin.OK_STATUS; } + private String checkInputElementsTypeValidation(List<EntityRow> inputElements) { + IStatus validateFieldTypeStatus = Status.OK_STATUS; + for (EntityRow entityRow: inputElements) { + String sig = null; + try { + sig = Signature.createTypeSignature(entityRow.getFqnTypeName(), true); + } catch (IllegalArgumentException e) { + String message = MessageFormat.format(EntityWizardMsg.EntityDataModelProvider_invalidArgument, e.getLocalizedMessage()); + validateFieldTypeStatus = new Status(IStatus.ERROR, JptUiPlugin.PLUGIN_ID, message); + break; + } + if (sig == null){ + validateFieldTypeStatus = JavaConventions.validateJavaTypeName(entityRow.getType(), JavaCore.VERSION_1_5, JavaCore.VERSION_1_5); + break; + } + int sigType = Signature.getTypeSignatureKind(sig); + if (sigType == Signature.BASE_TYPE_SIGNATURE) { + continue; + } + else if (sigType == Signature.ARRAY_TYPE_SIGNATURE) { + String elementSignature = Signature.getElementType(sig); + if (Signature.getTypeSignatureKind(elementSignature) == Signature.BASE_TYPE_SIGNATURE) { + continue; + } + } + } + if (!validateFieldTypeStatus.isOK()) { + return validateFieldTypeStatus.getMessage(); + } + return null; + } + + private String checkInputElementsTypeExistence(List<EntityRow> inputElements) { + IStatus validateFieldTypeStatus=Status.OK_STATUS; + for (EntityRow entityRow: inputElements) { + + String sig = Signature.createTypeSignature(entityRow.getFqnTypeName() ,true); + if (sig == null) { + String message = MessageFormat.format( + EntityWizardMsg.EntityDataModelProvider_entityNotInProjectClasspath, entityRow.getFqnTypeName()); + validateFieldTypeStatus = new Status(IStatus.ERROR, + JptUiPlugin.PLUGIN_ID, message); + break; + } + int sigType = Signature.getTypeSignatureKind(sig); + if (sigType == Signature.BASE_TYPE_SIGNATURE){ + continue; + } + else if (sigType == Signature.ARRAY_TYPE_SIGNATURE) { + String elementSignature = Signature.getElementType(sig); + if(Signature.getTypeSignatureKind(elementSignature) == Signature.BASE_TYPE_SIGNATURE){ + continue; + } + String qualifiedName = Signature.toString(elementSignature); + IProject project = (IProject) getProperty(INewJavaClassDataModelProperties.PROJECT); + IJavaProject javaProject = JavaCore.create(project); + IType type = null; + try { + type = javaProject.findType(qualifiedName); + } catch (JavaModelException e) { + validateFieldTypeStatus = e.getStatus(); + break; + } + if (type == null) { + String message = MessageFormat.format( + EntityWizardMsg.EntityDataModelProvider_entityNotInProjectClasspath, entityRow.getFqnTypeName()); + validateFieldTypeStatus = new Status(IStatus.ERROR, + JptUiPlugin.PLUGIN_ID, message); + break; + } + } + else { + IProject project = (IProject) getProperty(INewJavaClassDataModelProperties.PROJECT); + IJavaProject javaProject = JavaCore.create(project); + IType type = null; + try { + type = javaProject.findType(entityRow.getFqnTypeName()); + } catch (JavaModelException e) { + validateFieldTypeStatus = e.getStatus(); + break; + } + if (type == null) { + String message = MessageFormat.format( + EntityWizardMsg.EntityDataModelProvider_entityNotInProjectClasspath, entityRow.getFqnTypeName()); + validateFieldTypeStatus = new Status(IStatus.ERROR, + JptUiPlugin.PLUGIN_ID, message); + break; + } + } + } + if(!validateFieldTypeStatus.isOK()) { + return validateFieldTypeStatus.getMessage(); + } + return null; + } + + + /** * This method is intended for internal use only. It provides a simple algorithm for detecting * if there are duplicate entries in a list. It will accept a null parameter. It will return @@ -235,7 +350,6 @@ public class EntityDataModelProvider extends NewJavaClassDataModelProvider imple if (intEntity.getName().equals(entity.getName())) { return true; } - } } return false; |