Although today's IDEs are as powerful as "fighting beasts", you should know that no matter how powerful an IDE is, it cannot provide all the functions users want, so IDEs generally provide API interfaces for developers to expand on their own. Let’s take the plug-in development under Intellij IDEA 12 as an example to see how to further enhance the IDE to meet the needs of developers.
1. Create a Plugin project
If there is no optional SDK in the Module SDK, then click New to add a new SDK, and select the installation location of Intellij in the directory.
The structure of the created Plugin project is very simple, but there is an additional plugin.xml configuration file under META-INF, which will be introduced later on.
2. Let the plugin Say Hello
2.1 Add Component
In the src directory, you can see that there are three components listed in the New dialog box, corresponding to three levels: Application, Project, and Module Component. Here we select Application Component as an example, enter a name such as MyComponent in the pop-up box, and then a component will be created.
Then add a SayHello method to MyComponent. Other methods are not implemented for the time being. 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: 10:08 am */public class MyComponent implements ApplicationComponent { public MyComponent() { } public void initComponent() { // TODO: insert component initialization logic here } public void disposeComponent() { // TODO: insert component disposal logic here } @NotNull public String getComponentName() { return "MyComponent"; } public void saysHello() { // 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 to the plug-in through menus or other methods.
Action's main job is 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: 10:16 am */public class SayHelloAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { Application application = ApplicationManager.getApplication(); MyComponent myComponent = application.getComponent(MyComponent.class); myComponent.sayHello(); }}2.3 Configuration File
In fact, while creating the Component and Action in the first two steps, IDEA helps us automatically register them in META-INF/plugin.xml.
The Application Component and Action we just added will be under the <application-components> node, and the 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="[email protected]" 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" 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 and debug
Open the Run/Debug configuration dialog box, add a Plugin type, and use classpath of module to select the sample project just now.
When running, you will find that a new Intellij IDEA instance will be started. After going through the startup configuration process again, you can see that the name of the plugin is the value in <name> in plugin.xml. We can only select the plugin we just developed and ignore the others. Now we can trigger our plug-in through Window->Say Hello!, and the effect is that a dialog box will pop up.
Interestingly, some other descriptions in plugin.xml will be displayed to the user when the plugin crashes, reporting the problem to the plugin author.
4. Plug-in Configuration Panel
Many plug-ins have configuration pages in Settings. Now let me briefly introduce how to add a configuration page to our plug-in.
First, let’s modify the MyComponent class. The main change is to implement an additional Configurable interface. There is a createComponent method in this interface. This method returns the Swing JComponent object and will be displayed in Settings. In addition, it is quite convenient to use the Swing Designer designer provided by IDEA. In order to avoid being modified, the automatically generated style and layout code will not be seen by us (unlike NetBeans), so the final code is very concise.
This is the final effect. The panel we designed in the designer is embedded on the right.
5. Plugin with dialog box
A common plug-in is to click on the corresponding menu item of the plug-in and a dialog box pops up (such as searching for classes in the workspace, confirming code before submitting SVN, etc.). In fact, it is very simple. The implementation method is to create a Dialog first, then design the control layout in the Dialog in the Swing designer, and finally display the dialog box in the Action. I won't list the specific codes, please ask me for any needs.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.