diff --git a/designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java b/designer-base/src/main/java/com/fr/design/ui/Assistant.java similarity index 59% rename from designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java rename to designer-base/src/main/java/com/fr/design/ui/Assistant.java index 3475b20cbb..601f0ac36f 100644 --- a/designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java +++ b/designer-base/src/main/java/com/fr/design/ui/Assistant.java @@ -1,5 +1,9 @@ package com.fr.design.ui; +import com.fr.stable.StringUtils; +import com.teamdev.jxbrowser.chromium.Browser; +import com.teamdev.jxbrowser.chromium.BrowserContext; +import com.teamdev.jxbrowser.chromium.ProtocolService; import com.teamdev.jxbrowser.chromium.URLResponse; import java.io.DataInputStream; @@ -10,7 +14,7 @@ import java.io.InputStream; * @version 10.0 * Created by richie on 2019-03-07 */ -public class ModernUIAssist { +public class Assistant { public static URLResponse inputStream2Response(InputStream inputStream, String filePath) throws Exception { URLResponse response = new URLResponse(); @@ -25,6 +29,9 @@ public class ModernUIAssist { private static String getMimeType(String path) { + if (StringUtils.isBlank(path)) { + return "text/html"; + } if (path.endsWith(".html")) { return "text/html"; } @@ -36,4 +43,11 @@ public class ModernUIAssist { } return "text/html"; } + + public static void setEmbProtocolHandler(Browser browser, EmbProtocolHandler handler) { + BrowserContext browserContext = browser.getContext(); + ProtocolService protocolService = browserContext.getProtocolService(); + // 支持读取jar包中文件的自定义协议————emb:/com/fr/design/images/bbs.png + protocolService.setProtocolHandler("emb", handler); + } } diff --git a/designer-base/src/main/java/com/fr/design/ui/EmbProtocolHandler.java b/designer-base/src/main/java/com/fr/design/ui/EmbProtocolHandler.java new file mode 100644 index 0000000000..6636e28744 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/ui/EmbProtocolHandler.java @@ -0,0 +1,83 @@ +package com.fr.design.ui; + +import com.fr.general.IOUtils; +import com.fr.stable.StringUtils; +import com.fr.web.struct.AssembleComponent; +import com.fr.web.struct.AtomBuilder; +import com.fr.web.struct.PathGroup; +import com.fr.web.struct.category.ScriptPath; +import com.fr.web.struct.category.StylePath; +import com.teamdev.jxbrowser.chromium.ProtocolHandler; +import com.teamdev.jxbrowser.chromium.URLRequest; +import com.teamdev.jxbrowser.chromium.URLResponse; + +import java.io.InputStream; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-07 + */ +public class EmbProtocolHandler implements ProtocolHandler { + + private AssembleComponent component; + + public EmbProtocolHandler() { + + } + + public EmbProtocolHandler(AssembleComponent component) { + this.component = component; + } + + + @Override + public URLResponse onRequest(URLRequest req) { + try { + String path = req.getURL(); + if (path.startsWith("emb:dynamic")) { + URLResponse response = new URLResponse(); + response.setData(htmlText().getBytes()); + response.getHeaders().setHeader("Content-Type", "text/html"); + return response; + } else { + int index = path.indexOf("="); + if (index > 0) { + path = path.substring(index + 1); + } else { + path = path.substring(4); + } + InputStream inputStream = IOUtils.readResource(path); + return Assistant.inputStream2Response(inputStream, path); + } + } catch (Exception ignore) { + + } + return null; + } + + private String htmlText() { + PathGroup pathGroup = AtomBuilder.create().buildAssembleFilePath(ModernRequestClient.KEY, component); + StylePath[] stylePaths = pathGroup.toStylePathGroup(); + StringBuilder styleText = new StringBuilder(); + for (StylePath path : stylePaths) { + if (StringUtils.isNotBlank(path.toFilePath())) { + styleText.append(""); + } + } + String result = ModernUIConstants.HTML_TPL.replaceAll("##style##", styleText.toString()); + ScriptPath[] scriptPaths = pathGroup.toScriptPathGroup(); + StringBuilder scriptText = new StringBuilder(); + for (ScriptPath path : scriptPaths) { + if (StringUtils.isNotBlank(path.toFilePath())) { + scriptText.append(""); + } + } + result = result.replaceAll("##script##", scriptText.toString()); + return result; + } +} diff --git a/designer-base/src/main/java/com/fr/design/ui/ModernRequestClient.java b/designer-base/src/main/java/com/fr/design/ui/ModernRequestClient.java new file mode 100644 index 0000000000..8a19c84c84 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/ui/ModernRequestClient.java @@ -0,0 +1,22 @@ +package com.fr.design.ui; + +import com.fr.web.struct.browser.RequestClient; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-07 + */ +public enum ModernRequestClient implements RequestClient { + + KEY; + + @Override + public boolean isIE() { + return false; + } + + @Override + public boolean isLowIEVersion() { + return false; + }} diff --git a/designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java b/designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java index f17244cb72..ed6fb542e9 100644 --- a/designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java +++ b/designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java @@ -1,5 +1,7 @@ package com.fr.design.ui; +import com.fr.general.IOUtils; + /** * @author richie * @version 10.0 @@ -7,12 +9,7 @@ package com.fr.design.ui; */ class ModernUIConstants { - static final String SCRIPT_STRING = "var arr = \"%s\".split(\".\").reverse();\n" + - "var create = function(obj, names) {\n" + - "var name = names.pop();\n" + - "if (!name) {return;}\n" + - "if (!obj[name]) {obj[name] = {};}\n" + - " create(obj[name], names);\n" + - "}\n" + - "create(window, arr);"; + static final String SCRIPT_INIT_NAME_SPACE = IOUtils.readResourceAsString("/com/fr/design/ui/InitNameSpace.js"); + + static final String HTML_TPL = IOUtils.readResourceAsString("/com/fr/design/ui/tpl.html"); } 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 b3c72c9ac2..962e9cc637 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,15 +2,10 @@ package com.fr.design.ui; import com.fr.design.DesignerEnvManager; import com.fr.design.dialog.BasicPane; -import com.fr.general.IOUtils; +import com.fr.web.struct.AssembleComponent; 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; @@ -21,8 +16,6 @@ import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; -import java.io.InputStream; -import java.net.URL; /** * @author richie @@ -66,42 +59,11 @@ 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(ModernUIConstants.SCRIPT_STRING, namespace)); + event.getBrowser().executeJavaScript(String.format(ModernUIConstants.SCRIPT_INIT_NAME_SPACE, namespace)); } }); } @@ -150,7 +112,8 @@ public class ModernUIPane extends BasicPane { * 加载jar包中的资源 * @param path 资源路径 */ - public Builder withEMB(String path) { + public Builder withEMB(final String path) { + Assistant.setEmbProtocolHandler(pane.browser, new EmbProtocolHandler()); pane.browser.loadURL("emb:" + path); return this; } @@ -159,16 +122,28 @@ public class ModernUIPane extends BasicPane { * 加载url指向的资源 * @param url 文件的地址 */ - public Builder withURL(String url) { + public Builder withURL(final String url) { + Assistant.setEmbProtocolHandler(pane.browser, new EmbProtocolHandler()); pane.browser.loadURL(url); return this; } + /** + * 加载Atom组件 + * @param component Atom组件 + */ + public Builder withComponent(AssembleComponent component) { + Assistant.setEmbProtocolHandler(pane.browser, new EmbProtocolHandler(component)); + pane.browser.loadURL("emb:dynamic"); + return this; + } + /** * 加载html文本内容 * @param html 要加载html文本内容 */ public Builder withHTML(String html) { + Assistant.setEmbProtocolHandler(pane.browser, new EmbProtocolHandler()); pane.browser.loadHTML(html); return this; } diff --git a/designer-base/src/main/resources/com/fr/design/ui/InitNameSpace.js b/designer-base/src/main/resources/com/fr/design/ui/InitNameSpace.js new file mode 100644 index 0000000000..0274d5fa50 --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/ui/InitNameSpace.js @@ -0,0 +1,12 @@ +var arr ="%s".split(".").reverse(); +var create = function (obj, names) { + var name = names.pop(); + if (!name) { + return; + } + if (!obj[name]) { + obj[name] = {}; + } + create(obj[name], names); +}; +create(window, arr); \ No newline at end of file diff --git a/designer-base/src/main/resources/com/fr/design/ui/InsertScript.js b/designer-base/src/main/resources/com/fr/design/ui/InsertScript.js new file mode 100644 index 0000000000..8ad60313ee --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/ui/InsertScript.js @@ -0,0 +1,8 @@ +var arr = "%s".split(","); +var header = document.getElementsByTagName("head")[0]; +arr.forEach(function(el) { + var script = document.createElement("script") + script.type = "text/javascript"; + script.src = "emb:" + el; + header.appendChild(script); +}); \ No newline at end of file diff --git a/designer-base/src/main/resources/com/fr/design/ui/InsertStyle.js b/designer-base/src/main/resources/com/fr/design/ui/InsertStyle.js new file mode 100644 index 0000000000..18ccb7e410 --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/ui/InsertStyle.js @@ -0,0 +1,9 @@ +var arr = "%s".split(","); +var header = document.getElementsByTagName("head")[0]; +arr.forEach(function(el) { + var css = document.createElement("link"); + css.type = "text/css"; + css.rel = "stylesheet"; + css.href = "emb:" + el; + header.appendChild(css); +}); \ No newline at end of file diff --git a/designer-base/src/main/resources/com/fr/design/ui/tpl.html b/designer-base/src/main/resources/com/fr/design/ui/tpl.html new file mode 100644 index 0000000000..92df75539c --- /dev/null +++ b/designer-base/src/main/resources/com/fr/design/ui/tpl.html @@ -0,0 +1,12 @@ + + + + + Title + ##style## + ##script## + + + + + \ No newline at end of file diff --git a/designer-base/src/test/java/com/fr/design/ui/FineUIDemo.java b/designer-base/src/test/java/com/fr/design/ui/FineUIDemo.java new file mode 100644 index 0000000000..afd0a5d1d9 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/ui/FineUIDemo.java @@ -0,0 +1,28 @@ +package com.fr.design.ui; + +import com.fr.design.DesignerEnvManager; + +import javax.swing.*; +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-07 + */ +public class FineUIDemo { + + public static void main(String... args) { + final JFrame frame = new JFrame(); + frame.setSize(1200, 800); + JPanel contentPane = (JPanel) frame.getContentPane(); + // 是否需要开启调试窗口 + DesignerEnvManager.getEnvManager().setOpenDebug(true); + + final ModernUIPane pane = new ModernUIPane.Builder() + .withComponent(StartComponent.KEY).namespace("Pool").build(); + contentPane.add(pane, BorderLayout.CENTER); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} 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 79600a0682..9676ee998a 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 @@ -1,7 +1,6 @@ package com.fr.design.ui; import com.fr.design.DesignerEnvManager; -import com.fr.general.IOUtils; import javax.swing.*; import java.awt.*; diff --git a/designer-base/src/test/java/com/fr/design/ui/StartComponent.java b/designer-base/src/test/java/com/fr/design/ui/StartComponent.java new file mode 100644 index 0000000000..7811142f0b --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/ui/StartComponent.java @@ -0,0 +1,32 @@ +package com.fr.design.ui; + +import com.fr.web.struct.AssembleComponent; +import com.fr.web.struct.Atom; +import com.fr.web.struct.browser.RequestClient; +import com.fr.web.struct.category.ScriptPath; +import com.fr.web.struct.impl.FineUI; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-08 + */ +public class StartComponent extends AssembleComponent { + + public static final StartComponent KEY = new StartComponent(); + + private StartComponent() { + + } + + @Override + public ScriptPath script(RequestClient req) { + return ScriptPath.build("/com/fr/design/ui/script/start.js"); + } + + @Override + public Atom[] refer() { + return new Atom[] {FineUI.KEY}; + } + +} diff --git a/designer-base/src/test/resources/com/fr/design/ui/fineui.html b/designer-base/src/test/resources/com/fr/design/ui/fineui.html new file mode 100644 index 0000000000..a02004a358 --- /dev/null +++ b/designer-base/src/test/resources/com/fr/design/ui/fineui.html @@ -0,0 +1,11 @@ + + + + + Title + +