Skip to content

Commit 49e5030

Browse files
committed
Add open solution
1 parent 5908e90 commit 49e5030

File tree

15 files changed

+624
-53
lines changed

15 files changed

+624
-53
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.shuzijun.leetcode.plugin.actions.editor;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
5+
import com.intellij.openapi.application.ApplicationManager;
6+
import com.intellij.openapi.project.Project;
7+
import com.intellij.openapi.vfs.VirtualFile;
8+
import com.shuzijun.leetcode.plugin.manager.ArticleManager;
9+
import com.shuzijun.leetcode.plugin.manager.ViewManager;
10+
import com.shuzijun.leetcode.plugin.model.*;
11+
import com.shuzijun.leetcode.plugin.setting.ProjectConfig;
12+
import com.shuzijun.leetcode.plugin.window.SolutionPanel;
13+
import org.jetbrains.annotations.NotNull;
14+
15+
import java.util.List;
16+
import java.util.concurrent.atomic.AtomicReference;
17+
18+
/**
19+
* @author shuzijun
20+
*/
21+
public class OpenSolutionAction extends AbstractEditAction {
22+
23+
@Override
24+
public void update(@NotNull AnActionEvent anActionEvent) {
25+
VirtualFile vf = anActionEvent.getData(PlatformDataKeys.VIRTUAL_FILE);
26+
LeetcodeEditor leetcodeEditor = ProjectConfig.getInstance(anActionEvent.getProject()).getEditor(vf.getPath());
27+
if (leetcodeEditor == null) {
28+
return;
29+
}
30+
Question question = ViewManager.getQuestionById(leetcodeEditor.getQuestionId(), anActionEvent.getProject());
31+
if (question == null) {
32+
anActionEvent.getPresentation().setEnabled(false);
33+
return;
34+
}
35+
anActionEvent.getPresentation().setEnabled(!Constant.ARTICLE_LIVE_NONE.equals(question.getArticleLive()));
36+
}
37+
38+
@Override
39+
public void actionPerformed(AnActionEvent anActionEvent, Config config, Question question) {
40+
Project project = anActionEvent.getProject();
41+
if (Constant.ARTICLE_LIVE_ONE.equals(question.getArticleLive())) {
42+
ArticleManager.openArticle(question, project);
43+
} else if (Constant.ARTICLE_LIVE_LIST.equals(question.getArticleLive())) {
44+
List<Solution> solutionList = ArticleManager.getSolutionList(question, anActionEvent.getProject());
45+
if (solutionList.isEmpty()) {
46+
return;
47+
}
48+
AtomicReference<Solution> solution = new AtomicReference<>();
49+
ApplicationManager.getApplication().invokeAndWait(() -> {
50+
SolutionPanel.TableModel tableModel = new SolutionPanel.TableModel(solutionList);
51+
SolutionPanel dialog = new SolutionPanel(anActionEvent.getProject(), tableModel);
52+
dialog.setTitle(question.getFormTitle() + " Solutions");
53+
54+
if (dialog.showAndGet()) {
55+
solution.set(solutionList.get(dialog.getSelectedRow()));
56+
}
57+
});
58+
if(solution.get() !=null){
59+
question.setArticleSlug(solution.get().getSlug());
60+
ArticleManager.openArticle(question, project);
61+
}
62+
63+
}
64+
65+
}
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)