When I was young and ignorant, I chose my major that summer. My parents listened to other elders and said that it was good to choose a computer major. From then on, I have a deep brand of hospital planning. From dormitory to computer room, from computer room to library, C, C++, Java… As long as I was writing something I was interested in, I would sit for hours, but I was young, so I got up to pick up and jogged for the goddess.

Now I work, I do the development work unexpectedly, write code busy will forget to get up to exercise, often by the end of the busy feel very uncomfortable waist and legs. It was not until this year’s physical examination report that I realized that without a good body, I could not work well and fulfill my dream.

So how does development prevent “addiction”? Here’s how I see it most often at work:

1. Set the alarm clock

2. Drink regular glasses

3. Timer wake up bracelet

All of the above are periodic reminders, but is there a solution from the source (IDE)? For example, give your work environment (IDE) a break?

Recently, I found an interesting project StopCoding in IDEA plug-in. The IDEA is very simple. Today, I will introduce the operation of IDEA plug-in development with the help of this project and relevant development experience of EOS plug-in.

In this paper, IDEA plug-in development and StopCoding implementation will be introduced from the following aspects:

  • IntelliJ IDEA and IntelliJ Platform
  • Development environment setup
  • Development of a simple plug-in
  • StopCoding principle and source code analysis

<1> intelliJDEA and intelliJ Platform

IntelliJ IDEA, short for IDEA, is a JAVA development tool developed by Jetbrains. It supports the development of JAVA, Scala, Groovy and other languages, and supports the current mainstream technologies and frameworks. Good at enterprise application, mobile application and Web application development, providing rich functions, intelligent code assistant, code automatic prompt, reconstruction, J2EE support, various version tools (Git, SVN, etc.), JUnit, CVS integration, code analysis, innovative GUI design, etc.

The IntelliJ Platform is an open source Platform for building ides, including IntelliJ IDEA, WebStorm, DataGrip, and Android Studio. The IDEA plug-in is also developed based on the IntelliJ Platform.

<2> Setup of development environment

IntelliJ Platform SDK

Plugins.jetbrains.com/docs/intell…

Development tools using Intellij IDEA, download address:

www.jetbrains.com/idea/

IDEA is divided into two versions:

  • ** Community: ** Is completely free and open-source, but lacks some of the advanced features of the flagship version.
  • ** Free for 30 days, supports all features, code is not open source.

It is recommended to use the community edition for developing plug-ins for IDEA because the community edition is open source and allows debugging of the source code while developing the plug-in.

<3> Develop a simple plug-in

The official documentation guide gives two development approaches:

  • Gradle
  • DevKit

1. Create a project in Gradle mode

Create the following project directory:

1 my_gradle_plugin 2 ├ ─ ─ build. Gradle 3 ├ ─ ─ gradle 4 │ └ ─ ─ wrapper 5 │ ├ ─ ─ gradle - wrapper. Jar 6 │ └ ─ ─ Gradle - wrapper. The properties 7 ├ ─ ─ gradlew 8 ├ ─ ─ gradlew. Bat 9 ├ ─ ─ Settings. Gradle 10 └ ─ ─ the SRC 11 ├ ─ ─ the main 12 │ ├ ─ ─ Java 13 │ └ ─ ─ Resources 14 │ └ ─ ─ meta-inf 15 │ └ ─ ─ plugin. The XML 16 └ ─ ─ the test 17 ├ ─ ─ Java 18 └ ─ ─ the resourcesCopy the code

The root directory build.gradle is configured as follows:

1 plugins {2 id 'Java' 3 id 'org.jetbrains. Intellij 'version '0.6.5' 4} 5 6group 'com.your.company' 7version '1.0' 8sourceCompatibility = 1.8 10 10repositories {11 mavenCentral() 12} 13dependencies {14 testImplementation group: 'junit', name: 'junit', version: '4.12' 16 17 18 intellij / / See https://github.com/JetBrains/gradle-intellij-plugin/ 15} {version 19 20} '2020.1' 21patchPluginXml { 22 changeNotes """ 23 Add change notes here.<br/> 24 <em>most HTML tags may be usedem>""" 25}Copy the code

Gradle Running:

It is recommended to use Gradle mode to develop plug-ins. Gradle mode has low requirements on the local environment and does not need to configure complex SDK and other related content.

2.DevKit development mode

To enable the Plugin DevKit

Plugin DevKit is a Plugin for IntelliJ that uses IntelliJ IDEA’s own build system to support the development of IDEA plug-ins. Plugin DevKit needs to be installed and enabled before developing the IDEA plug-in.

Open the IDEA, navigate to the Settings | Plugins, if no Plugins Plugin list DevKit, click Install JetBrains Plugin, search for and Install.

Configure the IntelliJ Platform Plugin SDK

  • Navigate to the File | Project Structure, Platform selection dialog box on the left side of the column under the Settings of SDKs
  • Click + button, select JDK first, specify JDK path; Then create the IntelliJ Platform Plugin SDK and specify the home path as the installation path of IDEA, as shown in the figure:

This example uses the official action to add a simple action as an example, the original address is as follows:

Plugins.jetbrains.com/docs/intell…

1. Define actions

Define a Java class that inherits the AnAction class and override the actionPerformed method, as in:

1 public class PopupDialogAction extends AnAction { 2 @Override 3 public void actionPerformed(@NotNull AnActionEvent event) { 4// Using the event, create and show a dialog 5 Project currentProject = event.getProject(); 6 StringBuffer dlgMsg = new StringBuffer(event.getPresentation().getText() + " Selected!" ); 7 String dlgTitle = event.getPresentation().getDescription(); 8 // If an element is selected in the editor, add info about it. 9 Navigatable nav = event.getData**(**CommonDataKeys.NAVIGATABLE); 10 if (nav ! = null) { 11 dlgMsg.append(String.format("nSelected Element: %s", nav.toString())); 12 } 13 Messages.showMessageDialog(currentProject, dlgMsg.toString(), dlgTitle, Messages.getInformationIcon()); 14}} 15Copy the code

2. Register the Action

Register within the elements of the plugin.xml file.

 1 <action< span=""> id="org.intellij.sdk.action.PopupDialogAction" class="org.intellij.sdk.action.PopupDialogAction"
 2    text="Action Basics Plugin: Pop Dialog Action" description="SDK action example" icon="SdkIcons.Sdk_default_icon">
 3
 4   <override-< span="">text place="MainMenu" text="Pop Dialog Action"/>
 5
 6   <keyboard-shortcut< span=""> first-keystroke="control alt A" second-keystroke="C" keymap="$default"/>
 7
 8   <mouse-shortcut< span=""> keystroke="control button3 doubleClick" keymap="$default"/>
 9
10   <add-< span="">to-group group-id="ToolsMenu" anchor="first"/>
11
12  </action>
Copy the code

The above example defines a “Pop Dialog Action” menu that is added to the first location (Anchor = “first”) of the IDEA Tools menu, clicking on which will Pop up an “Action Basics Plugin: Pop Dialog Action Selected!” Item, as shown in the figure:

Click on the “Pop Dialog Action” item and a Dialog box prompts you to enter a name.

<4>StopCoding principle and source code analysis

Open source address:

Github.com/jogeen/Stop…

Step1. Then go to the menu bar tools->StopCoding.

Step2. Set parameters suitable for you and save.

Step3. Happy Coding, no longer need to worry about their “addiction”. When the working time is over, the next box will pop up to remind you. ** Of course, this box cannot be turned off. ** It will turn off automatically only if you rest for enough time.

Engineering structure analysis:

1. 2 ├ ─ ─ image 3 │ ├ ─ ─ step1. PNG 4 │ ├ ─ ─ step2. PNG 5 │ ├ ─ ─ step3. PNG 6 │ └ ─ ─ step. GIF 7 ├ ─ ─ LICENSE 8 ├ ─ ─ the readme. Md 9 ├ ─ ─ 11 │ readme_ZH. Md 10 ├ ─ ─ resources ├ ─ ─ img 12 │ │ └ ─ ─ stop. The PNG 13 │ └ ─ ─ 14 │ meta-inf ├ ─ ─ pluginIcon_dark. SVG 15 │ ├ ─ ─ PluginIcon. SVG 16 │ └ ─ ─ the plugin. The XML 17 ├ ─ ─ the SRC 18 │ └ ─ ─ icu 19 │ └ ─ ─ jogeen 20 │ └ ─ ─ stopcoding 21 │ ├ ─ ─ the data of 22 │ │ ├ ─ ─ DataCenter. Java 23 │ │ └ ─ ─ SettingData. Java 24 │ ├ ─ ─ service 25 │ │ └ ─ ─ TimerService. Java 26 │ ├ ─ ─ StopCodingSettingAction. Java 27 │ ├ ─ ─ task 28 │ │ ├ ─ ─ RestTask. Java 29 │ │ └ ─ ─ WorkTask. Java 30 │ └ ─ ─ the UI 31 │ ├ ─ ─ SettingDialog. Form 32 │ ├ ─ ─ SettingDialog. Java 33 │ ├ ─ ─ TipsDialog. Form 34 │ └ ─ ─ TipsDialog. Java 35 └ ─ ─ StopCoding. On imlCopy the code

As you can see in the figure above, the engineering structure is very simple and is developed based on DevKit mode.

  • The data package

SettingData, which corresponds to model. Datacenters, as run-time data centers, are static global variables.

  • service

TimerService the core code for timing calculations.

  • task

RestTask Indicates a scheduled task at rest. WorkTask Indicates a scheduled task during work.

  • ui

SettingDialog Dialog box for setting information. TipsDialog Is a dialog box that is reminded to rest. StopCodingSettingAction The action that starts the entry.

  • plugin.xml

This is the core configuration file of the plug-in project, which has added detailed notes for each item. If you have any questions, you can leave a message on the background of the public account, and we will discuss it together.

1 <idea-plugin> 2 <! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -- Plugin name, the name that others use when searching for your plugin in the official plugin library --> 6 7 <name>StopCoding</name> 8 <! 10 <version>1.2.1</version> 11 <! -- Vendor home page and email (cannot use default values, Must be modified to own) -- > 12 13 < vendor email = "[email protected]" url = "https://github.com/jogeen/StopCoding" > jogeen < / vendor > 14 15 <! -- The description of the plug-in (cannot use the default value, must be changed to its own. And need more than 40 characters) --> 16 17 <description><! [CDATA[ 18 <p>This is a work timer.It can set every working period to remind you that it's time to have a rest, drink some water and exercise your body. 19 Only in this way can you really work healthily</p> 20 <ol> 21 <li>In the tools menu bar, open stopcoding.</li> 22 <li>Set working hours and rest time, and save them.</li> 23 <li>When the set time comes, there will be a pop-up box to remind you to rest, </li> 24 </ OL > 25 <hr> 26 <p> If you too often get hooked on writing code and forget to take a water break, try this plugin <li> In the menu bar of Tools, open StopCoding plug-in to set </li> 29 <li> set working time and rest time, and save </li> 30 <li> when the set time is up, there will be a pop-up box to remind you to rest, so that you can not temporarily operate idea</li> 31 32 < / ol > < p > address: < a href = "https://github.com/jogeen/StopCoding" > https://github.com/jogeen/StopCoding < / p > 33 ]]></description> 34 35 <! -- Plugin version change information, support HTML tags; 36 will show in the Settings | Plugins dialog and plug-in warehouse Web page - > 37 < change - notes > <! [CDATA[38 <ul> 39 <li>V1.2 add icon(Thanks for the icon provided by my good friend Hu Wei).</li> 40 <li>V1.1 update Guide to use.</li> 41 < Li >V1.0 release.</li> 42 </ul> 43]]> 44 </change-notes> 45 46 <! -- Plugin compatible with IDEAbuild number (minimum version number)--> 47 <idea-version since-build="173.0"/> 48 <! - rely on other plugin id - > 49 < depends > com. Intellij. Modules. Lang < / depends > 50 51 52 < extensions defaultExtensionNs="com.intellij"> 53 <! -- Declare extensions to IDEA Core or other plug-ins --> 54 55 </ Extensions > 56 57 <! 58 < actions - write plug-ins action -- > > < action of 59 id = "StopCoding_setting_id" class = "icu. Jogeen. Stopcoding. StopCodingSettingAction" text="StopCoding" 60 description="setting"> 61 <add-to-group group-id="ToolsMenu" anchor="first"/> 62 <keyboard-shortcut  keymap="$default" first-keystroke="ctrl S" second-keystroke="C"/> 63 </action> 64 </actions> 65 66</idea-plugin>Copy the code

As you can see from public.xml, this plug-in is relatively simple, with only one StopCoding_setting_id configuration item, as well as the entry class

icu.jogeen.stopcoding.StopCodingSettingAction

1 public class StopCodingSettingAction extends AnAction { 2 3 @Override 4 public void actionPerformed(AnActionEvent e) {  5 SettingDialog settingDialog = new SettingDialog(); 6 settingDialog.setVisible(true); 8 7}}Copy the code

The source code just shows the SettingDialog, based on Swing to draw a visual interface.

icu.jogeen.stopcoding.ui.SettingDialog

1/2 / bind sure button events buttonOK. AddActionListener (new ActionListener () {3 public void actionPerformed (an ActionEvent e) {4 onOK(); 5}}); 7 8 buttonCancel / / bind cancel button event. AddActionListener (new ActionListener () {9 public void actionPerformed (an ActionEvent e) {10 onCancel(); 11}}); 14 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 15 addWindowListener(new WindowAdapter() { 16 public void windowClosing(WindowEvent e) { 17 onCancel(); 18}}); 20 contentPane.registerKeyboardAction(new ActionListener() { 21 public void actionPerformed(ActionEvent e) { 22 onCancel(); 23 } 24 }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 25 / / binding enabled option buttons only 26 openRbtn while addChangeListener (new ChangeListener () {27 @ Override 28 public void stateChanged(ChangeEvent e) { 29 openRbtn.setText(openRbtn.isSelected() ? "Running" : "Stopped"); 30}} 31);Copy the code

Events are listened on in the SettingDialog, with the main idea being schedule to add a scheduled task and cancel to cancel the task to stop the timer. Then a dialog pops up to prevent continuous coding.

< 5 > finally

For most programmers, life is not so much poetry and distance, but mostly overtime, code and bug fixes. I believe that with these basic introduction, interested partners to look at the source code, and try to write a small plug-in is not too big a problem, to know that programmers not only in front of the logic and code, but also should be healthy and energetic. Finally, I hope this article can help you as a programmer, and I hope everyone can be the best version of themselves.

Recommended reading

  • The Road to Technical Leadership: How to transition from developer to team leader

  • Top 9 Java Frameworks for 2020

Welcome to [JINGdong Technology] to learn about the developer community

More wonderful technical practice and exclusive dry goods analysis

Welcome to “JINGdong Technology Developer” public account