Intellij IDEA插件 - 未调用PersistentStateComponent loadState

Cat*_*ata 6 java plugins persistence intellij-idea intellij-plugin

我正在尝试为Intellij IDEA开发一个插件,我正在使用SDK 129.451.

我遇到的问题是,我不能像在插件中输入的一些列表项一样保留用户数据,并在IDE重新启动后恢复数据.

我使用PersistentStateComponent来持久保存数据,这个getState()方法似乎被调用但loadState()方法却没有.

这是一个扩展PersistentStateComponent的示例类:

    @State(name = "Test", storages = {@Storage(file = StoragePathMacros.APP_CONFIG+"/other.xml"
)})
public class Test implements PersistentStateComponent<Element> {

    String ceva;

    public Test() {
        ceva = "sad";
        System.out.println("constr");
    }

    public String getCeva() {
        return ceva;
    }

    public void setCeva(String ceva) {
        this.ceva = ceva;
    }

    public void loadState(Element state) {
        System.out.println("cstate load");

        ceva = (String) state.getContent().get(0);

    }

    public Element getState() {
        System.out.println("cstate retu");
        Element configurationsElement = new Element("testtt");
        configurationsElement.addContent(ceva);
        return configurationsElement;

    }
}
Run Code Online (Sandbox Code Playgroud)

我还在plugin.xml中添加了这个类:

<extensions defaultExtensionNs="com.intellij">
    <applicationService serviceImplementation="ro.catalin.prata.testflightuploader.controller.Test"/>
    <!-- Add your extensions here -->
    <toolWindow id="TF Uploader"   secondary="true" icon="/general/add.png" anchor="right"
                factoryClass="ro.catalin.prata.testflightuploader.view.TFUploader">
    </toolWindow>
</extensions>
Run Code Online (Sandbox Code Playgroud)

我还有一个工具窗口类:

public class TFUploader implements ToolWindowFactory {

    private JButton buttonAction;
    private ToolWindow myToolWindow;

    final Test test = ServiceManager.getService(Test.class);

    public TFUploader() {

        // I assume it should print the saved string but it doesn't
        System.out.println(test.getCeva());

        buttonAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // if I click a button I am setting some new value to the string I want to save
                test.setCeva(test.getCeva() + "-dddddd+");

            }
        });

}
Run Code Online (Sandbox Code Playgroud)

好吧,如果我关闭应用程序或最小化它,getState方法会按我的预期调用..但是当我打开应用程序时,loadState方法不会被调用..有人可以帮我解决这个问题吗?

我已经读过这篇文章,但它似乎对我帮助不大.另外我想使用PersistentStateComponent因为我想保存比简单的String更复杂的对象.

先感谢您!

Cat*_*ata 6

好的,我做到了!:)

我不知道究竟是什么问题,但我将Test类更改为:

@State(
        name = "Test", storages = {
        @Storage(
                id = "other",
                file = "$APP_CONFIG$/testpersist.xml")
})
public class Test implements PersistentStateComponent<Test> {

    String ceva;

    public Test() {
        ceva = "sad";
        System.out.println("constr");
    }

    public String getCeva() {
        return ceva;
    }

    public void setCeva(String ceva) {
        this.ceva = ceva;
    }

    public void loadState(Test state) {
        System.out.println("cstate load");

        XmlSerializerUtil.copyBean(state, this);
    }

    public Test getState() {
        System.out.println("cstate retu");
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

在TFUploader中,我改变了将Test类加载到此的方式:

final Test test = ServiceManager.getService(Test.class);
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助别人..