void () { final Project project = myEditor.getProject(); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(myEditor.getFile()); if(sourceRoot == null){ Messages.showErrorDialog( myEditor, UIDesignerBundle.message("error.cannot.create.class.not.in.source.root"), CommonBundle.getErrorTitle() ); return; } ApplicationManager.getApplication().runWriteAction( () -> CommandProcessor.getInstance().executeCommand( project, () -> { // 1. Create all necessary packages final int indexOfLastDot = myClassName.lastIndexOf('.'); final String packageName = myClassName.substring(0, indexOfLastDot != -1 ? indexOfLastDot : 0); final PsiDirectory psiDirectory; if(packageName.length() > 0){ final PackageWrapper packageWrapper = new PackageWrapper(PsiManager.getInstance(project), packageName); try { psiDirectory = CommonJavaRefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, sourceRoot); } catch (final IncorrectOperationException e) { ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog( myEditor, UIDesignerBundle.message("error.cannot.create.package", packageName, e.getMessage()), CommonBundle.getErrorTitle() )); return; } } else{ psiDirectory = PsiManager.getInstance(project).findDirectory(sourceRoot); LOG.assertTrue(psiDirectory != null); } // 2. Create class in the package try { final String name = myClassName.substring(indexOfLastDot != -1 ? indexOfLastDot + 1 : 0); final PsiClass aClass = JavaDirectoryService.getInstance().createClass(psiDirectory, name); createBoundFields(aClass); } catch (final IncorrectOperationException e) { ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog( myEditor, UIDesignerBundle.message("error.cannot.create.class", packageName, e.getMessage()), CommonBundle.getErrorTitle() )); } }, getName(), null ) ); }