diff --git a/designer-base/src/main/java/com/fr/design/dialog/BasicPane.java b/designer-base/src/main/java/com/fr/design/dialog/BasicPane.java index e684d81f6c..930dde76c7 100644 --- a/designer-base/src/main/java/com/fr/design/dialog/BasicPane.java +++ b/designer-base/src/main/java/com/fr/design/dialog/BasicPane.java @@ -3,6 +3,7 @@ package com.fr.design.dialog; import com.fr.common.annotations.Open; import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.itextfield.UITextField; +import com.fr.design.i18n.DesignSizeI18nManager; import com.fr.design.i18n.Toolkit; import com.fr.design.utils.gui.GUICoreUtils; import com.fr.stable.core.PropertyChangeAdapter; @@ -33,7 +34,7 @@ public abstract class BasicPane extends JPanel { * @return 对话框 */ public BasicDialog showWindow(Window window, DialogActionListener l) { - return showWindowWithCustomSize(window, l, BasicDialog.DEFAULT); + return showWindowWithCustomSize(window, l, DesignSizeI18nManager.getInstance().i18nDimension(this.i18nText4PopupWindow())); } /** @@ -236,6 +237,14 @@ public abstract class BasicPane extends JPanel { return title4PopupWindow(); } + /** + * 国际化标识 + * @return 默认是取当前类的全限定名 + */ + protected String i18nText4PopupWindow() { + return this.getClass().getName(); + } + /** * 作为名字面板 * @@ -403,4 +412,4 @@ public abstract class BasicPane extends JPanel { } -} \ No newline at end of file +} diff --git a/designer-base/src/main/java/com/fr/design/i18n/DesignSizeI18nManager.java b/designer-base/src/main/java/com/fr/design/i18n/DesignSizeI18nManager.java new file mode 100644 index 0000000000..1dd2306b89 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/i18n/DesignSizeI18nManager.java @@ -0,0 +1,57 @@ +package com.fr.design.i18n; + +import com.fr.design.dialog.BasicDialog; +import com.fr.general.GeneralContext; +import com.fr.locale.LocaleManager; +import com.fr.locale.impl.FineLocaleManager; + +import java.awt.Dimension; +import java.util.Map; + +/** + * Created by kerry on 2/23/21 + */ +public class DesignSizeI18nManager { + private static final String I18N_DIMENSION_PATH = "com/fr/design/i18n/dimension"; + private static final String DIMENSION_REGEX = "^[1-9]\\d*\\*[1-9]\\d*$"; + private static final String SEPARATOR_REGEX = "\\*"; + private static final int WIDTH_INDEX = 0; + private static final int HEIGHT_INDEX = 1; + private static final int SPLIT_LENGTH = 2; + + private static DesignSizeI18nManager instance = new DesignSizeI18nManager(); + + public static DesignSizeI18nManager getInstance() { + return instance; + } + + private LocaleManager localeManager = FineLocaleManager.create(); + + private DesignSizeI18nManager() { + localeManager.addResource(I18N_DIMENSION_PATH); + } + + public Dimension i18nDimension(String key) { + if (!containKey(key)) { + return BasicDialog.DEFAULT; + } + String dimension = localeManager.getLocalBundle(GeneralContext.getLocale()).getText(localeManager, key); + return parseDimensionFromText(dimension); + } + + private boolean containKey(String key) { + Map localeKV = localeManager.getLocalBundle(GeneralContext.getLocale()).getKV(localeManager); + return localeKV != null && localeKV.containsKey(key); + } + + private Dimension parseDimensionFromText(String dimensionText) { + if (!dimensionText.matches(DIMENSION_REGEX)) { + return BasicDialog.DEFAULT; + } + String[] arr = dimensionText.split(SEPARATOR_REGEX); + if (arr.length < SPLIT_LENGTH) { + return BasicDialog.DEFAULT; + } + return new Dimension(Integer.parseInt(arr[WIDTH_INDEX]), Integer.parseInt(arr[HEIGHT_INDEX])); + } +} diff --git a/designer-base/src/main/resources/com/fr/design/i18n/dimension_en.properties b/designer-base/src/main/resources/com/fr/design/i18n/dimension_en.properties new file mode 100644 index 0000000000..1cafa6d35d --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/i18n/dimension_en.properties @@ -0,0 +1 @@ +com.fr.design.report.ReportColumnsPane=800*600 diff --git a/designer-base/src/main/resources/com/fr/design/i18n/dimension_ja_JP.properties b/designer-base/src/main/resources/com/fr/design/i18n/dimension_ja_JP.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/designer-base/src/main/resources/com/fr/design/i18n/dimension_ko_KR.properties b/designer-base/src/main/resources/com/fr/design/i18n/dimension_ko_KR.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/designer-base/src/main/resources/com/fr/design/i18n/dimension_zh.properties b/designer-base/src/main/resources/com/fr/design/i18n/dimension_zh.properties new file mode 100644 index 0000000000..69c2fcef48 --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/i18n/dimension_zh.properties @@ -0,0 +1 @@ +# \u9ED8\u8BA4\u4E3A\u7C7B\u7684\u5168\u9650\u5B9A\u540D\uFF08\u53EF\u81EA\u5B9A\u4E49key\uFF09= width * height diff --git a/designer-base/src/main/resources/com/fr/design/i18n/dimension_zh_TW.properties b/designer-base/src/main/resources/com/fr/design/i18n/dimension_zh_TW.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/designer-base/src/test/java/com/fr/design/i18n/DesignSizeI18nManagerTest.java b/designer-base/src/test/java/com/fr/design/i18n/DesignSizeI18nManagerTest.java new file mode 100644 index 0000000000..da9789526b --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/i18n/DesignSizeI18nManagerTest.java @@ -0,0 +1,80 @@ +package com.fr.design.i18n; + +import com.fr.general.GeneralContext; +import com.fr.invoke.Reflect; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.awt.Dimension; +import java.util.Locale; + +/** + * Created by kerry on 2/24/21 + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(GeneralContext.class) +public class DesignSizeI18nManagerTest { + @Test + public void testI18nDimension() { + Dimension dimension = DesignSizeI18nManager.getInstance().i18nDimension("com.fr.design.report.ReportColumnsPane"); + validDimension(dimension, 660, 600); + + PowerMock.mockStatic(GeneralContext.class); + EasyMock.expect(GeneralContext.getLocale()).andReturn(Locale.ENGLISH).times(3); + PowerMock.replayAll(); + + dimension = DesignSizeI18nManager.getInstance().i18nDimension("com.fr.design.report.ReportColumnsPane"); + validDimension(dimension, 800, 600); + } + + @Test + public void testContainKey() { + PowerMock.mockStatic(GeneralContext.class); + EasyMock.expect(GeneralContext.getLocale()).andReturn(Locale.ENGLISH).times(3); + PowerMock.replayAll(); + + boolean result = Reflect.on(DesignSizeI18nManager.getInstance()).call("containKey", "testKey").get(); + Assert.assertFalse(result); + + result = Reflect.on(DesignSizeI18nManager.getInstance()).call("containKey", "com.fr.design.report.ReportColumnsPane").get(); + Assert.assertTrue(result); + + } + + @Test + public void testParseDimensionFromText() { + String dimensionText = "800*600"; + Dimension result = Reflect.on(DesignSizeI18nManager.getInstance()).call("parseDimensionFromText", dimensionText).get(); + validDimension(result, 800, 600); + + dimensionText = "800* 600"; + result = Reflect.on(DesignSizeI18nManager.getInstance()).call("parseDimensionFromText", dimensionText).get(); + validDimension(result, 660, 600); + + dimensionText = " 800*600"; + result = Reflect.on(DesignSizeI18nManager.getInstance()).call("parseDimensionFromText", dimensionText).get(); + validDimension(result, 660, 600); + + dimensionText = "800*600s"; + result = Reflect.on(DesignSizeI18nManager.getInstance()).call("parseDimensionFromText", dimensionText).get(); + validDimension(result, 660, 600); + + dimensionText = "800s*600"; + result = Reflect.on(DesignSizeI18nManager.getInstance()).call("parseDimensionFromText", dimensionText).get(); + validDimension(result, 660, 600); + + dimensionText = "800-600"; + result = Reflect.on(DesignSizeI18nManager.getInstance()).call("parseDimensionFromText", dimensionText).get(); + validDimension(result, 660, 600); + } + + private void validDimension(Dimension dimension, int width, int height) { + Assert.assertEquals(width, dimension.width); + Assert.assertEquals(height, dimension.height); + } +}