Get Selected File names from ProjectViewPane and add Action Group


Hi, I am trying to add an actionGroup in the projectViewPane menu displayed when user right clicks on a file. I want to display this group if the user clicks on project directory or .java files only. Below is the code that I am using.


The problem is that I cannot directly check from UI thread and using invoke later always returns empty actionGroup as the boolean value does not change. How do I wait till the invokeLater() is executed?



class RequiredActionGroup : ActionGroup(), DumbAware {
override fun getChildren(event: AnActionEvent?): Array<AnAction> {

val project = event!!.project

//Project Directory Path
val projectDirectory = project!!.basePath
println("ProjectDirectory: $projectDirectory")

//Array returning actions
var actionGroup: Array<AnAction> = arrayOf()

//Boolean Indicating whether to show action group or not
var toShow = false

//Boolean Indicating whether projectDirectory was selected or not
var isProjectDirectorySelected = false

//Getting user selected elements from ProjectViewPane
var selectedUserObjects: Array<out Any?>?
var selectedElements: Array<out Any>?

val getSelectedElementsRunnable = Runnable {
selectedUserObjects = ProjectView.getInstance(project).currentProjectViewPane.selectedUserObjects
selectedElements = ProjectView.getInstance(project).currentProjectViewPane.getSelectedValues(
println("Inside Runnable")

for(element in selectedElements!!){
if(element.javaClass =={
val selectedElement: PsiClassImpl = element as PsiClassImpl
toShow = true
}else if(element.javaClass =={
val selectedElement: PsiJavaDirectoryImpl = element as PsiJavaDirectoryImpl

if(selectedElement.virtualFile.canonicalPath == projectDirectory){
isProjectDirectorySelected = true
toShow = true


actionGroup= arrayOf(
RunAnalysisAction("Run Analysis",
"Run Analysis on this project",

return actionGroup

Comment actions Permalink

You can obtain selected items directly from given AnActionEvent via  `e.getData(PlatformCoreDataKeys.SELECTED_ITEMS)`

Comment actions Permalink

This worked perfectly. Thank you.


Please sign in to leave a comment.