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..40c3a12eb 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,28 @@ 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..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 @@ -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, help.zh_CN.10 + 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