/*
 * 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.openapi.components.ProjectComponent;
import com.intellij.openapi.project.DumbAwareRunnable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.VcsDirectoryMapping;
import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
public class PlatformVcsDetector implements ProjectComponent {
  private final Project myProject;
  private final ProjectLevelVcsManagerImpl myVcsManager;

  public PlatformVcsDetector(final Project project, final ProjectLevelVcsManagerImpl vcsManager) {
    myProject = project;
    myVcsManager = vcsManager;
  }

  public void projectOpened() {
    StartupManager.getInstance(myProject).runWhenProjectIsInitialized(new DumbAwareRunnable() {
      public void run() {
        VirtualFile file = ProjectBaseDirectory.getInstance(myProject).getBaseDir(myProject.getBaseDir());
        if (myVcsManager.needAutodetectMappings()) {
          AbstractVcs vcs = myVcsManager.findVersioningVcs(file);
          if (vcs != null && vcs != myVcsManager.getVcsFor(file)) {
            myVcsManager.removeDirectoryMapping(new VcsDirectoryMapping("", ""));
            myVcsManager.setAutoDirectoryMapping(file.getPath(), vcs.getName());
            myVcsManager.cleanupMappings();
            myVcsManager.updateActiveVcss();
          }
        }
      }
    });
  }

  public void projectClosed() {
  }

  @NonNls
  @NotNull
  public String getComponentName() {
    return "PlatformVcsDetector";
  }

  public void initComponent() {
  }

  public void disposeComponent() {
  }
}

@@ -1039,24 +1039,58 @@
 irtualFile;%0A
+import com.intellij.ui.AppUIUtil;%0A
 import org.j
@@ -1118,16 +1118,16 @@
 NonNls;%0A
-
 import o
@@ -1651,24 +1651,159 @@
 oid run() %7B%0A
+        AppUIUtil.invokeLaterIfProjectAlive(myProject, new DumbAwareRunnable() %7B%0A          @Override%0A          public void run() %7B%0A    
         Virt
@@ -1893,24 +1893,28 @@
 BaseDir());%0A
+    
         if (
@@ -1964,16 +1964,20 @@
         
+    
 Abstract
@@ -2030,24 +2030,28 @@
 ;%0A          
+  
+  
 if (vcs != n
@@ -2092,24 +2092,28 @@
 or(file)) %7B%0A
+    
             
@@ -2186,32 +2186,36 @@
 ));%0A            
+    
 myVcsManager.set
@@ -2271,32 +2271,36 @@
 ));%0A            
+  
+  
 myVcsManager.cle
@@ -2311,24 +2311,28 @@
 Mappings();%0A
+    
             
@@ -2360,24 +2360,54 @@
 tiveVcss();%0A
+              %7D%0A            %7D%0A
           %7D%0A
@@ -2398,32 +2398,32 @@
   %7D%0A          %7D%0A
-
         %7D%0A      
@@ -2411,24 +2411,26 @@
  %7D%0A        %7D
+);
 %0A      %7D%0A   

fix deadlock on startup -> do not set VCS mappings directly from whenProjectInitialized()