Intellij Idea插件开发之创建项目层级的右键菜单

时间:2021-05-20

在使用Android Studio的过程中,发现自带的一些插件无法满足项目的实际需要,便着手自己开发对应的插件。下面是我开发插件过程中的一个记录,会持续和大家分享。

分享一:创建Project右键菜单

1,按照项目向导一步一步创建一个Demo项目,就不再介绍了,可以参照这篇文章

2,创建Action,在plugin配置文件中你会看到

<action id="FirstAction" class="FirstAction" text="FirstAction" description="右键Action"> <add-to-group group-id="ProjectViewPopupMenu" anchor="after" relative-to-action="ReplaceInPath"/> </action>

3,运行后,IDE会另外开启一个IDE(由一个类似Genymotion的容器包裹)。看效果是不是很熟悉,对,这就是常用Project右键菜单:


4,根据触发的文件类型动态控制Action的隐藏显示

@Override public void update(AnActionEvent event) {//根据扩展名是否是jar,显示隐藏此Action String extension = getFileExtension(event.getDataContext()); this.getTemplatePresentation().setEnabled(extension != null && "jar".equals(extension)); }

完整代码:

import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.vfs.VirtualFile; /** * Created by ABC on 16/8/17. */ public class FirstAction extends AnAction { private Project mProject; @Override public void actionPerformed(AnActionEvent event) { mProject = event.getData(PlatformDataKeys.PROJECT); DataContext dataContext = event.getDataContext(); if ("jar".equals(getFileExtension(dataContext))) {//根据扩展名判定是否进行下面的处理 //获取选中的文件 VirtualFile file = DataKeys.VIRTUAL_FILE.getData(event.getDataContext()); if (file != null) { Messages.showMessageDialog(mProject, file.getName(), "select file", Messages.getInformationIcon()); } } } @Override public void update(AnActionEvent event) { //在Action显示之前,根据选中文件扩展名判定是否显示此Action String extension = getFileExtension(event.getDataContext()); this.getTemplatePresentation().setEnabled(extension != null && "jar".equals(extension)); } public static String getFileExtension(DataContext dataContext) { VirtualFile file = DataKeys.VIRTUAL_FILE.getData(dataContext); return file == null ? null : file.getExtension(); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章