Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shuzijun.leetcode.plugin.actions.toolbar;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.ui.components.JBScrollPane;
import com.shuzijun.leetcode.plugin.actions.AbstractAction;
import com.shuzijun.leetcode.plugin.manager.ViewManager;
Expand All @@ -21,6 +22,8 @@ public void actionPerformed(AnActionEvent anActionEvent, Config config) {
return;
}
JBScrollPane scrollPane = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_SCROLL);
ViewManager.pick(tree, scrollPane);
ApplicationManager.getApplication().invokeAndWait(() -> {
ViewManager.pick(tree, scrollPane);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.shuzijun.leetcode.plugin.listener;

import com.intellij.ui.ColorPicker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
* @author shuzijun
*/
public class ColorListener extends MouseAdapter {
private JLabel label;
private JPanel mainPanel;

public ColorListener(JPanel mainPanel,JLabel label) {
this.mainPanel = mainPanel;
this.label = label;
}

@Override
public void mouseClicked(MouseEvent e) {
Color newColor = ColorPicker.showDialog(mainPanel, label.getText()+" Color", label.getForeground(), true, null, true);
if(newColor!=null){
label.setForeground(newColor);
}
}


}
30 changes: 20 additions & 10 deletions src/main/java/com/shuzijun/leetcode/plugin/manager/CodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
Expand All @@ -14,19 +17,16 @@
import com.shuzijun.leetcode.plugin.setting.ProjectConfig;
import com.shuzijun.leetcode.plugin.utils.*;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author shuzijun
*/
public class CodeManager {

private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

public static void openCode(Question question, Project project) {
Config config = PersistentConfig.getInstance().getInitConfig();
String codeType = config.getCodeType();
Expand Down Expand Up @@ -173,7 +173,7 @@ public static void SubmitCode(Question question, Project project) {
if (response != null && response.getStatusCode() == 200) {
String body = response.getBody();
JSONObject returnObj = JSONObject.parseObject(body);
cachedThreadPool.execute(new SubmitCheckTask(returnObj, codeTypeEnum, question, project));
ProgressManager.getInstance().run(new SubmitCheckTask(returnObj, codeTypeEnum, question, project));
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending"));
} else if (response != null && response.getStatusCode() == 429) {
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending"));
Expand Down Expand Up @@ -218,7 +218,7 @@ public static void RunCodeCode(Question question, Project project) {

String body = response.getBody();
JSONObject returnObj = JSONObject.parseObject(body);
cachedThreadPool.execute(new RunCodeCheckTask(returnObj, project));
ProgressManager.getInstance().run(new RunCodeCheckTask(returnObj, project));
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending"));
} else {
LogUtils.LOG.error("RuncodeCode failure " + response.getBody());
Expand Down Expand Up @@ -336,24 +336,29 @@ private static String getContent(JSONObject jsonObject) {
return sb.toString();
}

private static class SubmitCheckTask implements Runnable {
private static class SubmitCheckTask extends Task.Backgroundable {

private Question question;
private JSONObject returnObj;
private CodeTypeEnum codeTypeEnum;
private Project project;

public SubmitCheckTask(JSONObject returnObj, CodeTypeEnum codeTypeEnum, Question question, Project project) {
super(project,"leetcode.editor.submitCheckTask",true);
this.returnObj = returnObj;
this.codeTypeEnum = codeTypeEnum;
this.question = question;
this.project = project;
}

@Override
public void run() {
public void run(@NotNull ProgressIndicator progressIndicator) {
String key = returnObj.getString("submission_id");
for (int i = 0; i < 50; i++) {
if(progressIndicator.isCanceled()){
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.cancel"));
return;
}
try {
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeSubmissions() + key + "/check/");
HttpResponse response = HttpRequestUtils.executeGet(httpRequest);
Expand Down Expand Up @@ -424,22 +429,27 @@ private static String buildErrorMsg(JSONObject errorBody) {
}


private static class RunCodeCheckTask implements Runnable {
private static class RunCodeCheckTask extends Task.Backgroundable {
private JSONObject returnObj;
private Project project;

public RunCodeCheckTask(JSONObject returnObj, Project project) {
super(project,"leetcode.editor.runCodeCheckTask",true);
this.returnObj = returnObj;
this.project = project;
}

@Override
public void run() {
public void run(@NotNull ProgressIndicator progressIndicator) {
String key = returnObj.getString("interpret_expected_id");
if (StringUtils.isBlank(key)) {
key = returnObj.getString("interpret_id");
}
for (int i = 0; i < 50; i++) {
if(progressIndicator.isCanceled()){
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.cancel"));
return;
}
String body = null;
try {
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeSubmissions() + key + "/check/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ public static void pick(JTree tree, JBScrollPane scrollPane) {
}
int i = (int) (Math.random() * node.getChildCount());
DefaultMutableTreeNode select = (DefaultMutableTreeNode) node.getChildAt(i);
tree.setSelectionPath(new TreePath(select.getPath()));
Point point = new Point(0, i < 3 ? 0 : (i - 3) * tree.getRowHeight());

TreePath toShowPath = new TreePath(select.getPath());
tree.setSelectionPath(toShowPath);
Rectangle bounds = tree.getPathBounds(toShowPath);
Point point = new Point(0, (int) bounds.getY());
JViewport viewport = scrollPane.getViewport();
viewport.setViewPosition(point);
return;
Expand Down
76 changes: 75 additions & 1 deletion src/main/java/com/shuzijun/leetcode/plugin/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.util.xmlb.annotations.Transient;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -82,7 +83,7 @@ public class Config {
/**
* 题目颜色
*/
private String levelColour = "#5CB85C;#F0AD4E;#D9534F";
private String levelColour = Constant.LEVEL_COLOUR;

private List<String> favoriteList;

Expand Down Expand Up @@ -232,8 +233,57 @@ public String getCookie(String user) {
public String getLevelColour() {
return levelColour;
}
@Transient
public Color[] getFormatLevelColour() {
Color[] formatColors = new Color[3];
formatColors[0] = new Color(92, 184, 92);
formatColors[1] = new Color(240, 173, 78);
formatColors[2] = new Color(217, 83, 79);
String[] colors = getLevelColour().split(";");
if (colors.length > 0) {
try {
formatColors[0] = new Color(Integer.parseInt(colors[0].replace("#", ""), 16));
} catch (Exception ignore) {
}
}
if (colors.length > 1) {
try {
formatColors[1] = new Color(Integer.parseInt(colors[1].replace("#", ""), 16));
} catch (Exception ignore) {
}
}
if (colors.length > 2) {
try {
formatColors[2] = new Color(Integer.parseInt(colors[2].replace("#", ""), 16));
} catch (Exception ignore) {
}
}
return formatColors;
}

public void setLevelColour(String levelColour) {
if(levelColour ==null || levelColour.isEmpty()){
this.levelColour = Constant.LEVEL_COLOUR;
}else {
this.levelColour = levelColour;
}
}

@Transient
public void setFormatLevelColour(Color... colors) {
String levelColour = "";
if (colors != null && colors.length > 0) {
for (Color color : colors) {
String R = Integer.toHexString(color.getRed());
R = R.length() < 2 ? ('0' + R) : R;
String G = Integer.toHexString(color.getGreen());
G = G.length() < 2 ? ('0' + G) : G;
String B = Integer.toHexString(color.getBlue());
B = B.length() < 2 ? ('0' + B) : B;

levelColour = levelColour + '#' + R + G + B + ";";
}
}
this.levelColour = levelColour;
}

Expand All @@ -244,4 +294,28 @@ public Boolean getEnglishContent() {
public void setEnglishContent(Boolean englishContent) {
this.englishContent = englishContent;
}


public boolean isModified(Config config){
if(config ==null){
return false;
}
if (version != null ? !version.equals(config.version) : config.version != null) return false;
if (loginName != null ? !loginName.equals(config.loginName) : config.loginName != null) return false;
if (filePath != null ? !filePath.equals(config.filePath) : config.filePath != null) return false;
if (codeType != null ? !codeType.equals(config.codeType) : config.codeType != null) return false;
if (url != null ? !url.equals(config.url) : config.url != null) return false;
if (update != null ? !update.equals(config.update) : config.update != null) return false;
if (proxy != null ? !proxy.equals(config.proxy) : config.proxy != null) return false;
if (customCode != null ? !customCode.equals(config.customCode) : config.customCode != null) return false;
if (englishContent != null ? !englishContent.equals(config.englishContent) : config.englishContent != null)
return false;
if (customFileName != null ? !customFileName.equals(config.customFileName) : config.customFileName != null)
return false;
if (customTemplate != null ? !customTemplate.equals(config.customTemplate) : config.customTemplate != null)
return false;
return levelColour != null ? levelColour.equals(config.levelColour) : config.levelColour == null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ public class Constant {
public static final Integer PLUGIN_CONFIG_VERSION_1 = 1;
//第二版本,不兼容之前的临时目录,从此版本开始更换新临时目录
public static final Integer PLUGIN_CONFIG_VERSION_2 = 2;

/**
* 默认题目颜色
*/
public static final String LEVEL_COLOUR = "#5CB85C;#F0AD4E;#D9534F";
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.shuzijun.leetcode.plugin.model.Config;
import com.shuzijun.leetcode.plugin.model.Question;
import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
import org.apache.commons.lang.StringUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
Expand All @@ -19,36 +18,22 @@
*/
public class CustomTreeCellRenderer extends NodeRenderer {

private Color Level1 = new Color(92, 184, 92);
private Color Level2 = new Color(240, 173, 78);
private Color Level3 = new Color(217, 83, 79);
private static Color Level1 = new Color(92, 184, 92);
private static Color Level2 = new Color(240, 173, 78);
private static Color Level3 = new Color(217, 83, 79);

public CustomTreeCellRenderer() {
loaColor();
}

public static void loaColor(){
Config config = PersistentConfig.getInstance().getInitConfig();
if (config != null) {
if (StringUtils.isNotBlank(config.getLevelColour())) {
String[] colors = config.getLevelColour().split(";");
if (colors.length > 0) {
try {
Level1 = new Color(Integer.parseInt(colors[0].replace("#",""), 16));
} catch (Exception ignore) {
}
}
if (colors.length > 1) {
try {
Level2 = new Color(Integer.parseInt(colors[1].replace("#",""), 16));
} catch (Exception ignore) {
}
}
if (colors.length > 2) {
try {
Level3 = new Color(Integer.parseInt(colors[2].replace("#",""), 16));
} catch (Exception ignore) {
}
}
}
Color[] colors = config.getFormatLevelColour();
Level1 = colors[0];
Level2 = colors[1];
Level3 = colors[2];
}

}

public static BufferedImage getResourceBufferedImage(String filePath) {
Expand All @@ -71,7 +56,6 @@ public void customizeCellRenderer(JTree tree, Object value, boolean selected, bo
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Question question = (Question) node.getUserObject();


if (question.getLevel() == null) {

} else if (question.getLevel() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public Runnable enableSearch(String option) {
@Override
public JComponent createComponent() {
mainPanel = new SettingUI();
mainPanel.createUI();
return mainPanel.getContentPane();
}

Expand All @@ -69,4 +68,5 @@ public void disposeUIResources() {
mainPanel.disposeUIResources();
mainPanel = null;
}

}
Loading