PythonSdkConfigurable uses ProjectSdksModel instead of editing SDKs directly, so...
authorDmitry Jemerov <yole@jetbrains.com>
Wed, 7 Jul 2010 16:18:00 +0000 (20:18 +0400)
committerDmitry Jemerov <yole@jetbrains.com>
Wed, 7 Jul 2010 16:46:05 +0000 (20:46 +0400)
platform/lang-api/src/com/intellij/openapi/projectRoots/SdkModel.java
platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/SdkConfigurationUtil.java
platform/lang-impl/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ProjectSdksModel.java

index 57ca897649ea2e26012e79c54bbb724a665f1a39..7d06888fbd8caced05bb5d5faf5740d2ed948604 100644 (file)
@@ -40,6 +40,13 @@ public interface SdkModel {
   @Nullable
   Sdk findSdk(String sdkName);
 
+  /**
+   * Adds the specified SDK (already created and initialized) to the model.
+   *
+   * @param sdk the SDK to add
+   */
+  void addSdk(Sdk sdk);
+
   /**
    * Allows to receive notifications when the JDK list has been changed by the
    * user configuring the JDKs.
index 1672ef264dde28bab55b72389f6865f0e953ea77..4b23325081bd634b64f186330064adf17c956d2d 100644 (file)
@@ -35,7 +35,6 @@ import com.intellij.openapi.roots.ModuleRootManager;
 import com.intellij.openapi.roots.ProjectRootManager;
 import com.intellij.openapi.ui.Messages;
 import com.intellij.openapi.util.Computable;
-import com.intellij.openapi.util.NullableComputable;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
@@ -54,7 +53,7 @@ public class SdkConfigurationUtil {
   }
 
   @Nullable
-  public static Sdk addSdk(final Project project, final SdkType... sdkTypes) {
+  public static Sdk createSdk(final Project project, final Sdk[] existingSdks, final SdkType... sdkTypes) {
     if (sdkTypes.length == 0) return null;
     final FileChooserDescriptor descriptor = createCompositeDescriptor(sdkTypes);
     final FileChooserDialog dialog = FileChooserFactory.getInstance().createFileChooser(descriptor, project);
@@ -66,7 +65,7 @@ public class SdkConfigurationUtil {
     if (selection.length > 0) {
       for (SdkType sdkType : sdkTypes) {
         if (sdkType.isValidSdkHome(selection[0].getPath())) {
-          return setupSdk(selection[0], sdkType, false);
+          return setupSdk(existingSdks, selection[0], sdkType, false, null, null);
         }
       }
     }
@@ -98,42 +97,46 @@ public class SdkConfigurationUtil {
     return descriptor;
   }
 
-  public static void removeSdk(final Sdk sdk) {
+  public static void addSdk(final Sdk sdk) {
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
+      @Override
       public void run() {
-        ProjectJdkTable.getInstance().removeJdk(sdk);
+        ProjectJdkTable.getInstance().addJdk(sdk);
       }
     });
   }
 
-  @Nullable
-  public static Sdk setupSdk(final VirtualFile homeDir, final SdkType sdkType, final boolean silent) {
-    return setupSdk(homeDir, sdkType, silent, null, null);
+  public static void removeSdk(final Sdk sdk) {
+    ApplicationManager.getApplication().runWriteAction(new Runnable() {
+      public void run() {
+        ProjectJdkTable.getInstance().removeJdk(sdk);
+      }
+    });
   }
 
   @Nullable
-  public static Sdk setupSdk(final VirtualFile homeDir, final SdkType sdkType, final boolean silent,
+  public static Sdk setupSdk(final Sdk[] allSdks,
+                             final VirtualFile homeDir, final SdkType sdkType, final boolean silent,
                              @Nullable final SdkAdditionalData additionalData,
                              @Nullable final String customSdkSuggestedName) {
-    final Sdk[] sdks = ProjectJdkTable.getInstance().getAllJdks();
-    final List<Sdk> sdksList = Arrays.asList(sdks);
+    final List<Sdk> sdksList = Arrays.asList(allSdks);
 
-    final ProjectJdkImpl projectJdk;
+    final ProjectJdkImpl sdk;
     try {
       final String sdkName = customSdkSuggestedName == null
                              ? createUniqueSdkName(sdkType, homeDir.getPath(), sdksList)
                              : createUniqueSdkName(customSdkSuggestedName, sdksList);
-      projectJdk = new ProjectJdkImpl(sdkName, sdkType);
+      sdk = new ProjectJdkImpl(sdkName, sdkType);
 
       if (additionalData != null) {
         // additional initialization.
         // E.g. some ruby sdks must be initialized before
         // setupSdkPaths() method invocation
-        projectJdk.setSdkAdditionalData(additionalData);
+        sdk.setSdkAdditionalData(additionalData);
       }
 
-      projectJdk.setHomePath(homeDir.getPath());
-      sdkType.setupSdkPaths(projectJdk);
+      sdk.setHomePath(homeDir.getPath());
+      sdkType.setupSdkPaths(sdk);
     }
     catch (Exception e) {
       if (!silent) {
@@ -145,17 +148,7 @@ public class SdkConfigurationUtil {
       }
       return null;
     }
-    return ApplicationManager.getApplication().runWriteAction(new NullableComputable<Sdk>() {
-        public Sdk compute() {
-          ProjectJdkTable.getInstance().addJdk(projectJdk);
-          return projectJdk;
-        }
-    });
-  }
-
-  @Nullable
-  public static Sdk setupSdk(final VirtualFile homeDir, final SdkType sdkType) {
-    return setupSdk(homeDir, sdkType, true);
+    return sdk;
   }
 
   public static void setDirectoryProjectSdk(final Project project, final Sdk sdk) {
@@ -210,7 +203,9 @@ public class SdkConfigurationUtil {
           }
         });
         if (sdkHome != null) {
-          return setupSdk(sdkHome, sdkType, true);
+          final Sdk newSdk = setupSdk(ProjectJdkTable.getInstance().getAllJdks(), sdkHome, sdkType, true, null, null);
+          addSdk(newSdk);
+          return newSdk;
         }
       }
     }
index 0eb06ef5da115c75b4dda7f70f47ec2ad6507d01..84f3f3914894f8067df6aab76886f02318893094 100644 (file)
@@ -109,6 +109,10 @@ public class ProjectSdksModel implements SdkModel {
     return myModified;
   }
 
+  public void apply() throws ConfigurationException {
+    apply(null);
+  }
+
   public void apply(@Nullable MasterDetailsComponent configurable) throws ConfigurationException {
     String[] errorString = new String[1];
     if (!canApply(errorString, configurable)) {
@@ -238,9 +242,19 @@ public class ProjectSdksModel implements SdkModel {
                                   ProjectBundle.message("sdk.java.corrupt.title"), Messages.getErrorIcon());
     }
 
-    myProjectSdks.put(newJdk, newJdk);
-    updateTree.consume(newJdk);
-    mySdkEventsDispatcher.getMulticaster().sdkAdded(newJdk);
+    doAdd(newJdk, updateTree);
+  }
+
+  public void addSdk(Sdk sdk) {
+    doAdd((ProjectJdkImpl) sdk, null);
+  }
+
+  private void doAdd(ProjectJdkImpl newSdk, @Nullable Consumer<Sdk> updateTree) {
+    myProjectSdks.put(newSdk, newSdk);
+    if (updateTree != null) {
+      updateTree.consume(newSdk);
+    }
+    mySdkEventsDispatcher.getMulticaster().sdkAdded(newSdk);
   }
 
   @Nullable