public static void selectSdkHome( @NotNull final SdkType sdkType, @NotNull final Consumer<String> consumer) { final FileChooserDescriptor descriptor = sdkType.getHomeChooserDescriptor(); if (ApplicationManager.getApplication().isUnitTestMode()) { Sdk sdk = ProjectJdkTable.getInstance().findMostRecentSdkOfType(sdkType); if (sdk == null) throw new RuntimeException("No SDK of type " + sdkType + " found"); consumer.consume(sdk.getHomePath()); return; } FileChooser.chooseFiles( descriptor, null, getSuggestedSdkRoot(sdkType), chosen -> { final String path = chosen.get(0).getPath(); if (sdkType.isValidSdkHome(path)) { consumer.consume(path); return; } final String adjustedPath = sdkType.adjustSelectedSdkHome(path); if (sdkType.isValidSdkHome(adjustedPath)) { consumer.consume(adjustedPath); } }); }
@Nullable public static Sdk findOrCreateSdk( @Nullable Comparator<Sdk> comparator, final SdkType... sdkTypes) { final Project defaultProject = ProjectManager.getInstance().getDefaultProject(); final Sdk sdk = ProjectRootManager.getInstance(defaultProject).getProjectSdk(); if (sdk != null) { for (SdkType type : sdkTypes) { if (sdk.getSdkType() == type) { return sdk; } } } for (SdkType type : sdkTypes) { List<Sdk> sdks = ProjectJdkTable.getInstance().getSdksOfType(type); if (!sdks.isEmpty()) { if (comparator != null) { Collections.sort(sdks, comparator); } return sdks.get(0); } } for (SdkType sdkType : sdkTypes) { final String suggestedHomePath = sdkType.suggestHomePath(); if (suggestedHomePath != null && sdkType.isValidSdkHome(suggestedHomePath)) { Sdk an_sdk = createAndAddSDK(suggestedHomePath, sdkType); if (an_sdk != null) return an_sdk; } } return null; }