/*
 * Copyright 2000-2009 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.platform;

import com.intellij.ide.GeneralSettings;
import com.intellij.internal.statistic.UsageTrigger;
import com.intellij.internal.statistic.beans.ConvertUsagesUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.projectImport.ProjectOpenedCallback;

import java.io.File;

/**
 * @author yole
 */
public class NewDirectoryProjectAction extends AnAction implements DumbAware {
  public void actionPerformed(final AnActionEvent e) {
    Project project = e.getData(PlatformDataKeys.PROJECT);
    NewDirectoryProjectDialog dlg = new NewDirectoryProjectDialog(project);
    dlg.show();
    if (dlg.getExitCode() != DialogWrapper.OK_EXIT_CODE) return;
    final DirectoryProjectGenerator generator = dlg.getProjectGenerator();
    final File location = new File(dlg.getNewProjectLocation());
    if (!location.exists() && !location.mkdirs()) {
      Messages.showErrorDialog(project, "Cannot create directory '" + location + "'", "Create Project");
      return;
    }

    final VirtualFile baseDir = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
      public VirtualFile compute() {
        return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(location);
      }
    });
    baseDir.refresh(false, true);

    if (baseDir.getChildren().length > 0) {
      int rc = Messages.showYesNoDialog(project,
                                        "The directory '" + location +
                                            "' is not empty. Would you like to create a project from existing sources instead?",
                                        "Create New Project", Messages.getQuestionIcon());
      if (rc == 0) {
        PlatformProjectOpenProcessor.getInstance().doOpenProject(baseDir, null, false);
        return;
      }
    }

    String generatorName = generator == null ? "empty" : ConvertUsagesUtil.ensureProperKey(generator.getName());
    UsageTrigger.trigger("NewDirectoryProjectAction." + generatorName);
    Object settings = null;
    if (generator != null) {
      try {
        settings = generator.showGenerationSettings(baseDir);
      }
      catch (ProcessCanceledException e1) {
        return;
      }
    }
    GeneralSettings.getInstance().setLastProjectLocation(location.getParent());
    final Object finalSettings = settings;
    PlatformProjectOpenProcessor.doOpenProject(baseDir, null, false, -1, new ProjectOpenedCallback() {
      @Override
      public void projectOpened(Project project, Module module) {
        if (generator != null) {
          generator.generateProject(project, baseDir, finalSettings, module);
        }
      }
    }, false);
  }
}

@@ -1490,16 +1490,59 @@
 allback;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aimport
@@ -1926,24 +1926,317 @@
 ODE) return;
+%0A%0A  %7D%0A%0A  protected Object showSettings(DirectoryProjectGenerator generator, VirtualFile baseDir)%0A      throws ProcessCanceledException %7B%0A    return generator.showGenerationSettings(baseDir);%0A  %7D%0A%0A  @Nullable%0A  protected Project generateProject(Project project, NewDirectoryProjectDialog dlg) %7B
 %0A    final D
@@ -2529,24 +2529,29 @@
       return
+ null
 ;%0A    %7D%0A%0A   
@@ -2997,20 +2997,16 @@
 ation +%0A
-    
         
@@ -3122,16 +3122,16 @@
 tead?%22,%0A
+
         
@@ -3238,24 +3238,31 @@
 ) %7B%0A        
+return 
 PlatformProj
@@ -3329,32 +3329,16 @@
 false);%0A
-        return;%0A
       %7D%0A
@@ -3621,41 +3621,32 @@
 s = 
-generator.showGenerationSettings(
+showSettings(generator, 
 base
@@ -3717,16 +3717,21 @@
   return
+ null
 ;%0A      
@@ -3861,20 +3861,27 @@
 ttings;%0A
-
     
+return 
 Platform

fix merge conflict