这篇博客主要分享三方面的内容:
1、idea插件开发的基础知识
2、搭建一个简易的后台
3、实现自己的一个简易插件

大的团队协作,每个人都会有自己的一些心得体会或者自己写的方便使用的工具类,自定义view等,当一个新人入职的时候或者自己对其它团队的业务不熟悉的时候,总是会不断的请教别人,如果我们有一个中央仓库,需要什么东西可以方便的查阅该多好。

比如小明想知道登录成功以后会有什么消息通知,他就可以搜索登录,然后在插件的右侧区域就可以看到相关的代码信息,这是一个简单的示例

idea开发java实用插件 idea插件 开发_List


又比如自己写了一个自定义view的库,也可以使用插件分享,供别人使用

idea开发java实用插件 idea插件 开发_idea插件开发_02


最终效果:

idea开发java实用插件 idea插件 开发_idea插件开发_03


这样一个插件可以帮助团队提高协作效率

接下来介绍实现过程

1、新建一个插件工程:新建插件工程
2、插件工程可能出现的问题:插件工程可能出现的问题
3、搭建一个简易的后台
3.1、tomcat安装和环境配置
3.2、idea新建sprintboot+mybits项目
3.3、自行安装mysql数据库

tomcat的安装是非常容易的,但是一个不了解后台的同学,要使用idea新建一个sprintboot+mybits项目可能比较麻烦

这里分享一下我的工程代码仅供参考

工程框架

idea开发java实用插件 idea插件 开发_idea插件开发_04


yml配置:

server:
  #和tomcat安装的端口一致
  port: 8082
spring:
  #数据库连接配置
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/code_wizard

#mybatis的相关配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.sina.code_wizard.data
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true

增删改查获取参数可以通过:@RequestParam获取

idea开发java实用插件 idea插件 开发_System_05


idea开发java实用插件 idea插件 开发_idea开发java实用插件_06


生成jar包或war包的方法

idea开发java实用插件 idea插件 开发_apache_07


idea开发java实用插件 idea插件 开发_List_08


放到tomcat的webapps目录下关闭idea工程再运行tomcat(否则可能会遇到一些端口冲突的异常)

idea开发java实用插件 idea插件 开发_idea插件开发_09


以上是idea+sprintboot的简单介绍,并非本文重点,不了解的同学可以参考我的工程代码

sprinBoot工程代码

以下为本文重点,下载工程代码后一边看代码一边看文章效果最佳

知识分享idea插件工程

4、知识分享插件的开发

按照上述步骤,搭建好一个插件工程后,我们要开始写插件的界面了
1、新建一个Action用来启动插件

public class WizardAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        //代码精灵工程路径 = C:/Users/hongda5/IntelliJIDEAProjects/MyApplication
//        Messages.showInfoMessage("代码精灵工程路径 = " + e.getProject().getBasePath(), "代码精灵");
        MainFrame frame = new MainFrame();
        //f.pack()做了这样一件事 f.setSize(f.getPreferredSize()); 在你没加f.pack()之前,f窗体的大小是(0,0) 当你加了以后,f的大小会被设置为最佳大小,也就是prefrredSize
//        frame.pack();
        frame.setVisible(true);
    }

}

2、新建一个GUI Form

这里要学习一下JTabbedPane的使用

idea开发java实用插件 idea插件 开发_apache_10


给JTabbedPane增加三个页面,工具集合,添加工具,Adapter页面

工具集合页面:

idea开发java实用插件 idea插件 开发_apache_11


搜索栏使用:JTextField

左侧使用:JScrollPane

右侧使用:JEditorPane来显示代码区

为了让代码区可以支持html显示,需要如下操作:

codePanel.setContentType("text/html");
        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        codePanel.setEditorKit(htmlEditorKit);
        Document document = htmlEditorKit.createDefaultDocument();
        codePanel.setDocument(document);

使用HttpClientHelp类从数据库获取数据,显示在插件上

网络访问类:(注意要换成自己的tomcat地址)

package wizard.net;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import wizard.data.AddCodeWizardResult;
import wizard.data.CodeWizard;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class HttpClientHelp {

    private static HttpClientHelp INSTANCE = new HttpClientHelp();
    private Gson gson;
    /**
     * 增加规则 注意要换成自己的tomcat地址
     */
    private final static String ADD_CODE_WIZARD = "http://10.235.51.50:8081/code_wizard/addCodeWizard";
    private final static String GET_ALL_CODE_WIZARD = "http://10.235.51.50:8081/code_wizard/getAllCodeWizard";
    private final static String DELETE_CODE_WIZARD = "http://10.235.51.50:8081/code_wizard/deleteById";
    private final static String GET_CODE_WIZARD_BY_ID = "http://10.235.51.50:8081/code_wizard/getCodeById";

    private HttpClientHelp() {
        gson = new Gson();
    }

    public static HttpClientHelp instance() {
        return INSTANCE;
    }

    /**
     * 查询所有的code wizard
     */
    public ServerResult<List<CodeWizard>> getAllCodeWizards() {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setConnectionManagerTimeout(5 * 1000);
        httpClient.getParams().setSoTimeout(5 * 1000);
        System.out.println("发起请求的链接 Url = " + GET_ALL_CODE_WIZARD);

        GetMethod getMethod = new GetMethod(GET_ALL_CODE_WIZARD);
        getMethod.setDoAuthentication(true);
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode == HttpStatus.SC_NOT_FOUND) {
                System.out.println("Method failed code=" + statusCode + ": " + getMethod.getStatusLine());
                return new ServerResult<List<CodeWizard>>(statusCode, "请求失败", new ArrayList<>());
            } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
                return new ServerResult<List<CodeWizard>>(statusCode, "服务器认证失败", null);
            } else if (statusCode == HttpStatus.SC_OK) {
                String response = new String(getMethod.getResponseBody(), StandardCharsets.UTF_8);
                System.out.println("请求的返回值 response = " + response);
                List<CodeWizard> parse = new Gson().fromJson(response.toString(), new TypeToken<List<CodeWizard>>() {
                }.getType());
                return new ServerResult<List<CodeWizard>>(0, "成功", parse);
            }
        } catch (Exception e) {
            return new ServerResult<List<CodeWizard>>(1, e.getMessage(), null);
        } finally {
            getMethod.releaseConnection();
        }
        return new ServerResult<List<CodeWizard>>(1, "未知错误", null);
    }

    /**
     * 增加ode wizard
     */
    public ServerResult<CodeWizard> addCodeWizards(String title, String notes, String author, String example, String keyWords) {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setConnectionManagerTimeout(5 * 1000);
        httpClient.getParams().setSoTimeout(5 * 1000);
        PostMethod postMethod = null;

        try {
            URI uri = new URIBuilder(ADD_CODE_WIZARD).build();
            System.out.println("发起请求的链接 Url = " + uri.toString());
            postMethod = new PostMethod(uri.toString());
            postMethod.setDoAuthentication(true);
            postMethod.addParameter(new NameValuePair("title", title) );
            postMethod.addParameter(new NameValuePair("notes", notes) );
            postMethod.addParameter(new NameValuePair("author", author) );
            postMethod.addParameter(new NameValuePair("example", example) );
            postMethod.addParameter(new NameValuePair("keyWords", keyWords) );
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");

            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode == HttpStatus.SC_NOT_FOUND) {
                System.out.println("Method failed code=" + statusCode + ": " + postMethod.getStatusLine());
                return new ServerResult<CodeWizard>(statusCode, "请求失败", new CodeWizard());
            } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
                return new ServerResult<CodeWizard>(statusCode, "服务器认证失败", null);
            } else if (statusCode == HttpStatus.SC_OK) {
                String response = new String(postMethod.getResponseBody(), StandardCharsets.UTF_8);
                System.out.println("addCodeWizards 请求的返回值 response = " + response);
                AddCodeWizardResult result = gson.fromJson(response.toString(), new TypeToken<AddCodeWizardResult>() {
                }.getType());
                return new ServerResult<CodeWizard>(result.getCode(), result.getMsg(), result.getData());
            }
        } catch (Exception e) {
            return new ServerResult<CodeWizard>(1, e.getMessage(), null);
        } finally {
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
        }
        return new ServerResult<CodeWizard>(1, "未知错误", null);
    }

    /**
     * 根据id删除一个code wizard
     *
     * @param id
     * @return
     */
    public ServerResult<CodeWizard> deleteCodeWizardsById(String id) {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setConnectionManagerTimeout(5 * 1000);
        httpClient.getParams().setSoTimeout(5 * 1000);
        GetMethod getMethod = null;
        try {
            URI uri = new URIBuilder(DELETE_CODE_WIZARD).addParameter("id", id).build();
            System.out.println("发起请求的链接 Url = " + uri.toString());
            getMethod = new GetMethod(uri.toString());
            getMethod.setDoAuthentication(true);
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode == HttpStatus.SC_NOT_FOUND) {
                System.out.println("Method failed code=" + statusCode + ": " + getMethod.getStatusLine());
                return new ServerResult<CodeWizard>(statusCode, "请求失败", new CodeWizard());
            } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
                return new ServerResult<CodeWizard>(statusCode, "服务器认证失败", null);
            } else if (statusCode == HttpStatus.SC_OK) {
                String response = new String(getMethod.getResponseBody(), StandardCharsets.UTF_8);
                System.out.println("请求的返回值 response = " + response);
                CodeWizard parse = gson.fromJson(response.toString(), new TypeToken<CodeWizard>() {
                }.getType());
                return new ServerResult<CodeWizard>(0, "成功", parse);
            }
        } catch (Exception e) {
            return new ServerResult<CodeWizard>(1, e.getMessage(), null);
        } finally {
            if (getMethod != null) {
                getMethod.releaseConnection();
            }
        }
        return new ServerResult<CodeWizard>(1, "未知错误", null);
    }

    /**
     * 根据id查找code wizard
     *
     * @param id
     * @return
     */
    public ServerResult<CodeWizard> getCodeWizardsById(String id) {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setConnectionManagerTimeout(5 * 1000);
        httpClient.getParams().setSoTimeout(5 * 1000);
        GetMethod getMethod = null;
        try {
            URI uri = new URIBuilder(GET_CODE_WIZARD_BY_ID).addParameter("id", id).build();
            System.out.println("发起请求的链接 Url = " + uri.toString());
            getMethod = new GetMethod(uri.toString());
            getMethod.setDoAuthentication(true);
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode == HttpStatus.SC_NOT_FOUND) {
                System.out.println("Method failed code=" + statusCode + ": " + getMethod.getStatusLine());
                return new ServerResult<CodeWizard>(statusCode, "请求失败", new CodeWizard());
            } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
                return new ServerResult<CodeWizard>(statusCode, "服务器认证失败", null);
            } else if (statusCode == HttpStatus.SC_OK) {
                String response = new String(getMethod.getResponseBody(), StandardCharsets.UTF_8);
                System.out.println("请求的返回值 response = " + response);
                CodeWizard parse = gson.fromJson(response.toString(), new TypeToken<CodeWizard>() {
                }.getType());
                return new ServerResult<CodeWizard>(0, "成功", parse);
            }
        } catch (Exception e) {
            return new ServerResult<CodeWizard>(1, e.getMessage(), null);
        } finally {
            if (getMethod != null) {
                getMethod.releaseConnection();
            }
        }
        return new ServerResult<CodeWizard>(1, "未知错误", null);
    }

    public static void main(String[] args) {
//        ServerResult result = instance().getAllCodeWizards();
//        System.out.println(result.getMsg());

//        ServerResult result = instance().addCodeWizards("test add", "测试添加", "hongda", "hongda example", "");
//        System.out.println(result.getMsg());

        ServerResult result = instance().getCodeWizardsById("1");
        System.out.println(result.getMsg());
    }

}

网络请求和界面渲染要放到子线程里去,防止插件页面卡顿

public void initData() {
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {
                ServerResult<List<CodeWizard>> allCodeWizards = HttpClientHelp.instance().getAllCodeWizards();
                if (allCodeWizards.getItems() == null || allCodeWizards.getItems().isEmpty()) {
                    JLabel label = new JLabel();
                    label.setText("代码精灵出去玩了,稍后再来找她吧");
                    System.out.println("空布局显示");
                    setContentPane(label);
                } else {
                    allWizards.clear();
                    allWizards.addAll(allCodeWizards.getItems());
                    addCodeWizards(allCodeWizards.getItems());
                }
            }
        });
    }

接下来就全部是java和jswing的编写了,突然感觉没啥可说的了0.0
直接参考代码工程即可