Browse Source

REPORT-112769 JxUIPane新增执行js方法

release/11.0
vito 5 months ago
parent
commit
743d96946e
  1. 21
      designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java
  2. 43
      designer-base/src/test/java/com/fr/design/jxbrowser/JxUIPaneTest.java

21
designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java

@ -206,12 +206,11 @@ public class JxUIPane<T> extends ModernUIPane<T> {
* 执行一段js
*
* @param javaScript 待执行的js脚本
* @see JxUIPane#executeJS(String)
*/
public void executeJavaScript(String javaScript) {
if (browser != null) {
browser.mainFrame().ifPresent(frame -> {
frame.executeJavaScript(javaScript);
});
browser.mainFrame().ifPresent(frame -> frame.executeJavaScript(javaScript));
}
}
@ -227,6 +226,22 @@ public class JxUIPane<T> extends ModernUIPane<T> {
return Optional.ofNullable(frame.executeJavaScript(name));
}
/**
* 执行js脚本并返回,使用范围包含{@link JxUIPane#executeJavaScript(String)},可以代替使用
*
* @param name 变量命名
* @return js对象
*/
public <P> Optional<P> executeJS(String name) {
if (browser != null) {
Optional<Frame> frame = browser.mainFrame();
if (frame.isPresent()) {
return Optional.ofNullable(frame.get().executeJavaScript(name));
}
}
return Optional.empty();
}
/**
* 由于自定义scheme目前走的是url因此路径会被自动转化比如windows路径下对冒号问题
* C:\\abc 变成 /C/abc这里对冒号进行编码转义

43
designer-base/src/test/java/com/fr/design/jxbrowser/JxUIPaneTest.java

@ -2,6 +2,7 @@ package com.fr.design.jxbrowser;
import com.fr.design.DesignerEnvManager;
import com.teamdev.jxbrowser.js.JsAccessible;
import com.teamdev.jxbrowser.js.JsObject;
import javax.swing.JButton;
import javax.swing.JFrame;
@ -10,6 +11,7 @@ import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Optional;
public class JxUIPaneTest {
@ -30,16 +32,49 @@ public class JxUIPaneTest {
JPanel panel = new JPanel(new FlowLayout());
contentPane.add(panel, BorderLayout.SOUTH);
testJxUIPaneUpdate(panel, pane);
testExecuteJS1(panel, pane);
testExecuteJS2(panel, pane);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private static void testJxUIPaneUpdate(JPanel container, JxUIPane<Model> pane) {
JButton button = new JButton("点击我可以看到Swing的弹框,输出填写的信息");
panel.add(button);
button.addActionListener(e -> {
Model returnValue = pane.update();
if (returnValue != null) {
JOptionPane.showMessageDialog(frame, String.format("姓名为:%s,年龄为:%d", returnValue.getName(), returnValue.getAge()));
JOptionPane.showMessageDialog(container.getRootPane(), String.format("姓名为:%s,年龄为:%d", returnValue.getName(), returnValue.getAge()));
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container.add(button);
}
private static void testExecuteJS1(JPanel container, JxUIPane<Model> pane) {
JButton button = new JButton("执行回显js对象内容");
button.addActionListener(e -> {
pane.executeJavaScript("var pagesize={\"page\":1,\"size\":10};");
Optional<JsObject> value = pane.executeJS("pagesize");
value.ifPresent(v -> JOptionPane.showMessageDialog(container.getRootPane(),
String.format("page为:%s,年龄为:%s", v.property("page").get(), v.property("size").get())));
});
container.add(button);
}
private static void testExecuteJS2(JPanel container, JxUIPane<Model> pane) {
JButton button = new JButton("执行回显数字内容");
button.addActionListener(e -> {
pane.executeJavaScript("var tom = {\"age\":18};");
Optional<Double> value = pane.executeJS("tom.age");
JOptionPane.showMessageDialog(container.getRootPane(),
String.format("var tom = {\"age\":18}; age:%s", value.get()));
});
container.add(button);
}
@JsAccessible

Loading…
Cancel
Save