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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

zhangmingCSDN   2021-03-28 我要评论
想了解Intellij Idea插件开发之创建项目层级的右键菜单的相关内容吗,zhangmingCSDN在本文为您仔细讲解Intellij Idea右键菜单的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Intellij,Idea右键菜单,下面大家一起来学习吧。

在使用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(); 
 } 
} 

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们