天天看點

IDEA-插件開發 入門篇前言一、插件入門 項目建立二、實戰案例 - mysql 列轉換總結

IDEA-插件開發 入門篇

文章目錄

    • IDEA-插件開發 入門篇
  • 前言
  • 一、插件入門 項目建立
  • 二、實戰案例 - mysql 列轉換
    • 1. 配置插件資訊
    • 2. 建立action
    • 3. 運作結果
  • 總結

前言

由于網上的idea 插件教程很少,無奈隻能自己研究,文章記錄的内容不能保證準确性,有問題希望可以評論指出!。

一、插件入門 項目建立

IDEA-插件開發 入門篇前言一、插件入門 項目建立二、實戰案例 - mysql 列轉換總結

二、實戰案例 - mysql 列轉換

插件作用 - 通常寫sql的時候,sql 需要把列名寫出來,比如 select s.u_id as uId,… from xxx, 對于我這種懶人黨說簡直巨麻煩,畢竟自動生成的列是沒有字首的… 像這樣 u_id,u_name

1. 配置插件資訊

配置 resources 下的 plugin.xml

IDEA-插件開發 入門篇前言一、插件入門 項目建立二、實戰案例 - mysql 列轉換總結

配置如下

<idea-plugin>
    <id>org.example.myPlugins</id>
    <name>MyPlugin</name>
    <vendor email="[email protected]" url="http://www.baidu.com">lieying</vendor>

    <description>first test plugin</description>


    <extensions defaultExtensionNs="com.intellij">
        <!-- Add your extensions here -->
    </extensions>

    <actions>
        <!-- Add your actions here -->
        <group id="MyPlugin.SampleMenu"  text="_Sample Menu" description="Sample menu">
            <add-to-group group-id="MainMenu" anchor="last"  />
            <action id="Myplugin.Textboxes" class="com.hunt.plugin.TextBoxes" text="Text _Boxes" description="A test menu item">
                <keyboard-shortcut keymap="$default" first-keystroke="ctrl alt Z" />
            </action>
        </group>


    </actions>
</idea-plugin>
           

2. 建立action

需要去繼承AnAction,當idea 的按鈕被觸發時會被調用actionPerformed 方法:

package com.hunt.plugin;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;

public class TextBoxes extends AnAction {

    // 定義按鈕的名稱
    public TextBoxes() {
        super("MYSQL_COLUMN_ADD_PRO");
    }

    public void actionPerformed(AnActionEvent event) {
        Project project = event.getData(PlatformDataKeys.PROJECT);

        // userInput
        String strMysqlColumns = Messages.showMultilineInputDialog(project, "Enter the column you need to add salt",
                "xyang", "example:\n" +
                        "\t user_id,\n " +
                        "\t user_name", Messages.getInformationIcon(), null);
        // 如果任意一個值是null, 那麼就不做
        if (strMysqlColumns == null || strMysqlColumns.trim().equals("")){
            return;
        }

        String prefix = Messages.showInputDialog(project, "Enter the prefix you need to add",
                "xyang", Messages.getInformationIcon());
        // 如果任意一個值是null, 那麼就不做
        if (prefix == null || prefix.trim().equals("")){
            return;
        }

        // 結果存放
        StringBuffer sb = new StringBuffer();

        // 處理列,加上字首
        String[] columns = strMysqlColumns.split(",");
        for (int i = 0; i < columns.length; i++) {
            // 單個column 加上字首
            sb.append(prefix);
            sb.append(".");
            sb.append(columns[i]);


            // 最後一次循環不執行
            if (columns.length - 1 > i){
                sb.append(",");
                sb.append("\n");
            }
        }

        // 傳回出結果
        Messages.showMessageDialog(project, sb.toString() + "\n ", "Thank you for your use.", Messages.getInformationIcon());
    }

}

           

3. 運作結果

IDEA-插件開發 入門篇前言一、插件入門 項目建立二、實戰案例 - mysql 列轉換總結

總結

例如:以上就是今天要講的内容,本文僅僅簡單介紹了定義簡單插件,而idea提供了大量接口支援我們自定義插件,後續更新自定義工具欄的章節