diff --git a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateDialog.java b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateDialog.java new file mode 100644 index 0000000000..ab2d3447a7 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateDialog.java @@ -0,0 +1,127 @@ +package com.fr.design.onlineupdate.push; + +import com.fr.design.dialog.UIDialog; +import com.fr.design.ui.ModernUIPane; +import com.fr.design.utils.gui.GUICoreUtils; + +import javax.swing.JPanel; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Frame; + +/** + * Created by plough on 2019/4/10. + */ +class DesignerPushUpdateDialog extends UIDialog { + public static final Dimension DEFAULT = new Dimension(640, 320); + + private ModernUIPane jsPane; + + private DesignerPushUpdateDialog(Frame parent) { + super(parent); + setModal(true); + initComponents(); + } + + static void createAndShow(Frame parent, DesignerUpdateInfo updateInfo) { + DesignerPushUpdateDialog dialog = new DesignerPushUpdateDialog(parent); + dialog.populate(updateInfo); + dialog.showDialog(); + } + + private void initComponents() { + JPanel contentPane = (JPanel) getContentPane(); + contentPane.setLayout(new BorderLayout()); + + jsPane = new ModernUIPane.Builder() + .withEMB("/com/fr/design/ui/onlineupdate/push/pushUpdate.html").namespace("Pool").build(); + + contentPane.add(jsPane); + } + + private void populate(DesignerUpdateInfo updateInfo) { + Model model = createModel(updateInfo); + jsPane.populate(model); + } + + private Model createModel(DesignerUpdateInfo updateInfo) { + Model model = new Model(); + model.setVersion(updateInfo.getPushVersion()); + model.setContent(updateInfo.getPushContent()); + model.setMoreInfoUrl(updateInfo.getMoreInfoUrl()); + model.setBackgroundUrl(updateInfo.getBackgroundUrl()); + return model; + } + + @Override + public void checkValid() throws Exception { + // do nothing + } + + /** + * 显示窗口 + */ + private void showDialog() { + setSize(DEFAULT); + setUndecorated(true); + GUICoreUtils.centerWindow(this); + setVisible(true); + } + + public class Model { + private String version; + private String content; + private String moreInfoUrl; + private String backgroundUrl; + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getMoreInfoUrl() { + return moreInfoUrl; + } + + public void setMoreInfoUrl(String moreInfoUrl) { + this.moreInfoUrl = moreInfoUrl; + } + + public String getBackgroundUrl() { + return backgroundUrl; + } + + public void setBackgroundUrl(String backgroundUrl) { + this.backgroundUrl = backgroundUrl; + } + + public void updateNow() { + DesignerPushUpdateManager.getInstance().doUpdate(); + exit(); + } + + public void remindNextTime() { + exit(); + } + + public void skipThisVersion() { + DesignerPushUpdateManager.getInstance().skipCurrentPushVersion(); + exit(); + } + + private void exit() { + DesignerPushUpdateDialog.this.dialogExit(); + } + } +} diff --git a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java index 3e9abd9822..ea80d122e9 100644 --- a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java +++ b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java @@ -1,16 +1,21 @@ package com.fr.design.onlineupdate.push; +import com.fr.design.mainframe.DesignerContext; +import com.fr.design.mainframe.DesignerFrame; import com.fr.general.CloudCenter; import com.fr.general.GeneralContext; import com.fr.general.GeneralUtils; import com.fr.general.http.HttpClient; import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StringUtils; import com.fr.workspace.WorkContext; /** * Created by plough on 2019/4/8. */ public class DesignerPushUpdateManager { + private static final String SPLIT_CHAR = "-"; private static DesignerPushUpdateManager singleton; private DesignerUpdateInfo updateInfo; private DesignerPushUpdateConfigManager config; @@ -26,17 +31,31 @@ public class DesignerPushUpdateManager { return singleton; } - private void initUpdateInfo() { - String currentVersion = GeneralUtils.readFullBuildNO(); + private void initUpdateInfo(String currentVersion, String latestVersion) { + String lastIgnoredVersion = config.getLastIgnoredVersion(); + String updatePushInfo = CloudCenter.getInstance().acquireUrlByKind("update.push"); + JSONObject pushData = new JSONObject(updatePushInfo); + + updateInfo = new DesignerUpdateInfo(currentVersion, latestVersion, lastIgnoredVersion, pushData); + } - // todo:耗时请求,可能需要优化 + private String getFullLatestVersion() { HttpClient hc = new HttpClient(CloudCenter.getInstance().acquireUrlByKind("jar10.update")); - String latestVersion = new JSONObject(hc.getResponseText()).optString("versionNO"); + return new JSONObject(hc.getResponseText()).optString("buildNO"); + } - String updatePushInfo = CloudCenter.getInstance().acquireUrlByKind("update.push"); - JSONObject pushData = new JSONObject(updatePushInfo); + private String getVersionByFullNO(String fullNO) { + if (fullNO.contains(SPLIT_CHAR)) { + fullNO = fullNO.substring(fullNO.lastIndexOf(SPLIT_CHAR) + 1); + } + return fullNO; + } - updateInfo = new DesignerUpdateInfo(currentVersion, latestVersion, pushData); + private String getPrefixByFullNO(String fullNO) { + if (fullNO.contains(SPLIT_CHAR)) { + return fullNO.substring(0, fullNO.lastIndexOf(SPLIT_CHAR)); + } + return StringUtils.EMPTY; } /** @@ -57,18 +76,72 @@ public class DesignerPushUpdateManager { /** * 检查更新,如果有合适的更新版本,则弹窗 */ - public void popUpDialog() { - if (!shouldPopUp()) { - return; - } - // todo: do pop + public void checkAndPop() { + new Thread() { + @Override + public void run() { + if (!shouldPopUp()) { + FineLoggerFactory.getLogger().debug("skip push update"); + return; + } + // todo: do pop + final DesignerFrame designerFrame = DesignerContext.getDesignerFrame(); + DesignerPushUpdateDialog.createAndShow(designerFrame, updateInfo); + } + }.start(); } private boolean shouldPopUp() { if (updateInfo == null) { - initUpdateInfo(); + String fullCurrentVersion = GeneralUtils.readFullBuildNO(); + // todo: 开发测试用 + if (!fullCurrentVersion.contains(SPLIT_CHAR)) { + fullCurrentVersion = "stable-2019.01.03.17.01.05.257"; + } + + + String fullLatestVersion = getFullLatestVersion(); + boolean isValidJarVersion = isValidJarVersion(fullCurrentVersion, fullLatestVersion); + if (!isValidJarVersion) { + FineLoggerFactory.getLogger().info("Jar version is not valid for push update."); + return false; + } else { + String currentVersion = getVersionByFullNO(fullCurrentVersion); + String latestVersion = getVersionByFullNO(fullLatestVersion); + initUpdateInfo(currentVersion, latestVersion); + } } - return isAutoPushUpdateSupported() && updateInfo.hasNewPushVersion(config.getLastIgnoredVersion()); + return isAutoPushUpdateSupported() && updateInfo.hasNewPushVersion(); + } + + private boolean isValidJarVersion(String fullCurrentVersion, String fullLatestVersion) { + // todo: 目前设定的逻辑是 feature/release/stable 都弹,且不区分版本号。后期肯定要变的,注释代码先留着 +// // 无效的情况: +// // 1. 版本号格式有误 +// // 2. 当前用的是 release 或 feature 的 jar 包 +// // 3. 代码启动的 +// String prefix = getPrefixByFullNO(fullLatestVersion); +// return StringUtils.isNotEmpty(prefix) && fullCurrentVersion.startsWith(prefix); + + // 无效的情况: + // 1. 版本号格式有误(正常情况下都有前缀,只有异常的时候才可能出现) + // 2. 代码启动的(fullCurrentVersion 为"不是安装版本") + String prefix = getPrefixByFullNO(fullLatestVersion); + return StringUtils.isNotEmpty(prefix) && fullCurrentVersion.contains(SPLIT_CHAR); + } + + /** + * 跳转到更新升级窗口,并自动开始更新 + */ + void doUpdate() { + // todo + } + + /** + * 跳过当前的推送版本 + */ + void skipCurrentPushVersion() { + // todo } } diff --git a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java index 0c3ecff4c5..8301d193e0 100644 --- a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java +++ b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java @@ -17,15 +17,17 @@ class DesignerUpdateInfo { private final String currentVersion; // 当前版本 private final String latestVersion; // 最新版本 + private final String lastIgnoredVersion; // 最近一次跳过的版本 private final String pushVersion; // 推送版本 private final String pushContent; // 推送更新内容 private final String backgroundUrl; // 推送背景图片 url private final String moreInfoUrl; // 更多新特性 - DesignerUpdateInfo(String currentVersion, String latestVersion, JSONObject pushData) { + DesignerUpdateInfo(String currentVersion, String latestVersion, String lastIgnoredVersion, JSONObject pushData) { this.currentVersion = currentVersion; this.latestVersion = latestVersion; + this.lastIgnoredVersion = lastIgnoredVersion; this.pushVersion = pushData.optString(KEY_VERSION); this.pushContent = pushData.optString(KEY_CONTENT); @@ -39,6 +41,7 @@ class DesignerUpdateInfo { } private boolean hasEmptyField() { + // lastIgnoredVersion 可以为空 return StringUtils.isEmpty(currentVersion) || StringUtils.isEmpty(latestVersion) || StringUtils.isEmpty(pushVersion) @@ -51,14 +54,18 @@ class DesignerUpdateInfo { return currentVersion; } - String getPushVersion() { - return pushVersion; - } - String getLatestVersion() { return latestVersion; } + public String getLastIgnoredVersion() { + return lastIgnoredVersion; + } + + String getPushVersion() { + return pushVersion; + } + String getPushContent() { return pushContent; } @@ -71,7 +78,7 @@ class DesignerUpdateInfo { return moreInfoUrl; } - boolean hasNewPushVersion(String lastIgnoredVersion) { + boolean hasNewPushVersion() { boolean result = ComparatorUtils.compare(pushVersion, currentVersion) > 0 && ComparatorUtils.compare(pushVersion, latestVersion) <= 0; if (StringUtils.isNotEmpty(lastIgnoredVersion)) { diff --git a/designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java b/designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java index a81d0dd903..81f9e051b5 100644 --- a/designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java +++ b/designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java @@ -75,13 +75,11 @@ public class ModernUIPane extends BasicPane { public void populate(final T t) { - browser.addLoadListener(new LoadAdapter() { + browser.addScriptContextListener(new ScriptContextAdapter() { @Override - public void onFinishLoadingFrame(FinishLoadingEvent event) { - if (event.isMainFrame()) { - JSValue ns = event.getBrowser().executeJavaScriptAndReturnValue("window." + namespace); - ns.asObject().setProperty(variable, t); - } + public void onScriptContextCreated(ScriptContextEvent event) { + JSValue ns = event.getBrowser().executeJavaScriptAndReturnValue("window." + namespace); + ns.asObject().setProperty(variable, t); } }); } diff --git a/designer-base/src/main/resources/com/fr/design/ui/onlineupdate/push/pushUpdate.html b/designer-base/src/main/resources/com/fr/design/ui/onlineupdate/push/pushUpdate.html new file mode 100644 index 0000000000..323295248f --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/ui/onlineupdate/push/pushUpdate.html @@ -0,0 +1,179 @@ + + + + + Push Update + + + + + + + + \ No newline at end of file diff --git a/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateDialogTest.java b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateDialogTest.java new file mode 100644 index 0000000000..841a194297 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateDialogTest.java @@ -0,0 +1,23 @@ +package com.fr.design.onlineupdate.push; + +import com.fr.design.DesignerEnvManager; +import com.fr.json.JSONObject; + +/** + * Created by plough on 2019/4/10. + */ +public class DesignerPushUpdateDialogTest { + + public static void main(String[] args) { + DesignerEnvManager.getEnvManager().setOpenDebug(true); + + JSONObject jo = JSONObject.create(); + jo.put("version", "2019.03.06.04.02.43.6"); + jo.put("content", "test content"); + jo.put("more", "http://baidu.com"); + jo.put("background", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555043827901&di=fc266992abef5a7e13b4e0cb98975a75&imgtype=0&src=http%3A%2F%2Fi5.3conline.com%2Fimages%2Fpiclib%2F201203%2F20%2Fbatch%2F1%2F130280%2F1332249463721rez0li5fg0_medium.jpg"); + DesignerUpdateInfo mockUpdateInfo = new DesignerUpdateInfo("111.22.11", "2211.231.1", "11.23.1", jo); + + DesignerPushUpdateDialog.createAndShow(null, mockUpdateInfo); + } +} diff --git a/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java index b346d9b4aa..5736793fd4 100644 --- a/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java +++ b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertTrue; public class DesignerUpdateInfoTest { private static final String CURRENT_VERSION = "2018.09.03.xx"; private static final String LATEST_VERSION = "2019.04.03.yy"; + private static final String LAST_IGNORED_VERSION = "2019.02.03.yy"; private static final String PUSH_VERSION = "2019.01.03.21.11"; private static final String PUSH_CONTENT = "the update desc content"; private static final String PUSH_BACKGROUND = "http://image.fr.com/123.jpg"; @@ -33,13 +34,14 @@ public class DesignerUpdateInfoTest { pushData.put("background", PUSH_BACKGROUND); pushData.put("more", PUSH_MORE); - updateInfo = new DesignerUpdateInfo(CURRENT_VERSION, LATEST_VERSION, pushData); + updateInfo = new DesignerUpdateInfo(CURRENT_VERSION, LATEST_VERSION, LAST_IGNORED_VERSION, pushData); } @Test public void testGetters() { assertEquals(CURRENT_VERSION, updateInfo.getCurrentVersion()); assertEquals(LATEST_VERSION, updateInfo.getLatestVersion()); + assertEquals(LAST_IGNORED_VERSION, updateInfo.getLastIgnoredVersion()); assertEquals(PUSH_VERSION, updateInfo.getPushVersion()); assertEquals(PUSH_CONTENT, updateInfo.getPushContent()); assertEquals(PUSH_BACKGROUND, updateInfo.getBackgroundUrl()); @@ -79,14 +81,14 @@ public class DesignerUpdateInfoTest { pushData.put("content", PUSH_CONTENT); pushData.put("background", PUSH_BACKGROUND); pushData.put("more", PUSH_MORE); - DesignerUpdateInfo updateInfo = new DesignerUpdateInfo(Y, Z, pushData); - return updateInfo.hasNewPushVersion(X0); + DesignerUpdateInfo updateInfo = new DesignerUpdateInfo(Y, Z, X0, pushData); + return updateInfo.hasNewPushVersion(); } @Test public void testParameterValidation() { try { - DesignerUpdateInfo updateInfo = new DesignerUpdateInfo(null, null, new JSONObject()); + DesignerUpdateInfo updateInfo = new DesignerUpdateInfo(null, null, null, new JSONObject()); Assert.fail("should not reach here!"); } catch (InvalidParameterException e) { // do nothing diff --git a/designer-realize/src/main/java/com/fr/start/module/DesignerActivator.java b/designer-realize/src/main/java/com/fr/start/module/DesignerActivator.java index f360f24b9b..f4bb4acaa8 100644 --- a/designer-realize/src/main/java/com/fr/start/module/DesignerActivator.java +++ b/designer-realize/src/main/java/com/fr/start/module/DesignerActivator.java @@ -53,6 +53,7 @@ import com.fr.design.mainframe.loghandler.DesignerLogAppender; import com.fr.design.mainframe.loghandler.LogMessageBar; import com.fr.design.mainframe.socketio.DesignerSocketIO; import com.fr.design.module.DesignModuleFactory; +import com.fr.design.onlineupdate.push.DesignerPushUpdateManager; import com.fr.design.parameter.FormParameterReader; import com.fr.design.parameter.ParameterPropertyPane; import com.fr.design.parameter.WorkBookParameterReader; @@ -129,6 +130,7 @@ public class DesignerActivator extends Activator { loadLogAppender(); DesignerSocketIO.update(); UserInfoPane.getInstance().updateBBSUserInfo(); + DesignerPushUpdateManager.getInstance().checkAndPop(); } private void loadLogAppender() {