|
7 | 7 |
|
8 | 8 | import com.intellij.codeInspection.LocalQuickFix;
|
9 | 9 | import com.intellij.codeInspection.ProblemDescriptor;
|
| 10 | +import com.intellij.openapi.command.WriteCommandAction; |
10 | 11 | import com.intellij.openapi.project.Project;
|
| 12 | +import com.intellij.openapi.ui.DialogBuilder; |
11 | 13 | import com.intellij.psi.PsiElement;
|
| 14 | +import com.jetbrains.php.refactoring.extract.extractInterface.PhpExtractInterfaceProcessor; |
12 | 15 | import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
|
13 | 16 | import com.jetbrains.php.lang.psi.elements.PhpClass;
|
| 17 | +import com.magento.idea.magento2plugin.bundles.InspectionBundle; |
14 | 18 | import com.magento.idea.magento2plugin.magento.files.GraphQlResolver;
|
15 | 19 | import org.jetbrains.annotations.NotNull;
|
| 20 | +import javax.swing.*; |
16 | 21 |
|
17 | 22 | public class PhpImplementResolverClassQuickFix implements LocalQuickFix {
|
| 23 | + private InspectionBundle inspectionBundle = new InspectionBundle(); |
| 24 | + |
18 | 25 | @NotNull
|
19 | 26 | @Override
|
20 | 27 | public String getFamilyName() {
|
21 |
| - return "Implements Resolver interface"; |
| 28 | + return inspectionBundle.message("inspection.graphql.resolver.fix.family"); |
22 | 29 | }
|
23 | 30 |
|
24 | 31 | @Override
|
25 | 32 | public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
26 |
| - PsiElement correctInterface = PhpPsiElementFactory.createImplementsList(project, GraphQlResolver.RESOLVER_INTERFACE); |
27 |
| - PhpClass graphQlResolverClass = (PhpClass) descriptor.getPsiElement().getParent(); |
28 |
| - graphQlResolverClass.getImplementsList().replace(correctInterface); |
| 33 | + String[] expectedInterface = { |
| 34 | + GraphQlResolver.BATCH_RESOLVER_INTERFACE, |
| 35 | + GraphQlResolver.BATCH_SERVICE_CONTRACT_RESOLVER_INTERFACE, |
| 36 | + GraphQlResolver.RESOLVER_INTERFACE |
| 37 | + }; |
| 38 | + final JComboBox expectedInterfaceDropdown = new JComboBox(expectedInterface); |
| 39 | + ((JLabel)expectedInterfaceDropdown.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER); |
| 40 | + DialogBuilder dialogBox = createDialogBox(expectedInterfaceDropdown, project); |
| 41 | + if (dialogBox.showAndGet()) { |
| 42 | + String getSelectedInterface = expectedInterfaceDropdown.getSelectedItem().toString(); |
| 43 | + final PsiElement erroredElement = descriptor.getPsiElement(); |
| 44 | + final PsiElement correctInterface = PhpPsiElementFactory.createImplementsList(project, getSelectedInterface); |
| 45 | + final PhpClass graphQlResolverClass = (PhpClass) erroredElement.getParent(); |
| 46 | + String[] implementedInterfaceNames = graphQlResolverClass.getInterfaceNames(); |
| 47 | + WriteCommandAction.runWriteCommandAction(project, () -> { |
| 48 | + if (implementedInterfaceNames.length == 0) { |
| 49 | + PhpExtractInterfaceProcessor.addImplementClause(project, graphQlResolverClass, getSelectedInterface); |
| 50 | + } else { |
| 51 | + graphQlResolverClass.getImplementsList().replace(correctInterface); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private DialogBuilder createDialogBox(JComboBox selectedClass, Project project) |
| 58 | + { |
| 59 | + JPanel panel = new JPanel(); |
| 60 | + panel.add(selectedClass); |
| 61 | + DialogBuilder builder = new DialogBuilder(project); |
| 62 | + builder.setTitle(inspectionBundle.message("inspection.graphql.resolver.fix.title")); |
| 63 | + builder.setCenterPanel(panel); |
| 64 | + builder.addOkAction(); |
| 65 | + builder.addCancelAction(); |
| 66 | + return builder; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean startInWriteAction() { |
| 71 | + return false; |
29 | 72 | }
|
30 | 73 | }
|
0 commit comments