[108179] fix for deployables to use virtual comp API: committed for JL
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/impl/ResourceTreeNode.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/impl/ResourceTreeNode.java
index aee3e3e..272130c 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/impl/ResourceTreeNode.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/impl/ResourceTreeNode.java
@@ -49,7 +49,7 @@
 	private final Map children = Collections.synchronizedMap(new HashMap());
 	private static final ComponentResource[] NO_MODULE_RESOURCES = new ComponentResource[]{};
 	private IPathProvider pathProvider;
-	private ResourceTreeNode parent;
+//	private ResourceTreeNode parent;
 	private String pathSegment;
 
 	public ResourceTreeNode(String aPathSegment, ResourceTreeNode parent, IPathProvider aPathProvider) {
@@ -184,7 +184,6 @@
 		if (hasModuleResources()) {
 			ComponentResource moduleResource = null;
 			IResource eclipseResource = null;
-			IResource foundResource = null;
 			IContainer eclipseContainer = null;
 			Set resultSet = new HashSet();
 			for (Iterator resourceIter = moduleResources.iterator(); resourceIter.hasNext();) {
@@ -198,16 +197,17 @@
 						IPath runtimeURI = moduleResource.getRuntimePath().append(aPath);
 						
 						// check for existing subpath in tree
-						ComponentResource newResource = 
-							findExistingComponentResource(moduleResource.getComponent(), runtimeURI);
+						ComponentResource newResource = findExistingComponentResource(moduleResource.getComponent(), runtimeURI);
 						
-						if(newResource == null) {
+						// add new resource if null or found resource does not have the same source path
+						IPath srcPath = eclipseContainer.getProjectRelativePath().append(aPath);
+						if(newResource == null || !newResource.getSourcePath().equals(srcPath)) {
 							// flesh out the tree
-							if ((toCreateResourceAlways) || (foundResource = eclipseContainer.findMember(aPath)) != null) {
+							if ((toCreateResourceAlways) || (eclipseContainer.findMember(aPath)) != null) {
 								newResource = ComponentcorePackage.eINSTANCE.getComponentcoreFactory().createComponentResource();
 								newResource.setComponent(moduleResource.getComponent());		
 								newResource.setRuntimePath(runtimeURI);
-								newResource.setSourcePath(eclipseContainer.getProjectRelativePath().append(aPath));
+								newResource.setSourcePath(srcPath);
 								resultSet.add(newResource);
 							}
 						}
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFile.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFile.java
index 0b2d960..ac66d2e 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFile.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFile.java
@@ -8,7 +8,11 @@
  **************************************************************************************************/
 package org.eclipse.wst.common.componentcore.internal.resources;
 
+import java.util.ArrayList;
+import java.util.List;
+
 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.CoreException;
@@ -91,7 +95,14 @@
 	}
 	
 	public IFile[] getUnderlyingFiles() {
-		return new IFile[] {getUnderlyingFile()};
+		IPath[] paths = getProjectRelativePaths();
+		List result = new ArrayList();
+		for (int i=0; i<paths.length; i++) {
+			IFile file = getProject().getFile(paths[i]);
+			if (file!=null && file.exists() && !result.contains(file))
+				result.add(file);
+		}
+		return (IFile[]) result.toArray(new IFile[result.size()]);
 	}
 
 	protected void doDeleteMetaModel(int updateFlags,IProgressMonitor monitor) {
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFolder.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFolder.java
index a0072af..281d8e7 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFolder.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualFolder.java
@@ -8,6 +8,9 @@
  **************************************************************************************************/
 package org.eclipse.wst.common.componentcore.internal.resources;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
@@ -86,7 +89,14 @@
 	}
 	
 	public IFolder[] getUnderlyingFolders() {
-		return new IFolder[] {getUnderlyingFolder()};
+		IPath[] paths = getProjectRelativePaths();
+		List result = new ArrayList();
+		for (int i=0; i<paths.length; i++) {
+			IFolder folder = getProject().getFolder(paths[i]);
+			if (folder!=null && folder.exists() && !result.contains(folder))
+				result.add(folder);
+		}
+		return (IFolder[]) result.toArray(new IFolder[result.size()]);
 	}
 
 	protected void doDeleteMetaModel(int updateFlags, IProgressMonitor monitor) {
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualResource.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualResource.java
index 7662cba..6d81784 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualResource.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/resources/VirtualResource.java
@@ -10,7 +10,9 @@
  *******************************************************************************/
 package org.eclipse.wst.common.componentcore.internal.resources;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFolder;
@@ -66,9 +68,9 @@
 		StructureEdit moduleCore = null;
 		try {
 			moduleCore = StructureEdit.getStructureEditForWrite(getComponentHandle().getProject());
-			WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
-			ComponentResource[] resources = component.findResourcesByRuntimePath(getRuntimePath());
-			component.getResources().removeAll(Arrays.asList(resources));
+			WorkbenchComponent aComponent = moduleCore.findComponentByName(getComponentHandle().getName());
+			ComponentResource[] resources = aComponent.findResourcesByRuntimePath(getRuntimePath());
+			aComponent.getResources().removeAll(Arrays.asList(resources));
 		} finally {
 			if (moduleCore != null) {
 				moduleCore.saveIfNecessary(monitor);
@@ -81,8 +83,13 @@
 	protected abstract void doDeleteRealResources(int updateFlags, IProgressMonitor monitor) throws CoreException;
 
 	public boolean exists() {
-		IResource resource = getUnderlyingResource();
-		return resource != null && resource.exists();
+		// verify all underlying resources exist for the virtual resource to exist
+		IResource[] resources = getUnderlyingResources();
+		for (int i=0; i<resources.length; i++) {
+			if (resources[i]==null || !resources[i].exists())
+				return false;
+		}
+		return true;
 	}
 
 	public String getFileExtension() {
@@ -103,61 +110,67 @@
 		return runtimePath;
 	}
 
-	public IPath getProjectRelativePath() {
+	public IPath[] getProjectRelativePaths() {
 		StructureEdit moduleCore = null;
 		try {
 			moduleCore = StructureEdit.getStructureEditForRead(getProject());
-			WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
-			if (component != null) {
-				ResourceTreeRoot root = ResourceTreeRoot.getDeployResourceTreeRoot(component);
+			WorkbenchComponent aComponent = moduleCore.findComponentByName(getComponentHandle().getName());
+			if (aComponent != null) {
+				ResourceTreeRoot root = ResourceTreeRoot.getDeployResourceTreeRoot(aComponent);
 				// still need some sort of loop here to search subpieces of the runtime path.
 				ComponentResource[] componentResources = null;
 
-				IPath estimatedPath = null;
+				IPath[] estimatedPaths = null;
 				IPath searchPath = null;
 				do {
 					searchPath = (searchPath == null) ? getRuntimePath() : searchPath.removeLastSegments(1);
 					componentResources = root.findModuleResources(searchPath, ResourceTreeNode.CREATE_NONE);
-					estimatedPath = findBestMatch(componentResources);
-				} while (estimatedPath == null && canSearchContinue(componentResources, searchPath));
-				return estimatedPath;
+					estimatedPaths = findBestMatches(componentResources);
+				} while (estimatedPaths == null && canSearchContinue(componentResources, searchPath));
+				return estimatedPaths;
 			}
 		} finally {
 			if (moduleCore != null) {
 				moduleCore.dispose();
 			}
 		}
-		return getRuntimePath();
+		return new IPath[] {getRuntimePath()};
 	}
 
+	public IPath getProjectRelativePath() {
+		return getProjectRelativePaths()[0];
+	}
+	
 	private boolean canSearchContinue(ComponentResource[] componentResources, IPath searchPath) {
 		return (searchPath.segmentCount() > 0);
 	}
 
-	private IPath findBestMatch(ComponentResource[] theComponentResources) {
-
+	private IPath[] findBestMatches(ComponentResource[] theComponentResources) {
+		List result = new ArrayList();
 		int currentMatchLength = 0;
 		int bestMatchLength = -1;
 		IPath estimatedPath = null;
 		IPath currentPath = null;
-		final IPath runtimePath = getRuntimePath();
+		final IPath aRuntimePath = getRuntimePath();
 		for (int i = 0; i < theComponentResources.length; i++) {
 			currentPath = theComponentResources[i].getRuntimePath();
-
-			if (currentPath.isPrefixOf(runtimePath)) {
-				if (currentPath.segmentCount() == runtimePath.segmentCount())
-					return theComponentResources[i].getSourcePath();
-
-				currentMatchLength = currentPath.matchingFirstSegments(runtimePath);
+			if (currentPath.isPrefixOf(aRuntimePath)) {
+				if (currentPath.segmentCount() == aRuntimePath.segmentCount()) {
+					result.add(theComponentResources[i].getSourcePath());
+					continue;
+				}	
+				currentMatchLength = currentPath.matchingFirstSegments(aRuntimePath);
 				if (currentMatchLength == currentPath.segmentCount() && currentMatchLength > bestMatchLength) {
 					bestMatchLength = currentMatchLength;
 					IPath sourcePath = theComponentResources[i].getSourcePath();
-					IPath subpath = runtimePath.removeFirstSegments(currentMatchLength);
+					IPath subpath = aRuntimePath.removeFirstSegments(currentMatchLength);
 					estimatedPath = sourcePath.append(subpath);
 				}
 			}
 		}
-		return estimatedPath;
+		if (result.size()>0)
+			return (IPath[]) result.toArray(new IPath[result.size()]);
+		return new IPath[] {estimatedPath};
 	}
 
 	public String getName() {
@@ -224,8 +237,8 @@
 		StructureEdit moduleCore = null;
 		try {
 			moduleCore = StructureEdit.getStructureEditForRead(getProject());
-			WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
-			ComponentResource[] resources = component.findResourcesByRuntimePath(getRuntimePath());
+			WorkbenchComponent aComponent = moduleCore.findComponentByName(getComponentHandle().getName());
+			ComponentResource[] resources = aComponent.findResourcesByRuntimePath(getRuntimePath());
 			for (int i = 0; i < resources.length; i++) {
 				resources[i].setResourceType(aResourceType);
 			}
@@ -242,8 +255,8 @@
 			StructureEdit moduleCore = null;
 			try {
 				moduleCore = StructureEdit.getStructureEditForRead(getProject());
-				WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
-				ComponentResource[] resources = component.findResourcesByRuntimePath(getRuntimePath());
+				WorkbenchComponent aComponent = moduleCore.findComponentByName(getComponentHandle().getName());
+				ComponentResource[] resources = aComponent.findResourcesByRuntimePath(getRuntimePath());
 				for (int i = 0; i < resources.length; i++) {
 					resourceType = resources[i].getResourceType();
 					return resourceType;
@@ -254,7 +267,7 @@
 				}
 			}
 		}
-		resourceType = "";
+		resourceType = ""; //$NON-NLS-1$
 		return resourceType;
 	}