In IntelliJ idea, develop a fishing and reading plug-in

Little brother Fu 2021-11-25 18:24:48
intellij idea develop fishing reading

author : Little brother Fu
Blog :https://bugstack.cn
original text :https://mp.weixin.qq.com/s/R8qvoSNyedVM95Ty8sbhgg

precipitation 、 Share 、 grow up , Let yourself and others have something to gain !

One 、 explain

The direction is wrong , Efforts in vain !

Someone always gets the demand for the product , Just rush to work , Anyway, I'm lazy to think about what will happen in development , How many people use... After online , Let's pile up the code and have a look , Just run anyway , Whether the code or you !

In fact, many times before writing code , Technical research required 、 Architecture design 、 Module layering 、 data structure 、 Detailed analysis 、 Scheme review, etc , And Three seven twenty-one guy By contrast , Seems to be a little slow . But this seemingly slow process , But it can solve many common and troublesome problems in the future , For example, product requirements iteration 、 Business process change 、 Code logic changes 、 Online exception troubleshooting . Although it looks slow , But the process of building a foundation is like laying a foundation , There must always be a stable foundation , To build the whole building . Ten thousand Zhang tall buildings rise from the ground , Don't build a platform in the sand

Two 、 The purpose of the demand

If you need to develop a plug-in with custom functions , Whether it's processing code 、 auxiliary ORM Generate 、 Log information recording, etc , It is necessary to configure the function of a plug-in, initialize and display the corresponding functions to the whole system IDEA In the right or bottom bar of the form , This can meet the basic needs of a plug-in .

So this needs to be done in IDEA forms File -> Settings Expand your own configuration form , And develop what you need ToolWindow Embedded in IDEA in ( left 、 On the right side 、 Lower side ), Here, the development of forms needs to use Swing But at the moment IDEA To develop such a function, you only need to drag and drop the form , It's still pretty easy .

So next, let's take one in IDEA The scene of fishing and reading is a case , Learn the function realization of configuration form and reading form .

3、 ... and 、 Case development

1. Engineering structure

guide-idea-plugin-tool-window
├── .gradle
└── src
├── main
│ └── java
│ └── cn.bugstack.guide.idea.plugin
│ └── factory
│ │ ├── ReadFactory.java
│ │ └── SettingFactory.java
│ └── ui
│ │ ├── ReadUI.java
│ │ ├── ReadUI.form
│ │ ├── SettingUI.java
│ │ └── SettingUI.form
│ └── Config
├── resources
│ └── META-INF
│ └── plugin.xml
├── build.gradle
└── gradle.properties
  • The source code for :# official account :bugstack Wormhole stack reply :idea You can download all IDEA Plug in development source code

This project mainly involves two parts , stay factory One of them is the configuration form 、 One is to read the form , The corresponding two groups UI The implementation of the . Last factory Class implementation will be configured to plugin.xml For use in , At the same time plugin.xml Control form position and icon in .

2. establish UI forms

2.1 How it was created

New -> Swing UI Designer -> GUI Form

  • stay Java The main ways to create forms in are AWT、Swing、JavaFx, because IDEA Use Swing Development , So here we create Swing Form compatibility will be better .
  • So here Swing The creation of forms can be their own handwritten form structure , You can also use visual drag and drop GUI Form If your form is not complex , In fact, the drag and drop method can meet the use .

2.2 Configure page form

public class SettingUI {
private JPanel mainPanel;
private JPanel settingPanel;
private JLabel urlLabel;
private JTextField urlTextField;
private JButton urlBtn;
public SettingUI() {
// Add an event to the button to select a file 
urlBtn.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.showOpenDialog(settingPanel);
File file = fileChooser.getSelectedFile();
urlTextField.setText(file.getPath());
});
}
public JComponent getComponent() {
return mainPanel;
}
public JTextField getUrlTextField() {
return urlTextField;
}
}

  • The configuration page form mainly provides the selection of article path , The tags used here include :JLabel、JTextField、JButton
  • In the use of GUI Form After creating the form , There will be such a visual page , On the right side, you can drag various labels to the middle panel , Set the display name and attribute name on the left .
  • Finally, the code tag code here will be displayed to SettingUI.java in , And the rendered content will be hidden , This method is also more convenient to control the addition of some custom content , For example, events and new forms
  • In addition to SettingUI.java in , You also need to add a button event to the constructor , Used to open the file selector , Put the files we need to open , Set to urlTextField in .

2.3 Read page form

public class ReadUI {
private JPanel mainPanel;
private JTextPane textContent;
public JComponent getComponent() {
return mainPanel;
}
public JTextPane getTextContent() {
return textContent;
}
}

  • Creating a form is the same as configuring a page form , Also by dragging into the panel , Used to display the contents of the path file .
  • You can add some other buttons as appropriate , For example, turn the page and read 、 Scroll bar 、 Word number display, etc .

3. ToolWindow Tool box

In order to realize our own Read the form Put it all over IDEA In the right sidebar , We need to create one that implements ToolWindowFactory The interface of , And configure the implementation class to plugin.xml in

public class ReadFactory implements ToolWindowFactory {
private ReadUI readUI = new ReadUI();
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
// Get an instance of the content factory 
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
// obtain ToolWindow Display content 
Content content = contentFactory.createContent(readUI.getComponent(), "", false);
// Set up ToolWindow Display content 
toolWindow.getContentManager().addContent(content);
// Global use 
Config.readUI = readUI;
}
}
  • Interface method ToolWindowFactory#createToolWindowContent It is a method that needs to be implemented by its own tool box class , In this createToolWindowContent Method ReadUI After instantiation, fill it in .
  • The subsidies for adding forms mainly depend on ContentFactory.SERVICE.getInstance() Create ContentFactory And finally use toolWindow Add form display UI that will do .
  • Here we have added an additional global attribute Config.readUI This is so that you can use this in the configuration form later UI Set the file content .

4. Configurable Configuration box

public class SettingFactory implements SearchableConfigurable {
private SettingUI settingUI = new SettingUI();
@Override
public @NotNull String getId() {
return "test.id";
}
@Override
public @Nls(capitalization = Nls.Capitalization.Title) String getDisplayName() {
return "test-config";
}
@Override
public @Nullable JComponent createComponent() {
return settingUI.getComponent();
}
@Override
public boolean isModified() {
return true;
}
@Override
public void apply() throws ConfigurationException {
String url = settingUI.getUrlTextField().getText();
// Set the text message 
try {
File file = new File(url);
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
randomAccessFile.seek(0);
byte[] bytes = new byte[1024 * 1024];
int readSize = randomAccessFile.read(bytes);
byte[] copy = new byte[readSize];
System.arraycopy(bytes, 0, copy, 0, readSize);
String str = new String(copy, StandardCharsets.UTF_8);
// Set contents 
Config.readUI.getTextContent().setText(str);
} catch (Exception ignore) {
}
}
}
  • Come true from SearchableConfigurable There are many interface methods , Include :getId、getDisplayName、createComponent、isModified、apply These are mainly used to write logic implementation createComponent and apply
  • createComponent The main method is to create our own UI The panel is provided to JComponent
  • apply It's an event , When we click Finish configuration OK、 complete , This method will be triggered when . In this method, we get the file URL Address using RandomAccessFile Read and parse the file , And finally show the file content to the reading form Config.readUI.getTextContent().setText(str);

5. To configure plugin.xml

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<!-- To configure File -> Settings -> Tools -->
<projectConfigurable groupId="tools" displayName="My Test Config" id="test.id"
instance="cn.bugstack.guide.idea.plugin.factory.SettingFactory"/>
<!-- forms (IDEA On the right side of the interface ) -->
<toolWindow id="Read-Book" secondary="false" anchor="right" icon="/icons/logo.png"
factoryClass="cn.bugstack.guide.idea.plugin.factory.ReadFactory"/>
</extensions>
  • This time at plugin.xml The main configuration content in is projectConfigurable and toolWindow, In addition to toolWindow There is also a icon Of logo, After the configuration is completed, it can be done in IDEA The page shows our own added form .

Four 、 Plug in testing

  • adopt Plugin Start the plug-in , A new one will be opened at this time IDEA forms , In this new form, you can see the functions we added .

Profile path

  • Click the select button , Choose your file location , Select and click OK

View the presentation file

  • After confirming the file path , You can see the contents of your own documents in the right column . Is it expanding , It's suitable for you to fish !?

5、 ... and 、 summary

  • Learn custom development UI, hold UI Fill to where you need to place IDEA Form position , And add functional process steps in the form , In fact, it mainly includes three aspects :Swing UI、Factory Implementation class 、plugin To configure .
  • stay plugin Configuration in progress , It mainly includes forms ID、 Location 、icon Icon 、 The corresponding implementation class , If you don't add these, you can't display the form information normally .
  • In addition, it can be based on this case , Add the functions you want to complete , For example, make the function of fishing and reading more perfect , Can support different types of files , It could even be PDF The reading of , And the books you want to read .

6、 ... and 、 Series recommendation

版权声明
本文为[Little brother Fu]所创,转载请带上原文链接,感谢
https://javamana.com/2021/11/20211109101113028M.html

  1. Java经典面试题详解,突围金九银十面试季(附详细答案,mysql集群架构部署方案
  2. java整理,java高级特性编程及实战第一章
  3. java教程——反射,mongodb下载教程
  4. Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day12,zookeeper原理作用
  5. Java后端互联网500道中高级面试题(含答案),linux钩子技术
  6. java8 Stream API及常用方法,java初级程序员面试
  7. java-集合-Map(双列)——迪迦重制版,2021Java开发社招面试解答之性能优化
  8. Flink处理函数实战之二:ProcessFunction类,java线程面试题目
  9. flex 布局详解,【Java面试题
  10. Linux basic command learning
  11. Why did docker lose to kubernetes? Docker employee readme!
  12. MySQL安装
  13. Elastic Search Aggregate Learning five: Problem Analysis of Uncertainty of sequencing results, Alibaba Java Performance Tuning Practical
  14. Installing, configuring, starting and accessing rabbitmq under Linux
  15. Oracle SQL injection summary
  16. Installation MySQL
  17. L'exposition à la photo d'essai sur la route i7 du nouveau vaisseau amiral de BMW Pure Electric a également été comparée à celle de Xiaopeng p7.
  18. spring JTA 关于异常处理的时机问题
  19. Le problème du temps de traitement des exceptions dans la JTA printanière
  20. Flink Handling Function Real War II: processfunction class, Java thread interview subject
  21. Oracle SQL injection summary
  22. [Java data structure] you must master the classic example of linked list interview (with super detailed illustration and code)
  23. Do you really know MySQL order by
  24. Record a java reference passing problem
  25. spring JTA 關於异常處理的時機問題
  26. Java - Set - Map (double file) - dija Rewriting, 2021 Java Developer's Performance Optimization
  27. Android入门教程 | OkHttp + Retrofit 取消请求的方法
  28. Java 8 Stream API and common methods, Java Junior Program interview
  29. Github 疯传!史上最强!BAT 大佬,2021年最新Java大厂面试笔试题分享
  30. git(3)Git 分支,zookeeper下载教程
  31. Java Backend Internet 500 questions d'entrevue moyennes et avancées (y compris les réponses), technologie de crochet Linux
  32. Entretien d'entretien d'usine Java post sprint de 100 jours - accumulation de jours et de mois, trois questions par jour [jour 12, fonction de principe de Zookeeper
  33. Tutoriel Java - reflection, tutoriel de téléchargement mongodb
  34. How to analyze several common key and hot issues in redis from multiple dimensions
  35. GIT (3) GIT Branch, Zookeeper Download tutoriel
  36. Tutoriel de démarrage Android | okhttp + Retrofit comment annuler une demande
  37. Design pattern [3.3] - Interpretation of cglib dynamic agent source code
  38. Share the actual operation of private collection project nodejs backend + Vue + Mysql to build a management system
  39. Springboot has 44 application initiators
  40. GitHub上霸榜久居不下的《Java面试突击宝典》,java图形用户界面设计基础
  41. GitHub上访问下载破百万的神仙文档《Java面试神技》看完我呆了,java面试问项目中遇到的问题
  42. GitHub上标星75k 超牛的《Java面试突击版,java高级工程师技能
  43. GitHub上标星2,java项目开发实训教程
  44. Docker development environment Preview
  45. JavaScript高級深入淺出:掌握 this 指向
  46. JavaScript Advanced Insight and outside: Mastering this direction
  47. Vue de l'application pratique de Javascript, drop drag Event
  48. docker 安装部署 Jenkins 2.322
  49. kafka安装
  50. 近九万字图文详解RabbitMQ
  51. Engaged in Java for one and a half years, how to break through yourself
  52. 输出9*9乘法表----java
  53. 判断一个数是不是素数-------java
  54. java项目,记录页面修改值,内部打“官司”用
  55. Docker installation Deployment Jenkins 2.322
  56. Comment porter un pantalon en hiver? Les petits hommes, les jambes épaisses et la largeur de l'entrejambe peuvent être vus. 3 techniques pour éviter la foudre
  57. MySQL下载和安装教程
  58. In depth analysis of rocketmq source code - message storage module
  59. Spring transaction management
  60. mysql恢复ibd数据,为何频频报错?