2019-05-06 idea 插件开发——自定义界面
2019-05-06 本文已影响0人
袁成_0e4c
准备:
1.idea 插件开发基础篇:https://www.jianshu.com/p/62d39ad54772
2.idea 插件开发官方文档:http://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/coordinates_system.html
开始:
1.新建dialog
![](https://img.haomeiwen.com/i17730032/704fbcc090d03112.png)
![](https://img.haomeiwen.com/i17730032/e79becd7a04ecd81.png)
![](https://img.haomeiwen.com/i17730032/8756de5750ff2c41.png)
2.编写界面
![](https://img.haomeiwen.com/i17730032/79c91e0bbcd4975a.png)
右边有很多组件,拖进去就行了,各组件具体的api请自行探索。
页面的变动会自动同步到 xxxDialog.java中,命名由左边蓝色选中的 "field name"属性决定。
做个简单的功能吧——在光标位置插入一个空的method代码块
![](https://img.haomeiwen.com/i17730032/0249750764ace0f5.png)
![](https://img.haomeiwen.com/i17730032/21ea012cebf37f08.png)
3.给按钮设置点击事件。。。看代码吧
import javax.swing.*;
import java.awt.event.*;
public class TestDialog extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JButton intButton;
private JButton shortButton;
private JButton longButton;
private JButton objectButton;
public TestDialog() {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionListener l = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
if (listener != null) {
listener.actionPerformed(e);
}
}
};
intButton.addActionListener(l);
shortButton.addActionListener(l);
longButton.addActionListener(l);
objectButton.addActionListener(l);
}
private ActionListener listener;
public void setListener(ActionListener listener) {
this.listener = listener;
}
private void onOK() {
// add your code here
dispose();
}
private void onCancel() {
// add your code here if necessary
dispose();
}
}
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SampleAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
Project project = anActionEvent.getProject();
TestDialog dialog = new TestDialog();
dialog.setListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = "public " + e.getActionCommand() + " methodName() {return null;}";
WriteCommandAction.runWriteCommandAction(project, () -> {
Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset();
editor.getDocument().insertString(offset, str);
});
}
});
dialog.setLocationRelativeTo(dialog);//居中
dialog.pack();
dialog.setVisible(true);
}
}
4.运行
![](https://img.haomeiwen.com/i17730032/713dc19f8c0e1b75.png)
![](https://img.haomeiwen.com/i17730032/9854b78402f78624.png)
5.打包
![](https://img.haomeiwen.com/i17730032/5ee2cf32e8390a80.png)
![](https://img.haomeiwen.com/i17730032/85972df7184bd030.png)
6.安装:打开idea的设置-plugin,将刚准备好的jar安装上。
7.其他:快下班了,不写了。
8.分享一个自己用的插件
![](https://img.haomeiwen.com/i17730032/fbc87a074ff99c8c.png)
下载链接:https://github.com/cryzahell/ideapluginimageplus/raw/master/ideapluginimageplus.jar
github项目链接:https://github.com/cryzahell/ideapluginimageplus