/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide.actions;

import com.intellij.ide.util.newProjectWizard.AddModuleWizard;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileChooser.FileChooserDialog;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.projectImport.ProjectImportBuilder;
import com.intellij.projectImport.ProjectImportProvider;
import com.intellij.util.containers.ContainerUtil;

/**
 * @author Dmitry Avdeev
 *         Date: 10/31/12
 */
public class ImportProjectAction extends AnAction {

  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = getEventProject(e);
    FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor();
    FileChooserDialog chooser = FileChooserFactory.getInstance().createFileChooser(descriptor, project, null);
    VirtualFile[] files = chooser.choose(null, project);
    if (files.length > 0) {
      final VirtualFile file = files[0];
      ProjectImportProvider[] providers = ProjectImportProvider.PROJECT_IMPORT_PROVIDER.getExtensions();
      ProjectImportProvider provider = ContainerUtil.find(providers, new Condition<ProjectImportProvider>() {
        @Override
        public boolean value(ProjectImportProvider provider) {
          return (project != null || provider.canCreateNewProject()) && provider.isMyFile(file);
        }
      });
      if (provider != null) {
        AddModuleWizard wizard = new AddModuleWizard(e.getPresentation().getText(), project, provider, file.getPath());
        if (wizard.getStepCount() > 0) {
          boolean b = wizard.showAndGet();
        }
        else {
          ProjectImportBuilder builder = provider.getBuilder();
          builder.setFileToImport(file.getPath());
          builder.commit(project);
        }
      }
    }
  }

  @Override
  public void update(AnActionEvent e) {
    Presentation presentation = e.getPresentation();
    presentation.setText(getEventProject(e) == null ? "Import Project..." : "Import Module/Project...");
    presentation.setVisible(ApplicationManager.getApplication().isInternal());
  }
}

@@ -1158,32 +1158,75 @@
 ChooserFactory;%0A
+import com.intellij.openapi.module.Module;%0A
 import com.intel
@@ -1506,16 +1506,112 @@
 nerUtil;
+%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aimport java.util.Collections;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -2166,24 +2166,211 @@
 files%5B0%5D;%0A  
+    doImport(project, file, e.getPresentation().getText());%0A    %7D%0A  %7D%0A%0A  public static List%3CModule%3E doImport(final Project project, @NotNull final VirtualFile file, String wizardTitle) %7B%0A
     ProjectI
@@ -2456,26 +2456,24 @@
 tensions();%0A
-  
     ProjectI
@@ -2564,26 +2564,24 @@
 ovider%3E() %7B%0A
-  
       @Overr
@@ -2590,18 +2590,16 @@
 e%0A      
-  
 public b
@@ -2641,26 +2641,24 @@
 provider) %7B%0A
-  
         retu
@@ -2742,30 +2742,26 @@
 ile);%0A      
-  %7D%0A  
+%7D%0A
     %7D);%0A    
@@ -2756,26 +2756,24 @@
     %7D);%0A    
- 
- 
 if (provider
@@ -2780,26 +2780,24 @@
  != null) %7B%0A
-  
       AddMod
@@ -2839,37 +2839,19 @@
 ard(
-e.getPresentation().getText()
+wizardTitle
 , pr
@@ -2890,18 +2890,16 @@
 ;%0A      
- 
- 
 if (wiza
@@ -2919,26 +2919,24 @@
 nt() %3E 0) %7B%0A
-  
         bool
@@ -2960,26 +2960,24 @@
 owAndGet();%0A
-  
       %7D%0A    
@@ -2982,19 +2982,15 @@
     
-  
 else %7B%0A
-  
     
@@ -3047,34 +3047,32 @@
 lder();%0A        
-  
 builder.setFileT
@@ -3100,25 +3100,30 @@
 ));%0A        
- 
+return
  builder.com
@@ -3138,30 +3138,26 @@
 ect);%0A      
-  %7D%0A  
+%7D%0A
     %7D%0A    %7D%0A
@@ -3150,25 +3150,55 @@
 %7D%0A    %7D%0A    
-%7D
+return Collections.emptyList();
 %0A  %7D%0A%0A  @Ove

test for adding module