Intellij IDEA plug-in development

Source: Internet
Author: User

Intellij IDEA plug-in development

Although today's IDE is as powerful as a "Storm", you must know that powerful IDE cannot provide all the functions that users want, therefore, IDE generally provides APIs for developers to expand on their own. The following uses plug-in development under Intellij IDEA 12 as an example to describe how to further enhance IDE To meet developers' needs.

1. Create a Plugin Project

If no SDK is available in the Module SDK, click New to add a New SDK, and select the Intellij idea installation path.


The structure of the created plug-in project is very simple, but a plug-in. xml configuration file is added under the META-INF, which will be described later.

2. Let the plug-in Say hello

2.1 Add Component

In the src directory, Alt + Insert, three components are listed in the New dialog box, which correspond to three levels: Application, Project, and Module Component. Here, we select Application Component as the instance, and enter a name in the pop-up box, for example, MyComponent, so that a Component is created.


Then add a SayHello method to MyComponent. Other methods are not implemented at the moment. The source code is as follows:

Package com. cdai. plugin. rapidg; import com. intellij. openapi. components. applicationComponent; import com. intellij. openapi. ui. messages; import org. jetbrains. annotations. notNull;/*** My Component * User: cdai * Date: 13-11-4 * Time: */public class MyComponent implements ApplicationComponent {public MyComponent () {} public void initComponent () {// TODO: insert component initialization logic he Re} public void disposeComponent () {// TODO: insert component disposal logic here} @ NotNull public String getComponentName () {return "MyComponent";} public void sayHello () {// Show dialog with message Messages. showMessageDialog ("Hello World! "," Sample ", Messages. getInformationIcon ());}}

2.2 Add Action

Now you need to add an Action so that users who use our plug-in can click the plug-in the menu or other ways.


Action is mainly used to create an Application and MyComponent object. The Code is as follows:

Package com. cdai. plugin. rapidg; import com. intellij. openapi. actionSystem. anAction; import com. intellij. openapi. actionSystem. anActionEvent; import com. intellij. openapi. application. application; import com. intellij. openapi. application. applicationManager;/*** Say Hello Action * User: cdai * Date: 13-11-4 * Time: am */public class SayHelloAction extends AnAction {@ Override public void actionreceivmed (AnActionEvent e) {Application application = ApplicationManager. getApplication (); MyComponent myComponent = application. getComponent (MyComponent. class); myComponent. sayHello ();}}

2.3 configuration file

In fact, the previous two steps to create Component and Action at the same time, IDEA is helping us automatically register them into the META-INF/plugin. xml.

The Application Component and Action we just added will be under the <application-components> node, and plugin. xml will eventually look like the following:

<idea-plugin version="2"> <id>com.cdai.plugin.rapidg</id> <name>CDai's Rapid Generator Plugin</name> <version>1.0</version> <vendor email="dc_726@163.com" url="http://www.yourcompany.com">CDai</vendor>  <description><![CDATA[   Enter short description for your plugin here.<br>   <small>most HTML tags may be used</small>   ]]></description>  <change-notes><![CDATA[   Add change notes here.<br>   <small>most HTML tags may be used</small>   ]]> </change-notes> <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description --> <idea-version since-build="107.105"/> <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products    on how to target different products --> <!-- uncomment to enable plugin in all products <depends>com.intellij.modules.lang</depends> --> <application-components>  <!-- Add your application components here -->   <component>     <implementation-class>com.cdai.plugin.rapidg.MyComponent</implementation-class>   </component> </application-components> <project-components>  <!-- Add your project components here --> </project-components> <actions>  <!-- Add your actions here -->   <action id="SayHello" class="com.cdai.plugin.rapidg.SayHelloAction" text="Say Hello!">     <add-to-group group-id="WindowMenu" anchor="first"/>   </action> </actions> <extensions defaultExtensionNs="com.intellij">  <!-- Add your extensions here --> </extensions></idea-plugin>

3. Run debugging

Open the Run/Debug configuration dialog box and add a new plug-in type. Use classpath of module to select the sample project.


After running, you will find that a new Intellij IDEA instance is started and the configuration process is re-started. You can see that the plug-in name is the value in <name> In plugin. xml. We can only select the plug-in we just developed and ignore other ones. Now we can use Window-> Say Hello! This will trigger our plug-in. The effect is that a dialog box will pop up.


Interestingly, some other descriptions in plugin. xml are displayed to the user when the plug-in crashes and the problem is reported to the plug-in author.


4. Plug-in configuration panel

Many plug-ins have configuration pages in Settings. Now we will briefly introduce how to add a configuration page for our plug-ins.

First, modify the MyComponent class. The main change is that one more retriable interface is implemented. This interface has a createComponent method. The JComponent object returned by this method is displayed in Settings. In addition, the Swing Designer provided by IDEA is quite convenient. The automatically generated style and layout code will not be seen (different from NetBeans) to avoid being modified ), therefore, the final code is concise.


This is the final result. The panel we designed in the designer is embedded on the right.


5. Plug-ins with dialog boxes

A common plug-in is to click the corresponding menu item of the plug-in, and a dialog box pops up (such as searching for classes in the workspace, checking the code before submitting SVN, and so on ). In fact, it is very simple. The implementation method is to first create a Dialog, then design the layout of controls in the Dialog in the Swing designer, and finally display the Dialog box in the Action. The specific code is not listed. You can ask for it if you need it.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.