|
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;
|
12 | 14 | import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
|
13 | 15 | import com.jetbrains.php.lang.psi.elements.PhpClass;
|
14 | 16 | import com.magento.idea.magento2plugin.magento.files.GraphQlResolver;
|
15 | 17 | import org.jetbrains.annotations.NotNull;
|
| 18 | +import javax.swing.*; |
16 | 19 |
|
17 | 20 | public class PhpImplementResolverClassQuickFix implements LocalQuickFix {
|
| 21 | + public static final String FAMILY_NAME = "Implements Resolver interface"; |
| 22 | + public static final String DIALOG_TITLE = "Select one of the following interface"; |
18 | 23 | @NotNull
|
19 | 24 | @Override
|
20 | 25 | public String getFamilyName() {
|
21 |
| - return "Implements Resolver interface"; |
| 26 | + return FAMILY_NAME; |
22 | 27 | }
|
23 | 28 |
|
24 | 29 | @Override
|
25 | 30 | 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); |
| 31 | + String[] expectedInterface = { GraphQlResolver.BATCH_RESOLVER_INTERFACE, |
| 32 | + GraphQlResolver.BATCH_SERVICE_CONTRACT_RESOLVER_INTERFACE, |
| 33 | + GraphQlResolver.RESOLVER_INTERFACE |
| 34 | + }; |
| 35 | + final JComboBox expectedInterfaceDropdown = new JComboBox(expectedInterface); |
| 36 | + DialogBuilder dialogBox = createDialogBox(expectedInterfaceDropdown, project); |
| 37 | + if (dialogBox.showAndGet()) { |
| 38 | + String getSelectedInterface = expectedInterfaceDropdown.getSelectedItem().toString(); |
| 39 | + PsiElement correctInterface = PhpPsiElementFactory.createImplementsList(project, getSelectedInterface); |
| 40 | + PhpClass graphQlResolverClass = (PhpClass) descriptor.getPsiElement().getParent(); |
| 41 | + WriteCommandAction.writeCommandAction(project).run(() -> { |
| 42 | + graphQlResolverClass.getImplementsList().replace(correctInterface); |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private DialogBuilder createDialogBox(JComboBox selectedClass, Project project) |
| 48 | + { |
| 49 | + JPanel panel = new JPanel(); |
| 50 | + panel.add(selectedClass); |
| 51 | + DialogBuilder builder = new DialogBuilder(project); |
| 52 | + builder.setTitle(DIALOG_TITLE); |
| 53 | + builder.setCenterPanel(panel); |
| 54 | + builder.addOkAction(); |
| 55 | + builder.addCancelAction(); |
| 56 | + return builder; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean startInWriteAction() { |
| 61 | + return false; |
29 | 62 | }
|
30 | 63 | }
|
0 commit comments