From 848aad82c225c43862cce9671c53f7e7c84b8370 Mon Sep 17 00:00:00 2001 From: Tesla Ice Zhang Date: Wed, 10 Jan 2018 20:46:31 +0800 Subject: [PATCH] Update sdk.md --- reference_guide/project_model/sdk.md | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/reference_guide/project_model/sdk.md b/reference_guide/project_model/sdk.md index 042986bb8..e8f803818 100644 --- a/reference_guide/project_model/sdk.md +++ b/reference_guide/project_model/sdk.md @@ -39,3 +39,68 @@ Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk(); ``` See the following [code sample](https://github.com/JetBrains/intellij-sdk-docs/blob/master/code_samples/project_model/src/com/intellij/tutorials/project/model/ProjectSdkAction.java) to get more familiar with SDK manipulation tool set. + +## Working with your own SDK + +To create your own SDK, You need to create a class extends `SdkType`, and set your recommended SDK name, SDK home path, a presentable name.
+Finally you need to implement `isValidSdkHome` to do the SDK validation, and this example just check if the given path is a directory. + +```java +import com.intellij.openapi.projectRoots.*; + +public class MySdkType extends SdkType { + public MySdkType() { + super("MY_SDK_NAME"); + } + + @Override public @Nullable String suggestHomePath() { + return "/usr/share/covscript"; + } + + @Override public boolean isValidSdkHome(@NotNull String path) { + return Files.isDirectory(Paths.get(path)); + } + + @Override public @Nullable String suggestSdkName(@NotNull String s, @NotNull String s1) { + return "Replace with your SDK name"; + } + + @Override public @Nullable AdditionalDataConfigurable createAdditionalDataConfigurable( + @NotNull SdkModel sdkModel, @NotNull SdkModificator sdkModificator) { + return null; // not needed + } + + @Override public @NotNull String getPresentableName() { + return "My Awesome Sdk"; + } + + @Override public void saveAdditionalData( + @NotNull SdkAdditionalData sdkAdditionalData, @NotNull Element element) { + // leave blank + } + + @Override public boolean setupSdkPaths(@NotNull Sdk sdk, @NotNull SdkModel sdkModel) { + SdkModificator modificator = sdk.getSdkModificator(); + modificator.setVersionString(getVersionString(sdk)); + modificator.commitChanges(); + return true; + } +} +``` + +Optionally, you can override `getIcon` to set an icon to your SDK type. + +The last method is not abstract, so it's possible to remove it. But to save your SDK settings +(say, the next time you open your IDE, the SDK will still be there), you should override it. + +To let users create or select an existing SDK, you may do this in some configuration panels (like, module wizard, facet, or run configuration): + +```java +ProjectJdksEditor editor = new ProjectJdksEditor(sdkCurrentlyUsed, project, parentGuiComponent); +editor.setTitle("Select a SDK"); +editor.show(); +if (editor.isOK()) { + Sdk newSdkSelected = editor.getSelectedJdk(); + // do your update job here +} +```