Author: small Fu Ge blog: bugstack. Cn, mp.weixin.qq.com/s/R8qvoSNye…
Precipitation, share, grow, let yourself and others can gain something! ๐
A, description,
Wrong direction, efforts in vain!
There are always people who get the demand of the product, and they are anxious to do it. Anyway, they are lazy about what will happen in the development. How many people will use it after the launch?
In fact, most of the time before writing code, the need to do technical research, architecture design, module layerization, data structure, detailed analysis, project review, etc., compared with the 37 21 guy, seems to be a little slow. However, this seemingly slow process can solve many common and troublesome problems in the future, such as product requirements iteration, business process change, code logic change, online exception detection. It may seem slow, but this process is like laying a foundation. You need a stable foundation to build the whole building. Great oaks from little acorns grow not on floating sand
Second, demand and purpose
If you need to develop a custom plug-in function, whether it is a processing code, auxiliary ORM generated, log information records, etc., the functional configuration will require a plug-in initialization and display the corresponding function to the whole IDEA form the bar on the right or bottom bar, so as to meet the basic requirements of a plug-in.
You will need to extend the configuration window in the IDEA form File -> Settings and develop the ToolWindow you need to embed it in the IDEA form (left, right, bottom). The form development here needs to use Swing, but currently developing such functionality in IDEA only requires dragging and dropping forms, which is pretty easy.
Then we take a scene of reading in IDEA as a case to learn the function realization of configuring forms and reading forms.
Case development
1. Engineering structure
Guide - idea - the plugin - tool - window โ โ โ the gradle โ โ โ the SRC โ โ โ the main โ โ โ โ Java โ โ โ โ cn. Bugstack. Guide. Idea. The plugin โ โ โ โ the factory โ โ โ โ โ ReadFactory. Java โ โ โ โ โ SettingFactory. Java โ โ โ โ the UI โ โ โ โ โ ReadUI. Java โ โ โ โ โ ReadUI. Form โ โ โ โ โ โโ Java โ โโ โโ download.java โ โโ download.java โ โโ download.java โ โโ download.java โ โโ download.java โ โโ download.java โ โโ download.java gradle.propertiesCopy the code
- The source code for# public account:
Bugstack wormhole stack
Reply:idea
You can download all IDEA plug-in development source code
This project mainly involves two parts, in factory, one is to configure the form, one is to read the form, and the corresponding two sets of UI implementation. The final implementation of the Factory class is configured to be used in plugin.xml, where it controls form positions and ICONS.
2. Create a UI form
2.1 Creation Mode
New -> Swing UI Designer -> GUI Form
- The main ways to create forms in Java are AWT, Swing, and JavaFx. Since IDEA is developed in Swing, it is more compatible to create Swing forms here.
- Swing forms can be created using hand-written Form structures, or they can be created using visual drag and drop GUI forms. If your Form is not complex, drag and drop will work just fine.
2.2 Configuration page form
public class SettingUI {
private JPanel mainPanel;
private JPanel settingPanel;
private JLabel urlLabel;
private JTextField urlTextField;
private JButton urlBtn;
public SettingUI(a) {
// Add a file selection event to the button
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(a) {
return mainPanel;
}
public JTextField getUrlTextField(a) {
returnurlTextField; }}Copy the code
- The configuration page window mainly provides the choice of the article path. The labels used here include JLabel, JTextField and JButton
- After you have created the Form using the GUI Form, you will see a visual page where you can drag various labels into the middle panel on the right and set display and property names on the left.
- And eventually the code tag here the code will show up
SettingUI.java
Render content is hidden, making it easier to control the addition of custom content such as events and new forms - In the other
SettingUI.java
We also need to add a button event in the constructor to open the file selector and set the file we want to open tourlTextField
In the.
2.3 Read the page form
public class ReadUI {
private JPanel mainPanel;
private JTextPane textContent;
public JComponent getComponent(a) {
return mainPanel;
}
public JTextPane getTextContent(a) {
returntextContent; }}Copy the code
- In the form creation and configuration page the form is the same, also by dragging and dropping to the panel, used to display the path file content.
- You can add other buttons, such as page turn, scroll bar, word display, etc.
3. ToolWindow tool frame
To put our own implementation of the reading form in the right sidebar of the entire IDEA, we need to create an interface that implements ToolWindowFactory and configure the implementation class into plugin.xml
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();
// Get what is displayed in ToolWindow
Content content = contentFactory.createContent(readUI.getComponent(), "".false);
// Set what ToolWindow displays
toolWindow.getContentManager().addContent(content);
// global useConfig.readUI = readUI; }}Copy the code
- Interface methods
ToolWindowFactory#createToolWindowContent
Is a method that needs to be implemented by its own toolbox class, in thiscreateToolWindowContent
Method into your own formReadUI
Instantiate it and fill it in. - The subsidy for adding forms mainly depends on
ContentFactory.SERVICE.getInstance()
Create a ContentFactory and finally use toolWindow to add a form to display the UI. - Here we have added an additional global attribute
Config.readUI
This is so that you can later use this UI to set the file contents in the configuration form.
4. The works frame is configured with different signals
public class SettingFactory implements SearchableConfigurable {
private SettingUI settingUI = new SettingUI();
@Override
public @NotNull String getId(a) {
return "test.id";
}
@Override
public @Nls(capitalization = Nls.Capitalization.Title) String getDisplayName(a) {
return "test-config";
}
@Override
public @Nullable JComponent createComponent(a) {
return settingUI.getComponent();
}
@Override
public boolean isModified(a) {
return true;
}
@Override
public void apply(a) 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 the content
Config.readUI.getTextContent().setText(str);
} catch (Exception ignore) {
}
}
}
Copy the code
- Methods implementing SearchableConfigurable interface include: getId, getDisplayName,
createComponent
, isModified,apply
These are mainly used to write logical implementationscreateComponent
ๅapply
- The createComponent method primarily provides our own UI panel to the JComponent
- Apply is an event that is triggered when we click OK to complete the configuration, done. In this method we get the URL of the file to use
RandomAccessFile
Read and parse the file, and finally display the contents of the file to the reading formConfig.readUI.getTextContent().setText(str);
5. To configure the plugin. XML
<extensions defaultExtensionNs="com.intellij"> <! -- Add your extensions here --> <! -- Configuration File -> Settings -> Tools --> < projectProjectGroupid ="tools" displayName="My Test Config" id="test.id"
instance="cn.bugstack.guide.idea.plugin.factory.SettingFactory"/ > <! -- form (right side of IDEA interface) --> <toolWindow ID ="Read-Book" secondary="false" anchor="right" icon="/icons/logo.png"
factoryClass="cn.bugstack.guide.idea.plugin.factory.ReadFactory"/>
</extensions>
Copy the code
- In the plugin.xml, the configuration of projectWindows and toolWindow is different, and an icon logo is added to the toolWindow. After the configuration is complete, we can display our own added forms on the IDEA page.
Iv. Plug-in testing
- Launching the Plugin via Plugin opens a new IDEA form in which you can see the functionality we added.
Configuration file path
- Click the Select button, select your file location, and then click OK
View the presentation file
- After confirming the file path, you can see your file display in the right pane.Is it expanding some, suitable for you to touch fish! ?
Five, the summary
- Learn to customize the development UI, fill the UI into the position of the IDEA window to be placed, and add functions in the window of the process steps, in fact, mainly includes three aspects: Swing UI, Factory implementation class, plugin configuration.
- Plugin configuration mainly includes window ID, location, icon icon and corresponding implementation class. If these are not added, window information cannot be displayed normally.
- In addition, based on this case, you can add the functions you want to complete, such as making the function of reading a fish more perfect, supporting different types of documents, even PDF reading, and the books you want to read.
Six, series recommendation
- How to develop IDEA plug-in?
- How do I publish jars to the Maven central repository
- 12 vo2Dto methods on beanutils.copyProperties pressure test most pull crotch!
- Fry! 1024, xiaofuge’s blog has been upgraded, the article is open source, support PR, wow!
- ยท What Do Interviewers Ask me?