void () { RadRootContainer root = (RadRootContainer)FormEditingUtil.getRoot(mySelection.get(0)); final PsiField[] boundFields = new PsiField[mySelection.size()]; for (int i = 0; i < mySelection.size(); i++) { boundFields[i] = BindingProperty.findBoundField(root, mySelection.get(i).getBinding()); } final PsiClass myClass = boundFields[0].getContainingClass(); if (!FileModificationService.getInstance().preparePsiElementForWrite(myClass)) return; try { PsiMethod constructor = findConstructorToInsert(myClass); final Module module = ModuleUtil.findModuleForPsiElement(myClass); PsiClass listenerClass = null; final String listenerClassName = myDescriptor.getListenerType().getName(); if (listenerClassName.endsWith(LISTENER_SUFFIX)) { String adapterClassName = listenerClassName.substring(0, listenerClassName.length() - LISTENER_SUFFIX.length()) + ADAPTER_SUFFIX; listenerClass = JavaPsiFacade.getInstance(myClass.getProject()) .findClass(adapterClassName, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)); } if (listenerClass == null) { listenerClass = JavaPsiFacade.getInstance(myClass.getProject()) .findClass(listenerClassName, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)); } if (listenerClass == null) { Messages.showErrorDialog(myClass.getProject(), UIDesignerBundle.message("create.listener.class.not.found"), CommonBundle.getErrorTitle()); return; } PsiElementFactory factory = JavaPsiFacade.getInstance(myClass.getProject()).getElementFactory(); final PsiCodeBlock body = constructor.getBody(); LOG.assertTrue(body != null); @NonNls StringBuilder builder = new StringBuilder(); @NonNls String variableName; if (boundFields.length == 1) { variableName = null; builder.append(boundFields[0].getName()); builder.append("."); builder.append(myDescriptor.getAddListenerMethod().getName()); builder.append("("); } else { builder.append(listenerClass.getQualifiedName()).append(" "); if (body.getLastBodyElement() == null) { variableName = "listener"; } else { final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myClass.getProject()); variableName = codeStyleManager.suggestUniqueVariableName("listener", body.getLastBodyElement(), false); } builder.append(variableName).append("="); } builder.append("new "); builder.append(listenerClass.getQualifiedName()); builder.append("() { } "); if (boundFields.length == 1) { builder.append(");"); } else { builder.append(";"); } PsiStatement stmt = WriteAction.compute(() -> { PsiStatement temp = factory.createStatementFromText(builder.toString(), constructor); temp = (PsiStatement)body.addAfter(temp, body.getLastBodyElement()); temp = (PsiStatement)JavaCodeStyleManager.getInstance(body.getProject()).shortenClassReferences(temp); if (boundFields.length > 1) { PsiElement anchor = temp; for (PsiField field : boundFields) { PsiElement addStmt = factory .createStatementFromText(field.getName() + "." + myDescriptor.getAddListenerMethod().getName() + "(" + variableName + ");", constructor); addStmt = body.addAfter(addStmt, anchor); anchor = addStmt; } } return temp; }); final SmartPsiElementPointer ptr = SmartPointerManager.getInstance(myClass.getProject()).createSmartPsiElementPointer(stmt); final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(myClass); final FileEditor[] fileEditors = virtualFile != null ? FileEditorManager.getInstance(myClass.getProject()).openFile(virtualFile, true, true) : null; IdeFocusManager.findInstance().doWhenFocusSettlesDown(new Runnable() { @Override public void run() { final PsiElement anonymousClassStatement = ptr.getElement(); if (anonymousClassStatement == null) { return; } final Ref<PsiClass> newClassRef = new Ref<>(); anonymousClassStatement.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitClass(@NotNull PsiClass aClass) { newClassRef.set(aClass); } }); final PsiClass newClass = newClassRef.get(); final Editor editor = getEditor(); if (editor != null && newClass != null) { PsiElement brace = newClass.getLBrace(); if (brace != null) { editor.getCaretModel().moveToOffset(brace.getTextOffset()); } CommandProcessor.getInstance().executeCommand(myClass.getProject(), () -> { if (!OverrideImplementExploreUtil.getMethodSignaturesToImplement(newClass).isEmpty()) { OverrideImplementUtil.chooseAndImplementMethods(newClass.getProject(), editor, newClass); } else { OverrideImplementUtil.chooseAndOverrideMethods(newClass.getProject(), editor, newClass); } }, "", null); } } private Editor getEditor() { if (fileEditors != null) { for (FileEditor fileEditor : fileEditors) { if (fileEditor instanceof TextEditor) { return ((TextEditor)fileEditor).getEditor(); } } } return null; } }, ModalityState.current()); } catch (IncorrectOperationException ex) { LOG.error(ex); } }