@NotNull public static List<MavenDomDependency> chooseDependencies( Collection<MavenDomDependency> candidates, final Project project) { List<MavenDomDependency> dependencies = new ArrayList<MavenDomDependency>(); MavenDomProjectModelMember[] memberCandidates = ContainerUtil.map2Array( candidates, MavenDomProjectModelMember.class, dependency -> new MavenDomProjectModelMember(dependency)); MemberChooser<MavenDomProjectModelMember> chooser = new MemberChooser<MavenDomProjectModelMember>(memberCandidates, true, true, project) { protected ShowContainersAction getShowContainersAction() { return new ShowContainersAction( MavenDomBundle.message("chooser.show.project.files"), MavenIcons.MavenProject); } @NotNull @Override protected String convertElementText(@NotNull String originalElementText) { return originalElementText; } protected String getAllContainersNodeName() { return MavenDomBundle.message("all.dependencies"); } }; chooser.setTitle(MavenDomBundle.message("dependencies.chooser.title")); chooser.setCopyJavadocVisible(false); chooser.show(); if (chooser.getExitCode() == MemberChooser.OK_EXIT_CODE) { final MavenDomProjectModelMember[] members = chooser.getSelectedElements(new MavenDomProjectModelMember[0]); if (members != null) { dependencies.addAll( ContainerUtil.mapNotNull( members, mavenDomProjectModelMember -> mavenDomProjectModelMember.getDependency())); } } return dependencies; }
@Override public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException { if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; PsiMethod[] constructors = myClass.getConstructors(); if (constructors.length == 0) { final AddDefaultConstructorFix defaultConstructorFix = new AddDefaultConstructorFix(myClass); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { defaultConstructorFix.invoke(project, editor, file); } }); constructors = myClass.getConstructors(); } Arrays.sort( constructors, new Comparator<PsiMethod>() { @Override public int compare(PsiMethod c1, PsiMethod c2) { final PsiMethod cc1 = RefactoringUtil.getChainedConstructor(c1); final PsiMethod cc2 = RefactoringUtil.getChainedConstructor(c2); if (cc1 == c2) return 1; if (cc2 == c1) return -1; if (cc1 == null) { return cc2 == null ? 0 : compare(c1, cc2); } else { return cc2 == null ? compare(cc1, c2) : compare(cc1, cc2); } } }); final ArrayList<PsiMethod> constrs = filterConstructorsIfFieldAlreadyAssigned(constructors, getField()); if (constrs.size() > 1) { final PsiMethodMember[] members = new PsiMethodMember[constrs.size()]; int i = 0; for (PsiMethod constructor : constrs) { members[i++] = new PsiMethodMember(constructor); } final List<PsiMethodMember> elements; if (ApplicationManager.getApplication().isUnitTestMode()) { elements = Arrays.asList(members); } else { final MemberChooser<PsiMethodMember> chooser = new MemberChooser<PsiMethodMember>(members, false, true, project); chooser.setTitle("Choose constructors to add parameter to"); chooser.show(); elements = chooser.getSelectedElements(); if (elements == null) return; } for (PsiMethodMember member : elements) { if (!addParameterToConstructor( project, file, editor, member.getElement(), new PsiField[] {getField()})) break; } } else if (!constrs.isEmpty()) { final Collection<SmartPsiElementPointer<PsiField>> fieldsToFix = getFieldsToFix(); try { final PsiMethod constructor = constrs.get(0); final LinkedHashSet<PsiField> fields = new LinkedHashSet<PsiField>(); getFieldsToFix().add(myField); for (SmartPsiElementPointer<PsiField> elementPointer : fieldsToFix) { final PsiField field = elementPointer.getElement(); if (field != null && isAvailable(field) && filterConstructorsIfFieldAlreadyAssigned(new PsiMethod[] {constructor}, field) .contains(constructor)) { fields.add(field); } } if (constrs.size() == constructors.length && fields.size() > 1 && !ApplicationManager.getApplication().isUnitTestMode()) { PsiFieldMember[] members = new PsiFieldMember[fields.size()]; int i = 0; for (PsiField field : fields) { members[i++] = new PsiFieldMember(field); } MemberChooser<PsiElementClassMember> chooser = new MemberChooser<PsiElementClassMember>(members, false, true, project); chooser.setTitle("Choose Fields to Generate Constructor Parameters for"); chooser.show(); if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE) return; final List<PsiElementClassMember> selectedElements = chooser.getSelectedElements(); if (selectedElements == null) return; fields.clear(); for (PsiElementClassMember member : selectedElements) { fields.add((PsiField) member.getElement()); } } addParameterToConstructor( project, file, editor, constructor, constrs.size() == constructors.length ? fields.toArray(new PsiField[fields.size()]) : new PsiField[] {getField()}); } finally { fieldsToFix.clear(); } } }
private void proceedWithChoosingMethods( Project project, PsiFile file, final Editor editor, List<CppMethodElementNode> candidates, boolean override) { final MemberChooser<CppMethodElementNode> chooser = new MemberChooser<CppMethodElementNode>( candidates.toArray(new CppMethodElementNode[candidates.size()]), false, true, project, false) {}; chooser.setTitle("Choose Methods to " + (override ? "Override" : "Implement")); chooser.setCopyJavadocVisible(false); chooser.show(); if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE) return; final List<CppMethodElementNode> selectedElements = chooser.getSelectedElements(); if (selectedElements == null || selectedElements.size() == 0) return; CommandProcessor.getInstance() .executeCommand( project, new Runnable() { public void run() { ApplicationManager.getApplication() .runWriteAction( new Runnable() { public void run() { Document document = editor.getDocument(); int offset = editor.getCaretModel().getOffset(); for (CppMethodElementNode c : selectedElements) { String cType = c.getType(); String methodText = "virtual " + cType + " " + c.getText() + " {\n" + (!"void".equals(cType) ? "return " : "") + (c.myIsAbstract ? "" : c.getParentName() + "::" + c.myName + "(" + c.myParamNames + ");") + "\n" + "}\n"; document.insertString(offset, methodText); offset += methodText.length(); } } }); } }, override ? "Override Methods" : "Implement Methods", this); }