[code] Libraries sample

This commit is contained in:
Anna Bulenkova 2014-12-17 13:26:34 +01:00
parent 36453ed153
commit bb6d645146
2 changed files with 108 additions and 36 deletions

View File

@ -25,21 +25,31 @@
</project-components> </project-components>
<actions> <actions>
<action id="ProjectModel.SourceRoots" class="com.intellij.plugins.project.model.ShowSourceRootsActions" text="Show Source Roots" <action id="ProjectModel.SourceRoots" class="com.intellij.plugins.project.model.ShowSourceRootsActions"
text="Show Source Roots"
description="Illustrates how to get source roots"> description="Illustrates how to get source roots">
<add-to-group group-id="ToolsMenu" anchor="after"/> <add-to-group group-id="ToolsMenu" anchor="after"/>
</action> </action>
<action id="ProjectModel.ProjectSdk" class="com.intellij.plugins.project.model.ProjectSdkAction" text="Show Sdk Info" <action id="ProjectModel.ProjectSdk" class="com.intellij.plugins.project.model.ProjectSdkAction"
text="Show Sdk Info"
description="Illustrates how to get source roots"> description="Illustrates how to get source roots">
<add-to-group group-id="ToolsMenu" anchor="after"/> <add-to-group group-id="ToolsMenu" anchor="after"/>
</action> </action>
<action id="ProjectModel.ProjectFileIndex" class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction" text="FileProjectIndex in Action" <action id="ProjectModel.ProjectFileIndex"
class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction"
text="FileProjectIndex in Action"
description="Illustrates how to get source roots"> description="Illustrates how to get source roots">
<add-to-group group-id="EditorPopupMenu" anchor="last"/> <add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action> </action>
<action id="ProjectModel.ModificationAction" class="com.intellij.plugins.project.model.ModificationAction" text="Project Modification in Action" <action id="ProjectModel.ModificationAction" class="com.intellij.plugins.project.model.ModificationAction"
text="Project Modification in Action"
description="Illustrates how to get source roots"> description="Illustrates how to get source roots">
<add-to-group group-id="EditorPopupMenu" anchor="last"/> <add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action> </action>
<action id="ProjectModel.LibrariesAction" class="com.intellij.plugins.project.model.LibrariesAction"
text="Libraries for file"
description="Illustrates accessing libraries">
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action>
</actions> </actions>
</idea-plugin> </idea-plugin>

View File

@ -0,0 +1,62 @@
package com.intellij.plugins.project.model;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
/**
* @author Anna Bulenkova
*/
public class LibrariesAction extends AnAction {
@Override
public void update(final AnActionEvent event) {
Project project = event.getProject();
if (project == null) return;
Navigatable element = event.getData(CommonDataKeys.NAVIGATABLE);
if (element instanceof PsiClass) {
PsiFile psiFile = ((PsiClass) element).getContainingFile();
if (psiFile == null) return;
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) return;
event.getPresentation().setEnabledAndVisible(true);
}
}
@Override
public void actionPerformed(AnActionEvent event) {
Project project = event.getProject();
if (project == null) return;
Navigatable element = event.getData(CommonDataKeys.NAVIGATABLE);
if (element instanceof PsiClass) {
PsiFile psiFile = ((PsiClass) element).getContainingFile();
if (psiFile == null) return;
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) return;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
StringBuilder jars = new StringBuilder();
for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) {
if (orderEntry instanceof LibraryOrderEntry) {
final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) orderEntry;
final Library library = libraryEntry.getLibrary();
if (library == null) continue;
VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
if (files.length == 0) continue;
for (VirtualFile jar : files) {
jars.append(jar.getName()).append(", ");
}
}
}
if (jars.length() > 0) {
Messages.showInfoMessage("Libraries for file " + virtualFile.getName() + ": " + jars.toString(), "Libraries Info");
}
}
}
}