[357421] Utility jars in EAR50/lib or EAR/ cannot be edited in in EAR Java EE Module Dependencies page
diff --git a/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html b/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
index 2e54105..c32244a 100644
--- a/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
+++ b/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
@@ -40,6 +40,7 @@
 <p>Bug <a href='https://bugs.eclipse.org/336295'>336295</a>. Adding a jar with main-class in MANIFEST.MF to EAR 1.4 is detected as application client</p>
 <p>Bug <a href='https://bugs.eclipse.org/349741'>349741</a>. Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies</p>
 <p>Bug <a href='https://bugs.eclipse.org/355244'>355244</a>. EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.</p>
+<p>Bug <a href='https://bugs.eclipse.org/357421'>357421</a>. Utility jars in EAR50/lib or EAR/ cannot be edited in in EAR Java EE Module Dependencies page</p>
 
 </body>
 </html>
\ No newline at end of file
diff --git a/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties b/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
index c4e618d..b633fdb 100644
--- a/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
+++ b/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
@@ -57,6 +57,7 @@
 Bug https://bugs.eclipse.org/336295 Adding a jar with main-class in MANIFEST.MF to EAR 1.4 is detected as application client\n\
 Bug https://bugs.eclipse.org/349741 Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies\n\
 Bug https://bugs.eclipse.org/355244 EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.\n\
+Bug https://bugs.eclipse.org/357421 Utility jars in EAR50/lib or EAR/ cannot be edited in in EAR Java EE Module Dependencies page\n\
 \n\
 
 # "copyright" property - text of the "Feature Update Copyright"
diff --git a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
index 0ab2a97..d1aab0c 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
+++ b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
@@ -21,6 +21,7 @@
 import java.util.Set;
 
 import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IWorkspaceRoot;
 import org.eclipse.core.resources.IWorkspaceRunnable;
@@ -1011,24 +1012,57 @@
 	}
 	
 	private boolean isPhysicallyAdded(VirtualArchiveComponent component) {
-		IPath p = null;
+		
+		VirtualArchiveComponent comp = (VirtualArchiveComponent)component;
+		
 		try {
-			if(component.getWorkspaceRelativePath() == null || !component.getWorkspaceRelativePath().segment(0).equals(earComponent.getName()))
-				return false;
-			p = component.getProjectRelativePath();
-			return true;
+			if(component.getWorkspaceRelativePath() != null){
+				if(!component.getWorkspaceRelativePath().segment(0).equals(earComponent.getName()))
+					return false;
+				else
+					return true;
+			}else{
+				IFile aFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(comp.getProject().getName() + IPath.SEPARATOR + comp.getName().substring(4)));
+				// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
+				if( aFile == null || ( aFile != null && !aFile.exists()) ){
+					return false;
+				}else{
+					if (aFile.getFullPath().segment(0).equals(earComponent.getName()))
+						return true;
+					else
+						return false;
+				}
+			}
 		} catch (IllegalArgumentException e) {
 			return false;
 		}
-	}		
+	}
 		
 	private boolean isInLibDir(VirtualArchiveComponent comp) {
+		String[] compPathSegs;
+		
 		// Get the content directory
 		int contentDirSegmentCount = earComponent.getRootFolder().getUnderlyingFolder().getProjectRelativePath().segmentCount();
 		
-		// Get segments of component's IPath
-		IPath p = comp.getProjectRelativePath();
-		String[] compPathSegs = p.segments();
+		if(comp.getWorkspaceRelativePath() != null){
+			// Get segments of component's IPath
+			IPath p = comp.getProjectRelativePath();
+			 compPathSegs = p.segments();
+		}else{
+			IFile aFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(comp.getProject().getName() + IPath.SEPARATOR + comp.getName().substring(4)));
+			// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
+			if( aFile == null || ( aFile != null && !aFile.exists()) ){
+				return false;
+			}else{
+				
+				if (aFile.getFullPath().segments().length > 1)
+				{
+					compPathSegs = aFile.getFullPath().removeFirstSegments(1).segments();
+				}
+				else
+					return false;
+			}
+		}
 		
 		// Get segments of the library directory of the current EAR
 		String strippedLibDir = stripSeparators(libDir);
@@ -1123,7 +1157,9 @@
 					}
 					boolean shouldBeDisabled = false;
 					if (element instanceof VirtualArchiveComponent) {
-						shouldBeDisabled = isPhysicallyAdded((VirtualArchiveComponent)element);
+						if (isPhysicallyAdded((VirtualArchiveComponent)element) && isInLibDir((VirtualArchiveComponent)element))
+							shouldBeDisabled = true;
+						
 						if (shouldBeDisabled) {
 							items[i].setChecked(true);
 							items[i].setGrayed(true);
diff --git a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
index b1887a7..acf2bca 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
+++ b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
@@ -17,8 +17,10 @@
 import java.util.Map;
 
 import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
@@ -141,20 +143,36 @@
 	}
 	
 	private boolean shouldShow(IVirtualComponent component) {
+		IPath p = null;
+		
 		if (!(component instanceof VirtualArchiveComponent)) 
 			return true;
 		
 		VirtualArchiveComponent comp = (VirtualArchiveComponent)component;
-		// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
-		// there's no point in showing it. Second logic expression: the first segment should be the current EAR, if not the component shouldn't be shown.
-		if(comp.getWorkspaceRelativePath() == null || !comp.getWorkspaceRelativePath().segment(0).equals(earComponent.getName()))
-			return false;
 		
-		IPath p = null;
-		try {
-			p = comp.getProjectRelativePath();
-		} catch (IllegalArgumentException e) {
-			return false;
+		// If getWorkspaceRelativePath() returns null it means whether the file doesn't exist or the method is misbehaving
+		// (This because we noticed that getWorkspaceRelativePath method returns path including ear when the jar 
+		// is "dragged and dropped" and doesn't include ear when it is imported and we didn't want to move the code there)
+		// then we try again but appending the ear name in the beginning.
+		if(comp.getWorkspaceRelativePath() != null ){
+			if(!comp.getWorkspaceRelativePath().segment(0).equals(earComponent.getName()))
+				// the first segment should be the current EAR, if not the component shouldn't be shown
+				return false;
+		}else{
+			IFile aFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(comp.getProject().getName() + IPath.SEPARATOR + comp.getName().substring(4)));
+			// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
+			if( aFile == null || ( aFile != null && !aFile.exists()) )
+				return false;
+			else
+				p = aFile.getProjectRelativePath();
+		}
+		
+		if (p == null){
+			try {
+				p = comp.getProjectRelativePath();
+			} catch (IllegalArgumentException e) {
+				return false;
+			}
 		}
 		
 		// If the path relative to the project is null it means that the file doesn't exist, component shouldn't be shown.
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java
index 16d8e0b..641eb6a 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/J2EEUtilityJarImportAssistantOperation.java
@@ -104,7 +104,7 @@
 
 		IVirtualComponent earcomponent = ComponentCore.createComponent(targetProject);
 
-		IVirtualComponent utilcomponent = ComponentCore.createArchiveComponent(targetProject, VirtualArchiveComponent.LIBARCHIVETYPE + IPath.SEPARATOR + utilityJarIFile.getFullPath().toString());
+		IVirtualComponent utilcomponent = ComponentCore.createArchiveComponent(targetProject, VirtualArchiveComponent.LIBARCHIVETYPE + IPath.SEPARATOR + utilityJarIFile.getProjectRelativePath().toString());
 
 		addArchiveProjectToEARDataModel.setProperty(ICreateReferenceComponentsDataModelProperties.SOURCE_COMPONENT, earcomponent);
 		addArchiveProjectToEARDataModel.setProperty(ICreateReferenceComponentsDataModelProperties.TARGET_COMPONENT_LIST, Collections.singletonList(utilcomponent));