Why I cannot persist plugin settings (project scope)?
Answered
I've created custom PropertiesComponent based on PropertiesComponentImpl.
The differences:
@State(
name = "TaskTimer",
storages = {@Storage(StoragePathMacros.WORKSPACE_FILE)}
)
above class declaraton and:
public void setTogglToken(String token) {
this.setValue("ttoken", token);
}
public String getTogglToken() {
return this.getValue("ttoken");
}
public void setTogglProject(String name) {
this.setValue("tproject", name);
}
public String getTogglProject() {
return this.getValue("tproject");
}
@NotNull
public String getComponentName() {
return "TaskTimer";
}
inside class. That's all differences from example implementation.
My Configurable implementation:
private final TaskTimerPropertiesComponent properties = new TaskTimerPropertiesComponent();
@Override
public void apply() throws ConfigurationException {
properties.setTogglToken(token.getText());
properties.setTogglProject(projectsList.getSelectedItem().toString());
}
but in createComponent() method getters returns always nulls, so it seems that those values weren't persisted after closing settings window.
What am I doing wrong here?
Please sign in to leave a comment.
`TaskTimerPropertiesComponent` must be registered as a service.
```
<projectService serviceImplementation="yourPackage.TaskTimerPropertiesComponent"/>
```
and instead `new`, add `getInstance` method to your class
```
public static PropertiesComponent getInstance(Project project) {
return ServiceManager.getService(project, TaskTimerPropertiesComponent.class);
}
```
Thanks, one more question how to get Project object in Configurable class for loading PropertiesComponent? (createComponent method exactly)