IDEA插件开发初体验之(GET/SET自动生成带模版注释插件的开发与使用)

1.准备环境,添加IDEA插件需要的SDK



2.新增Plugins的Module


3.创建Plugins的项目目录如下:

plugin.xml文件里包含各种配置信息,可以在官方文档里查看。


4.Plugins相关介绍

 (1).三种组件类型:Application、Project、Module Component。

 (2).创建一个Action

Action:让使用我们插件的用户通过菜单或其他方式点击进插件。创建一个插件如下:
Action ID:指Action的唯一ID
Add to Group:选择在那个菜单下显示
当新建一个Action的同时,IDEA会帮你在plugin.xml配置改Aciton,如下:


5.插件配置面板

   1.创建一个配置项的类,实现Configurable(或SearchableConfigurable),实现里面的一个createComponent方法,返回一个JComponent面板对象,这样就会显示在Settings里。

   2.IDEA提供的Swing Designer设计器,创建一个GUI FORM 

创建后就可以通过拖拽的形式来修改setting的配置面板,创建的GUI FORM 如下图:
 

       我写的GET/SET插件的配置类代码如下:(isModified方法用来判断是否已经修改,apply在配置文件应用的时候会调用,reset在撤回reset会调用)

package com.ctt.format.setting;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Created by admin on 2016/12/18.
 */
public class FormatConfigurable implements SearchableConfigurable {

    private FormatForm formatForm;
    private FormatSetting formatSetting = FormatSetting.getInstance();

    public FormatConfigurable() {
    }

    @NotNull
    public String getId() {
        return "DocFormat";
    }

    @Nls
    public String getDisplayName() {
        return this.getId();
    }

    @Nullable
    public String getHelpTopic() {
        return this.getId();
    }

    @Nullable
    public JComponent createComponent() {
        if(null == this.formatForm) {
            this.formatForm = new FormatForm();
        }

        return this.formatForm.mainPanel;
    }

    public boolean isModified() {
        return !this.formatSetting.getGetFormat().equals(this.formatForm.getFormatTextArea.getText()) || !this.formatSetting.getSetFormat().equals(this.formatForm.setFormatTextArea.getText());
    }

    public void apply() throws ConfigurationException {
        this.formatSetting.setGetFormat(this.formatForm.getFormatTextArea.getText());
        this.formatSetting.setSetFormat(this.formatForm.setFormatTextArea.getText());
    }

    public void reset() {
        this.formatForm.getFormatTextArea.setText(this.formatSetting.getGetFormat());
        this.formatForm.setFormatTextArea.setText(this.formatSetting.getSetFormat());
    }

}

       3.配置文件持久化,创建一个时间了PersistentStateComponent<Element>类,用于讲配置持久化到.xml文件里。其中当保存配置文件的时候会调用getState方法讲配置之久到到配置文件里。创建的FormatSetting如下
package com.ctt.format.setting;

/**
 * Created by admin on 2016/12/18.
 */

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 org.jdom.Element;
import org.jetbrains.annotations.Nullable;


@State(
        name = "FormatSetting",
        storages = {@Storage(
                id = "other",
                file = "$APP_CONFIG$/format.xml"
        )}
)
public class FormatSetting implements PersistentStateComponent<Element> {

    private String setFormat;

    private String getFormat;

    public FormatSetting() {
    }

    public static FormatSetting getInstance() {
        return (FormatSetting) ServiceManager.getService(FormatSetting.class);
    }

    @Nullable
    public Element getState() {
        Element element = new Element("FormatSetting");
        element.setAttribute("setFormat", this.getSetFormat());
        element.setAttribute("getFormat", this.getGetFormat());
        return element;
    }

    public void loadState(Element state) {
        this.setSetFormat(state.getAttributeValue("setFormat"));
        this.setGetFormat(state.getAttributeValue("getFormat"));
    }

    public String getSetFormat() {
        return this.setFormat == null ? StatementGenerator.defaultSetFormat : this.setFormat;
    }

    public void setSetFormat(String setFormat) {
        this.setFormat = setFormat;
    }

    public String getGetFormat() {
        return this.getFormat == null ? StatementGenerator.defaultGetFormat : this.getFormat;
    }

    public void setGetFormat(String getFormat) {
        this.getFormat = getFormat;
    }

}

6.插件实现后的效果如下:

1.配置面板如下:


2.使用插件的菜单是在java类里调用快捷键ALT+INSERT。如下:

 


3.自动生成GET/SET并带上注释模版上的注释,效果如下:


 

7.插件的GIT地址和插件的下载地址


2.插件下载地址https://git.oschina.net/cttcu/IDEA-Plugins/blob/master/generateGSDoc/generateGSDoc.zip?dir=0&filepath=generateGSDoc%2FgenerateGSDoc.zip&oid=4ecc84f7b127b76c367296e0d6a33b576f42fb95&sha=463a19bd008eb19882962ca6438b5c2151bc499c



参考资料和博客:

1.IntelliJ IDEA插件开发入门指南:http://www.doc88.com/p-7758859318883.html

2.IntelliJ IDEA插件结构 : http://blog.csdn.net/hawkdowen/article/details/41213001

3.Intellij IDEA插件开发入门 : http://blog.csdn.net/dc_726/article/details/14139155

4.IntelliJ IDEA 英文文档http://www.jetbrains.org/intellij/sdk/docs/index.html

猜你喜欢

转载自blog.csdn.net/u012889214/article/details/53745025
今日推荐