Search code examples
intellij-ideaintellij-plugin

Intellij Plugin Development: toolWindow extension is blank when opened in different project windows


When I open multiple(3-4) projects in different intellij windows, my JSON plugin tool window comes out blank. Here is my plugin.xml

<idea-plugin>
  <id>com.xxxxx.json.Editor</id>
  <name>JSON Editor</name>
  <version>1.5</version>
  <vendor email="xxxxxxxxxx" url="xxxxxxx">xxxxxx</vendor>

  <description><![CDATA[
      JetBrains IntelliJ IDE plugin for easy viewing and editing of currently opened JSON file in IDE using Tree Structure.
    ]]></description>

  <change-notes><![CDATA[
      Feature: Indentation changes - adding space after ':' char.
    ]]>
  </change-notes>

  <idea-version since-build="192.6"/>
  <extensions defaultExtensionNs="com.intellij">
    <toolWindow id="JSON Editor" anchor="right" factoryClass="JsonEditor" />
  </extensions>

  <actions>
  </actions>

</idea-plugin>

Here is the screenshot with the contents(buttons and Tree view) of the plugin JSON Editor tool window.

enter image description here

Here is the screenshot of the contents of the same toolWindow coming completely blank (which happens if multiple(3-4) projects are opened in different windows.)

enter image description here

Here is the code for createToolWindowContent method -

public class JsonEditor implements ToolWindowFactory {

    private JPanel mainContent;
    ....
    ....

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        this.project = project;
        ContentFactory contentFactory = 
        ContentFactory.SERVICE.getInstance();
        Content content = contentFactory.createContent(mainContent, "", false);
        toolWindow.getContentManager().addContent(content);
    }
}

How do I solve this issue? Also can I have different states of instances of the plugin extension opened in different?


Solution

  • You need to create a different instance of mainContent every time the createToolWindowContent method is invoked since an AWT widget can only have one parent.

    This method is invoked per IDE window so one window will "win".

    So instead of writing mainContent write createMainContentInstance(). You can then get access to the details you need globally by working with the Project object for the current window which will be different between both windows.