Idea插件开发-redis可视化客户端

tech2024-04-18  100

Idea插件开发-redis可视化客户端

开发背景

鉴于项目开发过程中频繁使用redis,需要频繁的查询redis、删除redis、修改redis值的常规操作,现状是找不到合适的开源可视化客户端工具,所以就琢磨了一下自研一套基于Java程序员常用的开发工具:Idea的插件,这样就很方便大家在开发时直接使用 而不用再多余打开其他客户端软件

设计思路

本次开发Idea插件是基于Idea的Tools菜单追加一个子项+弹出框的交互方式设计的 设计效果图:

开发步骤

新建插件项目

新建Action

生成的java 代码:

public class ToolsRedisAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { } }

生成的扩展文件:

<actions> <action id="tools.redis.action" class="com.patrick.plugins.ToolsRedisAction" text="ToolsRedisAction"> <add-to-group group-id="ToolsMenu" anchor="last"/> </action> </actions>```

编写可视化界面

public class RedisClientUI extends JFrame{ private JPanel panel1,panel2,panel3,panel4; private JLabel jlb1,jlb2,jlb3,jlbRedisKey,jlbRedisValue,jlbAuth; private JTextField hostText; private JTextField portText; private JTextField inputRedisKey,inputRedisValue; private JTextArea jTextArea; private JPasswordField passwordText; private JRadioButton jRadioButton1; private JRadioButton jRadioButton2; private ButtonGroup jRadioButtonGroup; private JButton connButton,addButton,delButton,queryButton; private JButton cancelButton; private JFrame frame; private Jedis jedis = null; public static void main(String[] args) { RedisClientUI ui = new RedisClientUI(); } public RedisClientUI(){ frame = new JFrame("新窗口");//构造一个新的JFrame,作为新窗口。 panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); jlb1 = new JLabel("Host"); jlb2 = new JLabel("Port"); jlb3 = new JLabel("Password"); jlbRedisKey = new JLabel("Key"); jlbRedisValue = new JLabel("Value"); jlbAuth = new JLabel("Auth"); hostText = new JTextField(10); portText = new JTextField(10); passwordText = new JPasswordField(10); jRadioButton1 = new JRadioButton("Y"); jRadioButton2 = new JRadioButton("N"); jRadioButtonGroup = new ButtonGroup(); connButton = new JButton("Connect"); inputRedisKey = new JTextField(10); inputRedisValue = new JTextField(20); addButton = new JButton("ADD"); delButton = new JButton("DEL"); jTextArea = new JTextArea("Redis查询结果",5,50); queryButton = new JButton("GET"); cancelButton = new JButton("Cancel"); jRadioButtonGroup.add(jRadioButton1); jRadioButtonGroup.add(jRadioButton2); jRadioButton2.setSelected(true); jTextArea.setLineWrap(true); //设置文本域中的文本为自动换行 jTextArea.setFont(new Font("楷体",Font.BOLD,16)); //修改字体样式 frame.add(panel1, BorderLayout.NORTH); frame.add(panel2,BorderLayout.CENTER); frame.add(panel4,BorderLayout.SOUTH); panel1.add(jlb1); panel1.add(hostText); panel1.add(jlb2); panel1.add(portText); panel1.add(jlb3); panel1.add(passwordText); panel1.add(jlbAuth); panel1.add(jRadioButton1); panel1.add(jRadioButton2); panel1.add(connButton); panel2.add(jlbRedisKey); panel2.add(inputRedisKey); panel2.add(jlbRedisValue); panel2.add(inputRedisValue); panel2.add(addButton); panel2.add(delButton); panel2.add(jTextArea); panel2.add(queryButton); panel4.add(cancelButton); //设置窗体属性 frame.setTitle("Patrick Redis Client"); //设置窗体标题 frame.setSize(700, 300); //设置窗体大小 frame.setLocation(200, 300); //设置窗体初始位置 frame.setVisible(true); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(false); // frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // System.exit(0); if(jedis!=null){ jedis.close(); } } }); connButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String hostName = hostText.getText(); String portName = portText.getText(); String password = new String(passwordText.getPassword()); if(StringUtils.isBlank(hostName)){ Messages.showMessageDialog("Redis主机-IP不能为空!","Error",Messages.getErrorIcon()); return; } if(StringUtils.isBlank(portName)){ Messages.showMessageDialog("Redis主机-端口不能为空!","Error",Messages.getErrorIcon()); return; } boolean isAuth = false; if(jRadioButtonGroup.getSelection()==jRadioButton1.getModel()){ if(StringUtils.isBlank(password)){ Messages.showMessageDialog("Redis主机- 授权模式-密码不能为空!","Error",Messages.getErrorIcon()); return; } isAuth = true; } jedis = new Jedis(hostName,Integer.parseInt(portName)); if(isAuth){ jedis.auth(password); System.out.println("设置Redis密码成功!"); } System.out.println("连接Redis成功!"); Messages.showMessageDialog("连接Redis成功!","Information",Messages.getInformationIcon()); } }); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { inputRedisValue.setEditable(true); String redisKey = inputRedisKey.getText(); String redisValue = inputRedisValue.getText(); if(StringUtils.isBlank(redisKey)){ Messages.showMessageDialog("Redis-Key不能为空!","Error",Messages.getErrorIcon()); return; } if(StringUtils.isBlank(redisValue)){ Messages.showMessageDialog("Redis-Value不能为空!","Error",Messages.getErrorIcon()); return; } if(jedis==null){ Messages.showMessageDialog("获取Redis链接出现异常!","Error",Messages.getErrorIcon()); return; } jedis.set(redisKey,redisValue); Messages.showMessageDialog("设置Redis键值成功!","Information",Messages.getInformationIcon()); } }); delButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { inputRedisValue.setEditable(false); String redisKey = inputRedisKey.getText(); if(StringUtils.isBlank(redisKey)){ Messages.showMessageDialog("Redis-Key不能为空!","Error",Messages.getErrorIcon()); return; } if(jedis==null){ Messages.showMessageDialog("获取Redis链接出现异常!","Error",Messages.getErrorIcon()); return; } jedis.del(redisKey); Messages.showMessageDialog("删除Redis键值成功!","Information",Messages.getInformationIcon()); } }); queryButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { inputRedisValue.setEditable(false); String redisKey = inputRedisKey.getText(); if(StringUtils.isBlank(redisKey)){ Messages.showMessageDialog("Redis-Key不能为空!","Error",Messages.getErrorIcon()); return; } if(jedis==null){ Messages.showMessageDialog("获取Redis链接出现异常!","Error",Messages.getErrorIcon()); return; } String redisValue = jedis.get(redisKey); jTextArea.setText(redisValue); jTextArea.setEditable(false); } }); } }

Action中启动可视化界面

public class ToolsRedisAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { //启动可视化界面 RedisClientUI redisClientUI = new RedisClientUI(); } }

发布插件

如下图所示,发布本次开发的插件

安装插件

如下图所示,安装本地发布的插件

打开安装后的插件

打开Tools 发现增加了一个选项:ToolsRedisAction 如下图所示 点击ToolsRedisAction弹出如下图所示的可视化界面:

结语

通过自研这套插件,发现开发自己常用的工具会大大提高大家的开发效率,希望借此提升自己的开源插件研发能力,欢迎大家提出意见~~
最新回复(0)