[325860] Regression: child projects not targeted to parent project's runtime
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/datamodel/properties/IAddReferenceDataModelProperties.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/datamodel/properties/IAddReferenceDataModelProperties.java
index 2367892..200a2dd 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/datamodel/properties/IAddReferenceDataModelProperties.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/datamodel/properties/IAddReferenceDataModelProperties.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *    Rob Stryker - initial implementation and ongoing maintenance
+ *    Carl Anderson (IBM) - SYNC_PRIMARY_RUNTIME
  ******************************************************************************/
 package org.eclipse.wst.common.componentcore.datamodel.properties;
 
@@ -40,4 +41,12 @@
 	 * </p>
 	 */
 	public static final String TARGET_REFERENCE_LIST = "IAddReferenceDataModelProperties.TARGET_REFERENCE_LIST"; //$NON-NLS-1$
+
+	/**
+	 * <p>
+	 * This required property is a {@link java.util.Boolean} that specifies whether the child project's primary runtime should be set 
+	 * to the same primary runtime as the parent.  The default is TRUE.
+	 * </p>
+	 */
+	public static final String SYNC_PRIMARY_RUNTIME = "IAddReferenceDataModelProperties.SYNC_PRIMARY_RUNTIME"; //$NON-NLS-1$
 }
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferenceDataModelProvider.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferenceDataModelProvider.java
index 40c6e65..083cea6 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferenceDataModelProvider.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferenceDataModelProvider.java
@@ -30,6 +30,7 @@
 		Set propertyNames = super.getPropertyNames();
 		propertyNames.add(SOURCE_COMPONENT);
 		propertyNames.add(TARGET_REFERENCE_LIST);
+		propertyNames.add(SYNC_PRIMARY_RUNTIME);
 		return propertyNames;
 	}
 
@@ -38,7 +39,10 @@
 	}
 
 	public Object getDefaultProperty(String propertyName) {
-		// No defaults, both must be set
-		return super.getDefaultProperty(propertyName);
+		Object retVal = super.getDefaultProperty(propertyName);
+		if (SYNC_PRIMARY_RUNTIME.equals(propertyName)){
+			retVal = Boolean.TRUE;
+		}
+		return retVal;
 	}
 }
diff --git a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferencesOp.java b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferencesOp.java
index 35812bf..a9f9ddf 100644
--- a/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferencesOp.java
+++ b/plugins/org.eclipse.wst.common.modulecore/modulecore-src/org/eclipse/wst/common/componentcore/internal/operation/AddReferencesOp.java
@@ -12,6 +12,7 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.List;
 
 import org.eclipse.core.commands.ExecutionException;
@@ -23,16 +24,21 @@
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.jem.util.UIContextDetermination;
+import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
 import org.eclipse.wst.common.componentcore.datamodel.properties.IAddReferenceDataModelProperties;
 import org.eclipse.wst.common.componentcore.internal.ModulecorePlugin;
 import org.eclipse.wst.common.componentcore.internal.StructureEdit;
 import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
 import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
+import org.eclipse.wst.common.core.util.UIContextDetermination;
 import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
 import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
 import org.eclipse.wst.common.internal.emfworkbench.validateedit.IValidateEditContext;
+import org.eclipse.wst.common.project.facet.core.IFacetedProject;
+import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
+import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
+import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;
 
 public class AddReferencesOp extends AbstractDataModelOperation {
 
@@ -45,7 +51,7 @@
 		if (!validateEdit().isOK())
 			return Status.CANCEL_STATUS;
 		addReferencedComponents(monitor);
-		addProjectReferences();
+		addProjectReferences(monitor);
 		return OK_STATUS;
 	}
 	
@@ -82,7 +88,10 @@
 	}
 	
 	protected void addProjectReferences() {
+		addProjectReferences(null);
+	}
 
+	protected void addProjectReferences(IProgressMonitor monitor) {
 		IVirtualComponent sourceComp = (IVirtualComponent) model.getProperty(IAddReferenceDataModelProperties.SOURCE_COMPONENT);
 		List modList = getListFromModel(model);
 		List targetprojectList = new ArrayList();
@@ -96,6 +105,39 @@
 		}
 		try {
 			ProjectUtilities.addReferenceProjects(sourceComp.getProject(), targetprojectList);
+
+			if (model.getBooleanProperty(IAddReferenceDataModelProperties.SYNC_PRIMARY_RUNTIME))
+			{
+				// set the primary runtime for the child modules to be the parent module's primary runtime, if necessary
+	            IFacetedProject sourceProject = ProjectFacetsManager.create( sourceComp.getProject() );
+	            IRuntime sourceRuntime = sourceProject.getPrimaryRuntime();
+	            for (Iterator tplItr = targetprojectList.iterator(); tplItr.hasNext();)
+	            {
+		            IFacetedProject moduleFacetedProject = ProjectFacetsManager.create( ((IProject)tplItr.next()) );
+		
+		            if( moduleFacetedProject != null && ! equals( sourceRuntime, moduleFacetedProject.getPrimaryRuntime() ) )
+		            {
+		            	boolean supports = true;
+		
+		            	if( sourceRuntime != null )
+		            	{
+		            		for( Iterator itr = moduleFacetedProject.getProjectFacets().iterator(); itr.hasNext(); )
+		            		{
+		            			IProjectFacetVersion fver = (IProjectFacetVersion) itr.next();
+		            			if( ! sourceRuntime.supports( fver ) )
+		            			{
+		            				supports = false;
+		            				break;
+		            			}
+		            		}
+		            	}
+			            if( supports )
+			            {
+			                moduleFacetedProject.setRuntime( sourceRuntime, submon( monitor, 1 ) );
+			            }
+			        }
+	            }
+            }
 		} catch (CoreException e) {
 			ModulecorePlugin.logError(e);
 		}
@@ -117,4 +159,23 @@
 		return null;
 	}
 
+	private static IProgressMonitor submon(final IProgressMonitor parent, final int ticks) {
+		return (parent == null ? null : new SubProgressMonitor(parent, ticks));
+	}
+
+	private static boolean equals( final Object obj1, final Object obj2 )
+	{
+		if( obj1 == obj2 )
+		{
+			return true;
+		}
+		else if( obj1 == null || obj2 == null )
+		{
+			return false;
+		}
+		else
+		{
+			return obj1.equals( obj2 );
+		}
+	}
 }