Skip to content

Commit 401649e

Browse files
authored
Merge pull request #328 from HongJinFeng/master
[feature]支持按照题解数量排序
2 parents 9fb1ae1 + 636a28f commit 401649e

14 files changed

+374
-7
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.shuzijun.leetcode.plugin.model.Question;
6+
import com.shuzijun.leetcode.plugin.utils.DataKeys;
7+
import com.shuzijun.leetcode.plugin.window.WindowFactory;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import javax.swing.*;
11+
import javax.swing.tree.DefaultMutableTreeNode;
12+
import javax.swing.tree.MutableTreeNode;
13+
import javax.swing.tree.TreeNode;
14+
import java.util.Enumeration;
15+
import java.util.LinkedList;
16+
import java.util.List;
17+
import java.util.Objects;
18+
19+
/**
20+
* @author hongjinfeng
21+
* @date 2021/5/19 4:32 下午
22+
*/
23+
public abstract class AbstractSortAction extends AnAction {
24+
25+
@Override
26+
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
27+
JTree tree = WindowFactory.getDataContext(Objects.requireNonNull(anActionEvent.getProject())).getData(DataKeys.LEETCODE_PROJECTS_TREE);
28+
assert tree != null;
29+
DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
30+
Enumeration<TreeNode> children = note.children();
31+
List<MutableTreeNode> childrenForSort = new LinkedList<>();
32+
while (children.hasMoreElements()) {
33+
childrenForSort.add((MutableTreeNode) children.nextElement());
34+
}
35+
Question tag = (Question) note.getUserObject();
36+
sortChildren(tag, childrenForSort);
37+
note.setUserObject(tag);
38+
note.removeAllChildren();
39+
for (TreeNode treeNode : childrenForSort) {
40+
note.add((MutableTreeNode) treeNode);
41+
}
42+
tree.updateUI();
43+
}
44+
45+
public abstract void sortChildren(Question tag, List<MutableTreeNode> childrenForSort);
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.intellij.openapi.actionSystem.DefaultActionGroup;
5+
6+
/**
7+
* @author hongjinfeng
8+
* @date 2021/5/20 12:20 下午
9+
*/
10+
public class SortActionGroup extends DefaultActionGroup {
11+
12+
@Override
13+
public void update(AnActionEvent event) {
14+
event.getPresentation().setEnabled(true);
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.shuzijun.leetcode.plugin.model.Question;
4+
5+
import javax.swing.tree.DefaultMutableTreeNode;
6+
import javax.swing.tree.MutableTreeNode;
7+
import java.util.List;
8+
9+
/**
10+
* @author hongjinfeng
11+
* @date 2021/5/20 9:42 上午
12+
*/
13+
public class SortByDifficultyAction extends AbstractSortAction {
14+
15+
@Override
16+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
17+
childrenForSort.sort((o1, o2) -> {
18+
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
19+
Question question1 = (Question) item1.getUserObject();
20+
DefaultMutableTreeNode item2 = (DefaultMutableTreeNode) o2;
21+
Question question2 = (Question) item2.getUserObject();
22+
return tag.getLevelSortTrend() * (question1.getLevel() - question2.getLevel());
23+
});
24+
tag.setLevelSortTrend(-tag.getLevelSortTrend());
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.shuzijun.leetcode.plugin.model.Question;
4+
5+
import javax.swing.tree.DefaultMutableTreeNode;
6+
import javax.swing.tree.MutableTreeNode;
7+
import java.util.List;
8+
9+
/**
10+
* @author hongjinfeng
11+
* @date 2021/5/20 9:40 上午
12+
*/
13+
public class SortByIdAction extends AbstractSortAction {
14+
15+
@Override
16+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
17+
childrenForSort.sort((o1, o2) -> {
18+
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
19+
Question question1 = (Question) item1.getUserObject();
20+
DefaultMutableTreeNode item2 = (DefaultMutableTreeNode) o2;
21+
Question question2 = (Question) item2.getUserObject();
22+
int i = Integer.MAX_VALUE;
23+
try {
24+
i = Integer.parseInt(question1.getFrontendQuestionId());
25+
} catch (Exception ignored) {
26+
}
27+
int j = Integer.MAX_VALUE;
28+
try {
29+
j = Integer.parseInt(question2.getFrontendQuestionId());
30+
} catch (Exception ignored) {
31+
}
32+
return tag.getIdSortTrend() * (i - j);
33+
});
34+
tag.setIdSortTrend(-tag.getIdSortTrend());
35+
}
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.shuzijun.leetcode.plugin.model.Question;
4+
5+
import javax.swing.tree.DefaultMutableTreeNode;
6+
import javax.swing.tree.MutableTreeNode;
7+
import java.util.List;
8+
9+
/**
10+
* @author hongjinfeng
11+
* @date 2021/5/20 9:41 上午
12+
*/
13+
public class SortByNameAction extends AbstractSortAction {
14+
15+
@Override
16+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
17+
childrenForSort.sort((o1, o2) -> {
18+
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
19+
Question question1 = (Question) item1.getUserObject();
20+
DefaultMutableTreeNode item2 = (DefaultMutableTreeNode) o2;
21+
Question question2 = (Question) item2.getUserObject();
22+
return tag.getNameSortTrend() * (question1.getTitle().compareTo(question2.getTitle()));
23+
});
24+
tag.setNameSortTrend(-tag.getNameSortTrend());
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.shuzijun.leetcode.plugin.model.Question;
4+
5+
import javax.swing.tree.DefaultMutableTreeNode;
6+
import javax.swing.tree.MutableTreeNode;
7+
import java.util.List;
8+
9+
/**
10+
* @author hongjinfeng
11+
* @date 2021/5/20 9:43 上午
12+
*/
13+
public class SortByOccurrenceFrequencyAction extends AbstractSortAction{
14+
15+
@Override
16+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
17+
childrenForSort.sort((o1, o2) -> {
18+
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
19+
Question question1 = (Question) item1.getUserObject();
20+
DefaultMutableTreeNode item2 = (DefaultMutableTreeNode) o2;
21+
Question question2 = (Question) item2.getUserObject();
22+
return tag.getOccurrenceFrequencySortTrend() * (question1.getOccurrenceFrequency().compareTo(question2.getOccurrenceFrequency()));
23+
});
24+
tag.setOccurrenceFrequencySortTrend(-tag.getOccurrenceFrequencySortTrend());
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.model.Question;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import javax.swing.tree.DefaultMutableTreeNode;
8+
import javax.swing.tree.MutableTreeNode;
9+
import java.util.List;
10+
11+
/**
12+
* @author hongjinfeng
13+
* @date 2021/5/20 9:41 上午
14+
*/
15+
public class SortByPassRateAction extends AbstractSortAction{
16+
17+
@Override
18+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
19+
childrenForSort.sort((o1, o2) -> {
20+
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
21+
Question question1 = (Question) item1.getUserObject();
22+
DefaultMutableTreeNode item2 = (DefaultMutableTreeNode) o2;
23+
Question question2 = (Question) item2.getUserObject();
24+
return tag.getPassingRateSortTrend() * (question1.getPassingRate().compareTo(question2.getPassingRate()));
25+
});
26+
tag.setPassingRateSortTrend(-tag.getPassingRateSortTrend());
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.shuzijun.leetcode.plugin.model.Question;
4+
5+
import javax.swing.tree.DefaultMutableTreeNode;
6+
import javax.swing.tree.MutableTreeNode;
7+
import java.util.List;
8+
9+
/**
10+
* @author hongjinfeng
11+
* @date 2021/5/19 4:35 下午
12+
*/
13+
public class SortBySolutionAction extends AbstractSortAction {
14+
15+
@Override
16+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
17+
childrenForSort.sort((o1, o2) -> {
18+
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
19+
Question question1 = (Question) item1.getUserObject();
20+
DefaultMutableTreeNode item2 = (DefaultMutableTreeNode) o2;
21+
Question question2 = (Question) item2.getUserObject();
22+
return tag.getSolutionSortTrend() * (question1.getTotalSolutionCount() - question2.getTotalSolutionCount());
23+
});
24+
tag.setSolutionSortTrend(-tag.getSolutionSortTrend());
25+
}
26+
}

src/main/java/com/shuzijun/leetcode/plugin/listener/TreeMouseListener.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.intellij.openapi.project.Project;
99
import com.intellij.ui.treeStructure.SimpleTree;
1010
import com.shuzijun.leetcode.plugin.manager.CodeManager;
11+
import com.shuzijun.leetcode.plugin.model.Constant;
1112
import com.shuzijun.leetcode.plugin.model.Question;
1213
import org.jetbrains.annotations.NotNull;
1314

@@ -44,20 +45,27 @@ public void mouseClicked(MouseEvent e) {
4445
if (e.getButton() == 3) { //鼠标右键
4546
final ActionManager actionManager = ActionManager.getInstance();
4647
final ActionGroup actionGroup = (ActionGroup) actionManager.getAction("leetcode.NavigatorActionsMenu");
48+
4749
if (actionGroup != null) {
4850
actionManager.createActionPopupMenu("", actionGroup).getComponent().show(e.getComponent(), e.getX(), e.getY());
4951
}
5052
} else if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
51-
ProgressManager.getInstance().run(new Task.Backgroundable(project,"leetcode.editor.openCode",false) {
53+
ProgressManager.getInstance().run(new Task.Backgroundable(project, "leetcode.editor.openCode", false) {
5254
@Override
5355
public void run(@NotNull ProgressIndicator progressIndicator) {
5456
CodeManager.openCode(question, project);
5557
}
5658
});
5759
}
60+
} else if (Constant.NODETYPE_TAG.equals(question.getNodeType()) || Constant.NODETYPE_PROBLEMS.equals(question.getNodeType())) {
61+
if (e.getButton() == 3) { //鼠标右键
62+
final ActionManager actionManager = ActionManager.getInstance();
63+
final ActionGroup actionGroup = (ActionGroup) actionManager.getAction("leetcode.editor.tree.menu");
64+
if (actionGroup != null) {
65+
actionManager.createActionPopupMenu("", actionGroup).getComponent().show(e.getComponent(), e.getX(), e.getY());
66+
}
67+
}
5868
}
5969
}
60-
61-
6270
}
6371
}

src/main/java/com/shuzijun/leetcode/plugin/manager/QuestionManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,15 @@ private static List<Question> parseQuestion(String str) {
208208
question.setLeaf(Boolean.TRUE);
209209
question.setQuestionId(object.getJSONObject("stat").getString("question_id"));
210210
question.setFrontendQuestionId(object.getJSONObject("stat").getString("frontend_question_id"));
211+
question.setTotalSolutionCount(object.getJSONObject("stat").getInteger("total_column_articles"));
212+
question.setPassingRate(object.getJSONObject("stat").getDouble("total_acs")/object.getJSONObject("stat").getInteger("total_submitted"));
211213
try {
212214
if (object.getBoolean("paid_only") && isPremium) {
213215
question.setStatus(object.getBoolean("paid_only") ? "lock" : null);
214216
} else {
215217
question.setStatus(object.get("status") == null ? "" : object.getString("status"));
216218
}
219+
question.setOccurrenceFrequency(object.getDouble("frequency"));
217220
} catch (Exception ee) {
218221
question.setStatus("");
219222
}

src/main/java/com/shuzijun/leetcode/plugin/manager/ViewManager.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public String apply(Question question) {
6767
DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();
6868
DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeMode.getRoot();
6969
root.removeAllChildren();
70-
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new Question(String.format("Problems(%d)", questionList.size())));
70+
Question problems = new Question(String.format("Problems(%d)", questionList.size()));
71+
problems.setNodeType(Constant.NODETYPE_PROBLEMS);
72+
DefaultMutableTreeNode node = new DefaultMutableTreeNode(problems);
7173
root.add(node);
7274
for (Question q : questionList) {
7375
node.add(new DefaultMutableTreeNode(q));
7476
}
7577
for (String key : filter.keySet()) {
76-
if(Constant.FIND_TYPE_CATEGORY.equals(key)){
78+
if (Constant.FIND_TYPE_CATEGORY.equals(key)) {
7779
continue;
7880
}
7981
DefaultMutableTreeNode filterNode = new DefaultMutableTreeNode(new Question(key));
@@ -254,8 +256,13 @@ private static void addChild(DefaultMutableTreeNode rootNode, List<Tag> Lists, M
254256
if (!Lists.isEmpty()) {
255257
for (Tag tag : Lists) {
256258
long qCnt = tag.getQuestions().stream().filter(q -> questionMap.get(q) != null).count();
257-
DefaultMutableTreeNode tagNode = new DefaultMutableTreeNode(new Question(String.format("%s(%d)",
258-
tag.getName(), qCnt)));
259+
Question item = new Question(String.format("%s(%d)",
260+
tag.getName(), qCnt));
261+
Question parent = (Question) rootNode.getUserObject();
262+
if (parent.getTitle().equals(Constant.FIND_TYPE_TAGS)) {
263+
item.setNodeType(Constant.NODETYPE_TAG);
264+
}
265+
DefaultMutableTreeNode tagNode = new DefaultMutableTreeNode(item);
259266
rootNode.add(tagNode);
260267
for (String key : tag.getQuestions()) {
261268
if (questionMap.get(key) != null) {

src/main/java/com/shuzijun/leetcode/plugin/model/Constant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class Constant {
3131
public static final String NODETYPE_CARD = "card";
3232
public static final String NODETYPE_CHAPTER = "chapter";
3333
public static final String NODETYPE_ITEM = "item";
34+
public static final String NODETYPE_TAG = "tag";
35+
public static final String NODETYPE_PROBLEMS = "problems";
3436
/**
3537
* 探索下列表内容
3638
*/

0 commit comments

Comments
 (0)