Skip to content

Commit 4729576

Browse files
committed
Add proxy support
1 parent e605263 commit 4729576

File tree

7 files changed

+271
-9
lines changed

7 files changed

+271
-9
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
<extensions defaultExtensionNs="com.intellij">
3030
<!-- Add your extensions here -->
31+
<applicationConfigurable groupId="tools" id="Android.localize"
32+
instance="config.AndroidLocalizeConfigurable"/>
3133
</extensions>
3234

3335
<actions>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="config.AndroidLocalizeConfigurable">
3+
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="20" y="20" width="500" height="400"/>
7+
</constraints>
8+
<properties/>
9+
<border type="empty"/>
10+
<children>
11+
<grid id="92294" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
12+
<margin top="0" left="0" bottom="0" right="0"/>
13+
<constraints>
14+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
15+
</constraints>
16+
<properties/>
17+
<border type="none" title="Proxy Settings">
18+
<font/>
19+
<size top="0" left="0" bottom="0" right="0"/>
20+
</border>
21+
<children>
22+
<component id="c9a2e" class="javax.swing.JRadioButton" binding="noProxy">
23+
<constraints>
24+
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
25+
</constraints>
26+
<properties>
27+
<selected value="false"/>
28+
<text value="No proxy"/>
29+
</properties>
30+
</component>
31+
<component id="dd983" class="javax.swing.JRadioButton" binding="enableProxy">
32+
<constraints>
33+
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
34+
</constraints>
35+
<properties>
36+
<text value="Enable proxy"/>
37+
</properties>
38+
</component>
39+
<component id="c3ff4" class="javax.swing.JLabel" binding="hostNameLabel">
40+
<constraints>
41+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="2" use-parent-layout="false"/>
42+
</constraints>
43+
<properties>
44+
<text value="Host name:"/>
45+
</properties>
46+
</component>
47+
<component id="cf6fc" class="javax.swing.JTextField" binding="hostNameField">
48+
<constraints>
49+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
50+
<preferred-size width="150" height="-1"/>
51+
</grid>
52+
</constraints>
53+
<properties/>
54+
</component>
55+
<component id="58ba3" class="javax.swing.JLabel" binding="portNumberLabel">
56+
<constraints>
57+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="2" use-parent-layout="false"/>
58+
</constraints>
59+
<properties>
60+
<text value="Port number:"/>
61+
</properties>
62+
</component>
63+
<component id="f0913" class="javax.swing.JSpinner" binding="portNumberSpinner">
64+
<constraints>
65+
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
66+
</constraints>
67+
<properties/>
68+
</component>
69+
</children>
70+
</grid>
71+
<vspacer id="b27c6">
72+
<constraints>
73+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
74+
</constraints>
75+
</vspacer>
76+
</children>
77+
</grid>
78+
<buttonGroups>
79+
<group name="proxyGroup">
80+
<member id="c9a2e"/>
81+
<member id="dd983"/>
82+
</group>
83+
</buttonGroups>
84+
</form>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2018 Airsaid. https://github.com/airsaid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package config;
18+
19+
import com.intellij.openapi.options.Configurable;
20+
import org.jetbrains.annotations.Nls;
21+
import org.jetbrains.annotations.Nullable;
22+
23+
import javax.swing.*;
24+
import java.awt.event.ItemEvent;
25+
26+
/**
27+
* @author airsaid
28+
*/
29+
public class AndroidLocalizeConfigurable implements Configurable {
30+
31+
private JPanel myPanel;
32+
private JRadioButton noProxy;
33+
private JLabel hostNameLabel;
34+
private JRadioButton enableProxy;
35+
private JLabel portNumberLabel;
36+
private JTextField hostNameField;
37+
private JSpinner portNumberSpinner;
38+
39+
private boolean isEnableProxy = PluginConfig.isEnableProxy();
40+
private String hostName = PluginConfig.getHostName();
41+
private int portNumber = PluginConfig.getPortNumber();
42+
43+
@Nls(capitalization = Nls.Capitalization.Title)
44+
@Override
45+
public String getDisplayName() {
46+
return "Android Localize";
47+
}
48+
49+
@Nullable
50+
@Override
51+
public JComponent createComponent() {
52+
init();
53+
return myPanel;
54+
}
55+
56+
private void init() {
57+
enableProxy(isEnableProxy);
58+
hostNameField.setText(hostName);
59+
portNumberSpinner.setValue(portNumber);
60+
61+
noProxy.addItemListener(e -> {
62+
if (e.getStateChange() == ItemEvent.SELECTED) {
63+
enableProxy(false);
64+
}
65+
});
66+
67+
enableProxy.addItemListener(e -> {
68+
if (e.getStateChange() == ItemEvent.SELECTED) {
69+
enableProxy(true);
70+
}
71+
});
72+
}
73+
74+
private void enableProxy(boolean enabled) {
75+
noProxy.setSelected(!enabled);
76+
enableProxy.setSelected(enabled);
77+
hostNameLabel.setEnabled(enabled);
78+
hostNameField.setEnabled(enabled);
79+
portNumberLabel.setEnabled(enabled);
80+
portNumberSpinner.setEnabled(enabled);
81+
}
82+
83+
@Override
84+
public boolean isModified() {
85+
boolean modifyEnable = isEnableProxy != enableProxy.isSelected();
86+
boolean modifyHostName = !hostName.equals(hostNameField.getText());
87+
boolean modifyPortNumber = portNumber != Integer.parseInt(portNumberSpinner.getValue().toString());
88+
return modifyEnable || modifyHostName || modifyPortNumber;
89+
}
90+
91+
@Override
92+
public void apply() {
93+
PluginConfig.setEnableProxy(enableProxy.isSelected());
94+
PluginConfig.setHostName(hostNameField.getText());
95+
PluginConfig.setPortNumber(Integer.parseInt(portNumberSpinner.getValue().toString()));
96+
}
97+
98+
}

src/config/PluginConfig.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2018 Airsaid. https://github.com/airsaid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package config;
18+
19+
import com.intellij.ide.util.PropertiesComponent;
20+
21+
/**
22+
* @author airsaid
23+
*/
24+
public final class PluginConfig {
25+
26+
private static final String PLUGIN_ID = "com.airsaid.androidLocalize";
27+
28+
private static final String KEY_ENABLE_PROXY = PLUGIN_ID.concat(".enableProxy");
29+
30+
private static final String KEY_HOST_NAME = PLUGIN_ID.concat(".hostName");
31+
32+
private static final String KEY_PORT_NUMBER = PLUGIN_ID.concat(".portNumber");
33+
34+
public static boolean isEnableProxy() {
35+
return PropertiesComponent.getInstance().getBoolean(KEY_ENABLE_PROXY, false);
36+
}
37+
38+
public static void setEnableProxy(boolean enable) {
39+
PropertiesComponent.getInstance().setValue(KEY_ENABLE_PROXY, enable);
40+
}
41+
42+
public static String getHostName() {
43+
return PropertiesComponent.getInstance().getValue(KEY_HOST_NAME, "");
44+
}
45+
46+
public static void setHostName(String hostName) {
47+
PropertiesComponent.getInstance().setValue(KEY_HOST_NAME, hostName, "");
48+
}
49+
50+
public static int getPortNumber() {
51+
return PropertiesComponent.getInstance().getInt(KEY_PORT_NUMBER, 0);
52+
}
53+
54+
public static void setPortNumber(int port) {
55+
PropertiesComponent.getInstance().setValue(KEY_PORT_NUMBER, port, 0);
56+
}
57+
58+
}

src/logic/ParseStringXml.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public static List<AndroidString> parse(@Nullable ProgressIndicator progressIndi
6060
if (rootTag != null) {
6161
XmlTag[] stringTags = rootTag.findSubTags("string");
6262
for (XmlTag stringTag : stringTags) {
63+
if (progressIndicator != null && progressIndicator.isCanceled()) {
64+
break;
65+
}
66+
6367
String name = stringTag.getAttributeValue("name");
6468
String translatableStr = stringTag.getAttributeValue("translatable");
6569
boolean translatable = Boolean.valueOf(translatableStr == null ? "true" : translatableStr);

src/task/TranslateTask.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,48 @@ public TranslateTask(@Nullable Project project, @Nls @NotNull String title, List
7272
public void run(@NotNull ProgressIndicator progressIndicator) {
7373
boolean isOverwriteExistingString = PropertiesComponent.getInstance(myProject)
7474
.getBoolean(Constants.KEY_IS_OVERWRITE_EXISTING_STRING);
75-
7675
Querier<AbstractTranslator> translator = new Querier<>();
7776
GoogleTranslator googleTranslator = new GoogleTranslator();
7877
translator.attach(googleTranslator);
7978
mWriteData.clear();
8079

8180
for (LANG toLanguage : mLanguages) {
81+
if (progressIndicator.isCanceled()) break;
82+
8283
progressIndicator.setText("Translating in the " + toLanguage.getEnglishName() + " language...");
8384

8485
if (isOverwriteExistingString) {
85-
translate(translator, toLanguage, null);
86+
translate(progressIndicator, translator, toLanguage, null);
8687
continue;
8788
}
8889

8990
ApplicationManager.getApplication().runReadAction(() -> {
9091
VirtualFile virtualFile = getVirtualFile(toLanguage);
9192

9293
if (virtualFile == null) {
93-
translate(translator, toLanguage, null);
94+
translate(progressIndicator, translator, toLanguage, null);
9495
return;
9596
}
9697

9798
PsiFile psiFile = PsiManager.getInstance(myProject).findFile(virtualFile);
9899
if (psiFile == null) {
99-
translate(translator, toLanguage, null);
100+
translate(progressIndicator, translator, toLanguage, null);
100101
return;
101102
}
102103

103104
List<AndroidString> androidStrings = ParseStringXml.parse(progressIndicator, psiFile);
104-
translate(translator, toLanguage, androidStrings);
105+
translate(progressIndicator, translator, toLanguage, androidStrings);
105106
});
106107
}
107108
googleTranslator.close();
108109
writeResultData(progressIndicator);
109110
}
110111

111-
private void translate(Querier<AbstractTranslator> translator, LANG toLanguage, @Nullable List<AndroidString> list) {
112+
private void translate(@NotNull ProgressIndicator progressIndicator, Querier<AbstractTranslator> translator, LANG toLanguage, @Nullable List<AndroidString> list) {
112113
List<AndroidString> writeAndroidString = new ArrayList<>();
113114
for (AndroidString androidString : mAndroidStrings) {
115+
if (progressIndicator.isCanceled()) break;
116+
114117
if (!androidString.isTranslatable()) {
115118
continue;
116119
}
@@ -137,13 +140,17 @@ private void translate(Querier<AbstractTranslator> translator, LANG toLanguage,
137140
}
138141

139142
private void writeResultData(ProgressIndicator progressIndicator) {
143+
if (progressIndicator.isCanceled()) return;
144+
140145
if (mWriteData == null) {
141146
translateError(new IllegalArgumentException("No translate data."));
142147
return;
143148
}
144149

145150
Set<String> keySet = mWriteData.keySet();
146151
for (String key : keySet) {
152+
if (progressIndicator.isCanceled()) break;
153+
147154
File writeFile = getWriteFileForCode(key);
148155
progressIndicator.setText("Write to " + writeFile.getParentFile().getName() + " data...");
149156
write(writeFile, mWriteData.get(key));

src/translate/trans/impl/GoogleTranslator.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package translate.trans.impl;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import config.PluginConfig;
45
import org.apache.http.HttpEntity;
6+
import org.apache.http.HttpHost;
57
import org.apache.http.client.config.RequestConfig;
68
import org.apache.http.client.methods.CloseableHttpResponse;
79
import org.apache.http.client.methods.HttpGet;
@@ -154,11 +156,18 @@ public String query() throws Exception {
154156
uri.addParameter(key, value);
155157
}
156158
HttpGet request = new HttpGet(uri.toString());
157-
RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT)
159+
160+
RequestConfig.Builder builder = RequestConfig.copy(RequestConfig.DEFAULT)
158161
.setSocketTimeout(5000)
159162
.setConnectTimeout(5000)
160-
.setConnectionRequestTimeout(5000)
161-
.build();
163+
.setConnectionRequestTimeout(5000);
164+
165+
if (PluginConfig.isEnableProxy()) {
166+
HttpHost proxy = new HttpHost(PluginConfig.getHostName(), PluginConfig.getPortNumber());
167+
builder.setProxy(proxy);
168+
}
169+
170+
RequestConfig config = builder.build();
162171
request.setConfig(config);
163172
CloseableHttpResponse response = httpClient.execute(request);
164173
HttpEntity entity = response.getEntity();

0 commit comments

Comments
 (0)