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?

0
3 comments

What does “init some variable” mean exactly? Can't you do that from the MyPluginFactory code, e.g. constructor? 

0

     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.

0

Use AppLifecycleListener can solve the problem ,Here is the code:

plugin.xml

<applicationListeners>
	<listener class="com.exmaple.MyApplicationStartListener" topic="com.intellij.ide.AppLifecycleListener"/>
</applicationListeners>

MyApplicationStartListener.java

public class MyApplicationStartListener implements AppLifecycleListener{
	@Override
	public void appFrameCreated(@NotNull List<String> commandLineArgs){
		//int some variables
		AppLifecycleListener.super.appFrameCreated(commandLineArgs);
	}
}

See alse https://github.com/JetBrains/intellij-community/blob/idea/233.13135.103/platform/platform-impl/src/com/intellij/ide/AppLifecycleListener.java

0

Please sign in to leave a comment.