Question

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!

Was it helpful?

Solution

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

OTHER TIPS

I have already commented here but will say again that in my case loadState(MyService state) wasn't called because of lack of getter and setter for stateValue from this example:

class MyService implements PersistentStateComponent<MyService> {
  public String stateValue;

  public MyService getState() {
    return this;
  }

  public void loadState(MyService state) {
    XmlSerializerUtil.copyBean(state, this);
  }
}

In my case I was getting a NullPointerException even before loadState was getting called. Similar to your code above I used an Element class as the state class. I had a constructor with some parameters in Element class. This was the problem as the framework could not create an instance of my state class. I tried to add a blank constructor without any parameters. This worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top