介绍
Demo说明
本文基于maven项目开发,idea版本为2022.3以上,jdk为1.8
本文在Tools插件之上进行开发
Tools插件说明
Tools插件是一个Idea插件,此插件提供统一Spi规范,极大的降低了idea插件的开发难度,并提供开发者模块,可以极大的为开发者开发此插件提供便利
Tools插件安装需要idea2022.3以上版本
- 插件下载连接:
https://download.youkuaiyun.com/download/qq_42413011/89702325
- sdk下载连接:
https://download.youkuaiyun.com/download/qq_42413011/89702330
- pojo-serializer插件:
https://gitee.com/myprofile/pojo-serializer
正文
打开上一篇文章项目
在pom中添加依赖
编写代码
package com.lhstack.aaa;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.lhstack.tools.plugins.IPlugin;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class PluginImpl implements IPlugin {
private final Map<String, JComponent> cache = new HashMap<>();
@Override
public JComponent createPanel(String projectHash) {
return cache.computeIfAbsent(projectHash, key -> {
JPanel panel = new JPanel(new BorderLayout());
JTextPane jTextPane = new JTextPane();
setting(jTextPane);
JScrollPane jScrollPane = new JScrollPane(jTextPane);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//json面板
panel.add(jScrollPane, BorderLayout.CENTER);
//事件按钮面板
JPanel actionsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
//添加格式化按钮
JButton formatButton = new JButton("格式化");
formatButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String json = jTextPane.getText();
if (json != null && !json.trim().isEmpty()) {
//格式化
try {
String formatJson = JSON.toJSONString(JSON.parse(json), JSONWriter.Feature.PrettyFormat);
jTextPane.setText(formatJson);
} catch (Throwable err) {
JOptionPane.showMessageDialog(null, err.toString(), "json格式化", JOptionPane.ERROR_MESSAGE);
}
}
}
});
actionsPanel.add(formatButton);
//添加压缩按钮
JButton compressButton = new JButton("压缩");
compressButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String json = jTextPane.getText();
if (json != null && !json.trim().isEmpty()) {
//压缩
try {
String formatJson = JSON.toJSONString(JSON.parse(json));
jTextPane.setText(formatJson);
} catch (Throwable err) {
JOptionPane.showMessageDialog(null, err.toString(), "json压缩", JOptionPane.ERROR_MESSAGE);
}
}
}
});
actionsPanel.add(compressButton);
//放在右下角
panel.add(actionsPanel, BorderLayout.SOUTH);
return panel;
});
}
private void setting(JTextPane textPane) {
//设置border
textPane.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY));
//设置tab
TabSet tabSet = new TabSet(new TabStop[]{new TabStop(4)});
StyleContext styleContext = StyleContext.getDefaultStyleContext();
AttributeSet aset = styleContext.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabSet);
textPane.setParagraphAttributes(aset, false);
}
@Override
public void closeProject(String projectHash) {
//关闭项目,移除项目对应打开的组件
cache.remove(projectHash);
}
@Override
public void unInstall() {
//清除缓存
cache.clear();
}
@Override
public Icon pluginIcon() {
try {
return new ImageIcon(ImageIO.read(Objects.requireNonNull(PluginImpl.class.getClassLoader().getResourceAsStream("DEMO-48x48.png"))));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public Icon pluginTabIcon() {
try {
return new ImageIcon(ImageIO.read(Objects.requireNonNull(PluginImpl.class.getClassLoader().getResourceAsStream("DEMO-16x16.png"))));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public String pluginName() {
return "Json工具";
}
@Override
public String pluginDesc() {
return "这是一个Json工具";
}
@Override
public String pluginVersion() {
return "0.0.1";
}
}
在开发面板中测试
点击格式化
点击压缩
打包插件
拖拽安装
测试
格式化
压缩