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();
}
}