Skip to content

Commit 6e73b14

Browse files
committed
1.整合插件打开方式;2.添加插件是否单例模式打开开关;3.修改打开日志功能路径
1 parent 649e0af commit 6e73b14

9 files changed

Lines changed: 53 additions & 171 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/controller/IndexController.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public class IndexController extends IndexView {
6060

6161
private IndexService indexService = new IndexService(this);
6262

63-
private ContextMenu contextMenu = new ContextMenu();
64-
6563
// 实现搜索用
6664
private List<PluginItemController> pluginItemControllers = new ArrayList<>();
6765

@@ -191,11 +189,6 @@ private void addMenu(PluginJarInfo jarInfo) {
191189
}
192190

193191
public void selectAction(String selectText) {
194-
// if (contextMenu.isShowing()) {
195-
// contextMenu.hide();
196-
// }
197-
// contextMenu = indexService.getSelectContextMenu(selectText);
198-
// contextMenu.show(myTextField, null, 0, myTextField.getHeight());
199192
boolean notSearching = StringUtils.isBlank(selectText);
200193
pluginItemControllers.forEach(itemController -> {
201194
itemController.setVisible(notSearching || itemController.matchKeyword(selectText));
@@ -255,7 +248,7 @@ private void setLanguageAction(ActionEvent event) throws Exception {
255248

256249
@FXML
257250
private void openLogFileAction() {
258-
String filePath = "logs/logFile." + DateFormatUtils.format(new Date(), "yyyy-MM-dd") + ".log";
251+
String filePath = "logs/logFile.log";
259252
JavaFxSystemUtil.openDirectory(filePath);
260253
}
261254

src/main/java/com/xwintop/xJavaFxTool/controller/index/PluginManageController.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
@Setter
4545
@Slf4j
4646
public class PluginManageController extends PluginManageView {
47-
4847
public static final String FXML = "/com/xwintop/xJavaFxTool/fxmlView/index/PluginManage.fxml";
4948

5049
private PluginManageService pluginManageService = new PluginManageService(this);
@@ -118,10 +117,7 @@ private void downloadPlugin(Map<String, String> dataRow, Button downloadButton)
118117
}
119118
}
120119

121-
private void afterDownload(
122-
Map<String, String> dataRow, Button downloadButton, PluginJarInfo pluginJarInfo
123-
) {
124-
120+
private void afterDownload(Map<String, String> dataRow, Button downloadButton, PluginJarInfo pluginJarInfo) {
125121
// 没有下载成功不做处理
126122
if (pluginJarInfo.getIsDownload() == null || !pluginJarInfo.getIsDownload()) {
127123
return;
@@ -149,7 +145,6 @@ private void afterDownload(
149145
}
150146

151147
private void initEvent() {
152-
153148
// 右键菜单
154149
MenuItem mnuSavePluginConfig = new MenuItem("保存配置");
155150
mnuSavePluginConfig.setOnAction(ev -> {
@@ -160,13 +155,10 @@ private void initEvent() {
160155
log.error("保存插件配置失败", ex);
161156
}
162157
});
163-
164158
ContextMenu contextMenu = new ContextMenu(mnuSavePluginConfig);
165159
pluginDataTableView.setContextMenu(contextMenu);
166-
167160
// 搜索
168-
selectPluginTextField.textProperty()
169-
.addListener((_ob, _old, _new) -> pluginManageService.searchPlugin(_new));
161+
selectPluginTextField.textProperty().addListener((_ob, _old, _new) -> pluginManageService.searchPlugin(_new));
170162
}
171163

172164
private void initService() {

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginContainer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,20 @@ public URL getResource(String path) {
5252
*/
5353
public FXMLLoader createFXMLLoader() {
5454
try {
55-
FXMLLoader pluginFxmlLoader = (FXMLLoader)
56-
pluginClassLoader.loadClass("javafx.fxml.FXMLLoader").getDeclaredConstructor().newInstance();
55+
FXMLLoader pluginFxmlLoader = (FXMLLoader) pluginClassLoader.loadClass("javafx.fxml.FXMLLoader").getDeclaredConstructor().newInstance();
5756

5857
pluginFxmlLoader.setClassLoader(pluginClassLoader);
5958

6059
URL resource = getResource(pluginJarInfo.getFxmlPath());
6160
if (resource == null) {
62-
FxAlerts.error("加载失败", "无法在插件 " + pluginJarInfo.getFile().getName() +
63-
" 内找到所需资源 " + pluginJarInfo.getFxmlPath());
61+
FxAlerts.error("加载失败", "无法在插件 " + pluginJarInfo.getFile().getName() + " 内找到所需资源 " + pluginJarInfo.getFxmlPath());
6462
return null;
6563
}
6664

6765
pluginFxmlLoader.setLocation(resource);
6866

6967
if (StringUtils.isNotEmpty(pluginJarInfo.getBundleName())) {
70-
ResourceBundle resourceBundle = ResourceBundle
71-
.getBundle(pluginJarInfo.getBundleName(), Config.defaultLocale, pluginClassLoader);
68+
ResourceBundle resourceBundle = ResourceBundle.getBundle(pluginJarInfo.getBundleName(), Config.defaultLocale, pluginClassLoader);
7269
pluginFxmlLoader.setResources(resourceBundle);
7370
}
7471
return pluginFxmlLoader;

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginLoader.java

Lines changed: 23 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.xwintop.xJavaFxTool.plugin;
22

33
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
4-
import com.xwintop.xJavaFxTool.utils.Config;
54
import com.xwintop.xcore.javafx.dialog.FxAlerts;
65
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
76
import javafx.fxml.FXMLLoader;
@@ -10,100 +9,41 @@
109
import javafx.scene.control.TabPane;
1110
import javafx.scene.image.Image;
1211
import javafx.scene.image.ImageView;
12+
import javafx.scene.layout.BorderPane;
1313
import javafx.scene.web.WebEngine;
1414
import javafx.scene.web.WebView;
1515
import lombok.extern.slf4j.Slf4j;
1616
import org.apache.commons.lang3.StringUtils;
1717

1818
import java.lang.ref.WeakReference;
19-
import java.net.URL;
20-
import java.util.ResourceBundle;
2119

2220
@Slf4j
2321
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-
4322
/**
44-
* 以新 Tab 方式打开插件
23+
* 以新 Tab 方式打开插件,但使用独立的 ClassLoader
4524
*/
46-
public static Tab loadPluginAsTab(PluginJarInfo plugin, TabPane tabPane) {
25+
public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane, boolean singleWindowBoot) {
4726
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) {
5131
return null;
5232
}
5333

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;
6837
}
6938

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 {
9139
Tab tab = new Tab(plugin.getTitle());
92-
9340
if (StringUtils.isNotEmpty(plugin.getIconPath())) {
9441
ImageView imageView = new ImageView(new Image(plugin.getIconPath()));
9542
imageView.setFitHeight(18);
9643
imageView.setFitWidth(18);
9744
tab.setGraphic(imageView);
9845
}
9946

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-
10747
Node root = generatingCodeFXMLLoader.load();
10848
Object controller = generatingCodeFXMLLoader.getController();
10949
WeakReference<Object> controllerRef = new WeakReference<>(controller);
@@ -136,20 +76,30 @@ public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane)
13676
return null;
13777
}
13878

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();
14281
WebEngine webEngine = browser.getEngine();
14382
String url = plugin.getPagePath();
14483
String title = plugin.getTitle();
14584

14685
if (url.startsWith("http")) {
14786
webEngine.load(url);
14887
} else {
88+
PluginContainer pluginContainer = new PluginContainer(plugin);
14989
webEngine.load(pluginContainer.getResource(url).toExternalForm());
15090
}
15191

92+
if (singleWindowBoot) {
93+
JavaFxViewUtil.getNewStage(title, plugin.getIconPath(), new BorderPane(browser));
94+
return null;
95+
}
15296
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+
}
153103
tab.setContent(browser);
154104
tabPane.getTabs().add(tab);
155105
tabPane.getSelectionModel().select(tab);

0 commit comments

Comments
 (0)