Hello everyone.
Today, I try to develop a personnal JETBRAINS plugin.
My problem, my code does not create the xml file corresponding to my PersistentStateComponent.
Here’s my code.
File 1 : ColorStateBean.java
package states.beans;
import com.intellij.util.xmlb.annotations.Attribute;
import com.intellij.util.xmlb.annotations.Property;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.Transient;
@Tag("color")
@Property(surroundWithTag = false, alwaysWrite = true, flat = true, style = Property.Style.ATTRIBUTE)
public class ColorStateBean {
// Color identifier
@Attribute("identifier")
public int _identifier;
// Hexadecimal color code.
@Attribute("hexadecimal_color_code")
public String _hexadecimal_color_code;
// Corresponding name.
@Attribute("corresponding_name")
public String _corresponding_name;
// Empty constructor for the PersistentStateComponent XML serialisation.
@SuppressWarnings({"UnusedDeclaration"})
public ColorStateBean() {}
public ColorStateBean(
ColorStateBean color
) {
System.out.println("ColorStateBean Constructor");
setId(color.getId());
setColor(color.getColor());
setName(color.getName());
}
public ColorStateBean(
int identifier,
String hexadecimal_color_code,
String corresponding_name
) {
System.out.println("ColorStateBean Constructor");
setId(identifier);
setColor(hexadecimal_color_code);
setName(corresponding_name);
}
@Transient
public int getId() {
return _identifier;
}
public void setId(int identifier) {
this._identifier = identifier;
}
@Transient
public String getColor() {
return _hexadecimal_color_code;
}
public void setColor(String hexadecimal_color_code) {
this._hexadecimal_color_code = hexadecimal_color_code;
}
@Transient
public String getName() {
return _corresponding_name;
}
public void setName(String corresponding_name) {
this._corresponding_name = corresponding_name;
}
}
File 2 : ColorsStateBean.java
package states.beans;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.XCollection;
import states.ColorsDefaultSettings;
import java.util.List;
public class ColorsStateBean {
@Tag("colors")
@XCollection()
public List<ColorStateBean> colorList;
public ColorsStateBean() {
System.out.println("ColorsStateBean Constructor");
colorList = ColorsDefaultSettings.getColorSettingsList();
}
}
File 3 : ColorsDefaultSettings.java
package states;
import states.beans.ColorStateBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ColorsDefaultSettings {
public static List<ColorStateBean> getColorSettingsList() {
return new ArrayList<ColorStateBean>(Arrays.asList(
new ColorStateBean(1, "#ff0000", "Red"),
new ColorStateBean(2, "#00ff00", "Green"),
new ColorStateBean(2, "#0000ff", "Blue")
));
}
}
File 4 : ProjectStateService.java
package services;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.*;
import com.intellij.openapi.components.State;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import states.beans.ColorStateBean;
import states.beans.ColorsStateBean;
@State(name = "CustomProjectTreeView", storages = {@Storage("customProjectTreeView_colors.xml")})
public class ProjectStateService implements PersistentStateComponent<ColorsStateBean> {
private ColorsStateBean colorsStateBean = new ColorsStateBean();
public ProjectStateService() {
System.out.println("ProjectStateService Constructor");
}
@Override
public @Nullable ColorsStateBean getState() {
System.out.println("getState");
return this.colorsStateBean;
}
@Override
public void loadState(@NotNull ColorsStateBean colorsStateBean) {
System.out.println("loadState");
XmlSerializerUtil.copyBean(colorsStateBean, this.colorsStateBean);
}
}
File 5 : ProjectStartupActivity.kt
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import functions.PluginFilesChecker
import services.ProjectStateService
class ProjectStartupActivity : ProjectActivity {
override suspend fun execute(project: Project) {
println("The project \"${project.name}\" is loaded.")
if (PluginFilesChecker.checkColorFileExistence(project)) {
println("The plugin color file exist.");
}
else {
println("The plugin color file does not exist.")
val projectStateService = ProjectStateService();
projectStateService.loadState();
}
}
}
File 6 : plugin.xml
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<!-- Unique identifier of the plugin. It should be FQN. It cannot be changed between the plugin versions. -->
<id>com.example.CustomProjectTreeView</id>
<!-- Public plugin name should be written in Title Case.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-name -->
<name>CustomProjectTreeView</name>
<!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
<vendor email="TODO" url="TODO">TODO</vendor>
<!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
<description>
<![CDATA[
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
]]>
</description>
<!-- Product and plugin compatibility requirements.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<projectService serviceImplementation="services.ProjectStateService"/>
<postStartupActivity implementation="ProjectStartupActivity"/>
</extensions>
</idea-plugin>
Hoping for some help.
Let me know if you need any clarification.