Get Selected File names from ProjectViewPane and add Action Group
Answered
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(
selectedUserObjects!!)
println("Inside Runnable")
if(selectedElements!=null){
for(element in selectedElements!!){
if(element.javaClass == PsiClassImpl::class.java){
val selectedElement: PsiClassImpl = element as PsiClassImpl
println(selectedElement.containingFile.virtualFile.canonicalPath)
toShow = true
}else if(element.javaClass == PsiJavaDirectoryImpl::class.java){
val selectedElement: PsiJavaDirectoryImpl = element as PsiJavaDirectoryImpl
if(selectedElement.virtualFile.canonicalPath == projectDirectory){
isProjectDirectorySelected = true
toShow = true
}
}
}
}
}
ApplicationManager.getApplication().invokeLater(getSelectedElementsRunnable)
if(toShow){
actionGroup= arrayOf(
RunAnalysisAction("Run Analysis",
"Run Analysis on this project",
AllIcons.Actions.Execute)
)
}
return actionGroup
}
}
Please sign in to leave a comment.
You can obtain selected items directly from given AnActionEvent via `e.getData(PlatformCoreDataKeys.SELECTED_ITEMS)`
This worked perfectly. Thank you.