溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Intellij Idea插件開發(fā)中如何創(chuàng)建項(xiàng)目層級(jí)的右鍵菜單

發(fā)布時(shí)間:2021-06-07 11:27:19 來源:億速云 閱讀:527 作者:小新 欄目:編程語言

這篇文章主要介紹了Intellij Idea插件開發(fā)中如何創(chuàng)建項(xiàng)目層級(jí)的右鍵菜單,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

分享一:創(chuàng)建Project右鍵菜單

1,按照項(xiàng)目向?qū)б徊揭徊絼?chuàng)建一個(gè)Demo項(xiàng)目,就不再介紹了,可以參照這篇文章https://www.jb51.net/article/135535.htm

2,創(chuàng)建Action,在plugin配置文件中你會(huì)看到

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

Intellij Idea插件開發(fā)中如何創(chuàng)建項(xiàng)目層級(jí)的右鍵菜單

3,運(yùn)行后,IDE會(huì)另外開啟一個(gè)IDE(由一個(gè)類似Genymotion的容器包裹)。看效果是不是很熟悉,對(duì),這就是常用Project右鍵菜單:

Intellij Idea插件開發(fā)中如何創(chuàng)建項(xiàng)目層級(jí)的右鍵菜單

4,根據(jù)觸發(fā)的文件類型動(dòng)態(tài)控制Action的隱藏顯示

@Override 
public void update(AnActionEvent event) {//根據(jù)擴(kuò)展名是否是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))) {//根據(jù)擴(kuò)展名判定是否進(jìn)行下面的處理 
   //獲取選中的文件 
   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顯示之前,根據(jù)選中文件擴(kuò)展名判定是否顯示此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(); 
 } 
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Intellij Idea插件開發(fā)中如何創(chuàng)建項(xiàng)目層級(jí)的右鍵菜單”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI