本文主要是介绍idea插件开发的第二天-写一个时间查看器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
Demo说明
本文基于maven项目开发,idea版本为2022.3以上,jdk为1.8
本文在Tools插件之上进行开发
Tools插件说明
Tools插件是一个Idea插件,此插件提供统一Spi规范,极大的降低了idea插件的开发难度,并提供开发者模块,可以极大的为开发者开发此插件提供便利
Tools插件安装需要idea2022.3以上版本
- 插件下载连接:
https://download.csdn.net/download/qq_42413011/89702325
- sdk下载连接:
https://download.csdn.net/download/qq_42413011/89702330
- pojo-serializer插件:
https://gitee.com/myprofile/pojo-serializer
正文
打开上一篇文章项目
调整代码如下
package com.lhstack.aaa;import com.lhstack.tools.plugins.IPlugin;import javax.imageio.ImageIO;
import javax.swing.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;public class PluginImpl implements IPlugin {private final Map<String, JLabel> cache = new HashMap<>();private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactory() {@Overridepublic Thread newThread(Runnable r) {Thread thread = new Thread(r);thread.setName("Demo-Plugin-Scheduled-Thread #" + thread.getId());return thread;}});@Overridepublic JComponent createPanel(String projectHash) {return cache.computeIfAbsent(projectHash, key -> {return new JLabel(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),JLabel.CENTER);});}@Overridepublic void closeProject(String projectHash) {//关闭项目,移除项目对应打开的组件cache.remove(projectHash);}@Overridepublic void unInstall() {//清除缓存cache.clear();//停止线程池scheduledExecutorService.shutdown();}@Overridepublic void install() {//初始化任务调度,更新所有组件时间scheduledExecutorService.schedule(new Runnable() {@Overridepublic void run() {String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));cache.forEach((key, value) -> value.setText(date));scheduledExecutorService.schedule(this, 200, TimeUnit.MILLISECONDS);}}, 200, TimeUnit.MILLISECONDS);}@Overridepublic Icon pluginIcon() {try {return new ImageIcon(ImageIO.read(Objects.requireNonNull(PluginImpl.class.getClassLoader().getResourceAsStream("DEMO-48x48.png"))));} catch (Throwable e) {throw new RuntimeException(e);}}@Overridepublic Icon pluginTabIcon() {try {return new ImageIcon(ImageIO.read(Objects.requireNonNull(PluginImpl.class.getClassLoader().getResourceAsStream("DEMO-16x16.png"))));} catch (Throwable e) {throw new RuntimeException(e);}}@Overridepublic String pluginName() {return "Demo";}@Overridepublic String pluginDesc() {return "这是一个Demo插件描述";}@Overridepublic String pluginVersion() {return "0.0.1";}
}
使用插件开发模块运行,查看效果
jconsole查看线程运行情况
停止运行,查看线程情况
线程释放了
打包安装
多个项目情况
这篇关于idea插件开发的第二天-写一个时间查看器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!