Skip to content

Commit 3dc0392

Browse files
author
liutao
committed
[Feature]新增设置key值功能
1 parent 9f3c40e commit 3dc0392

File tree

11 files changed

+214
-87
lines changed

11 files changed

+214
-87
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
1.0 支持中文转变量
2525
1.1 常量命名修改为下划线
2626
1.2 支持多结果展示
27+
1.3 增加自定义有道key
28+
2729

2830
使用方法:
2931

3032
1.下载项目中的var.jar
3133
2.google一下android studio 插件导入.
3234
3.安装成功之后,在android studio 状态栏 windows下,有个varTrans菜单,里面有对应快捷键
33-
35+
4.默认使用作者提供的key,每小时1000次,所有用户公用,建议用户自己去申请一个,设置位置在 other settings ->varTrans
3436
再奉上[GitHub地址](https://github.com/quietUncle/vartrans)
3537

3638

out/production/var/META-INF/plugin.xml

-51
This file was deleted.

resources/META-INF/plugin.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>com.qt.var</id>
33
<name>varTrans</name>
4-
<version>1.2</version>
4+
<version>1.3</version>
55
<vendor url="https://github.com/quietUncle/vartrans">安静大叔</vendor>
66

77
<description>中文花式转变量</description>
@@ -10,6 +10,7 @@
1010
1.0 支持中文转变量<br>
1111
1.1 常量命名修改为下划线
1212
1.2 支持多结果展示
13+
1.3 增加自定义有道key
1314
]]>
1415
</change-notes>
1516

@@ -26,6 +27,8 @@
2627
<!-- Add your extensions here -->
2728
<applicationService serviceInterface="com.qt.config.Config"
2829
serviceImplementation="com.qt.config.Config"/>
30+
<applicationConfigurable instance="com.qt.gui.Setting" />
31+
2932
</extensions>
3033

3134
<actions>
@@ -44,6 +47,8 @@
4447
<action id="Plugin.Pop" class="com.qt.var.PopAction" text="show pop" description="show pop">
4548
<keyboard-shortcut keymap="$default" first-keystroke="alt p"/>
4649
</action>
50+
<action id="Plugin.Var" class="com.qt.var.AboutAction" text="about" description="help">
51+
</action>
4752
</group>
4853
<group id="var_result_pop"/>
4954

src/com/qt/base/BaseAction.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.qt.base;
22

3+
import a.d.C;
34
import com.google.gson.Gson;
45
import com.google.gson.JsonParser;
56
import com.intellij.openapi.actionSystem.*;
67
import com.intellij.openapi.command.WriteCommandAction;
78
import com.intellij.openapi.editor.Editor;
89
import com.intellij.openapi.editor.EditorModificationUtil;
10+
import com.intellij.openapi.ui.Messages;
911
import com.intellij.openapi.ui.popup.JBPopupFactory;
1012
import com.intellij.openapi.ui.popup.ListPopup;
1113
import com.intellij.openapi.util.text.StringUtil;
@@ -26,11 +28,12 @@ public abstract class BaseAction extends AnAction implements VarParse {
2628
private static JsonParser parser = new JsonParser();
2729
private static Gson gson = new Gson();
2830
private String TAG = "var_result_pop";
31+
private static Config.State STATE= Config.getInstance().getState();
2932
@Override
3033
public void actionPerformed(AnActionEvent anActionEvent) {
31-
String defaultKey = Constants.DEFAULT_API_KEY_VAL;
32-
String defaultSercet = Constants.DEFAULT_SERCET_KEY_VAL;
33-
translate(anActionEvent, defaultSercet, defaultKey);
34+
String defaultKey = STATE.API_KEY;
35+
String defaultSecret = STATE.API_SECRET;
36+
translate(anActionEvent, defaultSecret, defaultKey);
3437
}
3538

3639

@@ -50,8 +53,11 @@ private void translate(AnActionEvent e, String sercet, String apiKey) {
5053

5154
String result = HttpClientPool.getHttpClient().get(Constants.genUrl(sercet, apiKey, text));
5255
ApiResult data = gson.fromJson(result, ApiResult.class);
53-
showPop(e, editor, resolveResult(text, data));
54-
56+
if(data.getErrorCode().equals("202")){
57+
Messages.showErrorDialog("请确认setting里面的ApiKey和ApiSecret是否正确","Api验证错误");
58+
}else{
59+
showPop(e, editor, resolveResult(text, data));
60+
}
5561
} catch (Exception exception) {
5662
onParseError(exception);
5763
// if(State.instance().isDebug()){

src/com/qt/config/Config.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* Created by KADO on 15/12/17.
1515
*/
1616
@State(
17-
name = "var_trans",
18-
storages = {
19-
@Storage(
20-
id = "varTrans",
21-
file = "$APP_CONFIG$/var_trans_mode_setting.xml"
22-
)
23-
}
17+
name = "var_trans",
18+
storages = {
19+
@Storage(
20+
id = "varTrans",
21+
file = "$APP_CONFIG$/var_trans_mode_setting.xml"
22+
)
23+
}
2424
)
2525
public class Config implements PersistentStateComponent<Config.State> {
2626

@@ -44,8 +44,9 @@ public Config() {
4444
}
4545

4646
public void defaultInitState() {
47-
48-
state.IS_POP=true;
47+
state.API_KEY = "108770214d7b90c8";
48+
state.API_SECRET = "oRTxCoNRm2DKQUDbI7ajiD5p5QPuNUf0";
49+
state.IS_POP = true;
4950
}
5051

5152
public static Config getInstance() {
@@ -56,9 +57,11 @@ public static final class State {
5657
/**
5758
* 是否显示列表
5859
*/
59-
public boolean IS_POP=true;
60-
61-
60+
public boolean IS_POP = true;
61+
public String API_KEY= "108770214d7b90c8";
62+
public String API_SECRET = "oRTxCoNRm2DKQUDbI7ajiD5p5QPuNUf0";
63+
public String DEFAULT_API_SECRET="oRTxCoNRm2DKQUDbI7ajiD5p5QPuNUf0";
64+
public String DEFAULT_API_KEY="108770214d7b90c8";
6265
}
6366

6467

src/com/qt/gui/ResultDialog.form

-13
This file was deleted.

src/com/qt/gui/ResultDialog.java

-4
This file was deleted.

src/com/qt/gui/Setting.form

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.qt.gui.Setting">
3+
<grid id="27dc6" binding="rootPanel" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="0" left="0" bottom="0" right="0"/>
5+
<constraints>
6+
<xy x="20" y="20" width="329" height="400"/>
7+
</constraints>
8+
<properties/>
9+
<border type="none"/>
10+
<children>
11+
<vspacer id="a00c3">
12+
<constraints>
13+
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
14+
</constraints>
15+
</vspacer>
16+
<component id="e31db" class="javax.swing.JLabel">
17+
<constraints>
18+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
19+
</constraints>
20+
<properties>
21+
<text value="Api secret"/>
22+
</properties>
23+
</component>
24+
<component id="c2f7a" class="javax.swing.JTextField" binding="apiSecret">
25+
<constraints>
26+
<grid row="1" column="1" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
27+
<preferred-size width="150" height="-1"/>
28+
</grid>
29+
</constraints>
30+
<properties/>
31+
</component>
32+
<grid id="12b9c" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
33+
<margin top="0" left="0" bottom="0" right="0"/>
34+
<constraints>
35+
<grid row="5" column="2" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
36+
</constraints>
37+
<properties/>
38+
<border type="none"/>
39+
<children/>
40+
</grid>
41+
<component id="f8b70" class="javax.swing.JLabel">
42+
<constraints>
43+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
44+
</constraints>
45+
<properties>
46+
<text value="default key"/>
47+
</properties>
48+
</component>
49+
<component id="cada1" class="javax.swing.JTextField" binding="defaultKey">
50+
<constraints>
51+
<grid row="2" column="1" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
52+
<preferred-size width="150" height="-1"/>
53+
</grid>
54+
</constraints>
55+
<properties/>
56+
</component>
57+
<component id="17148" class="javax.swing.JLabel">
58+
<constraints>
59+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
60+
</constraints>
61+
<properties>
62+
<text value="default secret"/>
63+
</properties>
64+
</component>
65+
<component id="47ec2" class="javax.swing.JTextField" binding="defaultSecret">
66+
<constraints>
67+
<grid row="3" column="1" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
68+
<preferred-size width="150" height="-1"/>
69+
</grid>
70+
</constraints>
71+
<properties/>
72+
</component>
73+
<component id="3824" class="javax.swing.JLabel" binding="addr">
74+
<constraints>
75+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
76+
</constraints>
77+
<properties>
78+
<text value="Api key 申请地址 "/>
79+
</properties>
80+
</component>
81+
<component id="c9fc3" class="javax.swing.JLabel">
82+
<constraints>
83+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
84+
</constraints>
85+
<properties>
86+
<text value="Api key"/>
87+
</properties>
88+
</component>
89+
<component id="f4023" class="javax.swing.JTextField" binding="apiKey">
90+
<constraints>
91+
<grid row="0" column="1" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
92+
<preferred-size width="150" height="-1"/>
93+
</grid>
94+
</constraints>
95+
<properties>
96+
<text value=""/>
97+
</properties>
98+
</component>
99+
</children>
100+
</grid>
101+
</form>

src/com/qt/gui/Setting.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.qt.gui;
2+
3+
import com.intellij.openapi.options.Configurable;
4+
import com.intellij.openapi.options.ConfigurationException;
5+
import com.qt.config.Config;
6+
import org.jetbrains.annotations.Nls;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import javax.swing.*;
10+
11+
public class Setting implements Configurable {
12+
private JTextField apiKey;
13+
private JTextField apiSecret;
14+
private JPanel rootPanel;
15+
private JTextField defaultKey;
16+
private JTextField defaultSecret;
17+
private JLabel addr;
18+
19+
20+
@Nls
21+
@Override
22+
public String getDisplayName() {
23+
return "varTrans";
24+
}
25+
26+
@Nullable
27+
@Override
28+
public JComponent createComponent() {
29+
apiKey.setText( Config.getInstance().getState().API_KEY);
30+
apiSecret.setText( Config.getInstance().getState().API_SECRET);
31+
defaultKey.setText( Config.getInstance().getState().DEFAULT_API_KEY);
32+
defaultSecret.setText( Config.getInstance().getState().DEFAULT_API_SECRET);
33+
addr.setText("<html>Api Key申请地址 <a href='http://ai.youdao.com'>http://ai.youdao.com");
34+
return rootPanel;
35+
}
36+
37+
@Override
38+
public boolean isModified() {
39+
if (apiKey.getText().trim().equals("")
40+
|| apiSecret.getText().trim().equals("")) {
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
47+
@Override
48+
public void apply() throws ConfigurationException {
49+
if (apiKey.getText().trim().equals("")
50+
) {
51+
throw new ConfigurationException("apiKey is empty ");
52+
}
53+
if (apiSecret.getText().trim().equals("")
54+
) {
55+
throw new ConfigurationException("apiSecret is empty");
56+
}
57+
Config.getInstance().getState().API_KEY = apiKey.getText();
58+
Config.getInstance().getState().API_SECRET = apiSecret.getText();
59+
}
60+
}

src/com/qt/var/AboutAction.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.qt.var;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.ui.Messages;
6+
import com.qt.base.BaseConfigAction;
7+
8+
/**
9+
* 显示软件基本信息
10+
*/
11+
public class AboutAction extends AnAction {
12+
13+
14+
@Override
15+
public void actionPerformed(AnActionEvent anActionEvent) {
16+
Messages.showInfoMessage("<html>一款中文转变量的插件,<br/><br/>作者:安静大叔<br/><br/><a href='https://github.com/quietUncle/vartrans'>https://github.com/quietUncle/vartrans","关于");
17+
}
18+
}

var.jar

2.46 KB
Binary file not shown.

0 commit comments

Comments
 (0)