|
1 | 1 | package com.xwintop.xJavaFxTool.plugin; |
2 | 2 |
|
3 | 3 | import com.xwintop.xJavaFxTool.model.PluginJarInfo; |
4 | | -import com.xwintop.xJavaFxTool.utils.Config; |
5 | 4 | import com.xwintop.xcore.javafx.dialog.FxAlerts; |
6 | 5 | import com.xwintop.xcore.util.javafx.JavaFxViewUtil; |
7 | 6 | import javafx.fxml.FXMLLoader; |
|
10 | 9 | import javafx.scene.control.TabPane; |
11 | 10 | import javafx.scene.image.Image; |
12 | 11 | import javafx.scene.image.ImageView; |
| 12 | +import javafx.scene.layout.BorderPane; |
13 | 13 | import javafx.scene.web.WebEngine; |
14 | 14 | import javafx.scene.web.WebView; |
15 | 15 | import lombok.extern.slf4j.Slf4j; |
16 | 16 | import org.apache.commons.lang3.StringUtils; |
17 | 17 |
|
18 | 18 | import java.lang.ref.WeakReference; |
19 | | -import java.net.URL; |
20 | | -import java.util.ResourceBundle; |
21 | 19 |
|
22 | 20 | @Slf4j |
23 | 21 | public class PluginLoader { |
24 | | - |
25 | | - /** |
26 | | - * 以新窗口方式打开插件 |
27 | | - */ |
28 | | - public static void loadPluginAsWindow(PluginJarInfo plugin) { |
29 | | - try { |
30 | | - FXMLLoader generatingCodeFXMLLoader = new FXMLLoader(PluginLoader.class.getResource(plugin.getFxmlPath())); |
31 | | - |
32 | | - if (StringUtils.isNotEmpty(plugin.getBundleName())) { |
33 | | - ResourceBundle resourceBundle = ResourceBundle.getBundle(plugin.getBundleName(), Config.defaultLocale); |
34 | | - generatingCodeFXMLLoader.setResources(resourceBundle); |
35 | | - } |
36 | | - |
37 | | - JavaFxViewUtil.getNewStage(plugin.getTitle(), plugin.getIconPath(), generatingCodeFXMLLoader); |
38 | | - } catch (Exception e) { |
39 | | - log.error("加载插件失败", e); |
40 | | - } |
41 | | - } |
42 | | - |
43 | 22 | /** |
44 | | - * 以新 Tab 方式打开插件 |
| 23 | + * 以新 Tab 方式打开插件,但使用独立的 ClassLoader |
45 | 24 | */ |
46 | | - public static Tab loadPluginAsTab(PluginJarInfo plugin, TabPane tabPane) { |
| 25 | + public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane, boolean singleWindowBoot) { |
47 | 26 | try { |
48 | | - URL resource = PluginLoader.class.getResource(plugin.getFxmlPath()); |
49 | | - if (resource == null) { |
50 | | - FxAlerts.error("加载插件失败", "无法读取资源文件 '" + plugin.getFxmlPath() + "'"); |
| 27 | + PluginContainer pluginContainer = new PluginContainer(PluginLoader.class.getClassLoader(), plugin); |
| 28 | + WeakReference<PluginContainer> containerRef = new WeakReference<>(pluginContainer); |
| 29 | + FXMLLoader generatingCodeFXMLLoader = pluginContainer.createFXMLLoader(); |
| 30 | + if (generatingCodeFXMLLoader == null) { |
51 | 31 | return null; |
52 | 32 | } |
53 | 33 |
|
54 | | - FXMLLoader generatingCodeFXMLLoader = new FXMLLoader(resource); |
55 | | - |
56 | | - if (StringUtils.isNotEmpty(plugin.getBundleName())) { |
57 | | - ResourceBundle resourceBundle = ResourceBundle.getBundle(plugin.getBundleName(), Config.defaultLocale); |
58 | | - generatingCodeFXMLLoader.setResources(resourceBundle); |
59 | | - } |
60 | | - |
61 | | - Tab tab = new Tab(plugin.getTitle()); |
62 | | - |
63 | | - if (StringUtils.isNotEmpty(plugin.getIconPath())) { |
64 | | - ImageView imageView = new ImageView(new Image(plugin.getIconPath())); |
65 | | - imageView.setFitHeight(18); |
66 | | - imageView.setFitWidth(18); |
67 | | - tab.setGraphic(imageView); |
| 34 | + if (singleWindowBoot) { |
| 35 | + JavaFxViewUtil.getNewStage(plugin.getTitle(), plugin.getIconPath(), generatingCodeFXMLLoader); |
| 36 | + return null; |
68 | 37 | } |
69 | 38 |
|
70 | | - tab.setContent(generatingCodeFXMLLoader.load()); |
71 | | - tabPane.getTabs().add(tab); |
72 | | - tabPane.getSelectionModel().select(tab); |
73 | | - |
74 | | - tab.setOnCloseRequest( |
75 | | - event -> JavaFxViewUtil.setControllerOnCloseRequest(generatingCodeFXMLLoader.getController(), event) |
76 | | - ); |
77 | | - |
78 | | - return tab; |
79 | | - } catch (Exception e) { |
80 | | - log.error("加载插件失败", e); |
81 | | - } |
82 | | - |
83 | | - return null; |
84 | | - } |
85 | | - |
86 | | - /** |
87 | | - * 以新 Tab 方式打开插件,但使用独立的 ClassLoader |
88 | | - */ |
89 | | - public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane) { |
90 | | - try { |
91 | 39 | Tab tab = new Tab(plugin.getTitle()); |
92 | | - |
93 | 40 | if (StringUtils.isNotEmpty(plugin.getIconPath())) { |
94 | 41 | ImageView imageView = new ImageView(new Image(plugin.getIconPath())); |
95 | 42 | imageView.setFitHeight(18); |
96 | 43 | imageView.setFitWidth(18); |
97 | 44 | tab.setGraphic(imageView); |
98 | 45 | } |
99 | 46 |
|
100 | | - PluginContainer pluginContainer = new PluginContainer(PluginLoader.class.getClassLoader(), plugin); |
101 | | - WeakReference<PluginContainer> containerRef = new WeakReference<>(pluginContainer); |
102 | | - FXMLLoader generatingCodeFXMLLoader = pluginContainer.createFXMLLoader(); |
103 | | - if (generatingCodeFXMLLoader == null) { |
104 | | - return null; |
105 | | - } |
106 | | - |
107 | 47 | Node root = generatingCodeFXMLLoader.load(); |
108 | 48 | Object controller = generatingCodeFXMLLoader.getController(); |
109 | 49 | WeakReference<Object> controllerRef = new WeakReference<>(controller); |
@@ -136,20 +76,30 @@ public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane) |
136 | 76 | return null; |
137 | 77 | } |
138 | 78 |
|
139 | | - public static Tab loadWebViewAsTab(PluginJarInfo plugin, TabPane tabPane) { |
140 | | - PluginContainer pluginContainer = new PluginContainer(plugin); |
141 | | - WebView browser = pluginContainer.createInstance(WebView.class); |
| 79 | + public static Tab loadWebViewAsTab(PluginJarInfo plugin, TabPane tabPane, boolean singleWindowBoot) { |
| 80 | + WebView browser = new WebView(); |
142 | 81 | WebEngine webEngine = browser.getEngine(); |
143 | 82 | String url = plugin.getPagePath(); |
144 | 83 | String title = plugin.getTitle(); |
145 | 84 |
|
146 | 85 | if (url.startsWith("http")) { |
147 | 86 | webEngine.load(url); |
148 | 87 | } else { |
| 88 | + PluginContainer pluginContainer = new PluginContainer(plugin); |
149 | 89 | webEngine.load(pluginContainer.getResource(url).toExternalForm()); |
150 | 90 | } |
151 | 91 |
|
| 92 | + if (singleWindowBoot) { |
| 93 | + JavaFxViewUtil.getNewStage(title, plugin.getIconPath(), new BorderPane(browser)); |
| 94 | + return null; |
| 95 | + } |
152 | 96 | Tab tab = new Tab(title); |
| 97 | + if (StringUtils.isNotEmpty(plugin.getIconPath())) { |
| 98 | + ImageView imageView = new ImageView(new Image(plugin.getIconPath())); |
| 99 | + imageView.setFitHeight(18); |
| 100 | + imageView.setFitWidth(18); |
| 101 | + tab.setGraphic(imageView); |
| 102 | + } |
153 | 103 | tab.setContent(browser); |
154 | 104 | tabPane.getTabs().add(tab); |
155 | 105 | tabPane.getSelectionModel().select(tab); |
|
0 commit comments