() {
129 | @Override
130 | public Point compute() {
131 | JRootPane rootPane = SwingUtilities.getRootPane(getWindow().getParent());
132 | if (rootPane == null) {
133 | rootPane = SwingUtilities.getRootPane(getWindow().getOwner());
134 | }
135 |
136 | Point p = rootPane.getLocationOnScreen();
137 | p.x += (rootPane.getWidth() - getWindow().getWidth()) / 2;
138 | return p;
139 | }
140 | });
141 | animate();
142 | if (SystemInfo.isJavaVersionAtLeast("1.7")) {
143 | try {
144 | Method method = Class.forName("java.awt.Window").getDeclaredMethod("setOpacity", float.class);
145 | if (method != null) method.invoke(getPeer().getWindow(), .8f);
146 | } catch (Exception exception) {
147 | }
148 | }
149 | setAutoAdjustable(false);
150 | setSize(getPreferredSize().width, 0);//initial state before animation, zero height
151 | }
152 | super.show();
153 | }
154 |
155 | private void animate() {
156 | final int height = getPreferredSize().height;
157 | final int frameCount = 10;
158 | final boolean toClose = isShowing();
159 |
160 |
161 | final AtomicInteger i = new AtomicInteger(-1);
162 | final Alarm animator = new Alarm(myDisposable);
163 | final Runnable runnable = new Runnable() {
164 | @Override
165 | public void run() {
166 | int state = i.addAndGet(1);
167 |
168 | double linearProgress = (double) state / frameCount;
169 | if (toClose) {
170 | linearProgress = 1 - linearProgress;
171 | }
172 | myLayout.myPhase = (1 - Math.cos(Math.PI * linearProgress)) / 2;
173 | Window window = getPeer().getWindow();
174 | Rectangle bounds = window.getBounds();
175 | bounds.height = (int) (height * myLayout.myPhase);
176 |
177 | window.setBounds(bounds);
178 |
179 | if (state == 0 && !toClose && window.getOwner() instanceof IdeFrame) {
180 | WindowManager.getInstance().requestUserAttention((IdeFrame) window.getOwner(), true);
181 | }
182 |
183 | if (state < frameCount) {
184 | animator.addRequest(this, 10);
185 | } else if (toClose) {
186 | AddFilterRuleDialog.super.dispose();
187 | }
188 | }
189 | };
190 | animator.addRequest(runnable, 10, ModalityState.stateForComponent(getRootPane()));
191 | }
192 |
193 | protected JComponent doCreateCenterPanel() {
194 | JPanel panel = new JPanel(new BorderLayout(5, 0));
195 |
196 | FilterRule.FilterRuleType[] types = FilterRule.FilterRuleType.values();
197 |
198 | ruleType = new ComboBox(types);
199 | ruleType.setEnabled(true);
200 | ruleType.setSelectedIndex(0);
201 |
202 | panel.add(ruleType, BorderLayout.WEST);
203 |
204 | filterName = new JTextField(20);
205 | PromptSupport.setPrompt("Set the string name here", filterName);
206 | panel.add(filterName, BorderLayout.CENTER);
207 |
208 | return panel;
209 | }
210 |
211 | @Override
212 | protected void doHelpAction() {
213 | // do nothing
214 | }
215 |
216 | private static class MyBorderLayout extends BorderLayout {
217 | private double myPhase = 0;//it varies from 0 (hidden state) to 1 (fully visible)
218 |
219 | private MyBorderLayout() {
220 | }
221 |
222 | @Override
223 | public void layoutContainer(Container target) {
224 | final Dimension realSize = target.getSize();
225 | target.setSize(target.getPreferredSize());
226 |
227 | super.layoutContainer(target);
228 |
229 | target.setSize(realSize);
230 |
231 | synchronized (target.getTreeLock()) {
232 | int yShift = (int) ((1 - myPhase) * target.getPreferredSize().height);
233 | Component[] components = target.getComponents();
234 | for (Component component : components) {
235 | Point point = component.getLocation();
236 | point.y -= yShift;
237 | component.setLocation(point);
238 | }
239 | }
240 | }
241 | }
242 | }
243 |
--------------------------------------------------------------------------------
/src/util/MyURLEncoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
3 | * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4 | *
5 | *
6 | *
7 | *
8 | *
9 | *
10 | *
11 | *
12 | *
13 | *
14 | *
15 | *
16 | *
17 | *
18 | *
19 | *
20 | *
21 | *
22 | *
23 | *
24 | */
25 |
26 | package util;
27 |
28 | import sun.security.action.GetPropertyAction;
29 |
30 | import java.io.CharArrayWriter;
31 | import java.io.UnsupportedEncodingException;
32 | import java.net.URLDecoder;
33 | import java.nio.charset.Charset;
34 | import java.nio.charset.IllegalCharsetNameException;
35 | import java.nio.charset.UnsupportedCharsetException;
36 | import java.security.AccessController;
37 | import java.util.BitSet;
38 |
39 | /**
40 | * Utility class for HTML form encoding. This class contains static methods
41 | * for converting a String to the application/x-www-form-urlencoded MIME
42 | * format. For more information about HTML form encoding, consult the HTML
43 | * specification.
44 | *
45 | *
46 | * When encoding a String, the following rules apply:
47 | *
48 | *
49 | * - The alphanumeric characters "{@code a}" through
50 | * "{@code z}", "{@code A}" through
51 | * "{@code Z}" and "{@code 0}"
52 | * through "{@code 9}" remain the same.
53 | *
- The special characters "{@code .}",
54 | * "{@code -}", "{@code *}", and
55 | * "{@code _}" remain the same.
56 | *
- The space character " " is
57 | * converted into a plus sign "{@code +}".
58 | *
- All other characters are unsafe and are first converted into
59 | * one or more bytes using some encoding scheme. Then each byte is
60 | * represented by the 3-character string
61 | * "{@code %xy}", where xy is the
62 | * two-digit hexadecimal representation of the byte.
63 | * The recommended encoding scheme to use is UTF-8. However,
64 | * for compatibility reasons, if an encoding is not specified,
65 | * then the default encoding of the platform is used.
66 | *
67 | *
68 | *
69 | * For example using UTF-8 as the encoding scheme the string "The
70 | * string ü@foo-bar" would get converted to
71 | * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character
72 | * ü is encoded as two bytes C3 (hex) and BC (hex), and the
73 | * character @ is encoded as one byte 40 (hex).
74 | *
75 | * @author Herb Jellinek
76 | * @since JDK1.0
77 | */
78 | public class MyURLEncoder {
79 | static BitSet dontNeedEncoding;
80 | static final int caseDiff = ('a' - 'A');
81 | static String dfltEncName = null;
82 |
83 | static {
84 |
85 | /* The list of characters that are not encoded has been
86 | * determined as follows:
87 | *
88 | * RFC 2396 states:
89 | * -----
90 | * Data characters that are allowed in a URI but do not have a
91 | * reserved purpose are called unreserved. These include upper
92 | * and lower case letters, decimal digits, and a limited set of
93 | * punctuation marks and symbols.
94 | *
95 | * unreserved = alphanum | mark
96 | *
97 | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
98 | *
99 | * Unreserved characters can be escaped without changing the
100 | * semantics of the URI, but this should not be done unless the
101 | * URI is being used in a context that does not allow the
102 | * unescaped character to appear.
103 | * -----
104 | *
105 | * It appears that both Netscape and Internet Explorer escape
106 | * all special characters from this list with the exception
107 | * of "-", "_", ".", "*". While it is not clear why they are
108 | * escaping the other characters, perhaps it is safest to
109 | * assume that there might be contexts in which the others
110 | * are unsafe if not escaped. Therefore, we will use the same
111 | * list. It is also noteworthy that this is consistent with
112 | * O'Reilly's "HTML: The Definitive Guide" (page 164).
113 | *
114 | * As a last note, Intenet Explorer does not encode the "@"
115 | * character which is clearly not unreserved according to the
116 | * RFC. We are being consistent with the RFC in this matter,
117 | * as is Netscape.
118 | *
119 | */
120 |
121 | dontNeedEncoding = new BitSet(256);
122 | int i;
123 | for (i = 'a'; i <= 'z'; i++) {
124 | dontNeedEncoding.set(i);
125 | }
126 | for (i = 'A'; i <= 'Z'; i++) {
127 | dontNeedEncoding.set(i);
128 | }
129 | for (i = '0'; i <= '9'; i++) {
130 | dontNeedEncoding.set(i);
131 | }
132 | dontNeedEncoding.set(' '); /* encoding a space to a + is done
133 | * in the encode() method */
134 | dontNeedEncoding.set('-');
135 | dontNeedEncoding.set('_');
136 | dontNeedEncoding.set('.');
137 | dontNeedEncoding.set('*');
138 |
139 | dfltEncName = AccessController.doPrivileged(
140 | new GetPropertyAction("file.encoding")
141 | );
142 | }
143 |
144 | /**
145 | * You can't call the constructor.
146 | */
147 | private MyURLEncoder() { }
148 |
149 | /**
150 | * Translates a string into {@code x-www-form-urlencoded}
151 | * format. This method uses the platform's default encoding
152 | * as the encoding scheme to obtain the bytes for unsafe characters.
153 | *
154 | * @param s {@code String} to be translated.
155 | * @deprecated The resulting string may vary depending on the platform's
156 | * default encoding. Instead, use the encode(String,String)
157 | * method to specify the encoding.
158 | * @return the translated {@code String}.
159 | */
160 | @Deprecated
161 | public static String encode(String s) {
162 |
163 | String str = null;
164 |
165 | try {
166 | str = encode(s, dfltEncName);
167 | } catch (UnsupportedEncodingException e) {
168 | // The system should always have the platform default
169 | }
170 |
171 | return str;
172 | }
173 |
174 | /**
175 | * Translates a string into {@code application/x-www-form-urlencoded}
176 | * format using a specific encoding scheme. This method uses the
177 | * supplied encoding scheme to obtain the bytes for unsafe
178 | * characters.
179 | *
180 | * Note: The
182 | * World Wide Web Consortium Recommendation states that
183 | * UTF-8 should be used. Not doing so may introduce
184 | * incompatibilities.
185 | *
186 | * @param s {@code String} to be translated.
187 | * @param enc The name of a supported
188 | * character
189 | * encoding.
190 | * @return the translated {@code String}.
191 | * @exception UnsupportedEncodingException
192 | * If the named encoding is not supported
193 | * @see URLDecoder#decode(String, String)
194 | * @since 1.4
195 | */
196 | public static String encode(String s, String enc)
197 | throws UnsupportedEncodingException {
198 |
199 | boolean needToChange = false;
200 | StringBuffer out = new StringBuffer(s.length());
201 | Charset charset;
202 | CharArrayWriter charArrayWriter = new CharArrayWriter();
203 |
204 | if (enc == null)
205 | throw new NullPointerException("charsetName");
206 |
207 | try {
208 | charset = Charset.forName(enc);
209 | } catch (IllegalCharsetNameException e) {
210 | throw new UnsupportedEncodingException(enc);
211 | } catch (UnsupportedCharsetException e) {
212 | throw new UnsupportedEncodingException(enc);
213 | }
214 |
215 | for (int i = 0; i < s.length();) {
216 | int c = (int) s.charAt(i);
217 | //System.out.println("Examining character: " + c);
218 | if (dontNeedEncoding.get(c)) {
219 | if (c == ' ') {
220 | c = '+';
221 | needToChange = true;
222 | }
223 | //System.out.println("Storing: " + c);
224 | out.append((char)c);
225 | i++;
226 | } else {
227 | // convert to external encoding before hex conversion
228 | do {
229 | charArrayWriter.write(c);
230 | /*
231 | * If this character represents the start of a Unicode
232 | * surrogate pair, then pass in two characters. It's not
233 | * clear what should be done if a bytes reserved in the
234 | * surrogate pairs range occurs outside of a legal
235 | * surrogate pair. For now, just treat it as if it were
236 | * any other character.
237 | */
238 | if (c >= 0xD800 && c <= 0xDBFF) {
239 | /*
240 | System.out.println(Integer.toHexString(c)
241 | + " is high surrogate");
242 | */
243 | if ( (i+1) < s.length()) {
244 | int d = (int) s.charAt(i+1);
245 | /*
246 | System.out.println("\tExamining "
247 | + Integer.toHexString(d));
248 | */
249 | if (d >= 0xDC00 && d <= 0xDFFF) {
250 | /*
251 | System.out.println("\t"
252 | + Integer.toHexString(d)
253 | + " is low surrogate");
254 | */
255 | charArrayWriter.write(d);
256 | i++;
257 | }
258 | }
259 | }
260 | i++;
261 | } while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));
262 |
263 | charArrayWriter.flush();
264 | String str = new String(charArrayWriter.toCharArray());
265 | byte[] ba = str.getBytes(charset);
266 | for (int j = 0; j < ba.length; j++) {
267 | out.append('%');
268 | char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
269 | // converting to use uppercase letter as part of
270 | // the hex value if ch is a letter.
271 | if (Character.isLetter(ch)) {
272 | ch -= caseDiff;
273 | }
274 | out.append(ch);
275 | ch = Character.forDigit(ba[j] & 0xF, 16);
276 | if (Character.isLetter(ch)) {
277 | ch -= caseDiff;
278 | }
279 | out.append(ch);
280 | }
281 | charArrayWriter.reset();
282 | needToChange = true;
283 | }
284 | }
285 |
286 | return (needToChange? out.toString() : s);
287 | }
288 | }
289 |
--------------------------------------------------------------------------------
/src/module/SupportedLanguages.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2015 Wesley Lin
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 module;
18 |
19 | import language_engine.TranslationEngineType;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | /**
25 | * Created by Wesley Lin on 11/29/14.
26 | */
27 | public enum SupportedLanguages {
28 | //https://cloud.google.com/translate/docs/languages
29 | Afrikaans("af", "Afrikaans", "Afrikaans","南非荷兰语"),
30 | Albanian("sq", "Shqiptar", "Albanian","阿尔巴尼亚语"),
31 | Amharic("am", "አማርኛ", "Amharic","阿姆哈拉语"),
32 | Arabic("ar", "العربية", "Arabic","阿拉伯语"),
33 | Armenian("ar", "Հայերեն", "Armenian","亚美尼亚语"),
34 | Azerbaijani("az", "Azərbaycan", "Azerbaijani","阿塞拜疆语"),
35 | Basque("eu", "Euskal", "Basque","巴斯克语"),
36 | Belarusian("be", "Беларускі", "Belarusian","白俄罗斯语"),
37 | Bengali("bn", "বাঙালি", "Bengali","孟加拉语"),
38 | Bosnian("bs", "Bosanski", "Bosnian","波斯尼亚语"),
39 | Bulgarian("bg", "Български", "Bulgarian","保加利亚语"),
40 | Catalan("ca", "Català", "Catalan","加泰罗尼亚语"),
41 | Cebuano("ceb", "Cebuano", "Cebuano","宿务语"),
42 | Chinese_Simplified("zh-CN", "简体中文", "Chinese Simplified","中文(简体)"),
43 | Chinese_Simplified_BING("zh-CHS", "简体中文", "Chinese Simplified","中文(简体)"),
44 | Chinese_Traditional("zh-TW", "正體中文", "Chinese Traditional","中文(繁体)"),
45 | Chinese_Traditional_BING("zh-CHT", "正體中文", "Chinese Traditional","中文(繁体)"),
46 | Corsican("co", "Corsu", "Corsican","科西嘉语"),
47 | Croatian("hr", "Hrvatski", "Croatian","克罗地亚语"),
48 | Czech("cs", "Čeština", "Czech","捷克语"),
49 | Danish("da", "Dansk", "Danish","丹麦语"),
50 | Dutch("nl", "Nederlands", "Dutch","荷兰语"),
51 | English("en", "English", "English","英语"),
52 | Esperanto("eo", "Esperanta", "Esperanto","世界语"),
53 | Estonian("et", "Eesti", "Estonian","爱沙尼亚语"),
54 | Filipino("tl", "Pilipino", "Filipino","菲律宾语"),
55 | Finnish("fi", "Suomi", "Finnish","芬兰语"),
56 | French("fr", "Français", "French","法语"),
57 | //Frisian("fy", "Frysk", "Frisian","弗里斯兰语"),
58 | Galician("gl", "Galego", "Galician","加利西亚语"),
59 | Georgian("ka", "ქართული", "Georgian","格鲁吉亚语"),
60 | German("de", "Deutsch", "German","德语"),
61 | Greek("el", "Ελληνικά", "Greek","希腊语"),
62 | Gujarati("gu", "ગુજરાતી", "Gujarati","古吉拉特语"),
63 | Haitian_Creole("ht", "Haitiancreole", "Haitian Creole","海地克里奥尔语"),
64 | Hausa("ha", "Hausa", "Hausa","豪萨语"),
65 | Hawaiian("haw", "ʻ .lelo Hawaiʻi", "Hawaiian","夏威夷语"),
66 | Hebrew("iw", "עברית", "Hebrew","希伯来语"),
67 | Hebrew_BING("he", "עברית", "Hebrew","希伯来语"),
68 | Hindi("hi", "हिंदी", "Hindi","印地语"),
69 | Hungarian("hu", "Magyar", "Hungarian","匈牙利语"),
70 | Icelandic("is", "Icelandic", "Icelandic","冰岛语"),
71 | Igbo("ig", "Ndi Igbo", "Igbo","伊博语"),
72 | Indonesian("id", "Indonesia", "Indonesian","印度尼西亚语"),
73 | Irish("ga", "Irish", "Irish","爱尔兰语"),
74 | Italian("it", "Italiano", "Italian","意大利语"),
75 | Japanese("ja", "日本語", "Japanese","日语"),
76 | Javanese("jv", "Basa Jawa", "Javanese","爪哇语"),
77 | Kannada("kn", "ಕನ್ನಡ", "Kannada","卡纳达语"),
78 | Korean("ko", "한국의", "Korean","韩语"),
79 | Latin("la", "Latina", "Latin","拉丁文"),
80 | Latvian("lv", "Latvijas", "Latvian","拉脱维亚语"),
81 | Lithuanian("lt", "Lietuvos", "Lithuanian","立陶宛语"),
82 | Macedonian("mk", "Македонски", "Macedonian","马其顿语"),
83 | Malay("ms", "Melayu", "Malay","马来语"),
84 | Maltese("mt", "Malti", "Maltese","马耳他语"),
85 | Norwegian("no", "Norsk", "Norwegian","挪威语"),
86 | Persian("fa", "فارسی", "Persian","波斯语"),
87 | Polish("pl", "Polski", "Polish","波兰语"),
88 | Portuguese("pt", "Português", "Portuguese","葡萄牙语"),
89 | Romanian("ro", "Român", "Romanian","罗马尼亚语"),
90 | Russian("ru", "Русский", "Russian","俄语"),
91 | Serbian("sr", "Српски", "Serbian","塞尔维亚语"),
92 | Slovak("sk", "Slovenčina", "Slovak","斯洛伐克语"),
93 | Slovenian("sl", "Slovenščina", "Slovenian","斯洛文尼亚语"),
94 | Spanish("es", "Español", "Spanish","西班牙语"),
95 | Swahili("sw", "Kiswahili", "Swahili","斯瓦希里语"),
96 | Swedish("sv", "Svenska", "Swedish","瑞典语"),
97 | Tamil("ta", "தமிழ்", "Tamil","泰米尔语"),
98 | Telugu("te", "తెలుగు", "Telugu","泰卢固语"),
99 | Thai("th", "ไทย", "Thai","泰文"),
100 | Turkish("tr", "Türk", "Turkish","土耳其语"),
101 | Ukrainian("uk", "Український", "Ukrainian","乌克兰语"),
102 | Urdu("ur", "اردو", "Urdu","乌尔都语"),
103 | Vietnamese("vi", "Tiếng Việt", "Vietnamese","越南语"),
104 | Welsh("cy", "Cymraeg", "Welsh","威尔士语"),
105 | Yiddish("yi", "ייִדיש", "Yiddish","意第绪语"),
106 |
107 | //http://api.fanyi.baidu.com/doc/21
108 | AUTO_BAIDU("auto","自动检测","auto check","自动检测"),
109 | Chinese_Simplified_BAIDU("zh", "简体中文", "Chinese Simplified","中文简体","zh-rCN"),
110 | English_BAIDU("en", "English", "English","英语","en"),
111 | Japanese_BAIDU("jp","日本語","Japanese","日语","ja"),
112 | Korean_BAIDU("kor","한국어","Korean","韩语","ko"),
113 | French_BAIDU("fra", "Français", "French","法语","fr"),
114 | Spanish_BAIDU("spa", "Español", "Spanish","西班牙语","es"),
115 | Thai_BAIDU("th", "ไทย", "Thai","泰语","th"),
116 | Arabic_BAIDU("ara", "العربية", "Arabic","阿拉伯语","ar"),
117 | Russian_BAIDU("ru", "Русский", "Russian","俄语","ru"),
118 | Portuguese_BAIDU("pt", "Português", "Portuguese","葡萄牙语","pt"),
119 | German_BAIDU("de", "Deutsch", "German","德语","de"),
120 | Italian_BAIDU("it", "Italiano", "Italian","意大利语","it"),
121 | Greek_BAIDU("el", "Ελληνικά", "Greek","希腊语","el"),
122 | Dutch_BAIDU("nl", "Nederlands", "Dutch","荷兰语","nl"),
123 | Polish_BAIDU("pl", "Polski", "Polish","波兰语","pl"),
124 | Bulgarian_BAIDU("bul", "Български", "Bulgarian","保加利亚语","bg"),
125 | Estonian_BAIDU("est", "Eesti", "Estonian","爱沙尼亚语","et"),
126 | Danish_BAIDU("dan", "Dansk", "Danish","丹麦语","da"),
127 | Finnish_BAIDU("fin", "Suomi", "Finnish","芬兰语","fi"),
128 | Czech_BAIDU("cs", "Čeština", "Czech","捷克语","cs"),
129 | Romanian_BAIDU("rom", "Român", "Romanian","罗马尼亚语","ro"),
130 | Slovenian_BAIDU("slo", "Slovenščina", "Slovenian","斯洛文尼亚语","sl"),
131 | Swedish_BAIDU("swe", "Svenska", "Swedish","瑞典语","sv"),
132 | Hungarian_BAIDU("hu", "Magyar", "Hungarian","匈牙利语","hu"),
133 | Chinese_Traditional_BAIDU("cht", "正體中文", "Chinese Traditional","中文繁体","zh-rTW"),
134 | Vietnamese_BAIDU("vie", "Tiếng Việt", "Vietnamese","越南语","vi");
135 |
136 | private String languageCode;
137 | private String languageDisplayName;
138 | private String languageEnglishDisplayName;
139 | private String languageChineseDisplayName;
140 | private String realLanguageCode;
141 |
142 | SupportedLanguages(String languageCode, String languageDisplayName, String languageEnglishDisplayName,String languageChineseDisplayName) {
143 | this.languageCode = languageCode;
144 | this.languageDisplayName = languageDisplayName;
145 | this.languageEnglishDisplayName = languageEnglishDisplayName;
146 | this.languageChineseDisplayName = languageChineseDisplayName;
147 | }
148 | SupportedLanguages(String languageCode, String languageDisplayName, String languageEnglishDisplayName,String languageChineseDisplayName,String realLanguageCode) {
149 | this.languageCode = languageCode;
150 | this.languageDisplayName = languageDisplayName;
151 | this.languageEnglishDisplayName = languageEnglishDisplayName;
152 | this.languageChineseDisplayName = languageChineseDisplayName;
153 | this.realLanguageCode = realLanguageCode;
154 | }
155 | public String getLanguageCode() {
156 | return languageCode;
157 | }
158 |
159 | public String getLanguageDisplayName() {
160 | return languageDisplayName;
161 | }
162 |
163 | public String getLanguageEnglishDisplayName() {
164 | return languageEnglishDisplayName;
165 | }
166 | public String getLanguageChineseDisplayName() {
167 | return languageChineseDisplayName;
168 | }
169 | public String getRealLanguageCode() {
170 | return realLanguageCode;
171 | }
172 |
173 | public static List getAllSupportedLanguages(TranslationEngineType type) {
174 | switch (type) {
175 | case Baidu:
176 | return getBaiduLanguages();
177 | case Bing:
178 | return getBingLanguages();
179 | case Google:
180 | return getGoogleLanguages();
181 | }
182 | return null;
183 | }
184 |
185 | public String toString() {
186 | return getLanguageEnglishDisplayName() + "(\"" + getLanguageCode() + "\", \"" + getLanguageDisplayName() + "\")";
187 | }
188 |
189 | // get the right value-XX suffix
190 | public String getAndroidStringFolderNameSuffix() {
191 | if (this.name().contains("BAIDU")){
192 | System.out.println(this.toString());
193 | return this.getRealLanguageCode();
194 | }
195 | if (this == Chinese_Simplified_BING || this == Chinese_Simplified)
196 | return "zh-rCN";
197 | if (this == Chinese_Traditional_BING || this == Chinese_Traditional)
198 | return "zh-rTW";
199 | if (this == Hebrew_BING)
200 | return Hebrew.getLanguageCode();
201 |
202 | return this.getLanguageCode();
203 | }
204 | // google supported language code: https://cloud.google.com/translate/v2/using_rest, language reference section
205 | private static List getBaiduLanguages() {
206 | List result = new ArrayList();
207 | result.add(Chinese_Simplified_BAIDU);
208 | result.add(Chinese_Traditional_BAIDU);
209 | result.add(English_BAIDU);
210 | result.add(Japanese_BAIDU);
211 | result.add(Korean_BAIDU);
212 | result.add(French_BAIDU);
213 | result.add(Spanish_BAIDU);
214 | result.add(Thai_BAIDU);
215 | result.add(Arabic_BAIDU);
216 | result.add(Russian_BAIDU);
217 | result.add(Portuguese_BAIDU);
218 | result.add(German_BAIDU);
219 | result.add(Italian_BAIDU);
220 | result.add(Greek_BAIDU);
221 | result.add(Dutch_BAIDU);
222 | result.add(Polish_BAIDU);
223 | result.add(Bulgarian_BAIDU);
224 | result.add(Estonian_BAIDU);
225 | result.add(Danish_BAIDU);
226 | result.add(Finnish_BAIDU);
227 | result.add(Czech_BAIDU);
228 | result.add(Romanian_BAIDU);
229 | result.add(Slovenian_BAIDU);
230 | result.add(Swedish_BAIDU);
231 | result.add(Hungarian_BAIDU);
232 | result.add(Vietnamese_BAIDU);
233 | return result;
234 | }
235 | // google supported language code: https://cloud.google.com/translate/docs/languages, language reference section
236 | private static List getGoogleLanguages() {
237 | List result = new ArrayList();
238 | result.add(Afrikaans);
239 | result.add(Albanian);
240 | result.add(Arabic);
241 | result.add(Azerbaijani);
242 | result.add(Basque);
243 | result.add(Bengali);
244 | result.add(Belarusian);
245 | result.add(Bulgarian);
246 | result.add(Catalan);
247 | result.add(Chinese_Simplified);
248 | result.add(Chinese_Traditional);
249 | result.add(Croatian);
250 | result.add(Czech);
251 | result.add(Danish);
252 | result.add(Dutch);
253 | result.add(English);
254 | result.add(Esperanto);
255 | result.add(Estonian);
256 | result.add(Filipino);
257 | result.add(Finnish);
258 | result.add(French);
259 | result.add(Galician);
260 | result.add(Georgian);
261 | result.add(German);
262 | result.add(Greek);
263 | result.add(Gujarati);
264 | result.add(Haitian_Creole);
265 | result.add(Hebrew);
266 | result.add(Hindi);
267 | result.add(Hungarian);
268 | result.add(Icelandic);
269 | result.add(Indonesian);
270 | result.add(Irish);
271 | result.add(Italian);
272 | result.add(Japanese);
273 | result.add(Kannada);
274 | result.add(Korean);
275 | result.add(Latin);
276 | result.add(Latvian);
277 | result.add(Macedonian);
278 | result.add(Malay);
279 | result.add(Maltese);
280 | result.add(Norwegian);
281 | result.add(Persian);
282 | result.add(Polish);
283 | result.add(Portuguese);
284 | result.add(Romanian);
285 | result.add(Russian);
286 | result.add(Serbian);
287 | result.add(Slovak);
288 | result.add(Slovenian);
289 | result.add(Spanish);
290 | result.add(Swahili);
291 | result.add(Swedish);
292 | result.add(Tamil);
293 | result.add(Telugu);
294 | result.add(Thai);
295 | result.add(Turkish);
296 | result.add(Ukrainian);
297 | result.add(Urdu);
298 | result.add(Vietnamese);
299 | result.add(Welsh);
300 | result.add(Yiddish);
301 | return result;
302 | }
303 |
304 | // bing supported language code: http://msdn.microsoft.com/en-us/library/hh456380.aspx
305 | private static List getBingLanguages() {
306 | List result = new ArrayList();
307 | result.add(Arabic);
308 | result.add(Bulgarian);
309 | result.add(Catalan);
310 | result.add(Chinese_Simplified_BING);
311 | result.add(Chinese_Traditional_BING);
312 | result.add(Czech);
313 | result.add(Danish);
314 | result.add(Dutch);
315 | result.add(English);
316 | result.add(Estonian);
317 | result.add(Finnish);
318 | result.add(French);
319 | result.add(German);
320 | result.add(Greek);
321 | result.add(Haitian_Creole);
322 | result.add(Hebrew_BING);
323 | result.add(Hindi);
324 | result.add(Hungarian);
325 | result.add(Indonesian);
326 | result.add(Italian);
327 | result.add(Japanese);
328 | result.add(Korean);
329 | result.add(Latvian);
330 | result.add(Lithuanian);
331 | result.add(Malay);
332 | result.add(Maltese);
333 | result.add(Norwegian);
334 | result.add(Persian);
335 | result.add(Polish);
336 | result.add(Portuguese);
337 | result.add(Romanian);
338 | result.add(Russian);
339 | result.add(Slovak);
340 | result.add(Slovenian);
341 | result.add(Spanish);
342 | result.add(Swedish);
343 | result.add(Thai);
344 | result.add(Turkish);
345 | result.add(Ukrainian);
346 | result.add(Urdu);
347 | result.add(Vietnamese);
348 | result.add(Welsh);
349 | return result;
350 | }
351 | }
352 |
--------------------------------------------------------------------------------
/src/ui/MultiSelectDialog.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2015 Wesley Lin
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 ui;
18 |
19 | import com.intellij.ide.util.PropertiesComponent;
20 | import com.intellij.openapi.application.ModalityState;
21 | import com.intellij.openapi.project.Project;
22 | import com.intellij.openapi.ui.DialogWrapper;
23 | import com.intellij.openapi.ui.Messages;
24 | import com.intellij.openapi.util.Computable;
25 | import com.intellij.openapi.util.SystemInfo;
26 | import com.intellij.openapi.wm.IdeFrame;
27 | import com.intellij.openapi.wm.WindowManager;
28 | import com.intellij.ui.BrowserHyperlinkListener;
29 | import com.intellij.ui.ScrollPaneFactory;
30 | import com.intellij.ui.mac.foundation.MacUtil;
31 | import com.intellij.util.Alarm;
32 | import com.intellij.util.ui.UIUtil;
33 | import data.StorageDataKey;
34 | import language_engine.TranslationEngineType;
35 | import module.SupportedLanguages;
36 | import org.jetbrains.annotations.NotNull;
37 | import org.jetbrains.annotations.Nullable;
38 |
39 | import javax.swing.*;
40 | import javax.swing.plaf.basic.BasicHTML;
41 | import javax.swing.text.html.HTMLEditorKit;
42 | import java.awt.*;
43 | import java.awt.event.ItemEvent;
44 | import java.awt.event.ItemListener;
45 | import java.lang.reflect.Method;
46 | import java.util.ArrayList;
47 | import java.util.List;
48 | import java.util.concurrent.atomic.AtomicInteger;
49 |
50 | /**
51 | * Created by Wesley Lin on 11/29/14.
52 | */
53 | public class MultiSelectDialog extends DialogWrapper {
54 |
55 | public static final double GOLDEN_RATIO = 0.618;
56 | public static final double REVERSE_GOLDEN_RATIO = 1 - GOLDEN_RATIO;
57 |
58 | public interface OnOKClickedListener {
59 | public void onClick(List selectedLanguages, boolean overrideChecked);
60 | }
61 |
62 | private PropertiesComponent propertiesComponent;
63 | protected String myMessage;
64 | private MyBorderLayout myLayout;
65 |
66 | private JCheckBox myCheckBox;
67 | private String myCheckboxText;
68 | private boolean myChecked;
69 |
70 | private List data;
71 | private List selectedLanguages = new ArrayList();
72 | private OnOKClickedListener onOKClickedListener;
73 |
74 | public void setOnOKClickedListener(OnOKClickedListener onOKClickedListener) {
75 | this.onOKClickedListener = onOKClickedListener;
76 | }
77 |
78 | public MultiSelectDialog(@Nullable Project project,
79 | String message,
80 | String title,
81 | @Nullable String checkboxText,
82 | boolean checkboxStatus,
83 | TranslationEngineType translationEngineType,
84 | boolean canBeParent) {
85 | super(project, canBeParent);
86 | data = SupportedLanguages.getAllSupportedLanguages(translationEngineType);
87 | _init(project, title, message, checkboxText, checkboxStatus, null);
88 | }
89 |
90 | protected void _init(Project project,
91 | String title,
92 | String message,
93 | @Nullable String checkboxText,
94 | boolean checkboxStatus,
95 | @Nullable DoNotAskOption doNotAskOption) {
96 | setTitle(title);
97 | if (Messages.isMacSheetEmulation()) {
98 | setUndecorated(true);
99 | }
100 | propertiesComponent = PropertiesComponent.getInstance(project);
101 | myMessage = message;
102 | myCheckboxText = checkboxText;
103 | myChecked = checkboxStatus;
104 | setButtonsAlignment(SwingConstants.RIGHT);
105 | setDoNotAskOption(doNotAskOption);
106 | init();
107 | if (Messages.isMacSheetEmulation()) {
108 | MacUtil.adjustFocusTraversal(myDisposable);
109 | }
110 | }
111 |
112 | @Override
113 | protected void doOKAction() {
114 | super.doOKAction();
115 | if (onOKClickedListener != null) {
116 | onOKClickedListener.onClick(selectedLanguages, myCheckBox.isSelected());
117 | }
118 | }
119 |
120 | @NotNull
121 | @Override
122 | protected Action[] createActions() {
123 | Action[] actions;
124 | if (SystemInfo.isMac) {
125 | actions = new Action[]{myCancelAction, myOKAction};
126 | } else {
127 | actions = new Action[]{myOKAction, myCancelAction};
128 | }
129 | return actions;
130 | }
131 |
132 | @Override
133 | public void doCancelAction() {
134 | close(-1);
135 | }
136 |
137 | @Override
138 | protected JComponent createCenterPanel() {
139 | return doCreateCenterPanel();
140 | }
141 |
142 | @NotNull
143 | protected LayoutManager createRootLayout() {
144 | return Messages.isMacSheetEmulation() ? myLayout = new MyBorderLayout() : new BorderLayout();
145 | }
146 |
147 | @Override
148 | protected void dispose() {
149 | if (Messages.isMacSheetEmulation()) {
150 | animate();
151 | } else {
152 | super.dispose();
153 | }
154 | }
155 |
156 | @Override
157 | public void show() {
158 | if (Messages.isMacSheetEmulation()) {
159 | setInitialLocationCallback(new Computable() {
160 | @Override
161 | public Point compute() {
162 | JRootPane rootPane = SwingUtilities.getRootPane(getWindow().getParent());
163 | if (rootPane == null) {
164 | rootPane = SwingUtilities.getRootPane(getWindow().getOwner());
165 | }
166 |
167 | Point p = rootPane.getLocationOnScreen();
168 | p.x += (rootPane.getWidth() - getWindow().getWidth()) / 2;
169 | return p;
170 | }
171 | });
172 | animate();
173 | if (SystemInfo.isJavaVersionAtLeast("1.7")) {
174 | try {
175 | Method method = Class.forName("java.awt.Window").getDeclaredMethod("setOpacity", float.class);
176 | if (method != null) method.invoke(getPeer().getWindow(), .8f);
177 | } catch (Exception exception) {
178 | }
179 | }
180 | setAutoAdjustable(false);
181 | setSize(getPreferredSize().width, 0);//initial state before animation, zero height
182 | }
183 | super.show();
184 | }
185 |
186 | private void animate() {
187 | final int height = getPreferredSize().height;
188 | final int frameCount = 10;
189 | final boolean toClose = isShowing();
190 |
191 |
192 | final AtomicInteger i = new AtomicInteger(-1);
193 | final Alarm animator = new Alarm(myDisposable);
194 | final Runnable runnable = new Runnable() {
195 | @Override
196 | public void run() {
197 | int state = i.addAndGet(1);
198 |
199 | double linearProgress = (double) state / frameCount;
200 | if (toClose) {
201 | linearProgress = 1 - linearProgress;
202 | }
203 | myLayout.myPhase = (1 - Math.cos(Math.PI * linearProgress)) / 2;
204 | Window window = getPeer().getWindow();
205 | Rectangle bounds = window.getBounds();
206 | bounds.height = (int) (height * myLayout.myPhase);
207 |
208 | window.setBounds(bounds);
209 |
210 | if (state == 0 && !toClose && window.getOwner() instanceof IdeFrame) {
211 | WindowManager.getInstance().requestUserAttention((IdeFrame) window.getOwner(), true);
212 | }
213 |
214 | if (state < frameCount) {
215 | animator.addRequest(this, 10);
216 | } else if (toClose) {
217 | MultiSelectDialog.super.dispose();
218 | }
219 | }
220 | };
221 | animator.addRequest(runnable, 10, ModalityState.stateForComponent(getRootPane()));
222 | }
223 |
224 | protected JComponent doCreateCenterPanel() {
225 | final JPanel panel = new JPanel(new BorderLayout(15, 0));
226 |
227 | /*if (myMessage != null) {
228 | final JTextPane messageComponent = createMessageComponent(myMessage);
229 |
230 | final Dimension screenSize = messageComponent.getToolkit().getScreenSize();
231 | final Dimension textSize = messageComponent.getPreferredSize();
232 | if (myMessage.length() > 100) {
233 | final JScrollPane pane = ScrollPaneFactory.createScrollPane(messageComponent);
234 | pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
235 | pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
236 | pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
237 | final int scrollSize = (int) new JScrollBar(Adjustable.VERTICAL).getPreferredSize().getWidth() + 12;
238 | final Dimension preferredSize =
239 | new Dimension(Math.min(textSize.width, (int) (screenSize.width * REVERSE_GOLDEN_RATIO)) + scrollSize,
240 | Math.min(textSize.height, screenSize.height / 3) + scrollSize);
241 | pane.setPreferredSize(preferredSize);
242 | panel.add(pane, BorderLayout.NORTH);
243 | } else {
244 | panel.add(messageComponent, BorderLayout.NORTH);
245 | }
246 | }*/
247 |
248 | if (!data.isEmpty()) {
249 | final Container container = new Container();
250 |
251 | final JCheckBox checkbox_selectAll = new JCheckBox("Select All");
252 | checkbox_selectAll.setMargin(new Insets(22, 300, 0, 0));
253 | panel.add(checkbox_selectAll, BorderLayout.NORTH);
254 |
255 | checkbox_selectAll.addItemListener(new ItemListener() {
256 | @Override
257 | public void itemStateChanged(ItemEvent e) {
258 | if (e.getStateChange() == ItemEvent.SELECTED) {
259 | selectedLanguages.addAll(data);
260 | checkbox_selectAll.setSelected(true);
261 |
262 | for(Component component:container.getComponents()){
263 | JCheckBox checkBox = (JCheckBox) component;
264 | checkBox.setSelected(true);
265 | }
266 |
267 | } else if (e.getStateChange() == ItemEvent.DESELECTED) {
268 | checkbox_selectAll.setSelected(false);
269 | selectedLanguages.removeAll(data);
270 |
271 | for(Component component:container.getComponents()){
272 | JCheckBox checkBox = (JCheckBox) component;
273 | checkBox.setSelected(false);
274 | }
275 | }
276 | }
277 | });
278 |
279 |
280 | int gridCol = 3;
281 | int gridRow = (data.size() % gridCol == 0) ? data.size() / gridCol : data.size() / gridCol + 1;
282 | container.setLayout(new GridLayout(gridRow, gridCol));
283 | boolean showEnglish= PropertiesComponent.getInstance().getValue(StorageDataKey.SettingLanguageShowWhenChoose,"English").equals("English");
284 | for (final SupportedLanguages language : data) {
285 | String display;
286 | if(showEnglish)
287 | display=language.getLanguageEnglishDisplayName();
288 | else display=language.getLanguageChineseDisplayName();
289 | JCheckBox checkbox = new JCheckBox(display
290 | + " (" + language.getLanguageDisplayName() + ") ");
291 | checkbox.addItemListener(new ItemListener() {
292 | @Override
293 | public void itemStateChanged(ItemEvent e) {
294 | if (e.getStateChange() == ItemEvent.SELECTED) {
295 | if (!selectedLanguages.contains(language)) {
296 | selectedLanguages.add(language);
297 | }
298 | } else if (e.getStateChange() == ItemEvent.DESELECTED) {
299 | if (selectedLanguages.contains(language)) {
300 | selectedLanguages.remove(language);
301 | }
302 | }
303 | }
304 | });
305 | checkbox.setSelected(
306 | propertiesComponent.getBoolean(StorageDataKey.SupportedLanguageCheckStatusPrefix + language.getLanguageCode(), false));
307 | container.add(checkbox);
308 | }
309 |
310 | panel.add(container, BorderLayout.CENTER);
311 | }
312 |
313 | if (myCheckboxText != null) {
314 |
315 | myCheckBox = new JCheckBox(myCheckboxText);
316 | myCheckBox.setSelected(myChecked);
317 | myCheckBox.setMargin(new Insets(2, -4, 0, 0));
318 |
319 | panel.add(myCheckBox, BorderLayout.SOUTH);
320 | }
321 |
322 | return panel;
323 | }
324 |
325 | protected static JTextPane createMessageComponent(final String message) {
326 | final JTextPane messageComponent = new JTextPane();
327 | return configureMessagePaneUi(messageComponent, message);
328 | }
329 |
330 | @Override
331 | protected void doHelpAction() {
332 | // do nothing
333 | }
334 |
335 | @NotNull
336 | public static JTextPane configureMessagePaneUi(JTextPane messageComponent, String message) {
337 | return configureMessagePaneUi(messageComponent, message, true);
338 | }
339 |
340 | @NotNull
341 | public static JTextPane configureMessagePaneUi(JTextPane messageComponent,
342 | String message,
343 | final boolean addBrowserHyperlinkListener) {
344 | messageComponent.setFont(UIUtil.getLabelFont());
345 | if (BasicHTML.isHTMLString(message)) {
346 | final HTMLEditorKit editorKit = new HTMLEditorKit();
347 | editorKit.getStyleSheet().addRule(UIUtil.displayPropertiesToCSS(UIUtil.getLabelFont(), UIUtil.getLabelForeground()));
348 | messageComponent.setEditorKit(editorKit);
349 | messageComponent.setContentType(UIUtil.HTML_MIME);
350 | if (addBrowserHyperlinkListener) {
351 | messageComponent.addHyperlinkListener(BrowserHyperlinkListener.INSTANCE);
352 | }
353 | }
354 | messageComponent.setText(message);
355 | messageComponent.setEditable(false);
356 | if (messageComponent.getCaret() != null) {
357 | messageComponent.setCaretPosition(0);
358 | }
359 |
360 | if (UIUtil.isUnderNimbusLookAndFeel()) {
361 | messageComponent.setOpaque(false);
362 | messageComponent.setBackground(UIUtil.TRANSPARENT_COLOR);
363 | } else {
364 | messageComponent.setBackground(UIUtil.getOptionPaneBackground());
365 | }
366 |
367 | messageComponent.setForeground(UIUtil.getLabelForeground());
368 | return messageComponent;
369 | }
370 |
371 | private static class MyBorderLayout extends BorderLayout {
372 | private double myPhase = 0;//it varies from 0 (hidden state) to 1 (fully visible)
373 |
374 | private MyBorderLayout() {
375 | }
376 |
377 | @Override
378 | public void layoutContainer(Container target) {
379 | final Dimension realSize = target.getSize();
380 | target.setSize(target.getPreferredSize());
381 |
382 | super.layoutContainer(target);
383 |
384 | target.setSize(realSize);
385 |
386 | synchronized (target.getTreeLock()) {
387 | int yShift = (int) ((1 - myPhase) * target.getPreferredSize().height);
388 | Component[] components = target.getComponents();
389 | for (Component component : components) {
390 | Point point = component.getLocation();
391 | point.y -= yShift;
392 | component.setLocation(point);
393 | }
394 | }
395 | }
396 | }
397 | }
398 |
--------------------------------------------------------------------------------
/src/data/task/GetTranslationTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2015 Wesley Lin
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 data.task;
18 |
19 | import action.AndroidLocalization;
20 | import com.intellij.ide.util.PropertiesComponent;
21 | import com.intellij.openapi.application.ApplicationManager;
22 | import com.intellij.openapi.fileEditor.FileEditorManager;
23 | import com.intellij.openapi.progress.ProgressIndicator;
24 | import com.intellij.openapi.progress.Task;
25 | import com.intellij.openapi.project.Project;
26 | import com.intellij.openapi.vfs.LocalFileSystem;
27 | import com.intellij.openapi.vfs.VirtualFile;
28 | import data.Log;
29 | import data.SerializeUtil;
30 | import data.StorageDataKey;
31 | import language_engine.TranslationEngineType;
32 | import language_engine.baidu.BaiduTranslationApi;
33 |
34 | import language_engine.google.GoogleTranslationApi;
35 | import module.*;
36 | import org.jetbrains.annotations.NotNull;
37 | import org.jetbrains.annotations.Nullable;
38 | import util.Logger;
39 |
40 | import java.io.*;
41 | import java.util.ArrayList;
42 | import java.util.List;
43 |
44 | /**
45 | * Created by Wesley Lin on 12/1/14.
46 | */
47 | public class GetTranslationTask extends Task.Backgroundable {
48 |
49 | private List selectedLanguages;
50 | private final List androidStrings;
51 | private double indicatorFractionFrame;
52 | private TranslationEngineType translationEngineType;
53 | private boolean override;
54 | private VirtualFile clickedFile;
55 |
56 | private static final String GoogleErrorUnknown = "Error, please check API key in the settings panel.";
57 | private static final String GoogleDailyLimitError = "Daily Limit Exceeded, please note that Google Translation API " +
58 | "is a paid service.";
59 |
60 | private String errorMsg = null;
61 |
62 | public GetTranslationTask(Project project, String title,
63 | List selectedLanguages,
64 | List androidStrings,
65 | TranslationEngineType translationEngineType,
66 | boolean override,
67 | VirtualFile clickedFile) {
68 | super(project, title);
69 | this.selectedLanguages = selectedLanguages;
70 | this.androidStrings = androidStrings;
71 | this.translationEngineType = translationEngineType;
72 | this.indicatorFractionFrame = 1.0d / (double) (this.selectedLanguages.size());
73 | this.override = override;
74 | this.clickedFile = clickedFile;
75 | }
76 |
77 | @Override
78 | public void run(ProgressIndicator indicator) {
79 | try {
80 | for (int i = 0; i < selectedLanguages.size(); i++) {
81 |
82 | SupportedLanguages language = selectedLanguages.get(i);
83 |
84 | if (language != null && !"".equals(language) /*&& !language.equals(SupportedLanguages.English)*/) {
85 |
86 | List androidStringList = filterAndroidString(androidStrings, language, override);
87 |
88 | List> filteredAndSplittedString
89 | = splitAndroidString(androidStringList, translationEngineType);
90 |
91 | List translationResult = new ArrayList();
92 | for (int j = 0; j < filteredAndSplittedString.size(); j++) {
93 |
94 | List strings = getTranslationEngineResult(
95 | filteredAndSplittedString.get(j),
96 | language,
97 | SupportedLanguages.AUTO_BAIDU,
98 | translationEngineType
99 | );
100 |
101 | if (strings == null) {
102 | Log.i("language===" + language);
103 | continue;
104 | }
105 | translationResult.addAll(strings);
106 | indicator.setFraction(indicatorFractionFrame * (double) (i)
107 | + indicatorFractionFrame / filteredAndSplittedString.size() * (double) (j));
108 | indicator.setText("Translating to " + language.getLanguageEnglishDisplayName()
109 | + " (" + language.getLanguageDisplayName() + ")");
110 | }
111 | String fileName = getValueResourcePath(language);
112 | Logger.info("output path:" + fileName);
113 | List fileContent = getTargetAndroidStrings(androidStrings, translationResult, fileName, override);
114 | writeAndroidStringToLocal(myProject, fileName, fileContent);
115 | }
116 | }
117 | }catch (Exception e){
118 | Logger.error(e.getLocalizedMessage());
119 | }
120 | }
121 |
122 |
123 | @Override
124 | public void onSuccess() {
125 |
126 | if (errorMsg == null || errorMsg.isEmpty())
127 | return;
128 | AndroidLocalization.showSuccessDialog(getProject(), "translation Success");
129 | }
130 |
131 | private String getValueResourcePath(SupportedLanguages language) {
132 | String resPath = clickedFile.getParent().getParent().getPath();
133 |
134 | /* String resPath = clickedFile.getPath().substring(0,
135 | clickedFile.getPath().indexOf("/res/") + "/res/".length());*/
136 |
137 | return resPath + "/values-" + language.getAndroidStringFolderNameSuffix()
138 | + "/" + clickedFile.getName();
139 | }
140 |
141 | // todo: if got error message, should break the background task
142 | private List getTranslationEngineResult(@NotNull List needToTranslatedString,
143 | @NotNull SupportedLanguages targetLanguageCode,
144 | @NotNull SupportedLanguages sourceLanguageCode,
145 | TranslationEngineType translationEngineType) {
146 |
147 | List querys = AndroidString.getAndroidStringValues(needToTranslatedString);
148 | Log.i(querys.toString());
149 |
150 | List result = null;
151 |
152 | switch (translationEngineType) {
153 | case Baidu:
154 | result = BaiduTranslationApi.getTranslationJSON(querys,targetLanguageCode,sourceLanguageCode);
155 | break;
156 | case Bing:
157 | break;
158 | case Google:
159 | result = GoogleTranslationApi.getTranslationJSON(querys, targetLanguageCode, sourceLanguageCode);
160 | if (result == null) {
161 | errorMsg = GoogleErrorUnknown;
162 | return null;
163 | } else if (result.isEmpty() && !querys.isEmpty()) {
164 | errorMsg = GoogleDailyLimitError;
165 | return null;
166 | }
167 | break;
168 | }
169 | if (result == null || result.size() <= 0){
170 | return null;
171 | }
172 |
173 | List translatedAndroidStrings = new ArrayList<>();
174 | // Logger.error(needToTranslatedString.size());
175 | // Logger.info("needToTranslatedString.size(): " + needToTranslatedString.size()+
176 | // "result.size(): " + result.size());
177 | for (int i = 0,j=0; i < needToTranslatedString.size()&&j child = ((AndroidStringArrayEntity) oldAndroidString).getChild();
184 | for(StringArrayItem item:child ){
185 | if(item.isLink()){
186 | androidStringArrayEntity.addChild(item);
187 | }else {
188 | androidStringArrayEntity.addChild(new StringArrayItem(result.get(i)));
189 | i++;
190 | }
191 | }
192 | translatedAndroidStrings.add(androidStringArrayEntity);
193 | }else{
194 | if(oldAndroidString.isLink()){
195 | translatedAndroidStrings.add(oldAndroidString);
196 | }else {
197 | translatedAndroidStrings.add(new AndroidString(
198 | oldAndroidString.getKey(), result.get(i)));
199 | i++;
200 | }
201 | }
202 |
203 | }
204 | return translatedAndroidStrings;
205 | }
206 |
207 | private List> splitAndroidString(List origin, TranslationEngineType engineType) {
208 |
209 | List> splited = new ArrayList>();
210 | int splitFragment = 50;
211 | switch (engineType) {
212 | case Baidu:
213 | splitFragment = 50;
214 | break;
215 | case Bing:
216 | splitFragment = 50;
217 | break;
218 | case Google:
219 | splitFragment = 50;
220 | break;
221 | }
222 |
223 | if (origin != null && origin.size() > 0) {
224 | if (origin.size() <= splitFragment) {
225 | splited.add(origin);
226 | } else {
227 | int count = (origin.size() % splitFragment == 0) ? (origin.size() / splitFragment) : (origin.size() / splitFragment + 1);
228 | for (int i = 1; i <= count; i++) {
229 | int end = i * splitFragment;
230 | if (end > origin.size()) {
231 | end = origin.size();
232 | }
233 |
234 | splited.add(origin.subList((i - 1) * splitFragment, end));
235 | }
236 | }
237 | }
238 |
239 | return splited;
240 | }
241 |
242 | private List filterAndroidString(List origin,
243 | SupportedLanguages language,
244 | boolean override) {
245 | List result = new ArrayList();
246 |
247 |
248 |
249 |
250 | String rulesString = PropertiesComponent.getInstance().getValue(StorageDataKey.SettingFilterRules);
251 | List filterRules = new ArrayList();
252 | if (rulesString == null) {
253 | filterRules.add(FilterRule.DefaultFilterRule);
254 | } else {
255 | filterRules = SerializeUtil.deserializeFilterRuleList(rulesString);
256 | }
257 | // Log.i("targetAndroidString: " + targetAndroidStrings.toString());
258 | for (AndroidString androidString : origin) {
259 | // filter rules
260 | if (FilterRule.inFilterRule(androidString.getKey(), filterRules))
261 | continue;
262 |
263 | // override
264 | /*if (!override && !targetAndroidStrings.isEmpty()) {
265 | // check if there is the androidString in this file
266 | // if there is, filter it
267 | if (isAndroidStringListContainsKey(targetAndroidStrings, androidString.getKey())) {
268 | continue;
269 | }
270 | }*/
271 |
272 | result.add(androidString);
273 | }
274 |
275 | return result;
276 | }
277 |
278 | private static List getTargetAndroidStrings(List sourceAndroidStrings,
279 | List translatedAndroidStrings,
280 | String fileName,
281 | boolean override) {
282 |
283 | if (translatedAndroidStrings == null) {
284 | translatedAndroidStrings = new ArrayList();
285 | }
286 |
287 | VirtualFile existenceFile = LocalFileSystem.getInstance().findFileByPath(fileName);
288 | List existenceAndroidStrings = null;
289 | if (existenceFile != null && !override) {
290 | try {
291 | // existenceAndroidStrings = AndroidString.getAndroidStringsList(existenceFile.contentsToByteArray());
292 | existenceAndroidStrings = AndroidString.getAndroidStrings(existenceFile.getInputStream());
293 | } catch (IOException e) {
294 | e.printStackTrace();
295 | }
296 | } else {
297 | existenceAndroidStrings = new ArrayList();
298 | }
299 |
300 | Log.i("sourceAndroidStrings: " + sourceAndroidStrings,
301 | "translatedAndroidStrings: " + translatedAndroidStrings,
302 | "existenceAndroidStrings: " + existenceAndroidStrings);
303 |
304 | List targetAndroidStrings = new ArrayList();
305 |
306 | for (int i = 0; i < sourceAndroidStrings.size(); i++) {
307 | AndroidString string = sourceAndroidStrings.get(i);
308 | AndroidString resultString ;
309 | if(string instanceof AndroidStringArrayEntity) resultString= new AndroidStringArrayEntity(string.getKey()); else resultString=new AndroidString(string);
310 | replaceValueOrChildren(translatedAndroidStrings, resultString);
311 | // if override is checked, skip setting the existence value, for performance issue
312 | if (!override) {//不覆盖原有的
313 | replaceValueOrChildren(existenceAndroidStrings, resultString);
314 | }
315 | targetAndroidStrings.add(resultString);
316 | }
317 | Log.i("targetAndroidStrings: " + targetAndroidStrings);
318 | return targetAndroidStrings;
319 | }
320 |
321 |
322 |
323 | private static void writeAndroidStringToLocal(final Project myProject, String filePath, List fileContent) {
324 | File file = new File(filePath);
325 | final VirtualFile virtualFile;
326 | boolean fileExits = true;
327 | try {
328 | file.getParentFile().mkdirs();
329 | if (!file.exists()) {
330 | fileExits = false;
331 | file.createNewFile();
332 | }
333 | //Change by GodLikeThomas FIX: Appeared Messy code under windows --start;
334 | //FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
335 | //BufferedWriter writer = new BufferedWriter(fileWriter);
336 | //writer.write(getFileContent(fileContent));
337 | //writer.close();
338 | FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
339 | OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
340 | osw.write(getFileContent(fileContent));
341 | osw.close();
342 | //Change by GodLikeThomas FIX: Appeared Messy code under windows --end;
343 | } catch (IOException e) {
344 | e.printStackTrace();
345 | }
346 |
347 | if (fileExits) {
348 | virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
349 | if (virtualFile == null)
350 | return;
351 | virtualFile.refresh(true, false, new Runnable() {
352 | @Override
353 | public void run() {
354 | openFileInEditor(myProject, virtualFile);
355 | }
356 | });
357 | } else {
358 | virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
359 | openFileInEditor(myProject, virtualFile);
360 | }
361 | }
362 |
363 | private static void openFileInEditor(final Project myProject, @Nullable final VirtualFile file) {
364 | if (file == null)
365 | return;
366 |
367 | // run in UI thread:
368 | // https://theantlrguy.atlassian.net/wiki/display/~admin/Intellij+plugin+development+notes#Intellijplugindevelopmentnotes-GUIandthreads,backgroundtasks
369 | ApplicationManager.getApplication().invokeLater(new Runnable() {
370 | @Override
371 | public void run() {
372 | final FileEditorManager editorManager = FileEditorManager.getInstance(myProject);
373 | editorManager.openFile(file, true);
374 | }
375 | });
376 | }
377 |
378 | private static String getFileContent(List fileContent) {
379 | String xmlHeader = "\n";
380 | String stringResourceHeader = "\n\n";
381 | String stringResourceTail = "\n";
382 |
383 | StringBuilder sb = new StringBuilder();
384 | sb.append(xmlHeader).append(stringResourceHeader);
385 | for (AndroidString androidString : fileContent) {
386 | sb.append("\t").append(androidString.toString()).append("\n");
387 | }
388 | sb.append("\n").append(stringResourceTail);
389 | return sb.toString();
390 | }
391 |
392 |
393 |
394 | public static AndroidString getAndroidStringInList(List androidStrings, String key) {
395 | for (AndroidString androidString : androidStrings) {
396 | if (androidString.getKey().equals(key)) {
397 | return androidString;
398 | }
399 | }
400 | return null;
401 | }
402 | private static void replaceValueOrChildren(List translatedAndroidStrings, AndroidString resultString) {
403 | AndroidString translatedValue = getAndroidStringInList(translatedAndroidStrings, resultString.getKey());
404 | if (translatedValue != null) {
405 | resultString.setValue(translatedValue.getValue());
406 | if(translatedValue instanceof AndroidStringArrayEntity){
407 | ((AndroidStringArrayEntity) resultString).setChild( ((AndroidStringArrayEntity) translatedValue).getChild());
408 | }
409 | }
410 | }
411 | }
412 |
--------------------------------------------------------------------------------
/src/settings/SettingConfigurable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014-2015 Wesley Lin
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 settings;
18 |
19 | import com.intellij.ide.util.PropertiesComponent;
20 | import com.intellij.openapi.options.Configurable;
21 | import com.intellij.openapi.options.ConfigurationException;
22 | import com.intellij.openapi.ui.ComboBox;
23 | import com.intellij.ui.components.JBList;
24 | import com.intellij.ui.components.JBScrollPane;
25 | import data.Log;
26 | import data.SerializeUtil;
27 | import data.StorageDataKey;
28 | import language_engine.TranslationEngineType;
29 | import module.FilterRule;
30 | import org.jdesktop.swingx.VerticalLayout;
31 | import org.jdesktop.swingx.prompt.PromptSupport;
32 | import org.jetbrains.annotations.Nls;
33 | import org.jetbrains.annotations.Nullable;
34 | import ui.AddFilterRuleDialog;
35 | import ui.GoogleAlertDialog;
36 |
37 | import javax.swing.*;
38 | import java.awt.*;
39 | import java.awt.event.ActionEvent;
40 | import java.awt.event.ActionListener;
41 | import java.awt.event.MouseAdapter;
42 | import java.awt.event.MouseEvent;
43 | import java.io.IOException;
44 | import java.net.URI;
45 | import java.net.URISyntaxException;
46 | import java.util.ArrayList;
47 |
48 | /**
49 | * Created by Wesley Lin on 12/8/14.
50 | */
51 | public class SettingConfigurable implements Configurable, ActionListener {
52 |
53 | private static final String DEFAULT_CLIENT_ID = "Default client id";
54 | private static final String DEFAULT_CLIENT_SECRET = "Default client secret";
55 | private static final String DEFAULT_BAIDU_APPID_PROMPT = "Please input your Baidu APP ID";
56 | private static final String DEFAULT_BAIDU_KEY_PROMPT = "Please input your Baidu SecretKey";
57 |
58 | private static final String DEFAULT_GOOGLE_API_KEY = "Enter API key here";
59 |
60 | private static final String BING_HOW_TO = "How to get ClientId and ClientSecret?";
61 | private static final String BAIDU_HOW_TO = "How to get APP ID and SecretKey?";
62 |
63 | private MouseAdapter baiduHowTo = new MouseAdapter() {
64 | @Override
65 | public void mouseClicked(MouseEvent e) {
66 | try {
67 | Desktop.getDesktop().browse(new URI("http://api.fanyi.baidu.com/api/trans/product/index"));
68 | } catch (URISyntaxException | IOException e1) {
69 | e1.printStackTrace();
70 | }
71 | }
72 | };
73 | private MouseAdapter bingHowTo = new MouseAdapter() {
74 | @Override
75 | public void mouseClicked(MouseEvent e) {
76 | try {
77 | Desktop.getDesktop().browse(new URI("http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx"));
78 | } catch (URISyntaxException | IOException e1) {
79 | e1.printStackTrace();
80 | }
81 | }
82 | };
83 |
84 | private static final String GOOGLE_HOW_TO = "How to set up Google Translation API key?";
85 | private MouseAdapter googleHowTo = new MouseAdapter() {
86 | @Override
87 | public void mouseClicked(MouseEvent e) {
88 | try {
89 | Desktop.getDesktop().browse(new URI("https://cloud.google.com/translate/v2/getting_started#intro"));
90 | } catch (URISyntaxException | IOException e1) {
91 | e1.printStackTrace();
92 | }
93 | }
94 | };
95 |
96 | private JPanel settingPanel;
97 | private JComboBox languageEngineBox;
98 | private JComboBox showLanguageWhenChoose;
99 | private TranslationEngineType currentEngine;
100 |
101 | private JLabel howToLabel;
102 | private JLabel line1Text;
103 | private JTextField line1TextField;
104 | private JLabel line2Text;
105 | private JTextField line2TextField;
106 |
107 | private JBList filterList;
108 | private JButton btnAddFilter;
109 | private JButton btnDeleteFilter;
110 |
111 | private java.util.List filterRules = new ArrayList();
112 | private boolean languageEngineChanged = false;
113 | private boolean filterRulesChanged = false;
114 |
115 | @Nls
116 | @Override
117 | public String getDisplayName() {
118 | return "AndroidTranslation";
119 | }
120 |
121 | @Nullable
122 | @Override
123 | public String getHelpTopic() {
124 | return getDisplayName();
125 | }
126 |
127 | @Nullable
128 | @Override
129 | public JComponent createComponent() {
130 | if (settingPanel == null) {
131 | settingPanel = new JPanel(new VerticalLayout(18));
132 |
133 | // header UI
134 | Container container = new Container();
135 | container.setLayout(new BorderLayout());
136 |
137 | currentEngine = TranslationEngineType.fromName(
138 | PropertiesComponent.getInstance().getValue(StorageDataKey.SettingLanguageEngine));
139 | TranslationEngineType[] items = TranslationEngineType.getLanguageEngineArray();
140 | String[] showLanguage = new String[]{"English","中文"};
141 | languageEngineBox = new ComboBox(items);
142 | showLanguageWhenChoose = new ComboBox(showLanguage);
143 | languageEngineBox.setEnabled(true);
144 | showLanguageWhenChoose.setEnabled(true);
145 | languageEngineBox.setSelectedItem(currentEngine);
146 | showLanguageWhenChoose.setSelectedItem(PropertiesComponent.getInstance().getValue(StorageDataKey.SettingLanguageShowWhenChoose,"English"));
147 | languageEngineBox.addActionListener(this);
148 | showLanguageWhenChoose.addActionListener(e -> {
149 | JComboBox comboBox = (JComboBox) e.getSource();
150 | String type = (String) comboBox.getSelectedItem();
151 | Log.i("selected language: " + type);
152 | PropertiesComponent.getInstance().setValue(StorageDataKey.SettingLanguageShowWhenChoose,type);
153 |
154 | });
155 |
156 | container.add(new JLabel("Language engine: "), BorderLayout.WEST);
157 | container.add(languageEngineBox, BorderLayout.CENTER);
158 |
159 | Container container1 = new Container();
160 | container1.setLayout(new BorderLayout());
161 | container1.add(new JLabel("Language when choose: "), BorderLayout.WEST);
162 | container1.add(showLanguageWhenChoose, BorderLayout.CENTER);
163 |
164 | settingPanel.add(container);
165 | settingPanel.add(container1);
166 |
167 | initContentContainer();
168 | initAndAddFilterContainer();
169 | }
170 | return settingPanel;
171 | }
172 |
173 | @Override
174 | public boolean isModified() {
175 | if (languageEngineChanged)
176 | return true;
177 |
178 | if (filterRulesChanged)
179 | return true;
180 |
181 | PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
182 | switch (currentEngine) {
183 | case Baidu:
184 | String baiduClientIdStored = propertiesComponent.getValue(StorageDataKey.BaiduClientIdStored);
185 | String baiduClientSecretStored = propertiesComponent.getValue(StorageDataKey.BaiduClientSecretStored);
186 |
187 | boolean baiduClientIdChanged = false;
188 | boolean baiduClientSecretChanged = false;
189 | if (baiduClientIdStored == null) {
190 | if (!line1TextField.getText().isEmpty())
191 | baiduClientIdChanged = true;
192 | } else {
193 | if (!line1TextField.getText().equals(baiduClientIdStored)
194 | && !line1TextField.getText().trim().isEmpty())
195 | baiduClientIdChanged = true;
196 | }
197 |
198 | if (baiduClientSecretStored == null) {
199 | if (!line2TextField.getText().isEmpty())
200 | baiduClientSecretChanged = true;
201 | } else {
202 | if (!line2TextField.getText().equals(baiduClientSecretStored)
203 | && !line2TextField.getText().trim().isEmpty())
204 | baiduClientSecretChanged = true;
205 | }
206 | return baiduClientIdChanged || baiduClientSecretChanged;
207 | case Bing: {
208 | String bingClientIdStored = propertiesComponent.getValue(StorageDataKey.BingClientIdStored);
209 | String bingClientSecretStored = propertiesComponent.getValue(StorageDataKey.BingClientSecretStored);
210 |
211 | boolean bingClientIdChanged = false;
212 | boolean bingClientSecretChanged = false;
213 |
214 | if (bingClientIdStored == null) {
215 | if (!line1TextField.getText().isEmpty())
216 | bingClientIdChanged = true;
217 | } else {
218 | if (!line1TextField.getText().equals(bingClientIdStored)
219 | && !line1TextField.getText().trim().isEmpty())
220 | bingClientIdChanged = true;
221 | }
222 |
223 | if (bingClientSecretStored == null) {
224 | if (!line2TextField.getText().isEmpty())
225 | bingClientSecretChanged = true;
226 | } else {
227 | if (!line2TextField.getText().equals(bingClientSecretStored)
228 | && !line2TextField.getText().trim().isEmpty())
229 | bingClientSecretChanged = true;
230 | }
231 |
232 | return bingClientIdChanged || bingClientSecretChanged;
233 | }
234 | case Google: {
235 | String googleApiKeyStored = propertiesComponent.getValue(StorageDataKey.GoogleApiKeyStored);
236 | boolean googleApiKeyStoredChanged = false;
237 |
238 | if (googleApiKeyStored == null) {
239 | if (!line1TextField.getText().isEmpty())
240 | googleApiKeyStoredChanged = true;
241 | } else {
242 | if (!line1TextField.getText().equals(googleApiKeyStored)
243 | && !line1TextField.getText().trim().isEmpty())
244 | googleApiKeyStoredChanged = true;
245 | }
246 | return googleApiKeyStoredChanged;
247 | }
248 | }
249 | return false;
250 | }
251 |
252 | @Override
253 | public void apply() throws ConfigurationException {
254 | Log.i("apply clicked");
255 | if (languageEngineBox == null || filterList == null
256 | || btnAddFilter == null || btnDeleteFilter == null
257 | || line1TextField == null || line2TextField == null)
258 | return;
259 |
260 | PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
261 |
262 | languageEngineChanged = false;
263 | propertiesComponent.setValue(StorageDataKey.SettingLanguageEngine, currentEngine.toName());
264 |
265 | switch (currentEngine) {
266 | case Baidu:
267 | if (!line1TextField.getText().trim().isEmpty()) {
268 | propertiesComponent.setValue(StorageDataKey.BaiduClientIdStored, line1TextField.getText());
269 | PromptSupport.setPrompt(line1TextField.getText(), line1TextField);
270 | }
271 |
272 | if (!line2TextField.getText().trim().isEmpty()) {
273 | propertiesComponent.setValue(StorageDataKey.BaiduClientSecretStored, line2TextField.getText());
274 | PromptSupport.setPrompt(line2TextField.getText(), line2TextField);
275 | }
276 | line1TextField.setText("");
277 | line2TextField.setText("");
278 | break;
279 | case Bing: {
280 | if (!line1TextField.getText().trim().isEmpty()) {
281 | propertiesComponent.setValue(StorageDataKey.BingClientIdStored, line1TextField.getText());
282 | PromptSupport.setPrompt(line1TextField.getText(), line1TextField);
283 | }
284 |
285 | if (!line2TextField.getText().trim().isEmpty()) {
286 | propertiesComponent.setValue(StorageDataKey.BingClientSecretStored, line2TextField.getText());
287 | PromptSupport.setPrompt(line2TextField.getText(), line2TextField);
288 | }
289 | line1TextField.setText("");
290 | line2TextField.setText("");
291 | }
292 | break;
293 | case Google: {
294 | if (!line1TextField.getText().trim().isEmpty()) {
295 | propertiesComponent.setValue(StorageDataKey.GoogleApiKeyStored, line1TextField.getText());
296 | PromptSupport.setPrompt(line1TextField.getText(), line1TextField);
297 | }
298 | line1TextField.setText("");
299 | }
300 | break;
301 | }
302 | languageEngineBox.requestFocus();
303 |
304 | filterRulesChanged = false;
305 | propertiesComponent.setValue(StorageDataKey.SettingFilterRules,
306 | SerializeUtil.serializeFilterRuleList(filterRules));
307 | }
308 |
309 | @Override
310 | public void reset() {
311 | if (settingPanel == null || languageEngineBox == null || filterList == null
312 | || btnAddFilter == null || btnDeleteFilter == null)
313 | return;
314 | PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
315 |
316 | currentEngine = TranslationEngineType.fromName(
317 | propertiesComponent.getValue(StorageDataKey.SettingLanguageEngine));
318 | languageEngineBox.setSelectedItem(currentEngine);
319 | languageEngineChanged = false;
320 | initUI(currentEngine);
321 |
322 | Log.i("reset, current engine: " + currentEngine);
323 |
324 | languageEngineBox.requestFocus();
325 |
326 | // filter rules
327 | filterRulesChanged = false;
328 | resetFilterList();
329 | }
330 |
331 | @Override
332 | public void disposeUIResources() {
333 |
334 | }
335 |
336 | @Override
337 | public void actionPerformed(ActionEvent e) {
338 | JComboBox comboBox = (JComboBox) e.getSource();
339 | TranslationEngineType type = (TranslationEngineType) comboBox.getSelectedItem();
340 | if ((type == currentEngine) && (!languageEngineChanged))
341 | return;
342 |
343 | languageEngineChanged = true;
344 | Log.i("selected type: " + type.name());
345 | currentEngine = type;
346 |
347 | initUI(currentEngine);
348 |
349 | // default: false, if user set 'never show', set true
350 | boolean GoogleAlertMsgShownSetting = PropertiesComponent.getInstance().getBoolean(StorageDataKey.GoogleAlertMsgShownSetting, false);
351 | if (currentEngine == TranslationEngineType.Google && !GoogleAlertMsgShownSetting) {
352 | new GoogleAlertDialog(settingPanel, false).show();
353 | }
354 | }
355 |
356 | private void initUI(TranslationEngineType engineType) {
357 | if (settingPanel == null)
358 | return;
359 |
360 | PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
361 | switch (engineType) {
362 | case Baidu: {
363 | line1Text.setText("APP ID:");
364 | line2Text.setText("SecretKey:");
365 | line2Text.setVisible(true);
366 | line2TextField.setVisible(true);
367 | howToLabel.setText(BAIDU_HOW_TO);
368 | howToLabel.removeMouseMotionListener(googleHowTo);
369 | howToLabel.removeMouseMotionListener(bingHowTo);
370 | howToLabel.addMouseListener(baiduHowTo);
371 |
372 | String baiduClientIdStored = propertiesComponent.getValue(StorageDataKey.BaiduClientIdStored);
373 | String baiduClientSecretStored = propertiesComponent.getValue(StorageDataKey.BaiduClientSecretStored);
374 |
375 | if (baiduClientIdStored != null) {
376 | PromptSupport.setPrompt(baiduClientIdStored, line1TextField);
377 | } else {
378 | PromptSupport.setPrompt(DEFAULT_BAIDU_APPID_PROMPT, line1TextField);
379 | }
380 | line1TextField.setText("");
381 |
382 | if (baiduClientSecretStored != null) {
383 | PromptSupport.setPrompt(baiduClientSecretStored, line2TextField);
384 | } else {
385 | PromptSupport.setPrompt(DEFAULT_BAIDU_KEY_PROMPT, line2TextField);
386 | }
387 | line2TextField.setText("");
388 | }
389 | break;
390 | case Bing: {
391 | line1Text.setText("Client Id:");
392 | line2Text.setText("Client secret:");
393 | line2Text.setVisible(true);
394 |
395 | line2TextField.setVisible(true);
396 |
397 | howToLabel.setText(BING_HOW_TO);
398 | howToLabel.removeMouseMotionListener(googleHowTo);
399 | howToLabel.removeMouseMotionListener(baiduHowTo);
400 | howToLabel.addMouseListener(bingHowTo);
401 |
402 | String bingClientIdStored = propertiesComponent.getValue(StorageDataKey.BingClientIdStored);
403 | String bingClientSecretStored = propertiesComponent.getValue(StorageDataKey.BingClientSecretStored);
404 |
405 | if (bingClientIdStored != null) {
406 | PromptSupport.setPrompt(bingClientIdStored, line1TextField);
407 | } else {
408 | PromptSupport.setPrompt(DEFAULT_CLIENT_ID, line1TextField);
409 | }
410 | line1TextField.setText("");
411 |
412 | if (bingClientSecretStored != null) {
413 | PromptSupport.setPrompt(bingClientSecretStored, line2TextField);
414 | } else {
415 | PromptSupport.setPrompt(DEFAULT_CLIENT_SECRET, line2TextField);
416 | }
417 | line2TextField.setText("");
418 | }
419 | break;
420 | case Google: {
421 | line1Text.setText("API key:");
422 | line2Text.setVisible(false);
423 |
424 | line2TextField.setVisible(false);
425 |
426 | howToLabel.setText(GOOGLE_HOW_TO);
427 | howToLabel.removeMouseMotionListener(bingHowTo);
428 | howToLabel.removeMouseMotionListener(baiduHowTo);
429 | howToLabel.addMouseListener(googleHowTo);
430 |
431 | String googleAPIKey = propertiesComponent.getValue(StorageDataKey.GoogleApiKeyStored);
432 |
433 | if (googleAPIKey != null) {
434 | PromptSupport.setPrompt(googleAPIKey, line1TextField);
435 | } else {
436 | PromptSupport.setPrompt(DEFAULT_GOOGLE_API_KEY, line1TextField);
437 | }
438 | line1TextField.setText("");
439 | }
440 | break;
441 | }
442 | }
443 |
444 | private void initContentContainer() {
445 | line1TextField = new JTextField();
446 | line2TextField = new JTextField();
447 |
448 | line1Text = new JLabel("Client Id:");
449 | line2Text = new JLabel("Client Secret:");
450 |
451 | Container outContainer = new Container();
452 | outContainer.setLayout(new BorderLayout(0, 5));
453 |
454 | howToLabel = new JLabel();
455 | howToLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
456 | outContainer.add(howToLabel, BorderLayout.NORTH);
457 |
458 | Container contentContainer = new Container();
459 | contentContainer.setLayout(new GridBagLayout());
460 | ((GridBagLayout) contentContainer.getLayout()).columnWidths = new int[]{0, 0, 0};
461 | ((GridBagLayout) contentContainer.getLayout()).rowHeights = new int[]{0, 0, 0};
462 | ((GridBagLayout) contentContainer.getLayout()).columnWeights = new double[]{0.0, 0.0, 1.0E-4};
463 | ((GridBagLayout) contentContainer.getLayout()).rowWeights = new double[]{0.0, 0.0, 1.0E-4};
464 |
465 | line1Text.setHorizontalAlignment(SwingConstants.RIGHT);
466 | contentContainer.add(line1Text, new GridBagConstraints(0, 0, 1, 1, 0.5, 0.0,
467 | GridBagConstraints.CENTER, GridBagConstraints.BOTH,
468 | new Insets(0, 0, 5, 5), 0, 0));
469 |
470 | contentContainer.add(line1TextField, new GridBagConstraints(1, 0, 1, 1, 10.0, 0.0,
471 | GridBagConstraints.CENTER, GridBagConstraints.BOTH,
472 | new Insets(0, 0, 5, 0), 0, 0));
473 |
474 | line2Text.setHorizontalAlignment(SwingConstants.RIGHT);
475 | contentContainer.add(line2Text, new GridBagConstraints(0, 1, 1, 1, 0.5, 0.0,
476 | GridBagConstraints.CENTER, GridBagConstraints.BOTH,
477 | new Insets(0, 0, 0, 5), 0, 0));
478 | contentContainer.add(line2TextField, new GridBagConstraints(1, 1, 1, 1, 10.0, 0.0,
479 | GridBagConstraints.CENTER, GridBagConstraints.BOTH,
480 | new Insets(0, 0, 0, 0), 0, 0));
481 |
482 | outContainer.add(contentContainer, BorderLayout.CENTER);
483 | settingPanel.add(outContainer);
484 | }
485 |
486 | private void initAndAddFilterContainer() {
487 | Container filterSettingContainer = new Container();
488 | filterSettingContainer.setLayout(new BorderLayout(0, 5));
489 |
490 | final JLabel filterLabel = new JLabel("Filter setting");
491 | filterSettingContainer.add(filterLabel, BorderLayout.NORTH);
492 |
493 | Container listPane = new Container();
494 | listPane.setLayout(new BorderLayout());
495 |
496 | JBScrollPane scrollPane = new JBScrollPane();
497 | filterList = new JBList(new String[]{"1,", "2"});
498 |
499 | filterList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
500 | scrollPane.setViewportView(filterList);
501 | listPane.add(scrollPane, BorderLayout.NORTH);
502 |
503 | Container btnPane = new Container();
504 | btnPane.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
505 | btnAddFilter = new JButton("+");
506 | btnDeleteFilter = new JButton("-");
507 | btnPane.add(btnAddFilter);
508 | btnPane.add(btnDeleteFilter);
509 |
510 | filterList.addMouseListener(new MouseAdapter() {
511 | @Override
512 | public void mouseClicked(MouseEvent e) {
513 | if (SwingUtilities.isLeftMouseButton(e)) {
514 | if (filterList.getSelectedIndex() <= 0) {
515 | btnDeleteFilter.setEnabled(false);
516 | } else {
517 | btnDeleteFilter.setEnabled(true);
518 | }
519 | }
520 | }
521 | });
522 |
523 | btnAddFilter.addActionListener(new ActionListener() {
524 | @Override
525 | public void actionPerformed(ActionEvent actionEvent) {
526 | filterRulesChanged = true;
527 | AddFilterRuleDialog dialog = new AddFilterRuleDialog(settingPanel,
528 | "Set your filter rule", false);
529 | dialog.setOnOKClickedListener(new AddFilterRuleDialog.OnOKClickedListener() {
530 | @Override
531 | public void onClick(FilterRule.FilterRuleType ruleType, String filterNameString) {
532 | filterRules.add(new FilterRule(ruleType, filterNameString));
533 | int index = filterList.getSelectedIndex();
534 | filterList.setListData(getFilterRulesDisplayString());
535 | filterList.setSelectedIndex(index);
536 | }
537 | });
538 | dialog.show();
539 | }
540 | });
541 |
542 | btnDeleteFilter.addActionListener(new ActionListener() {
543 | @Override
544 | public void actionPerformed(ActionEvent actionEvent) {
545 | filterRulesChanged = true;
546 | int index = filterList.getSelectedIndex();
547 | filterRules.remove(index);
548 | filterList.setListData(getFilterRulesDisplayString());
549 | if (index < filterRules.size()) {
550 | filterList.setSelectedIndex(index);
551 | } else {
552 | if (filterRules.size() == 1) {
553 | btnDeleteFilter.setEnabled(false);
554 | }
555 | filterList.setSelectedIndex(filterRules.size() - 1);
556 | }
557 | }
558 | });
559 |
560 | listPane.add(btnPane, BorderLayout.CENTER);
561 | filterSettingContainer.add(listPane, BorderLayout.CENTER);
562 |
563 | settingPanel.add(filterSettingContainer);
564 | }
565 |
566 | private void resetFilterList() {
567 | btnDeleteFilter.setEnabled(false);
568 | filterRules.clear();
569 | filterRules.addAll(FilterRule.getFilterRulesFromLocal());
570 |
571 | filterList.setListData(getFilterRulesDisplayString());
572 | }
573 |
574 | private String[] getFilterRulesDisplayString() {
575 | String[] displayStrings = new String[filterRules.size()];
576 | for (int i = 0; i < filterRules.size(); i++) {
577 | displayStrings[i] = filterRules.get(i).toString();
578 | }
579 | return displayStrings;
580 | }
581 | }
582 |
--------------------------------------------------------------------------------