Casting tool window
Answered
I have created a tool window by implementing the ToolWindowFactory with my own Class CustomWindow. In another part of my code, I am trying to get this tool window using ToolWindowManager.getInstance(project).getToolWindow(ID) which works fine. But I cannot cast it to my Class CustomWindow from a ToolWindowImpl. Is there any of getting the tool window in the form of my CustomWindow?
Thanks
Please sign in to leave a comment.
First of all ToolWindowImpl is final class, it's a standard container for your custom content. So, you should create window like this:
MyCustomToolWindowFactory factory = new MyCustomToolWindowFactory();
window = ToolWindowManager.getInstance(project).registerToolWindow(MY_CUSTOM_ID, true, ToolWindowAnchor.RIGHT, project, true);
factory.createToolWindowContent(project, window);
Let's look inside:
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
//here you may setup toolWindow
ContentManager contentManager = toolWindow.getContentManager();
Content content = contentManager.getFactory().createContent(createMyCustomComponent(), null, false);
//here you may setup content
contentManager.setSelectedContent(content, true);
}
Then you should call ToolWindowManager.getInstance(project).getToolWindow(MY_CUSOM_ID).getContentManager().get... to obtain your content (and your cusom component inside it).
Took me a moment to realize that I had to add my custom component within the content.
But thanks that works perfectly!