How to Save Customized Settings

Hi,
      I'm trying to develop a plugin for IDEA. I created it as a  tool window so it can be accessible in view -> tool windows menu. And  I want to add a customized settings for this plugin. I implement  ProjectComponent, Configurable, ToolWindowFactory interfaces in my  class. So I can create a simple gui for my settings(just one label and  textfield) and I can access it in file->settings->project  settings. I want to save String in the textfield for my plugin settings  but I can't save it. How can I save a string for my plugin settings?

7 comments
Comment actions Permalink

Thank you for your reply. But I can't  handle this. Do I need to register in plugin.xml? Or is there any working example?

0
Comment actions Permalink

There's many many examples of this in IntelliJ Community Edition source code. For Example, com.intellij.ide.ui.AppearanceConfigurable

0
Comment actions Permalink

@State(
        name = "RenameComponent",
        storages = {@Storage(id = "RenamePlugin", file = StoragePathMacros.PROJECT_FILE)}
)
public class RenameComponent implements ToolWindowFactory, ProjectComponent, Configurable, PersistentStateComponent<RenameComponent>{

    private JPanel myToolWindowContent;
    private ToolWindow myToolWindow;
    private JTable myTable;
    private String location = "C:\\Users\\Utku Ozdil\\workspace2\\rename\\";
    public String phrase = "ChangeLog.xml";
    private RenameConfig form = new RenameConfig();
    private Project myProject;

    @Override
    public void createToolWindowContent(Project project, ToolWindow toolWindow){
        myToolWindow = toolWindow;
        myProject = project;
        this.createTable(myProject);
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        Content content = contentFactory.createContent(myToolWindowContent, "", false);
        myToolWindow.getContentManager().addContent(content);
    }

    public RenameComponent getInstance() {
        return ServiceManager.getService(myProject,RenameComponent.class);
    }

    @Override
    public void projectOpened() {

    }

    @Override
    public void projectClosed() {

    }

    @Override
    public void initComponent() {

    }

    @Override
    public void disposeComponent() {

    }

    @NotNull
    @Override
    public String getComponentName() {
        return "Rename Plugin";
    }

    public String getPhrase() {
        return phrase;
    }

    public void setPhrase(final String phrase) {
        this.phrase = phrase;
    }

    @Nls
    @Override
    public String getDisplayName() {
        return "Rename Plugin Settings";
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        //RenameComponent settings = RenameComponent.this.getInstance();
        //form.myTextField.setText(settings.phrase);
        return form.getMyPanel();
    }

    @Override
    public boolean isModified() {
        return form != null && form.isModified(this);
    }

    @Override
    public void apply() throws ConfigurationException {
        if(form != null){
            setPhrase(form.myTextField.getText());
            form.getData(this);
            this.createTable(myProject);
        }
    }

    @Override
    public void reset() {
    }

    @Override
    public void disposeUIResources() {

    }

    @Nullable
    @Override
    public RenameComponent getState() {
        return this;
    }

    @Override
    public void loadState(RenameComponent renameComponent) {
        XmlSerializerUtil.copyBean(renameComponent,this);
    }

Here's my main code

public class RenameConfig {
    public JTextField myTextField;
    private JLabel myLabel;
    private JPanel myPanel;

    public RenameConfig(){
        myLabel.setLabelFor(myTextField);
    }

    public JComponent getMyPanel() {
        return myPanel;
    }

    public void setData(RenameComponent data) {
        myTextField.setText(data.getPhrase());
    }

    public void getData(RenameComponent data) {
        data.setPhrase(myTextField.getText());
    }

    public boolean isModified(RenameComponent data) {
        return myTextField.getText() != null ? !myTextField.getText().equals(data.getPhrase()) : data.getPhrase() != null;
    }
}

Here's my form code in File->Settings.

I still can't save phrase, I can't figure what I miss.

0
Comment actions Permalink

Please don't put all of your code in a single class, this won't work as you expect. IntelliJ IDEA will create multiple instances of your class, one as a Configurable and another as a PersistentStateComponent, so the instance in which you put the data when the settings are saved will not be the same instance from which it will take the data to be saved. Instead, you need to use a separate class for the implementation of each interface and use the getComponent() or ServiceManager.getService() methods to connect them with each other. You can see multiple examples of this everywhere in the IntelliJ IDEA codebase.

0
Comment actions Permalink

Thank you for reply. I'm looking at an example now. I'm trying to develop plugin based on this document: https://www.jetbrains.com/idea/docs/TheBasicsofPluginDevelopment.pdf
And in this document single class works.

I have a question: In my class, I have implemented 4 interface, so I need to make 4 class or more?

Another question: I need to store just one string, so instead of PersistentStateComponent, can I use PropertiesComponent.GetInstance(project).setValue and getValue?

0
Comment actions Permalink

This PDF is completely out of date, please follow documentation here http://confluence.jetbrains.com/display/IDEADEV/PluginDevelopment or just look for examples in Community Edition.

Using PropertiesComponent is also possible, of course, but then quite limited.

0

Please sign in to leave a comment.