环境搭建完了,接下来可以开始写代码了:

1、新建 plugin 项目

2、编辑 plugin.xml,修改一下里面的插件名那些信息,该文件的配置项可以看这里:plugin.xml

其中比较关键的有一个是 <depends>com.intellij.modules.platform</depends>,如果不加这个,你点击运行的时候插件可以正常运作,但是打包之后安装却会用不了。

<idea-plugin>
<id>com.eleven24.helloWorld</id>
<name>HelloWorld</name>
<version>1.0</version>
<vendor email="rubymay21s@gmail.com" url="http://www.cnblogs.com/eleven24">YourCompany</vendor> <description><![CDATA[
Hello World Plugin.
]]></description> <change-notes><![CDATA[
first version.
]]>
</change-notes> <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="145.0"/> <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
<depends>com.intellij.modules.platform</depends> <extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions> <actions>
<!-- Add your actions here -->
</actions> </idea-plugin>

 基本的信息如上,下面会添加一些菜单项的配置以及注册插件的配置

3、初始化的方法

package com.eleven24;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.components.ApplicationComponent;
import org.jetbrains.annotations.NotNull; public class HelloWorldRegistration implements ApplicationComponent {
@NotNull
public String getComponentName() {
return "HelloWorldPlugin";
} public void initComponent() {
ActionManager am = ActionManager.getInstance();
MenuAction menuAction = new MenuAction(); am.registerAction("menuAction", menuAction); DefaultActionGroup windowsM = (DefaultActionGroup) am.getAction("WindowMenu"); windowsM.addSeparator();
windowsM.add(menuAction);
} // Disposes system resources.
public void disposeComponent() {
}
}

  

4、添加应用组件的配置到 plugin.xml 中,这样 ide 才会知道我们这个插件要做什么

<application-components>
<component>
<implementation-class>com.eleven24.HelloWorldRegistration</implementation-class>
</component>
</application-components>

5、添加一个点击菜单之后的处理方法,需要继承 AnAction 类

package com.eleven24;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages; public class MenuAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
Messages.showInfoMessage("Hello World!", "First Plugin");
}
}

5、添加菜单,这个的话在 plugin.xml 中配置,同时也要指定该菜单点击之后对应的处理类(上面的menuAction)

我们添加一个组名叫 "HelloWorld" 的菜单组,添加到主菜单栏末尾,然后在其下添加一个名为 "menu action" 的菜单。具体配置如下:

 <actions>
<!-- Add your actions here -->
<group id="HelloWorld" text="_HelloWorld">
<add-to-group group-id="MainMenu" anchor="last" />
</group> <action class="com.eleven24.MenuAction" id="menuAction" text="menu action">
<add-to-group group-id="HelloWorld" anchor="first" />
</action>
</actions>

  

6、到此为止,我们已经完成了菜单的添加,点击右上角的运行按钮可以查看效果,运行的时候会再打开一个 ide,会有点慢。

7、效果图

8、下面是最终的配置文件

<idea-plugin>
<id>com.eleven24.helloWorld</id>
<name>HelloWorld</name>
<version>1.0</version>
<vendor email="rubymay21s@gmail.com" url="http://www.cnblogs.com/eleven24">YourCompany</vendor> <description><![CDATA[
Hello World Plugin.
]]></description> <change-notes><![CDATA[
first version.
]]>
</change-notes> <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="145.0"/> <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
<depends>com.intellij.modules.platform</depends> <extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions> <actions>
<!-- Add your actions here -->
<group id="HelloWorld" text="_HelloWorld" description="menu group">
<add-to-group group-id="MainMenu" anchor="last" />
<action class="com.eleven24.MenuAction" id="helloWorld.menuAction" text="menu action" />
</group>
</actions> <application-components>
<component>
<implementation-class>com.eleven24.HelloWorldRegistration</implementation-class>
</component>
</application-components> </idea-plugin>

  

好了,就这么多了。

jetBrains 插件开发第一课-- 在主菜单栏新增一个菜单的更多相关文章

  1. Spring MVC第一课:用IDEA构建一个基于Spring MVC, Hibernate, My SQL的Maven项目

    作为一个Spring MVC新手最基本的功夫就是学会如何使用开发工具创建一个完整的Spring MVC项目,本文站在一个新手的角度讲述如何一步一步创建一个基于Spring MVC, Hibernate ...

  2. visual Studio 2017 扩展开发(一)《向Visual Studio菜单栏新增一个菜单》

    最近有接触到关于visual studio 2017 扩展的开发,特此记录,也是为了督促自己去深入了解其原理. 开始开发Visual Studio 扩展,在这里我安装了visual studio 20 ...

  3. NeHe OpenGL教程 第一课:OpenGL窗口

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. Python之路第一课Day6--随堂笔记(面向对象 )

    本节内容: 1. 面向对象编程介绍 2. 为什么要用面向对象进行开发? 3. 面向对象的特性:封装.继承.多态 4. 类.方法   一.面向过程 VS 面向对象  1. 编程范式 编程是 程序 员 用 ...

  5. Octave Tutorial(《Machine Learning》)之第一课《数据表示和存储》

    Octave Tutorial 第一课 Computation&Operation 数据表示和存储 1.简单的四则运算,布尔运算,赋值运算(a && b,a || b,xor( ...

  6. Android入门第一课之Java基础

    通知:由于本周六场地申请没通过,所以本周的培训临时取消. 今天给大家带来的是Android入门的第一课,由于教室申请的不确定性,因此,每次培训的内容都会在博客先提前释放出来.首先Android的APP ...

  7. vue.js学习(第一课)

    学习资料 来自台湾小凡! vue.js是javascript的一个库,只专注于UI层面,核心价值永远是 API的简洁. 第一课: 不支持IE8. 1.声明式渲染: el元素的简称 element : ...

  8. Magento学习第一课——目录结构介绍

    Magento学习第一课--目录结构介绍 一.Magento为何强大 Magento是在Zend框架基础上建立起来的,这点保证了代码的安全性及稳定性.选择Zend的原因有很多,但是最基本的是因为zen ...

  9. <-0基础学python.第一课->

    初衷:我电脑里面的歌曲很久没换了,我想听一下新的歌曲,把他们下载下来听,比如某个榜单的,但是一首一首的点击下载另存为真的很恶心 所以我想有没有办法通过程序的方式来实现,结果还真的有,而且网上已经有有人 ...

随机推荐

  1. Pairs Forming LCM LightOJ - 1236 素因子分解

    Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;    fo ...

  2. 广东ACM省赛 E题

    题意: 输入一个P 使得存在一个一个N大于等于P, 并且存在m 等于 m/n * (m-1)/(n-1)=1/2. 思路 此题可以利用佩尔方程求解, 也可以打表解决.本次我解决利用的是佩尔方程(其实也 ...

  3. 53. [LeetCode] Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  4. ubuntu 设置全局代理

    ubuntu配置shadowsocks全局代理 在mac.window平台下都有shadowsocks客户端,因此这两个平台不叙述太多,现在介绍ubuntu下的配置方法. 1.安装python lin ...

  5. Alpha事后诸葛会议

    [设想和目标] Q1:我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? "小葵日记"是为了解决18-30岁年轻用户在记录生活时希望得到一美体验友好 ...

  6. HDU 5661 Claris and XOR 贪心

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  7. HDU 5418 Victor and World 允许多次经过的TSP

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...

  8. gmssl

    一.安装 1.1 github地址 1.2 官网地址 由于我本地虚拟机跑的是centos,按照官网的安装步骤,没有安装成功.后来使用github上提供的安装步骤完美编译安装成功. 二.使用 由于gms ...

  9. MiniOS系统

    实验一  命令解释程序的编写 一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS,UNIX的 ...

  10. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...