@Override public void update(AnActionEvent e) { boolean enabled = isEnabledInCurrentOS(); if (enabled) { FileSystemTree tree = FileSystemTree.DATA_KEY.getData(e.getDataContext()); if (tree == null || Boolean.TRUE != tree.getData(JavaSdkImpl.KEY)) { enabled = false; } } e.getPresentation().setEnabled(enabled); e.getPresentation().setVisible(enabled); }
/** * @param pantsExecutable path to the pants executable file for the * project. This function will return erroneous output if you use a directory path. The * pants executable can be found from a project path with {@link #findPantsExecutable(String)}. * @param parentDisposable Disposable object to use if a new JDK is added to * the project jdk table (otherwise null). Integration tests should use getTestRootDisposable() for * this argument to avoid exceptions during teardown. * @return The default Sdk object to use for the project at the given pants * executable path. * * This method will add a JDK to the project JDK table if it needs to create * one, which mutates global state (protected by a read/write lock). */ public static Optional<Sdk> getDefaultJavaSdk(@NotNull final String pantsExecutable, @Nullable final Disposable parentDisposable) { Optional<Sdk> sdkForPants = Arrays.stream(ProjectJdkTable.getInstance().getAllJdks()) // If a JDK belongs to this particular `pantsExecutable`, then its name will contain the path to Pants. .filter(sdk -> sdk.getName().contains(pantsExecutable) && sdk.getSdkType() instanceof JavaSdk) .findFirst(); if (sdkForPants.isPresent()) { SdkModificator modificator = sdkForPants.get().getSdkModificator(); JavaSdkImpl.attachJdkAnnotations(modificator); ApplicationManager.getApplication().invokeAndWait(() -> { ApplicationManager.getApplication().runWriteAction(() -> { modificator.commitChanges(); }); }); return sdkForPants; } final SimpleExportResult exportResult = SimpleExportResult.getExportResult(pantsExecutable); if (versionCompare(exportResult.getVersion(), "1.0.7") < 0) { return Optional.empty(); } boolean strict = PantsOptions.getPantsOptions(pantsExecutable).usesStrictJvmVersionForJUnit(); Optional<String> jdkHome = exportResult.getJdkHome(strict); if (!jdkHome.isPresent()) { return Optional.empty(); } String jdkName = null; JdkVersionDetector.JdkVersionInfo jdkInfo = JdkVersionDetector.getInstance().detectJdkVersionInfo(jdkHome.get()); if (jdkInfo != null) { // Using IJ's framework to detect jdk version. so jdkInfo.getVersion() returns `java version "1.8.0_121"` for (String version : ContainerUtil.newArrayList("1.6", "1.7", "1.8", "1.9")) { if (jdkInfo.getVersion().contains(version)) { jdkName = String.format("%s_from_%s", version, pantsExecutable); break; } } } if (jdkName == null) { jdkName = String.format("1.x_from_%s", pantsExecutable); } // Finally if we need to create a new JDK, it needs to be registered in the `ProjectJdkTable` on the IDE level // before it can be used. Sdk jdk = JavaSdk.getInstance().createJdk(jdkName, jdkHome.get()); ApplicationManager.getApplication().invokeAndWait(() -> { ApplicationManager.getApplication().runWriteAction(() -> { if (parentDisposable == null) { ProjectJdkTable.getInstance().addJdk(jdk); } else { ProjectJdkTable.getInstance().addJdk(jdk, parentDisposable); } }); }); return Optional.of(jdk); }