在使用android studio的過程中,發現自帶的一些插件無法滿足項目的實際需要,便著手自己開發對應的插件。下面是我開發插件過程中的一個記錄,會持續和大家分享。
分享一:創建project右鍵菜單
1,按照項目向導一步一步創建一個demo項目,就不再介紹了,可以參照這篇文章 http://www.ythuaji.com.cn/article/153905.html
2,創建action,在plugin配置文件中你會看到
1
2
3
|
<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的隱藏顯示
1
2
3
4
5
|
@override public void update(anactionevent event) { //根據擴展名是否是jar,顯示隱藏此action string extension = getfileextension(event.getdatacontext()); this .gettemplatepresentation().setenabled(extension != null && "jar" .equals(extension)); } |
完整代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家
原文鏈接:http://blog.csdn.net/zhangbuzhangbu/article/details/52227403