Search code examples
intellij-ideacodenameoneintellij-plugin

Configuring a ModuleType with the SDK option in the beginning


In my plugin I was able to build a new project wizard but the first page doesn't look right. E.g. this is what my plugin has:

My plugin

And this is what most other plugins have:

Other plugins

The thing that's missing for me is the "SDK" option. Our plugin requires Java 8 and I'd like the option to pick an SDK to show up there. But I don't understand where that's configured.

This is in my plugins.xml:

<extensions defaultExtensionNs="com.intellij">
   <!-- moduleBuilder builderClass="com.codename1.plugin.intellij.module.CodenameOneModuleType"/ -->
   <moduleType id="CodenameOne.Module" implementationClass="com.codename1.plugin.intellij.module.CodenameOneModuleType" classpathProvider="true"  />
</extensions>

CodenameOneModuleType just extends ModuleType<NewCodenameOneProject> where NewCodenameOneProject is basically a JavaModuleBuilder. I can post some more sources if that would help.

BTW is there a javadoc for the plugin API's?

I know where the sources are but is there no ready made JavaDoc?


Solution

  • I didn't find a totally clear solution to this but I did reach several conclusions based on the discussion here:

    • Setting the SDK for the project should be done via: ProjectRootManager.getInstance(project).setProjectSdk(jdk);

    • Instead of overriding createWizardSteps which starts on the second page of the wizard I can just override the first page by using getCustomOptionsStep

    Once I understood those I just recreated the combo box UI above using the IntelliJ/IDEA gui builder and some logic:

    private void refreshSDKCombo(Sdk preferredSdk) {
        DefaultComboBoxModel<Sdk> sdks = new DefaultComboBoxModel<>();
        Sdk[] s = ProjectJdkTable.getInstance().getAllJdks();
        for (Sdk ss : s) {
            if (ss.getSdkType() instanceof JavaSdkType) {
                String str = ss.getVersionString();
                if (str.contains("1.8.") || str.contains("1.9.")) {
                    sdks.addElement(ss);
                }
            }
        }
        if(preferredSdk != null) {
            String str = preferredSdk.getVersionString();
            if (str.contains("1.8.") || str.contains("1.9.")) {
                projectSDKCombo.setSelectedItem(preferredSdk);
            }
        }
        projectSDKCombo.setModel(sdks);
        if(sdks.getSize() > 0) {
            projectSDKCombo.setSelectedIndex(0);
        }
    }
    

    Noticed I limited the options to JDK 8+ which is required for our use case!

    I bound the button logic to configure the SDK as such:

        newButton.addActionListener(e -> {
            ProjectJdksEditor editor = new ProjectJdksEditor((Sdk)projectSDKCombo.getSelectedItem(),
                    ProjectManager.getInstance().getDefaultProject(),
                    newButton);
            if (editor.showAndGet()) {
                Sdk selectedJdk = editor.getSelectedJdk();
                refreshSDKCombo(selectedJdk);
            }
        });