mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 08:47:50 +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">
|
||||
<id>org.jetbrains.plugins.sample.ProjectModel</id>
|
||||
<name>Editor basics</name>
|
||||
<version>1.0</version>
|
||||
<vendor email="support@jetbrains.com" url="http://www.jetbrains.com">JetBrains</vendor>
|
||||
<id>org.jetbrains.plugins.sample.ProjectModel</id>
|
||||
<name>Editor basics</name>
|
||||
<version>1.0</version>
|
||||
<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 -->
|
||||
<idea-version since-build="131"/>
|
||||
<depends>com.intellij.modules.lang</depends>
|
||||
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
|
||||
<idea-version since-build="131"/>
|
||||
<depends>com.intellij.modules.lang</depends>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<!-- Add your extensions here -->
|
||||
</extensions>
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<!-- Add your extensions here -->
|
||||
</extensions>
|
||||
|
||||
<application-components>
|
||||
<!-- Add your application components here -->
|
||||
</application-components>
|
||||
<application-components>
|
||||
<!-- Add your application components here -->
|
||||
</application-components>
|
||||
|
||||
<project-components>
|
||||
<!-- Add your project components here -->
|
||||
</project-components>
|
||||
<project-components>
|
||||
<!-- Add your project components here -->
|
||||
</project-components>
|
||||
|
||||
<actions>
|
||||
<action id="ProjectModel.SourceRoots" class="com.intellij.plugins.project.model.ShowSourceRootsActions" text="Show Source Roots"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||
</action>
|
||||
<action id="ProjectModel.ProjectSdk" class="com.intellij.plugins.project.model.ProjectSdkAction" text="Show Sdk Info"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||
</action>
|
||||
<action id="ProjectModel.ProjectFileIndex" class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction" text="FileProjectIndex in Action"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
|
||||
</action>
|
||||
<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>
|
||||
</actions>
|
||||
<actions>
|
||||
<action id="ProjectModel.SourceRoots" class="com.intellij.plugins.project.model.ShowSourceRootsActions"
|
||||
text="Show Source Roots"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||
</action>
|
||||
<action id="ProjectModel.ProjectSdk" class="com.intellij.plugins.project.model.ProjectSdkAction"
|
||||
text="Show Sdk Info"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||
</action>
|
||||
<action id="ProjectModel.ProjectFileIndex"
|
||||
class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction"
|
||||
text="FileProjectIndex in Action"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
|
||||
</action>
|
||||
<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>
|
@ -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