简介
IDEA是我们常用的开发工具,我们也会用到很多IDEA提供的插件,如lombok插件,checkstyle插件等,今天我们就来开发一个自己的插件。
环境准备
IDEA安装PluginDevkit插件,这是一个帮助我们开发插件的插件。
创建插件项目
创建成功之后的项目结构为
plugin.xml为项目的核心配置文件
<idea-plugin>
<!--插件Id-->
<id>com.strongmore.secondplugin.id</id>
<!--插件名称-->
<name>SecondPlugin</name>
<!--插件版本号-->
<version>1.0</version>
<!--开发者信息-->
<vendor email="test@163.com" url="http://www.github.com">strongmore</vendor>
<!--插件描述信息-->
<description><![CDATA[
this is my second plugin that to show in blog
]]></description>
<!--插件版本更新信息-->
<change-notes><![CDATA[
this is my second plugin that to show in blog
]]>
</change-notes>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="173.0"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!--uncomment to enable plugin in all products-->
<!--项目创建之后这个标签是注释的,需要打开,不然不能部署到IDEA的插件库-->
<depends>com.intellij.modules.lang</depends>
<!--配置一些扩展功能,如工具窗-->
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>
<!--配置我们定义的动作命令,如右键点击-->
<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>
开发功能
我们开发一个点击右键,每天一碗毒鸡汤的小功能
创建我们的动作命令
创建成功之后自动在plugin.xml中添加配置
<!--配置我们定义的动作命令,如右键点击-->
<actions>
<!-- Add your actions here -->
<action id="EditorPopupTauntId" class="com.strongmore.secondplugin.action.EditorPopupTaunt"
text="毒鸡汤" description="毒鸡汤">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
</action>
</actions>
当点击我们的按钮时就会执行此方法
创建对话框
import com.intellij.openapi.ui.DialogWrapper;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jetbrains.annotations.Nullable;
public class TauntDialog extends DialogWrapper {
private JLabel jLabel;
public TauntDialog() {
super(true);
setTitle("每天一碗毒鸡汤");
init();
}
@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel jPanel = new JPanel();
jLabel = new JLabel();
jLabel.setText("测试");
jPanel.add(jLabel);
return jPanel;
}
@Override
protected JComponent createSouthPanel() {
JPanel jPanel = new JPanel();
JButton jButton = new JButton("再来一碗");
jButton.addActionListener((e) -> {
jLabel.setText("测试2");
});
jPanel.add(jButton);
return jPanel;
}
}
现在我们的毒鸡汤内容是写死的,需要不断的变化,我们从公共的API获取,毒鸡汤API,网络请求我们使用spring-web的RestTemplate.
添加第三方jar包
使用网络请求更新毒鸡汤内容
public class HttpUtility {
public static String getTauntContent() {
TauntResponse tauntResponse = httpGet("https://api.nextrt.com/api/dutang", TauntResponse.class);
if (Objects.nonNull(tauntResponse) && tauntResponse.isSuccess()) {
return tauntResponse.getData().getContent();
}
return null;
}
private static <T> T httpGet(String url, Class<T> clazz) {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> request = new HttpEntity<>(new HttpHeaders());
ResponseEntity<T> entity = restTemplate.exchange(url, HttpMethod.GET, request, clazz);
if (entity.getStatusCode().is2xxSuccessful()) {
return entity.getBody();
}
return null;
}
}
public class TauntResponse {
private String status;
private String msg;
private TauntInfo data;
private Long timestamp;
public void setStatus(String status) {
this.status = status;
}
public void setData(TauntInfo data) {
this.data = data;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Long getTimestamp() {
return timestamp;
}
public String getMsg() {
return msg;
}
public String getStatus() {
return status;
}
public TauntInfo getData() {
return data;
}
public boolean isSuccess() {
return "1".equals(status);
}
}
public class TauntInfo {
private Integer id;
private String content;
public void setContent(String content) {
this.content = content;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public String getContent() {
return content;
}
}
定义网络请求工具类和接收响应的数据结构
public class TauntDialog extends DialogWrapper {
private JLabel jLabel;
public TauntDialog() {
super(true);
setTitle("每天一碗毒鸡汤");
init();
}
@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel jPanel = new JPanel();
jLabel = new JLabel();
jLabel.setText(HttpUtility.getTauntContent());
jPanel.add(jLabel);
return jPanel;
}
@Override
protected JComponent createSouthPanel() {
JPanel jPanel = new JPanel();
JButton jButton = new JButton("再来一碗");
jButton.addActionListener((e) -> {
jLabel.setText(HttpUtility.getTauntContent());
});
jPanel.add(jButton);
return jPanel;
}
}
运行调试
运行之后会重新启动一个IDEA界面
点击右键就会出现我们定义的action,点击
打包
打包之后会生成一个zip包
部署
我们可以将插件安装到我们的IDEA上
也可以发布到IDEA的插件库,需要注册IDEA账号,注册之后去我的插件管理,
选择打包好的zip包上传,等待审核就好了