How to create component that puts a button around the outside of the main window?
What component/interface do I have to implement in order to create a button (similar to the 'Changes' and 'Todo' buttons) that can be clicked and interact with my pre-existing ModuleComponent?
请先登录再写评论。
(I will just add that I dont have access to the IDEA source code, and I'm not allowed to download it)
Hello Nick,
You need to use ToolWindowManager and ToolWindow.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Hello Nick,
Why would you be disallowed to download an Apache 2 licensed codebase?
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
I didn't make the rules...
Thanks.
Ok, so now I have a ModuleComponent, and a ProjectComponent. Is there a recommended way for these two components to communicate, or should I just use a Singleton to access the ProjectComponent? Could there be an issue with component load ordering - i.e. are Project level components instantiated before module level ones?
The use case is that I have a project open with multiple modules. Some of the modules might be 'special' in-house modules, and if so my ModuleComponent steps in and does its magic. I'd like for each of these special modules (via the ModuleComponent) to be able to provide feedback and status information to the ProjectComponent.
Thanks,
Nick
Here's the relevant code in case it's of any use. I make no reps as to whether this is the best (or correct) way to do this - please feel free to make suggestions for improvements.
from plugin.xml:
<project-components>
<component>
<implementation-class>com.test.TestProjectComponent</implementation-class>
</component>
</project-components>
and the implementation class:
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import javax.swing.Icon;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
public class TestProjectComponent extends AbstractProjectComponent
{
protected TestProjectComponent( Project project )
{
super( project );
}
@Override
public void projectOpened()
{
super.projectOpened();
setupToolWindow();
}
@Override
public void initComponent()
{
super.initComponent();
}
private void setupToolWindow()
{
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance( myProject );
JPanel panel = createPanel();
//
// registerToolWindow() is deprecated, but this works.
// I hit NPEs when using any of the non-deprecated methods.
//
ToolWindow toolWindow = toolWindowManager.registerToolWindow( "TestBtn", panel, ToolWindowAnchor.LEFT );
//
// This next class simply loads an Icon file from the JAR to show in the button
//
TestIconProvider iconProvider = new TestIconProvider();
Icon icon = iconProvider.getIcon();
toolWindow.setIcon( icon );
}
private JPanel createPanel()
{
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
panel.setBackground( Color.WHITE );
// All the other GUI components go in here..
return panel;
}
}
Hello Nick,
IntelliJ IDEA's components are instantiated via PicoContainer, so you can
simply have a parameter of type TestProjectComponent in the constructor of
your module component, and IDEA will automatically fill it with the instance
of the component from the correct project. A singleton can't be used because
you can have several projects open in an instance of IDEA, so there may be
several instance of your ProjectComponent in the same classloader.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Many thanks - I'm up and running.
Regards
Nick