Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij IDEA plugin - PersistentStateComponent loadState not called

I am trying to develop a plugin for Intellij IDEA, I am working with SDK 129.451.

The issue I have is that I can't persist the user data like some list items he can input in the plugin and have the data back after the IDE restarts..

I am using PersistentStateComponent to persist the data, the getState() method seems to be called but the loadState() method doesn't.

Here is a sample class that extends 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;

    }
}

Also I added this class in plugin.xml here:

<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>

And I also have a tool window class:

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+");

            }
        });

}

Ok so, if I close the app or minimize it, the getState method gets called as I expected.. but when I open the app, the loadState method doesn't get called.. can somebody help me how I can solve this?

I already read this but it doesn't seem to help me to much. Also I want to use PersistentStateComponent as I want to save objects more complex than a simple String.

Thank you in advance!

like image 933
Cata Avatar asked Jun 06 '13 20:06

Cata


1 Answers

Ok, I made it! :)

I don't know exactly what the issue was but I changed the Test class to this:

@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;
    }
}

And in the TFUploader I changed the way I loaded the Test class to this:

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

I hope it helps others..

like image 83
Cata Avatar answered Sep 19 '22 17:09

Cata