How to get the folder of currenctly selected file

I need to get the folder of the currently selected file ( when
ProjectViewPopupMenu is clicked ).
I tried to get it from the Project , but not succeeded.

Thx


4 comments
Comment actions Permalink

Hello Buminda,

B> I need to get the folder of the currently selected file ( when
B> ProjectViewPopupMenu is clicked ).
B> I tried to get it from the Project , but not succeeded.

public void actionPerformed(AnActionEvent e) {
VirtualFile file = (VirtualFile) e.getDataContext().getData(DataConstants.VIRTUAL_FILE);
VirtualFile folder = file.getParent();
}

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

Looks like the new way to do this is:

   public void actionPerformed(AnActionEvent e)
   {
      VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
      VirtualFile folder = file.getParent();
   }

0
Comment actions Permalink

e.getData(DataKeys.VIRTUAL_FILE) is better because it's shorter.

0
Comment actions Permalink

Hi George,

Please note that there is a possible case that more than one element is selected at the project view. You can use PlatformDataKeys.VIRTUAL_FILE_ARRAY for obtaining all of them then.

Denis

0

Please sign in to leave a comment.