From cf3bea4e9a16a0137271f2bd37f0dc18d12d997c Mon Sep 17 00:00:00 2001 From: neil <459208047@qq.com> Date: Wed, 25 Jul 2018 09:52:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?REPORT-9982=20&=20REPORT-9970=20=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=89=88=E6=9C=AC=E4=BD=BF=E7=94=A8=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E5=B8=AE=E5=8A=A9=E6=96=87=E6=A1=A3=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=20&=20=E8=91=A1=E8=90=84=E7=89=99=E8=AF=AD=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E5=8C=96=E6=94=AF=E6=8C=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/DesignerEnvManager.java | 27 ++++++- .../design/actions/help/TutorialAction.java | 72 ++++++++++++------- .../com/fr/design/DesignerEnvManagerTest.java | 33 +++++++++ .../actions/help/TutorialActionTest.java | 40 +++++++++++ .../fr/start/module/PreStartActivator.java | 4 +- 5 files changed, 147 insertions(+), 29 deletions(-) create mode 100644 designer-base/src/test/java/com/fr/design/DesignerEnvManagerTest.java create mode 100644 designer-base/src/test/java/com/fr/design/actions/help/TutorialActionTest.java diff --git a/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java b/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java index 034e4968b..d5b401eea 100644 --- a/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java +++ b/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java @@ -19,8 +19,9 @@ import com.fr.general.ComparatorUtils; import com.fr.general.FRLogFormatter; import com.fr.general.GeneralContext; import com.fr.general.IOUtils; - +import com.fr.general.Inter; import com.fr.general.xml.GeneralXMLTools; +import com.fr.locale.InterProviderFactory; import com.fr.log.FineLoggerFactory; import com.fr.stable.Constants; import com.fr.stable.EnvChangedListener; @@ -55,6 +56,7 @@ import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.logging.FileHandler; @@ -700,6 +702,29 @@ public class DesignerEnvManager implements XMLReadable, XMLWriter { return this.language; } + /** + * 返回语言类型 + */ + public Locale getLocale() { + // 性能 + if (language <= 1) { + return Locale.CHINA; + } + + Locale[] locales = supportLocale(); + if (language <= locales.length) { + return locales[language - 1]; + } + return Locale.CHINA; + } + + // 当前系统支持的语言 + protected Locale[] supportLocale() { + Inter.getInstance(); + Map languageMap = InterProviderFactory.getProvider().getSupportLocaleMap(); + return languageMap.keySet().toArray(new Locale[languageMap.size()]); + } + /** * 设置语言参数 */ diff --git a/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java b/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java index 32efb23dd..01acba8d8 100644 --- a/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java +++ b/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java @@ -1,24 +1,28 @@ package com.fr.design.actions.help; -import java.awt.Desktop; -import java.awt.event.ActionEvent; -import java.awt.event.KeyEvent; -import java.io.File; -import java.io.IOException; -import java.net.URI; - -import javax.swing.KeyStroke; - import com.fr.base.BaseUtils; import com.fr.base.FRContext; import com.fr.base.Utils; import com.fr.design.actions.UpdateAction; import com.fr.design.menu.MenuKeySet; import com.fr.general.CloudCenter; - -import com.fr.general.http.HttpClient; -import com.fr.stable.OperatingSystem; +import com.fr.general.http.HttpToolbox; +import com.fr.stable.CommonUtils; +import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; +import com.fr.stable.StringUtils; +import com.fr.stable.os.OperatingSystem; +import com.fr.third.org.apache.http.HttpStatus; +import com.fr.third.org.apache.http.StatusLine; +import com.fr.third.org.apache.http.client.methods.HttpGet; + +import javax.swing.KeyStroke; +import java.awt.Desktop; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.io.File; +import java.io.IOException; +import java.net.URI; public class TutorialAction extends UpdateAction { @@ -56,28 +60,44 @@ public class TutorialAction extends UpdateAction { * @param evt 事件 */ public void actionPerformed(ActionEvent evt) { - String helpURL = CloudCenter.getInstance().acquireUrlByKind("help." + FRContext.getLocale()); - - if (helpURL != null) { - HttpClient client = new HttpClient(helpURL); - if(client.getResponseCode() != -1) { - try { - Desktop.getDesktop().browse(new URI(helpURL)); - return; - } catch (Exception e) { - //出了异常的话, 依然打开本地教程 - } + String helpURL = CloudCenter.getInstance().acquireUrlByKind(createDocKey()); + if (isServerOnline(helpURL)) { + try { + Desktop.getDesktop().browse(new URI(helpURL)); + return; + } catch (Exception e) { } } - if (OperatingSystem.isMacOS()) { + if (OperatingSystem.isUnix()) { nativeExcuteMacInstallHomePrograms("helptutorial.app"); - } - else { + } else { Utils.nativeExcuteInstallHomePrograms("helptutorial.exe"); } } + // 生成帮助文档 sitecenter key + protected String createDocKey() { + String locale = FRContext.getLocale().toString(); + return CommonUtils.join(new String[]{ "help", locale, ProductConstants.MAIN_VERSION }, "."); + } + + // 判断是否可以访问在线文档 + protected boolean isServerOnline(String url) { + if (StringUtils.isEmpty(url)) { + return false; + } + + HttpGet getHelp = new HttpGet(url); + try { + StatusLine statusLine = HttpToolbox.getHttpClient(url).execute(getHelp).getStatusLine(); + return statusLine.getStatusCode() == HttpStatus.SC_OK; + } catch (Exception ignore) { + // 网络异常 + return false; + } + } + public static final MenuKeySet HELP_TUTORIAL = new MenuKeySet() { @Override public char getMnemonic() { diff --git a/designer-base/src/test/java/com/fr/design/DesignerEnvManagerTest.java b/designer-base/src/test/java/com/fr/design/DesignerEnvManagerTest.java new file mode 100644 index 000000000..65a3e78df --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/DesignerEnvManagerTest.java @@ -0,0 +1,33 @@ +package com.fr.design; + +import com.fr.locale.InterProviderFactory; +import junit.framework.TestCase; + +import java.util.HashSet; +import java.util.Locale; + +public class DesignerEnvManagerTest extends TestCase{ + + public void testGetLocale() { + DesignerEnvManager envManager = new DesignerEnvManager(); + assertEquals("默认值", envManager.getLocale(), Locale.CHINA); + + HashSet result = new HashSet<>(); + Locale[] locales = envManager.supportLocale(); + int len = locales.length; + assertEquals(len, InterProviderFactory.getProvider().getSupportLocaleMap().size()); + for (int i = 0; i < len; i++) { + envManager.setLanguage(i + 1); + Locale locale = envManager.getLocale(); + result.add(locale); + } + assertEquals("每个int都有对应的locale", result.size(), len); + + envManager.setLanguage(998); + assertEquals("上边界", envManager.getLocale(), Locale.CHINA); + + envManager.setLanguage(-998); + assertEquals("下边界", envManager.getLocale(), Locale.CHINA); + + } +} diff --git a/designer-base/src/test/java/com/fr/design/actions/help/TutorialActionTest.java b/designer-base/src/test/java/com/fr/design/actions/help/TutorialActionTest.java new file mode 100644 index 000000000..e538a637e --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/actions/help/TutorialActionTest.java @@ -0,0 +1,40 @@ +package com.fr.design.actions.help; + +import com.fr.general.GeneralContext; +import com.fr.stable.ProductConstants; +import junit.framework.TestCase; + +import java.util.Locale; +import java.util.UUID; + +public class TutorialActionTest extends TestCase{ + + public void testCreateKey() { + TutorialAction action = new TutorialAction(); + GeneralContext.setLocale(Locale.US); + String enKey = action.createDocKey(); + assertTrue(enKey.indexOf(Locale.US.toString()) != -1); + assertTrue(enKey.indexOf(ProductConstants.MAIN_VERSION) != -1); + + GeneralContext.setLocale(Locale.CHINA); + String zhKey = action.createDocKey(); + assertTrue(zhKey.indexOf(Locale.CHINA.toString()) != -1); + + Locale pt = new Locale("pt", "PT"); + GeneralContext.setLocale(pt); + String ptKey = action.createDocKey(); + assertTrue(ptKey.indexOf(pt.toString()) != -1); + + GeneralContext.setLanguage(998); + assertTrue(action.createDocKey().indexOf(Locale.CHINA.toString()) != -1); + } + + public void testServerOnline() { + TutorialAction action = new TutorialAction(); + assertFalse(action.isServerOnline(null)); + assertFalse(action.isServerOnline("中文")); + assertTrue("百度暂时不会挂", action.isServerOnline("http://www.baidu.com")); + assertTrue("https测试", action.isServerOnline("https://www.baidu.com")); + assertFalse("连上是正常的", action.isServerOnline("https://www.fine-" + UUID.randomUUID() + ".com")); + } +} diff --git a/designer-realize/src/main/java/com/fr/start/module/PreStartActivator.java b/designer-realize/src/main/java/com/fr/start/module/PreStartActivator.java index 38f8c1c20..779747990 100644 --- a/designer-realize/src/main/java/com/fr/start/module/PreStartActivator.java +++ b/designer-realize/src/main/java/com/fr/start/module/PreStartActivator.java @@ -1,11 +1,11 @@ package com.fr.start.module; -import com.fr.base.FRContext; import com.fr.design.DesignerEnvManager; import com.fr.design.RestartHelper; import com.fr.design.utils.DesignUtils; import com.fr.general.CloudCenter; import com.fr.general.ComparatorUtils; +import com.fr.general.GeneralContext; import com.fr.module.Activator; import com.fr.stable.ProductConstants; @@ -64,7 +64,7 @@ public class PreStartActivator extends Activator { private void initLanguage() { //这两句的位置不能随便调换,因为会影响语言切换的问题 - FRContext.setLanguage(DesignerEnvManager.getEnvManager(false).getLanguage()); + GeneralContext.setLocale(DesignerEnvManager.getEnvManager(false).getLocale()); } @Override From 3682744bf9670e3d1cfe1e70e4bbdc7ed4ca72ed Mon Sep 17 00:00:00 2001 From: neil <459208047@qq.com> Date: Wed, 25 Jul 2018 09:53:08 +0800 Subject: [PATCH 2/3] doc --- .../src/main/java/com/fr/design/DesignerEnvManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java b/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java index d5b401eea..40c3a12eb 100644 --- a/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java +++ b/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java @@ -710,7 +710,6 @@ public class DesignerEnvManager implements XMLReadable, XMLWriter { if (language <= 1) { return Locale.CHINA; } - Locale[] locales = supportLocale(); if (language <= locales.length) { return locales[language - 1]; From 20f8316ab3f13146968b6a0e9ad75962b819f123 Mon Sep 17 00:00:00 2001 From: neil <459208047@qq.com> Date: Wed, 25 Jul 2018 09:55:35 +0800 Subject: [PATCH 3/3] doc --- .../main/java/com/fr/design/actions/help/TutorialAction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java b/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java index 01acba8d8..4e3ea8320 100644 --- a/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java +++ b/designer-base/src/main/java/com/fr/design/actions/help/TutorialAction.java @@ -76,7 +76,7 @@ public class TutorialAction extends UpdateAction { } } - // 生成帮助文档 sitecenter key + // 生成帮助文档 sitecenter key, help.zh_CN.10 protected String createDocKey() { String locale = FRContext.getLocale().toString(); return CommonUtils.join(new String[]{ "help", locale, ProductConstants.MAIN_VERSION }, ".");