Browse Source

完善一个示例代码

bugfix/10.0
richie 6 years ago
parent
commit
227b967ede
  1. 12
      designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java
  2. 8
      designer-base/src/test/java/com/fr/design/ui/ModernUIPaneTest.java
  3. 10
      designer-base/src/test/resources/com/fr/design/ui/demo.html

12
designer-base/src/main/java/com/fr/design/ui/ModernUIPane.java

@ -7,6 +7,8 @@ import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.JSValue; import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter; import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.events.ScriptContextAdapter;
import com.teamdev.jxbrowser.chromium.events.ScriptContextEvent;
import com.teamdev.jxbrowser.chromium.swing.BrowserView; import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*; import javax.swing.*;
@ -21,7 +23,7 @@ import java.awt.*;
public class ModernUIPane<T> extends BasicPane { public class ModernUIPane<T> extends BasicPane {
private Browser browser; private Browser browser;
private String namespace = "NS"; private String namespace = "Pool";
private String variable = "data"; private String variable = "data";
private String expression = "update()"; private String expression = "update()";
@ -54,6 +56,13 @@ public class ModernUIPane<T> extends BasicPane {
private void initializeBrowser() { private void initializeBrowser() {
browser = new Browser(); browser = new Browser();
// 初始化的时候,就把命名空间对象初始化好,确保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));
}
});
} }
@Override @Override
@ -67,7 +76,6 @@ public class ModernUIPane<T> extends BasicPane {
@Override @Override
public void onFinishLoadingFrame(FinishLoadingEvent event) { public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) { if (event.isMainFrame()) {
event.getBrowser().executeJavaScript(String.format(ModernUI.SCRIPT_STRING, namespace));
JSValue ns = event.getBrowser().executeJavaScriptAndReturnValue("window." + namespace); JSValue ns = event.getBrowser().executeJavaScriptAndReturnValue("window." + namespace);
ns.asObject().setProperty(variable, t); ns.asObject().setProperty(variable, t);
} }

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

@ -16,13 +16,13 @@ import java.awt.event.ActionListener;
public class ModernUIPaneTest { public class ModernUIPaneTest {
public static void main(String... args) { public static void main(String... args) {
JFrame frame = new JFrame(); final JFrame frame = new JFrame();
frame.setSize(1200, 800); frame.setSize(1200, 800);
JPanel contentPane = (JPanel) frame.getContentPane(); JPanel contentPane = (JPanel) frame.getContentPane();
// 是否需要开启调试窗口 // 是否需要开启调试窗口
DesignerEnvManager.getEnvManager().setOpenDebug(true); DesignerEnvManager.getEnvManager().setOpenDebug(true);
final ModernUIPane<Model> pane = new ModernUIPane.Builder<Model>() final ModernUIPane<Model> pane = new ModernUIPane.Builder<Model>()
.withHTML(IOUtils.readResourceAsString("/com/fr/design/ui/demo.html")).namespace("MyNS").build(); .withHTML(IOUtils.readResourceAsString("/com/fr/design/ui/demo.html")).namespace("Pool").build();
contentPane.add(pane, BorderLayout.CENTER); contentPane.add(pane, BorderLayout.CENTER);
Model model = new Model(); Model model = new Model();
@ -32,14 +32,14 @@ public class ModernUIPaneTest {
JPanel panel = new JPanel(new FlowLayout()); JPanel panel = new JPanel(new FlowLayout());
contentPane.add(panel, BorderLayout.SOUTH); contentPane.add(panel, BorderLayout.SOUTH);
JButton button = new JButton("点击我可以看到JAVA控制台输出的新值:99"); JButton button = new JButton("点击我可以看到Swing的弹框,输出填写的信息");
panel.add(button); panel.add(button);
button.addActionListener(new ActionListener() { button.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Model returnValue = pane.update(); Model returnValue = pane.update();
if (returnValue != null) { if (returnValue != null) {
System.out.println(returnValue.getAge()); JOptionPane.showMessageDialog(frame, String.format("姓名为:%s,年龄为:%d", returnValue.getName(), returnValue.getAge()));
} }
} }
}); });

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

@ -4,14 +4,16 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Title</title> <title>Title</title>
<script type="text/javascript"> <script type="text/javascript">
window.MyNS = window.MyNS || {}; Pool.update = function () {
MyNS.update = function () { Pool.data.setAge(parseInt(document.getElementById("age").value));
MyNS.data.setAge(111.11); Pool.data.setName(document.getElementById("name").value);
return MyNS.data; return Pool.data;
}; };
</script> </script>
</head> </head>
<body> <body>
<div>测试页面,请点击最下面的按钮</div> <div>测试页面,请点击最下面的按钮</div>
<input id="name" type="text" placeholder="请输入名字"/>
<input id="age" type="number" placeholder="请输入年龄"/>
</body> </body>
</html> </html>
Loading…
Cancel
Save