mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
[code] Libraries sample
This commit is contained in:
parent
36453ed153
commit
bb6d645146
@ -1,45 +1,55 @@
|
|||||||
<idea-plugin version="2" url="www.jetbrains.com">
|
<idea-plugin version="2" url="www.jetbrains.com">
|
||||||
<id>org.jetbrains.plugins.sample.ProjectModel</id>
|
<id>org.jetbrains.plugins.sample.ProjectModel</id>
|
||||||
<name>Editor basics</name>
|
<name>Editor basics</name>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<vendor email="support@jetbrains.com" url="http://www.jetbrains.com">JetBrains</vendor>
|
<vendor email="support@jetbrains.com" url="http://www.jetbrains.com">JetBrains</vendor>
|
||||||
|
|
||||||
<description>Project model illustration</description>
|
<description>Project model illustration</description>
|
||||||
|
|
||||||
<change-notes>Initial commit</change-notes>
|
<change-notes>Initial commit</change-notes>
|
||||||
|
|
||||||
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
|
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
|
||||||
<idea-version since-build="131"/>
|
<idea-version since-build="131"/>
|
||||||
<depends>com.intellij.modules.lang</depends>
|
<depends>com.intellij.modules.lang</depends>
|
||||||
|
|
||||||
<extensions defaultExtensionNs="com.intellij">
|
<extensions defaultExtensionNs="com.intellij">
|
||||||
<!-- Add your extensions here -->
|
<!-- Add your extensions here -->
|
||||||
</extensions>
|
</extensions>
|
||||||
|
|
||||||
<application-components>
|
<application-components>
|
||||||
<!-- Add your application components here -->
|
<!-- Add your application components here -->
|
||||||
</application-components>
|
</application-components>
|
||||||
|
|
||||||
<project-components>
|
<project-components>
|
||||||
<!-- Add your project components here -->
|
<!-- Add your project components here -->
|
||||||
</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"
|
||||||
description="Illustrates how to get source roots">
|
text="Show Source Roots"
|
||||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
description="Illustrates how to get source roots">
|
||||||
</action>
|
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||||
<action id="ProjectModel.ProjectSdk" class="com.intellij.plugins.project.model.ProjectSdkAction" text="Show Sdk Info"
|
</action>
|
||||||
description="Illustrates how to get source roots">
|
<action id="ProjectModel.ProjectSdk" class="com.intellij.plugins.project.model.ProjectSdkAction"
|
||||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
text="Show Sdk Info"
|
||||||
</action>
|
description="Illustrates how to get source roots">
|
||||||
<action id="ProjectModel.ProjectFileIndex" class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction" text="FileProjectIndex in Action"
|
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||||
description="Illustrates how to get source roots">
|
</action>
|
||||||
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
|
<action id="ProjectModel.ProjectFileIndex"
|
||||||
</action>
|
class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction"
|
||||||
<action id="ProjectModel.ModificationAction" class="com.intellij.plugins.project.model.ModificationAction" text="Project Modification in Action"
|
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>
|
||||||
</actions>
|
<action id="ProjectModel.ModificationAction" class="com.intellij.plugins.project.model.ModificationAction"
|
||||||
|
text="Project Modification in Action"
|
||||||
|
description="Illustrates how to get source roots">
|
||||||
|
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
|
||||||
|
</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>
|
||||||
</idea-plugin>
|
</idea-plugin>
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user