void (@NotNull final DataContext dataContext) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); final IdeView ideView = LangDataKeys.IDE_VIEW.getData(dataContext); if (project == null || ideView == null) return; String copied = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); if (copied == null) return; String fileText = StringUtil.convertLineSeparators(copied); String className = detectClassName(project, fileText); if (className == null) return; final PsiDirectory targetDir = ideView.getOrChooseDirectory(); if (targetDir == null) return; String fileName = className + ".java"; PsiFile existingFile = targetDir.findFile(fileName); if (existingFile != null && !MessageDialogBuilder.yesNo(IdeBundle.message("title.file.already.exists"), CoreBundle.message("prompt.overwrite.project.file", fileName, "")).ask(project)) { return; } VirtualFile virtualFile = WriteCommandAction.writeCommandAction(project).withName( JavaBundle.message("paste.class.command.name", className)).compute(() -> { PsiFile file = existingFile; if (file == null) { try { file = targetDir.createFile(fileName); } catch (IncorrectOperationException e) { return null; } } final Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document != null) { document.setText(fileText); PsiDocumentManager.getInstance(project).commitDocument(document); } if (file instanceof PsiJavaFile) { updatePackageStatement((PsiJavaFile)file, targetDir); } return file.getVirtualFile(); }); if (virtualFile != null) { PsiNavigationSupport.getInstance().createNavigatable(project, virtualFile, -1).navigate(true); } }