我无法让我的插件继续保持其状态。
永远不会创建文件configProvider.xml,@State
注释也没有任何效果(显然)。
这是plugin.xml中的相关部分
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/>
</extensions>
这是提供应该持久保存的对象的类:
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import java.util.LinkedHashMap;
@State(
name = "ConfigProvider",
storages = {
@Storage(id = "main", file = "$APP_CONFIG$/configProvider.xml")
}
)
public class ConfigProvider implements PersistentStateComponent<ConfigProvider.State> {
private State state = new State();
class State {
public State() {}
public LinkedHashMap<String, String> commitTypes = null;
public Integer selectedDefaultCommitTypeIndex = null;
public String jiraApiUrl;
public String jiraAuthorization;
public String jiraFilterId;
}
public static ConfigProvider getInstance() {
return ServiceManager.getService(ConfigProvider.class);
}
@Override
@org.jetbrains.annotations.Nullable
public ConfigProvider.State getState() {
return state; // gets called regulary
}
@Override
public void loadState(ConfigProvider.State state) {
this.state = state; // not called at all
}
}
我错过了什么?
谢谢, 克里斯托弗
答案 0 :(得分:4)
页面Persisting State of Components上有必要的注释:
请注意,扩展实例无法通过实施
来保持其状态PersistentStateComponent
因此,由于组件被列为<extensions>
的一部分,您的第一次尝试无法解决。
第二个版本有效,因为该组件现在在<application-components>
内注册
可能需要删除extension
部分以使答案更精确。
答案 1 :(得分:3)
现在这是一个有效的例子:
package com.foo
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;
@State(
name = "ConfigProviderState",
storages = {
@Storage(id = "other", file = "$APP_CONFIG$/configProvider.xml")
}
)
public class ConfigProvider implements ApplicationComponent, PersistentStateComponent<ConfigProvider.State> {
@NotNull
@Override
public String getComponentName() {
return getClass().getSimpleName();
}
@Override
public void disposeComponent() {}
@Override
public void initComponent() {}
public State state = new State();
public static class State {
public State() {}
public String foo;
}
@Override
@org.jetbrains.annotations.Nullable
public ConfigProvider.State getState() {
return state; //Saves all public variables to disk.
}
@Override
public void loadState(ConfigProvider.State state) {
this.state = state; //restores state from disk
}
public String getFoo() {
if (this.state.foo == null) {
this.state.foo = "https://jira.rz.is/rest/api/2/";
}
return this.state.foo;
}
public void setFoo(String foo) {
this.state.foo = foo;
}
}
的plugin.xml
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<applicationService
serviceImplementation="com.foo.ConfigProvider"
serviceInterface="com.foo.ConfigProvider"
overrides="true"
/>
</extensions>
<application-components>
<component>
<implementation-class>com.foo.ConfigProvider</implementation-class>
<interface-class>com.foo.ConfigProvider</interface-class>
</component>
</application-components>
<project-components>
<!-- Add your project components here -->
</project-components
答案 2 :(得分:0)
这是否有帮助,使State
类成为公共和静态?
public static class State {
//...
}