|
| 1 | +package com.shuzijun.leetcode.plugin.actions.tree; |
| 2 | + |
| 3 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 4 | +import com.intellij.openapi.application.ApplicationManager; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.shuzijun.leetcode.plugin.manager.ArticleManager; |
| 7 | +import com.shuzijun.leetcode.plugin.manager.ViewManager; |
| 8 | +import com.shuzijun.leetcode.plugin.model.Config; |
| 9 | +import com.shuzijun.leetcode.plugin.model.Constant; |
| 10 | +import com.shuzijun.leetcode.plugin.model.Question; |
| 11 | +import com.shuzijun.leetcode.plugin.model.Solution; |
| 12 | +import com.shuzijun.leetcode.plugin.utils.DataKeys; |
| 13 | +import com.shuzijun.leetcode.plugin.window.SolutionPanel; |
| 14 | +import com.shuzijun.leetcode.plugin.window.WindowFactory; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import javax.swing.*; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.atomic.AtomicReference; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author shuzijun |
| 23 | + */ |
| 24 | +public class OpenSolutionAction extends AbstractTreeAction { |
| 25 | + |
| 26 | + @Override |
| 27 | + public void update(@NotNull AnActionEvent anActionEvent) { |
| 28 | + JTree tree = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_TREE); |
| 29 | + if (tree == null) { |
| 30 | + anActionEvent.getPresentation().setEnabled(false); |
| 31 | + return; |
| 32 | + } |
| 33 | + Question question = ViewManager.getTreeQuestion(tree, anActionEvent.getProject()); |
| 34 | + if (question == null) { |
| 35 | + anActionEvent.getPresentation().setEnabled(false); |
| 36 | + return; |
| 37 | + } |
| 38 | + if (Constant.ARTICLE_LIVE_NONE.equals(question.getArticleLive())) { |
| 39 | + anActionEvent.getPresentation().setEnabled(false); |
| 40 | + } else { |
| 41 | + anActionEvent.getPresentation().setEnabled(true); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void actionPerformed(AnActionEvent anActionEvent, Config config, JTree tree, Question question) { |
| 47 | + Project project = anActionEvent.getProject(); |
| 48 | + if (Constant.ARTICLE_LIVE_ONE.equals(question.getArticleLive())) { |
| 49 | + ArticleManager.openArticle(question, project); |
| 50 | + } else if (Constant.ARTICLE_LIVE_LIST.equals(question.getArticleLive())) { |
| 51 | + List<Solution> solutionList = ArticleManager.getSolutionList(question, anActionEvent.getProject()); |
| 52 | + if (solutionList.isEmpty()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + AtomicReference<Solution> solution = new AtomicReference<>(); |
| 56 | + ApplicationManager.getApplication().invokeAndWait(() -> { |
| 57 | + SolutionPanel.TableModel tableModel = new SolutionPanel.TableModel(solutionList); |
| 58 | + SolutionPanel dialog = new SolutionPanel(anActionEvent.getProject(), tableModel); |
| 59 | + dialog.setTitle(question.getFormTitle() + " Solutions"); |
| 60 | + |
| 61 | + if (dialog.showAndGet()) { |
| 62 | + solution.set(solutionList.get(dialog.getSelectedRow())); |
| 63 | + } |
| 64 | + }); |
| 65 | + if(solution.get() !=null){ |
| 66 | + question.setArticleSlug(solution.get().getSlug()); |
| 67 | + ArticleManager.openArticle(question, project); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | +} |
0 commit comments