Java教程

编写Idea插件 - Serial Number

本文主要是介绍编写Idea插件 - Serial Number,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Serial Number-填补Idea插件市场空白

1.简述

  为了满足生产需要,总是需要生成各式各样的测试用例,命名的时候Id又不能重复,因此最好的办法就是在Id后面添加序号,便于区分。但是找遍了Idea的插件,无果,于是萌生了自己写一个的想法。

2.主要代码

package com.joker.plugin.dialog;

import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.joker.plugin.util.StringMessage;
import com.joker.plugin.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * @author Joker
 * @since 2021/08/15
 */
public class FormDialog extends DialogWrapper {


    private final JLabel start = new JLabel(StringMessage.LABEL_START, SwingConstants.CENTER);
    private final JTextField startContent = new JTextField();

    private final JLabel interval = new JLabel(StringMessage.LABEL_INTERVAL, SwingConstants.CENTER);
    private final JTextField intervalContent = new JTextField();

    private final Editor editor;

    public FormDialog(@Nullable Editor editor) {
        super(true);
        this.editor = editor;
        setTitle(StringMessage.TITLE);
        init();
    }

    @Override
    protected JComponent createNorthPanel() {
        return null;
    }

    @Override
    protected @Nullable ValidationInfo doValidate() {
        if (!editor.getDocument().isWritable()) {
            return new ValidationInfo(StringMessage.CANNOT_BE_WRITTEN);
        }
        return super.doValidate();
    }

    @Override
    protected Action @NotNull [] createActions() {
        DialogWrapperExitAction exitAction = new DialogWrapperExitAction(StringMessage.BUTTON_CANCEL, CANCEL_EXIT_CODE);
        CustomOKAction okAction = new CustomOKAction();
        // 设置默认的焦点按钮
        okAction.putValue(DialogWrapper.DEFAULT_ACTION, true);
        return new Action[]{okAction, exitAction};
    }

    @Override
    protected JComponent createCenterPanel() {
        JPanel center = new JPanel();
        center.setLayout(new GridLayout(2, 2));
        center.add(start);
        startContent.setText("1");
        center.add(startContent);
        center.add(interval);
        intervalContent.setText("1");
        center.add(intervalContent);
        return center;
    }

    protected class CustomOKAction extends DialogWrapperAction {

        protected CustomOKAction() {
            super(StringMessage.BUTTON_OK);
        }

        @Override
        protected void doAction(ActionEvent e) {

            ValidationInfo validationInfo = doValidate();
            if (validationInfo != null) {
                Messages.showMessageDialog(validationInfo.message, StringMessage.PLEASE_CHECK, Messages.getInformationIcon());
                return;
            }

            String startText = startContent.getText();
            String intervalText = intervalContent.getText();

            if (!StringUtils.hasText(startText) || !StringUtils.hasText(intervalText)) {
                Messages.showMessageDialog(StringMessage.CANNOT_BE_BLANK, StringMessage.ERROR_TITLE, Messages.getInformationIcon());
                return;
            }

            long startNum, intervalNum;
            try {
                startNum = Long.parseLong(startText);
                intervalNum = Long.parseLong(intervalText);
            } catch (Exception ex) {
                Messages.showMessageDialog(StringMessage.TYPE_INT, StringMessage.ERROR_TITLE, Messages.getInformationIcon());
                return;
            }

            generate(startNum, intervalNum);
            close(CANCEL_EXIT_CODE);

        }
    }

    private void generate(long startNum, long intervalNum) {
        List<Caret> allCarets = editor.getCaretModel().getAllCarets();
        Document document = editor.getDocument();
        WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> {
            for (int i = 0; i < allCarets.size(); i++) {
                long num = startNum + intervalNum * i;
                Caret caretTemp = allCarets.get(i);
                int offset = caretTemp.getOffset();
                if (caretTemp.hasSelection()) {
                    offset = caretTemp.getSelectionStart();
                    int selectionEnd = caretTemp.getSelectionEnd();
                    document.deleteString(offset, selectionEnd);
                    document.insertString(offset, num + "");
                    caretTemp.moveToOffset(offset + getNumLenght(num));
                    continue;
                }
                document.insertString(offset, num + "");
                caretTemp.moveToOffset(offset + getNumLenght(num));
            }
        });
    }

    private int getNumLenght(long num) {
        num = num > 0 ? num : -num;
        return String.valueOf(num).length();
    }
}

3.插件样式

4.插件演示

1) 从toolbar中寻找

2) 右键中寻找

5.项目地址

https://github.com/Joker-Q4/serial_number

这篇关于编写Idea插件 - Serial Number的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!