這篇文章主要介紹了Intellij Idea插件開發中如何創建項目層級的右鍵菜單,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
分享一:創建Project右鍵菜單
1,按照項目向導一步一步創建一個Demo項目,就不再介紹了,可以參照這篇文章https://www.jb51.net/article/135535.htm
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();
}
}感謝你能夠認真閱讀完這篇文章,希望小編分享的“Intellij Idea插件開發中如何創建項目層級的右鍵菜單”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。