ToolWindow Instantiation for each project opened

Let's say i have a tool window instantiated from plugin.xml (I think so):

<extensions defaultExtensionNs="com.intellij">
<toolWindow id="IntellijEnoviaTool" anchor="bottom" factoryClass="tool.EnoviaTool" icon="/resources/images/Icon.gif"/>
</extensions>

tool.EnoviaTool is class that implements ToolWindowFactory.

It creates a tool window on the bottom as i want to, and when i open a new project - it creates a new toolWindow for that project, but all the content for in previous ToolWindow just disappears.

I suspect it has something to do with instantiating, for i don't see any reasons on my end why instantiating shouldn't work - i don't have static fields or anything
0
6 comments
Avatar
Permanently deleted user

You should create unique per-project content like this:

public class UniqueToolWindowFactory implements ToolWindowFactory {
  @Override
  public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
    UniqueComponent unique = new UniqueComponent(project);
     ContentManager contentManager = toolWindow.getContentManager();
     Content content = contentManager.getFactory().createContent(unique, null, false);
    contentManager.addContent(content);
    Disposer.register(project, unique);
  }

  private class UniqueComponent extends JPanel implements Disposable {

...

  }
}

1
Avatar
Permanently deleted user

I have the JPanel (toolWindowContent) described in .form file, it's bound to a variable in ToolWindowFactory.

How do i extend it, and what should dispose() do?

0
Avatar
Permanently deleted user

I cheked it - each time i create a project, toolWindow is created anew, but all the buttons, tables, labels, et cetera - they all stay the same, as if they were static (checked by id in java debugger)
What the hell!? 

0
Avatar
Permanently deleted user

ToolWindowFactory is per-application entity and it's single for all projects. Obviously, you need to create some new component in createToolWindowContent().

JPanel (.form) also should be bound to this component, not to the factory.

0
Avatar
Permanently deleted user

Got it. The guy who wrote the code did all the logic inside the factory itself and seemingly haven't known that the factory is only created once.
I moved all the logic and all the content to a separate component class and factory now only produces this new component.
Thank you very much! 

0
Avatar
Permanently deleted user

Hello! I am facing just the same problem. Hopefully I will fix my plugin with the help of your answers. Though, i would like to notice that reading this very manual: https://github.com/JetBrains/intellij-sdk-docs/blob/master/code_samples/tool_window/src/myToolWindow/MyToolWindowFactory.java lead me to making such mistake. Consider refactoring code in this repo.

1

Please sign in to leave a comment.