/**
   * Adds the selected objects as child modules.
   *
   * <p>{@inheritDoc}
   */
  @Override
  public boolean execute(IActionHandler actionHandler, Map<String, Object> context) {
    List<?> selectedModels = getActionParameter(context);
    if (selectedModels == null) {
      selectedModels = getSelectedModels(context);
    }

    Module moduleToSelect = null;
    for (Object nextSelectedModuleObject : selectedModels) {

      preSelectModuleObject(nextSelectedModuleObject, context);

      Module parentModule = findDestinationModule(nextSelectedModuleObject, context);
      IComponentDescriptor<?> beanComponentDescriptor =
          getBeanComponentDescriptor(parentModule, context);

      List<Module> childModules = parentModule.getSubModules();
      Module newSubModule = null;

      Module nextSubModule =
          createChildModule(
              parentModule, beanComponentDescriptor, nextSelectedModuleObject, context);
      int nextSubModuleIndex = -1;
      if (childModules != null) {
        nextSubModuleIndex = childModules.indexOf(nextSubModule);
      }
      if (nextSubModuleIndex < 0) {
        newSubModule = nextSubModule;
        if (moduleToSelect == null) {
          moduleToSelect = nextSubModule;
        }
      } else if (moduleToSelect == null) {
        moduleToSelect = childModules.get(nextSubModuleIndex);
      }

      if (newSubModule != null) {
        parentModule.addSubModule(newSubModule);
      }

      postSelectModuleObject(nextSelectedModuleObject, context);
    }

    setActionParameter(moduleToSelect, context);
    return super.execute(actionHandler, context);
  }
 private Module findCurrentModule(
     Module rootModule, Object nextSelectedModuleObject, Stack<Module> stack) {
   stack.push(rootModule);
   if (rootModule instanceof BeanModule
       && nextSelectedModuleObject.equals(((BeanModule) rootModule).getModuleObject())) {
     return rootModule;
   }
   if (rootModule.getSubModules() != null) {
     for (Module sub : rootModule.getSubModules()) {
       Module parentModule = findCurrentModule(sub, nextSelectedModuleObject, stack);
       if (parentModule != null) {
         return parentModule;
       }
     }
   }
   stack.pop();
   return null;
 }
 /**
  * Finds the existing module for a module object or name.
  *
  * @param rootModule the root module to start the search from.
  * @param moduleObject the module object.
  * @param moduleName the module name.
  * @return the stack of modules containing the found module.
  */
 protected Module findModule(Module rootModule, String moduleName, Object moduleObject) {
   if (moduleName != null && moduleName.equals(rootModule.getName())) {
     return rootModule;
   }
   if (moduleObject != null
       && (rootModule instanceof BeanModule)
       && moduleObject.equals(((BeanModule) rootModule).getModuleObject())) {
     return rootModule;
   }
   if (rootModule.getSubModules() != null) {
     for (Module sub : rootModule.getSubModules()) {
       Module parentModule = findModule(sub, moduleName, moduleObject);
       if (parentModule != null) {
         return parentModule;
       }
     }
   }
   return null;
 }