PersistentStateComponent - loadState()

I'm creating a plugin and am currently working on saving/loading its state. The plugin consists of a single project component. When I run the plugin in the 'sandbox' Idea instance, i see the getState() method called several times, including when i close the current project. I expected the loadState() method to be called when i re-opened the project inside the 'sandbox' instance. This did not happen. Therefore, the plugin can not be restored to it's state when the project was closed.

When is the loadState method called?

Thanks in advance!

-- Chad --

3 comments
Comment actions Permalink

Hello Chad,

I'm creating a plugin and am currently working on saving/loading its
state. The plugin consists of a single project component. When I run
the plugin in the 'sandbox' Idea instance, i see the getState() method
called several times, including when i close the current project. I
expected the loadState() method to be called when i re-opened the
project inside the 'sandbox' instance. This did not happen.
Therefore, the plugin can not be restored to it's state when the
project was closed.

When is the loadState method called?


The most probable reason for your confusion is an optimization that we do
for persistent state components. If the state of your component does not
differ from its default state (the state returned when calling getState()
on a brand new instance of the component), we don't save anything to the
settings directory. Consequently, loadState() will not be called at all.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

I had read that in some documentation. But what I'm seeing doesn't seem to show that. I put some "System.out"'s in the main component methods and ran the plugin to show you what I'm seeing.

In this first set, you can see the project is opened and getState() is called 2 times before I modify one of the member variables of the component class. I then close the project and reopen it. the loadState() method is not called. From the impression I have, since I modified the component instance after the first getState() method was called, loadState() should have been invoked upon reopening the project. What am I missing?

In the second set of output, I want to show you something else. The getState() method is not called immediately after the project is opened. In this case am using the plugin and one of it's member variables is changing before getState() is called for the first time. I then close and reopen the project, and as expected don't see the loadState() method called. My understanding here is that it worked properly. Nothing changed since the first getState() call, so loadState() would not be called when the project is reopened. However, there was time for the state to change before getState() was called the first time. Is this working as it was designed?

I've included my getState() and loadState() methods below. Is the problem in my getState() method?


Run #1
-


public constructor
initComponent()
projectOpened()
getState()
getState()
adding a String to private member variable "items" (an ArrayList)
adding a String to private member variable "items" (an ArrayList)
getState()
projectClosed()
disposeComponent()
public constructor
initComponent()
projectOpened()
getState()
getState()
projectClosed()
disposeComponent()


Run #2
-


public constructor
initComponent()
projectOpened()
adding a String to private member vriable "items" (an ArrayList)
adding a String to private member vriable "items" (an ArrayList)
getState()
getState()
projectClosed()
disposeComponent()
public constructor
initComponent()
projectOpened()
getState()
getState()
projectClosed()
disposeComponent()



public Object getState() {
System.out.println("getState()");
return this;
}


public void loadState(Object state) {
System.out.println("loadState()");
XmlSerializerUtil.copyBean(state,this);
}

0
Comment actions Permalink

I have the same behaviour.
I also deleted the xml file, but it does not generate a new one.


EDIT: ok, i solved this by changing the storage id.
In my opinion this makes none sense, because now everything works like it should.

EDIT2: ok, this is rediculous. Changing the id back to the old value works now too. I'm very sure right now its not my fault ;)

0

Please sign in to leave a comment.