How to init variable before toolWindow init?
Answered
Hello team
I'm developing a plugin and define custom toolWindow ,here is the plugin.xml
<extensions defaultExtensionNs="com.intellij">
<toolWindow id = "myPlugin" anchor="right" factoryClass="com.example.MyPluginFactory">
</extensions>
Then I want to init some variable before MyPluginFactory.createToolWindowContent
execute. At first I use compoents to do that and it work well , here is the code :
<application-components>
<component>
<implementation-class>com.example.MyComponent</implementation-class>
</component>
</application-components>
public class MyComponent implements BaseComponent{
@SneakyThrows
@Override
public void initComponent(){
//init some variable
}
}
But I found the BaseComponent is marked as deprecated and see the doc https://plugins.jetbrains.com/docs/intellij/plugin-components.html I use StartupActivity instead like this:
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="com.example.MyStartupActivity"/>
<toolWindow id = "myPlugin" anchor="right" factoryClass="com.example.MyPluginFactory">
</extensions>
public class MyStartupActivity implements StartupActivity{
@Override
public void runActivity(@NotNull Project project){
//init some variable
}
}
Then I found MyStartupActivity.runActivity
execute after the MyPluginFactory.createToolWindowContent
.
I think I use wrong method, so how to implement my requirement?
Please sign in to leave a comment.
What does “init some variable” mean exactly? Can't you do that from the
MyPluginFactory
code, e.g. constructor?I want to init some variable like
userName,userToken,serverAddress etc.
, which were saved as user's config file in tmp path.Of course, this code can be executed in
MyPluginFactory
, but I think it is not appropriate to place it here from the perspective of code structure and overall logic.Use
AppLifecycleListener
can solve the problem ,Here is the code:plugin.xml
MyApplicationStartListener.java
See alse https://github.com/JetBrains/intellij-community/blob/idea/233.13135.103/platform/platform-impl/src/com/intellij/ide/AppLifecycleListener.java