"Choose Destination Directory" dialog

My plugin creates class-files based on other classes. In those cases where a project contains several source folders I'd like to let the user chose the source folder to place the class in. IDEA accomplishes this out of the box with the "Choose Destination Directory"-dialog, which makes me suspect that this has already been implemented in the Open API, the question is: How?


e.g. when you use a TextFieldWithBrowseButton, then you'd use the method addBrowseFolderListener(message, fileChooserDescriptor, etc)

and something like

yourField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
public void textChanged(DocumentEvent event) {



I am still not quite sure whether or not this actually does the trick. Let me clearify, when creating a file in my plugin I want the user to be confronted with the Choose Destination Directory dialog allowing him/her to pick one of the source or test root folders. When clicking OK I would like to get the chosen directory so I know me where to actually store the file. I'll attach a picture of the standard dialog seen in many Move/Copy-situations in IDEA just to illustrate what I actually am referring to.


For those like me who had the same question and were disappointed not to find any answer, here's a way to do it:

PsiDirectory directory = CreateClassUtil.obtainDirectoryRootForPackage(module, "my.package.name");

Where module is the Module where you want to create your directory. If the module contains a single source root, the dialog won't be shown and the method will return this source root.

Please sign in to leave a comment.