You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
12 KiB
283 lines
12 KiB
package com.tptj.tool.hg.fineui.swing; |
|
|
|
import com.eclipsesource.v8.V8; |
|
import com.eclipsesource.v8.V8Array; |
|
import com.eclipsesource.v8.V8Object; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.tptj.tool.hg.engine.js.Debugger; |
|
import com.tptj.tool.hg.engine.js.JsEngineMaker; |
|
import com.tptj.tool.hg.engine.js.JsProcess; |
|
import com.tptj.tool.hg.fineui.swing.element.Element; |
|
import com.tptj.tool.hg.fineui.swing.element.JPanelElement; |
|
|
|
import javax.swing.*; |
|
import java.awt.event.WindowEvent; |
|
import java.awt.event.WindowListener; |
|
|
|
/** |
|
* @author 秃破天际 |
|
* @version 10.0 |
|
* Created by 秃破天际 on 2021/10/28 |
|
**/ |
|
public class FineUIEngine { |
|
|
|
public JsEngineMaker engine; |
|
|
|
private static volatile FineUIEngine instance = null; |
|
|
|
public static FineUIEngine getInstance() { |
|
if (null == instance) { |
|
synchronized (FineUIEngine.class) { |
|
if (null == instance) { |
|
instance = new FineUIEngine(); |
|
} |
|
} |
|
} |
|
return instance; |
|
} |
|
|
|
private FineUIEngine() { |
|
init(); |
|
} |
|
|
|
private void init() { |
|
try { |
|
//之间一次引入全部fineui,会因为各种环境参数方法的缺失而大量报错,因为调试难度很大, |
|
// 所以采用逐渐引入,逐渐调试的方式去逐渐覆盖, |
|
// 待全面适配成功后应该可以直接引入fineui.min.js了 |
|
engine = JsEngineMaker.newInstance(); |
|
JsProcess.create(engine.getEngine(), () -> { |
|
engine.importJs( |
|
//com/fr/fineui 目录下的都是fineui自身的源码 |
|
"com/fr/fineui/core/0.foundation.js", |
|
"com/fr/fineui/core/1.lodash.js", |
|
"com/fr/fineui/core/2.base.js", |
|
"com/fr/fineui/core/3.ob.js", |
|
"com/fr/fineui/core/4.widget.js", |
|
"com/fr/fineui/core/5.shortcut.js", |
|
"com/fr/fineui/core/6.inject.js", |
|
"com/fr/fineui/core/7.plugin.js", |
|
"com/fr/fineui/core/system.js", |
|
"com/fr/fineui/core/version.js", |
|
|
|
"com/fr/fineui/core/constant/date.i18n.js", |
|
"com/fr/fineui/core/constant/events.js", |
|
"com/fr/fineui/core/constant/var.js", |
|
|
|
"com/fr/fineui/core/func/alias.js", |
|
"com/fr/fineui/core/func/array.js", |
|
"com/fr/fineui/core/func/date.js", |
|
"com/fr/fineui/core/func/function.js", |
|
"com/fr/fineui/core/func/number.js", |
|
"com/fr/fineui/core/func/string.js", |
|
|
|
"com/fr/fineui/core/structure/aes.js", |
|
"com/fr/fineui/core/structure/aspect.js", |
|
"com/fr/fineui/core/structure/base64.js", |
|
"com/fr/fineui/core/structure/cache.js", |
|
"com/fr/fineui/core/structure/cellSizeAndPositionManager.js", |
|
"com/fr/fineui/core/structure/heap.js", |
|
"com/fr/fineui/core/structure/linkedHashMap.js", |
|
"com/fr/fineui/core/structure/lru.js", |
|
"com/fr/fineui/core/structure/prefixIntervalTree.js", |
|
"com/fr/fineui/core/structure/queue.js", |
|
"com/fr/fineui/core/structure/sectionManager.js", |
|
"com/fr/fineui/core/structure/tree.js", |
|
"com/fr/fineui/core/structure/vector.js", |
|
|
|
"com/fr/fineui/core/utils/chinesePY.js", |
|
"com/fr/fineui/core/utils/i18n.js", |
|
|
|
"com/fr/fineui/core/wrapper/layout.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.absolute.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.adaptive.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.border.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.default.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.flow.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.horizontal.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.tape.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.grid.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.window.js", |
|
"com/fr/fineui/core/wrapper/layout/layout.vertical.js", |
|
"com/fr/fineui/core/wrapper/layout/adapt/adapt.center.js", |
|
"com/fr/fineui/core/wrapper/layout/adapt/adapt.vertical.js", |
|
|
|
"com/fr/fineui/core/wrapper/layout/flex/flex.vertical.js", |
|
"com/fr/fineui/core/wrapper/layout/flex/flex.horizontal.js", |
|
"com/fr/fineui/core/wrapper/layout/flex/flex.center.js", |
|
"com/fr/fineui/core/wrapper/layout/flex/flex.horizontal.center.js", |
|
"com/fr/fineui/core/wrapper/layout/flex/flex.vertical.center.js", |
|
"com/fr/fineui/core/wrapper/layout/flex/flex.leftrightvertical.center.js", |
|
|
|
//com/tptj/tool/hg/fineui/swing 下的都是适配swing的js代码 |
|
// 自定义布局 |
|
"com/tptj/tool/hg/fineui/swing/core/wrapper/layout.static.js", |
|
"com/tptj/tool/hg/fineui/swing/core/wrapper/layout.horizontal.js", |
|
"com/tptj/tool/hg/fineui/swing/core/wrapper/layout.vertical.js", |
|
"com/tptj/tool/hg/fineui/swing/core/wrapper/layout.vertical_adapt.js", |
|
"com/tptj/tool/hg/fineui/swing/core/wrapper/layout.center_adapt.js", |
|
|
|
|
|
"com/tptj/tool/hg/fineui/swing/core/platform/swing/config.js", |
|
"com/tptj/tool/hg/fineui/swing/core/platform/swing/dom.js", |
|
"com/tptj/tool/hg/fineui/swing/core/platform/swing/function.js", |
|
|
|
|
|
"com/tptj/tool/hg/fineui/swing/core/element.js", |
|
"com/tptj/tool/hg/fineui/swing/core/extra.js", |
|
|
|
//自定义的基础控件,最基本的控件需要由swing实现后,映射到fineui中,高级的组件就可以 |
|
//由开发者按照fineui的组件继承方式进行组合创建新的组件了 |
|
"com/tptj/tool/hg/fineui/swing/base/single/label.js", |
|
"com/tptj/tool/hg/fineui/swing/widget/editor/editor.text.js" |
|
|
|
); |
|
return null; |
|
}).run(); |
|
engine.setDebugger(new Debugger() { |
|
@Override |
|
public void debug(Object... obj) { |
|
//用来打断点查看JS的属性和变量的,因为V8调试是很麻烦的,所以一般开发者可以简单的 |
|
//利用这个入口对调试的变量进行查看 |
|
//console.debug(变量1,变量2,变量3,....) |
|
int a = 1; |
|
} |
|
}); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e, "FineUI swing引擎初始化失败!{}", e.getMessage()); |
|
} |
|
} |
|
|
|
/** |
|
* 设置本次渲染的根节点 |
|
* |
|
* @param element |
|
*/ |
|
public FineUIEngine body(Element element) { |
|
engine.set("body", element); |
|
return this; |
|
} |
|
|
|
public FineUIEngine importJs(String... path) { |
|
engine.importJs(path); |
|
return this; |
|
} |
|
|
|
public void close() { |
|
engine.close(); |
|
} |
|
|
|
public static Object trans(V8 v8, Object result) { |
|
return JsEngineMaker.trans(v8, result); |
|
} |
|
|
|
public static void close(Object value) { |
|
JsEngineMaker.close(value); |
|
} |
|
|
|
public static Object call(V8Object js, String method, Object... args) throws Exception { |
|
//为了保证跨线程调用成功,在java中调用JS对象的方法时, |
|
// 需要使用JsProcess.create(V8Object,Action)的方式 |
|
return JsProcess.create(js, () -> { |
|
return executeFunction(js, method, args); |
|
}).run(); |
|
} |
|
|
|
public static Object executeFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeFunction(js, method, args); |
|
} |
|
|
|
public static V8Array executeArrayFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeArrayFunction(js, method, args); |
|
} |
|
|
|
public static V8Object executeObjectFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeObjectFunction(js, method, args); |
|
} |
|
|
|
public static String executeStringFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeStringFunction(js, method, args); |
|
} |
|
|
|
public static boolean executeBooleanFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeBooleanFunction(js, method, args); |
|
} |
|
|
|
public static double executeDoubleFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeDoubleFunction(js, method, args); |
|
} |
|
|
|
public static int executeIntegerFunction(V8Object js, String method, Object... args) { |
|
return JsEngineMaker.executeIntegerFunction(js, method, args); |
|
} |
|
|
|
public static void executeVoidFunction(V8Object js, String method, Object... args) { |
|
JsEngineMaker.executeVoidFunction(js, method, args); |
|
} |
|
|
|
public static void main(String[] args) throws Exception { |
|
//最终UI整个结构就在el里面~再用java把el渲染出来即可 |
|
//目前利用这个框架简单的实现了 绝对布局 边界(圣杯)布局、水平带布局、垂直带布局、网格布局、窗口布局 几个经典的fineui静态布局 |
|
//设计器插件本身其实也基本都是静态布局即可满足需要 |
|
JPanelElement el = new JPanelElement(); |
|
FineUIEngine engine = FineUIEngine.getInstance(); |
|
JsProcess.create(engine.engine.getEngine(), () -> { |
|
engine.body(el) |
|
//demo下面的就是实际使用时的代码了,该框架包开发完毕,使用者要写的就是按照demo目录下那些代码了 |
|
// .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.border.js"); |
|
// .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.tape.js"); |
|
// .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.grid.js"); |
|
// .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.window.js"); |
|
.importJs("com/tptj/tool/hg/fineui/swing/demo/layout.horizontal.js"); |
|
return null; |
|
}).run(); |
|
|
|
|
|
JFrame frame = new JFrame("Fine UI Demo"); |
|
frame.setSize(800, 600); |
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
|
frame.getContentPane().add(el.getWrapper()); |
|
frame.addWindowListener(new WindowListener() { |
|
@Override |
|
public void windowOpened(WindowEvent e) { |
|
|
|
} |
|
|
|
@Override |
|
public void windowClosing(WindowEvent e) { |
|
|
|
} |
|
|
|
@Override |
|
public void windowClosed(WindowEvent e) { |
|
//hugh:使用的是V8的引擎,这个引擎要求必须是主动释放资源,否则会内存泄露 |
|
//所以在使用完毕后一定要记得关闭 |
|
engine.close(); |
|
} |
|
|
|
@Override |
|
public void windowIconified(WindowEvent e) { |
|
|
|
} |
|
|
|
@Override |
|
public void windowDeiconified(WindowEvent e) { |
|
|
|
} |
|
|
|
@Override |
|
public void windowActivated(WindowEvent e) { |
|
|
|
} |
|
|
|
@Override |
|
public void windowDeactivated(WindowEvent e) { |
|
|
|
} |
|
}); |
|
// 显示窗口 |
|
frame.setVisible(true); |
|
System.out.println(el); |
|
} |
|
}
|
|
|