Skip to content

Commit fcc1180

Browse files
committed
1. 修复插件下载 NPE;2. newui: 下载插件后不将其加到 Classpath
1 parent bd72696 commit fcc1180

5 files changed

Lines changed: 42 additions & 17 deletions

File tree

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,33 @@
66
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
77
import com.xwintop.xJavaFxTool.plugin.PluginManager;
88
import com.xwintop.xJavaFxTool.services.index.PluginManageService;
9-
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
109
import com.xwintop.xJavaFxTool.view.index.PluginManageView;
1110
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
1211
import com.xwintop.xcore.util.javafx.TooltipUtil;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.net.URL;
15+
import java.util.Map;
16+
import java.util.ResourceBundle;
17+
import java.util.function.Consumer;
1318
import javafx.collections.FXCollections;
1419
import javafx.collections.ObservableList;
1520
import javafx.collections.transformation.FilteredList;
1621
import javafx.event.ActionEvent;
1722
import javafx.fxml.FXML;
1823
import javafx.fxml.FXMLLoader;
19-
import javafx.scene.control.*;
24+
import javafx.scene.control.Button;
25+
import javafx.scene.control.ContentDisplay;
26+
import javafx.scene.control.ContextMenu;
27+
import javafx.scene.control.MenuItem;
28+
import javafx.scene.control.TableCell;
29+
import javafx.scene.control.TableColumn;
2030
import javafx.stage.Window;
2131
import javafx.util.Callback;
2232
import lombok.Getter;
2333
import lombok.Setter;
2434
import lombok.extern.slf4j.Slf4j;
2535

26-
import java.io.File;
27-
import java.io.IOException;
28-
import java.net.URL;
29-
import java.util.Map;
30-
import java.util.ResourceBundle;
31-
import java.util.function.Consumer;
32-
3336
/**
3437
* @ClassName: PluginManageController
3538
* @Description: 插件管理
@@ -128,8 +131,6 @@ private void afterDownload(
128131
downloadButton.setText("已下载");
129132
downloadButton.setDisable(true);
130133

131-
XJavaFxSystemUtil.addJarClass(pluginJarInfo.getFile());
132-
133134
pluginDataTableView.refresh();
134135
PluginManager.getInstance().saveToFile();
135136
TooltipUtil.showToast("插件 " + dataRow.get("nameTableColumn") + " 下载完成");

src/main/java/com/xwintop/xJavaFxTool/event/AppEvents.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.xwintop.xJavaFxTool.event;
22

3-
import javafx.event.EventType;
4-
53
import java.util.ArrayList;
4+
import java.util.Collections;
65
import java.util.List;
76
import java.util.Map;
87
import java.util.concurrent.ConcurrentHashMap;
98
import java.util.function.Consumer;
9+
import javafx.event.EventType;
1010

1111
/**
1212
* 应用全局事件注册和触发
@@ -22,7 +22,9 @@ public class AppEvents {
2222
* @param appEvent 事件对象
2323
*/
2424
public static void fire(AppEvent appEvent) {
25-
List<Consumer> handlers = instance.listeners.get(appEvent.getEventType());
25+
List<Consumer> handlers = instance.listeners
26+
.getOrDefault(appEvent.getEventType(), Collections.emptyList());
27+
2628
for (Consumer handler : handlers) {
2729
handler.accept(appEvent);
2830
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public PluginContainer(ClassLoader parentClassLoader, PluginJarInfo pluginJarInf
2626
this.pluginJarInfo = pluginJarInfo;
2727
}
2828

29+
public PluginJarInfo getPluginJarInfo() {
30+
return pluginJarInfo;
31+
}
32+
2933
/**
3034
* 从 ClassLoader 中创建 FXMLLoader 对象
3135
*/

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.xwintop.xJavaFxTool.utils.Config;
55
import com.xwintop.xcore.javafx.dialog.FxAlerts;
66
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
7+
import java.lang.ref.WeakReference;
78
import java.net.URL;
89
import java.util.ResourceBundle;
910
import javafx.fxml.FXMLLoader;
11+
import javafx.scene.Node;
1012
import javafx.scene.control.Tab;
1113
import javafx.scene.control.TabPane;
1214
import javafx.scene.image.Image;
@@ -99,19 +101,27 @@ public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane)
99101
}
100102

101103
PluginContainer pluginContainer = new PluginContainer(PluginLoader.class.getClassLoader(), plugin);
104+
WeakReference<PluginContainer> containerRef = new WeakReference<>(pluginContainer);
102105
FXMLLoader generatingCodeFXMLLoader = pluginContainer.createFXMLLoader();
103106
if (generatingCodeFXMLLoader == null) {
104107
return null;
105108
}
106109

107-
tab.setContent(generatingCodeFXMLLoader.load());
110+
Node root = generatingCodeFXMLLoader.load();
111+
Object controller = generatingCodeFXMLLoader.getController();
112+
113+
tab.setContent(root);
108114
tabPane.getTabs().add(tab);
109115
tabPane.getSelectionModel().select(tab);
110116

111117
tab.setOnCloseRequest(
112118
event -> {
113-
JavaFxViewUtil.setControllerOnCloseRequest(generatingCodeFXMLLoader.getController(), event);
114-
pluginContainer.unload();
119+
JavaFxViewUtil.setControllerOnCloseRequest(controller, event);
120+
PluginContainer container = containerRef.get();
121+
if (container != null) {
122+
log.info("插件关闭:" + pluginContainer.getPluginJarInfo().getName());
123+
pluginContainer.unload();
124+
}
115125
}
116126
);
117127

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import cn.hutool.http.HttpUtil;
44
import com.alibaba.fastjson.JSON;
55
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
6+
import com.xwintop.xJavaFxTool.utils.Config;
7+
import com.xwintop.xJavaFxTool.utils.Config.Keys;
8+
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
69
import java.io.File;
710
import java.io.FileOutputStream;
811
import java.io.IOException;
@@ -207,6 +210,11 @@ public File downloadPlugin(
207210
plugin.setIsDownload(true);
208211
plugin.setIsEnable(true);
209212
plugin.setLocalVersionNumber(plugin.getVersionNumber());
213+
214+
if (!Config.getBoolean(Keys.NewLauncher, false)) {
215+
XJavaFxSystemUtil.addJarClass(pluginJarInfo.getFile());
216+
}
217+
210218
return file;
211219
}
212220

0 commit comments

Comments
 (0)