当前位置: 移动技术网 > IT编程>开发语言>Java > Intellij Idea插件开发之创建项目层级的右键菜单

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网