From 18361d2bda53da8b4e3ddc1cc58e4aac6cab78c5 Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 7 Mar 2019 09:42:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0JIRA=E4=BB=BB=E5=8A=A1=20=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E6=8F=90=E4=BE=9B=E7=9A=84web=E5=AE=B9=E5=99=A8API?= =?UTF-8?q?=EF=BC=8C=E7=AE=80=E5=8C=96=E5=8A=A0=E8=BD=BDjar=E5=8C=85?= =?UTF-8?q?=E4=B8=ADhtml/js/css=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/DesignerEnvManager.java | 1 + .../com/fr/design/extra/WebViewDlgHelper.java | 3 +- .../java/com/fr/design/ui/ModernUIAssist.java | 39 +++++++++ .../{ModernUI.java => ModernUIConstants.java} | 2 +- .../java/com/fr/design/ui/ModernUIPane.java | 82 ++++++++++++++++++- .../com/fr/design/ui/ModernUIPaneTest.java | 2 +- .../test/resources/com/fr/design/ui/demo.html | 1 + 7 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java rename designer-base/src/main/java/com/fr/design/ui/{ModernUI.java => ModernUIConstants.java} (94%) 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 de108c388..522718d2d 100644 --- a/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java +++ b/designer-base/src/main/java/com/fr/design/DesignerEnvManager.java @@ -1687,6 +1687,7 @@ public class DesignerEnvManager implements XMLReadable, XMLWriter { writeHttpsParas(writer); writeAlphaFineAttr(writer); writeRecentColor(writer); + writeOpenDebug(writer); writer.end(); } diff --git a/designer-base/src/main/java/com/fr/design/extra/WebViewDlgHelper.java b/designer-base/src/main/java/com/fr/design/extra/WebViewDlgHelper.java index 54ddcd8c0..a0abb1c93 100644 --- a/designer-base/src/main/java/com/fr/design/extra/WebViewDlgHelper.java +++ b/designer-base/src/main/java/com/fr/design/extra/WebViewDlgHelper.java @@ -214,7 +214,8 @@ public class WebViewDlgHelper { private static void showPluginDlg() { try { - Class clazz = Class.forName("com.fr.design.extra.PluginWebPane"); + Class clazz = Class.forName("com.fr.design.extra.modern.PluginModernPane"); + //Class clazz = Class.forName("com.fr.design.extra.PluginWebPane"); Constructor constructor = clazz.getConstructor(String.class, String.class); Component webPane = (Component) constructor.newInstance(installHome, MAIN_JS_PATH); diff --git a/designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java b/designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java new file mode 100644 index 000000000..3475b20cb --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java @@ -0,0 +1,39 @@ +package com.fr.design.ui; + +import com.teamdev.jxbrowser.chromium.URLResponse; + +import java.io.DataInputStream; +import java.io.InputStream; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-07 + */ +public class ModernUIAssist { + + public static URLResponse inputStream2Response(InputStream inputStream, String filePath) throws Exception { + URLResponse response = new URLResponse(); + DataInputStream stream = new DataInputStream(inputStream); + byte[] data = new byte[stream.available()]; + stream.readFully(data); + response.setData(data); + String mimeType = getMimeType(filePath); + response.getHeaders().setHeader("Content-Type", mimeType); + return response; + } + + + private static String getMimeType(String path) { + if (path.endsWith(".html")) { + return "text/html"; + } + if (path.endsWith(".css")) { + return "text/css"; + } + if (path.endsWith(".js")) { + return "text/javascript"; + } + return "text/html"; + } +} diff --git a/designer-base/src/main/java/com/fr/design/ui/ModernUI.java b/designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java similarity index 94% rename from designer-base/src/main/java/com/fr/design/ui/ModernUI.java rename to designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java index ab829f4e9..f17244cb7 100644 --- a/designer-base/src/main/java/com/fr/design/ui/ModernUI.java +++ b/designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java @@ -5,7 +5,7 @@ package com.fr.design.ui; * @version 10.0 * Created by richie on 2019-03-05 */ -class ModernUI { +class ModernUIConstants { static final String SCRIPT_STRING = "var arr = \"%s\".split(\".\").reverse();\n" + "var create = function(obj, names) {\n" + 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 ac5e4922d..b3c72c9ac 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 @@ -2,17 +2,27 @@ package com.fr.design.ui; import com.fr.design.DesignerEnvManager; import com.fr.design.dialog.BasicPane; +import com.fr.general.IOUtils; import com.teamdev.jxbrowser.chromium.Browser; +import com.teamdev.jxbrowser.chromium.BrowserContext; import com.teamdev.jxbrowser.chromium.BrowserPreferences; import com.teamdev.jxbrowser.chromium.JSValue; +import com.teamdev.jxbrowser.chromium.ProtocolHandler; +import com.teamdev.jxbrowser.chromium.ProtocolService; +import com.teamdev.jxbrowser.chromium.URLRequest; +import com.teamdev.jxbrowser.chromium.URLResponse; import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; import com.teamdev.jxbrowser.chromium.events.LoadAdapter; +import com.teamdev.jxbrowser.chromium.events.LoadListener; import com.teamdev.jxbrowser.chromium.events.ScriptContextAdapter; import com.teamdev.jxbrowser.chromium.events.ScriptContextEvent; +import com.teamdev.jxbrowser.chromium.events.ScriptContextListener; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; +import java.io.InputStream; +import java.net.URL; /** * @author richie @@ -56,11 +66,42 @@ public class ModernUIPane extends BasicPane { private void initializeBrowser() { browser = new Browser(); + BrowserContext browserContext = browser.getContext(); + ProtocolService protocolService = browserContext.getProtocolService(); + // 支持从jar中读取资源文件 + protocolService.setProtocolHandler("jar", new ProtocolHandler() { + @Override + public URLResponse onRequest(URLRequest request) { + try { + String path = request.getURL(); + URL url = new URL(path); + InputStream inputStream = url.openStream(); + return ModernUIAssist.inputStream2Response(inputStream, path); + } catch (Exception ignored) { + } + return null; + } + }); + // 支持读取jar包中文件的自定义协议————emb:/com/fr/design/images/bbs.png + protocolService.setProtocolHandler("emb", new ProtocolHandler() { + @Override + public URLResponse onRequest(URLRequest req) { + try { + String path = req.getURL(); + path = path.substring(4); + InputStream inputStream = IOUtils.readResource(path); + return ModernUIAssist.inputStream2Response(inputStream, path); + } catch (Exception ignore) { + + } + return null; + } + }); // 初始化的时候,就把命名空间对象初始化好,确保window.a.b.c("a.b.c"为命名空间)对象都是初始化过的 browser.addScriptContextListener(new ScriptContextAdapter() { @Override public void onScriptContextCreated(ScriptContextEvent event) { - event.getBrowser().executeJavaScript(String.format(ModernUI.SCRIPT_STRING, namespace)); + event.getBrowser().executeJavaScript(String.format(ModernUIConstants.SCRIPT_STRING, namespace)); } }); } @@ -95,26 +136,65 @@ public class ModernUIPane extends BasicPane { private ModernUIPane pane = new ModernUIPane<>(); + public Builder prepare(ScriptContextListener contextListener) { + pane.browser.addScriptContextListener(contextListener); + return this; + } + + public Builder prepare(LoadListener loadListener) { + pane.browser.addLoadListener(loadListener); + return this; + } + + /** + * 加载jar包中的资源 + * @param path 资源路径 + */ + public Builder withEMB(String path) { + pane.browser.loadURL("emb:" + path); + return this; + } + + /** + * 加载url指向的资源 + * @param url 文件的地址 + */ public Builder withURL(String url) { pane.browser.loadURL(url); return this; } + /** + * 加载html文本内容 + * @param html 要加载html文本内容 + */ public Builder withHTML(String html) { pane.browser.loadHTML(html); return this; } + /** + * 设置该前端页面做数据交换所使用的对象 + * @param namespace 对象名 + */ public Builder namespace(String namespace) { pane.namespace = namespace; return this; } + /** + * java端往js端传数据时使用的变量名字 + * @param name 变量的名字 + */ public Builder variable(String name) { pane.variable = name; return this; } + /** + * js端往java端传数据时执行的函数表达式 + * @param expression 函数表达式 + */ public Builder expression(String expression) { pane.expression = expression; return this; diff --git a/designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java b/designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java index 1489cf226..79600a068 100644 --- a/designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java +++ b/designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java @@ -22,7 +22,7 @@ public class ModernUIPaneTest { // 是否需要开启调试窗口 DesignerEnvManager.getEnvManager().setOpenDebug(true); final ModernUIPane pane = new ModernUIPane.Builder() - .withHTML(IOUtils.readResourceAsString("/com/fr/design/ui/demo.html")).namespace("Pool").build(); + .withEMB("/com/fr/design/ui/demo.html").namespace("Pool").build(); contentPane.add(pane, BorderLayout.CENTER); Model model = new Model(); diff --git a/designer-base/src/test/resources/com/fr/design/ui/demo.html b/designer-base/src/test/resources/com/fr/design/ui/demo.html index 144c8661e..2beae25a7 100644 --- a/designer-base/src/test/resources/com/fr/design/ui/demo.html +++ b/designer-base/src/test/resources/com/fr/design/ui/demo.html @@ -15,5 +15,6 @@
测试页面,请点击最下面的按钮
+ \ No newline at end of file