Skip to content

Commit 4c7c52a

Browse files
hongjinfenghongjinfeng
authored andcommitted
[feature]support sort by id,name,passing rate,frequency,solution count
1 parent 5e9d183 commit 4c7c52a

File tree

13 files changed

+278
-30
lines changed

13 files changed

+278
-30
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
package com.shuzijun.leetcode.plugin.actions.tree;
22

33
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;
418

519
/**
620
* @author hongjinfeng
721
* @date 2021/5/19 4:32 下午
822
*/
923
public abstract class AbstractSortAction extends AnAction {
1024

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);
1146
}
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: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
package com.shuzijun.leetcode.plugin.actions.tree;
22

3-
import com.intellij.openapi.actionSystem.AnActionEvent;
43
import com.shuzijun.leetcode.plugin.model.Question;
5-
import com.shuzijun.leetcode.plugin.utils.DataKeys;
6-
import com.shuzijun.leetcode.plugin.window.WindowFactory;
7-
import org.jetbrains.annotations.NotNull;
84

9-
import javax.swing.*;
105
import javax.swing.tree.DefaultMutableTreeNode;
116
import javax.swing.tree.MutableTreeNode;
12-
import javax.swing.tree.TreeNode;
13-
import java.util.Comparator;
14-
import java.util.Enumeration;
15-
import java.util.LinkedList;
167
import java.util.List;
17-
import java.util.Objects;
188

199
/**
2010
* @author hongjinfeng
@@ -23,16 +13,7 @@
2313
public class SortBySolutionAction extends AbstractSortAction {
2414

2515
@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<TreeNode> childrenForSort = new LinkedList<>();
32-
while (children.hasMoreElements()) {
33-
childrenForSort.add(children.nextElement());
34-
}
35-
Question tag = (Question) note.getUserObject();
16+
public void sortChildren(Question tag, List<MutableTreeNode> childrenForSort) {
3617
childrenForSort.sort((o1, o2) -> {
3718
DefaultMutableTreeNode item1 = (DefaultMutableTreeNode) o1;
3819
Question question1 = (Question) item1.getUserObject();
@@ -41,11 +22,5 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
4122
return tag.getSolutionSortTrend() * (question1.getTotalSolutionCount() - question2.getTotalSolutionCount());
4223
});
4324
tag.setSolutionSortTrend(-tag.getSolutionSortTrend());
44-
note.setUserObject(tag);
45-
note.removeAllChildren();
46-
for (TreeNode treeNode : childrenForSort) {
47-
note.add((MutableTreeNode) treeNode);
48-
}
49-
tree.updateUI();
5025
}
5126
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
5757
}
5858
});
5959
}
60-
} else if (Constant.NODETYPE_TAG.equals(question.getNodeType())) {
60+
} else if (Constant.NODETYPE_TAG.equals(question.getNodeType()) || Constant.NODETYPE_PROBLEMS.equals(question.getNodeType())) {
6161
if (e.getButton() == 3) { //鼠标右键
6262
final ActionManager actionManager = ActionManager.getInstance();
6363
final ActionGroup actionGroup = (ActionGroup) actionManager.getAction("leetcode.editor.tree.menu");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,13 +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
}
217-
question.setTotalSolutionCount(object.getJSONObject("stat").getInteger("total_column_articles"));
219+
question.setOccurrenceFrequency(object.getDouble("frequency"));
218220
} catch (Exception ee) {
219221
question.setStatus("");
220222
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ 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));
@@ -257,7 +259,7 @@ private static void addChild(DefaultMutableTreeNode rootNode, List<Tag> Lists, M
257259
Question item = new Question(String.format("%s(%d)",
258260
tag.getName(), qCnt));
259261
Question parent = (Question) rootNode.getUserObject();
260-
if (parent.getTitle().equals(Constant.FIND_TYPE_TAGS)){
262+
if (parent.getTitle().equals(Constant.FIND_TYPE_TAGS)) {
261263
item.setNodeType(Constant.NODETYPE_TAG);
262264
}
263265
DefaultMutableTreeNode tagNode = new DefaultMutableTreeNode(item);

0 commit comments

Comments
 (0)