Browse Source

Merge pull request #790 in DESIGN/design from ~RICHIE/design:release/10.0 to release/10.0

* commit '692d7d82c48d2cedb663e5d187f848b67029eaa3':
  误提交了测试代码
  无JIRA任务 完善提供的web容器API,简化加载jar包中html/js/css的方式
bugfix/10.0
richie 6 years ago
parent
commit
f367577b6b
  1. 1
      designer-base/src/main/java/com/fr/design/DesignerEnvManager.java
  2. 39
      designer-base/src/main/java/com/fr/design/ui/ModernUIAssist.java
  3. 2
      designer-base/src/main/java/com/fr/design/ui/ModernUIConstants.java
  4. 82
      designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java
  5. 2
      designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java
  6. 1
      designer-base/src/test/resources/com/fr/design/ui/demo.html

1
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();
}

39
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";
}
}

2
designer-base/src/main/java/com/fr/design/ui/ModernUI.java → 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" +

82
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<T> 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<T> extends BasicPane {
private ModernUIPane<T> pane = new ModernUIPane<>();
public Builder<T> prepare(ScriptContextListener contextListener) {
pane.browser.addScriptContextListener(contextListener);
return this;
}
public Builder<T> prepare(LoadListener loadListener) {
pane.browser.addLoadListener(loadListener);
return this;
}
/**
* 加载jar包中的资源
* @param path 资源路径
*/
public Builder<T> withEMB(String path) {
pane.browser.loadURL("emb:" + path);
return this;
}
/**
* 加载url指向的资源
* @param url 文件的地址
*/
public Builder<T> withURL(String url) {
pane.browser.loadURL(url);
return this;
}
/**
* 加载html文本内容
* @param html 要加载html文本内容
*/
public Builder<T> withHTML(String html) {
pane.browser.loadHTML(html);
return this;
}
/**
* 设置该前端页面做数据交换所使用的对象
* @param namespace 对象名
*/
public Builder<T> namespace(String namespace) {
pane.namespace = namespace;
return this;
}
/**
* java端往js端传数据时使用的变量名字
* @param name 变量的名字
*/
public Builder<T> variable(String name) {
pane.variable = name;
return this;
}
/**
* js端往java端传数据时执行的函数表达式
* @param expression 函数表达式
*/
public Builder<T> expression(String expression) {
pane.expression = expression;
return this;

2
designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java

@ -22,7 +22,7 @@ public class ModernUIPaneTest {
// 是否需要开启调试窗口
DesignerEnvManager.getEnvManager().setOpenDebug(true);
final ModernUIPane<Model> pane = new ModernUIPane.Builder<Model>()
.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();

1
designer-base/src/test/resources/com/fr/design/ui/demo.html

@ -15,5 +15,6 @@
<div>测试页面,请点击最下面的按钮</div>
<input id="name" type="text" placeholder="请输入名字"/>
<input id="age" type="number" placeholder="请输入年龄"/>
<img src="emb:/com/fr/design/images/splash_10.gif">
</body>
</html>
Loading…
Cancel
Save