Skip to content

Commit 56ac043

Browse files
committed
重构插件管理界面
1 parent 753d6d1 commit 56ac043

4 files changed

Lines changed: 91 additions & 43 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ private void initView() {
6969
pluginManageService.setIsEnableTableColumn(index);
7070
});
7171

72+
// TODO 实现插件的启用禁用
73+
7274
downloadTableColumn.setCellFactory(
7375
new Callback<TableColumn<Map<String, String>, String>, TableCell<Map<String, String>, String>>() {
7476
@Override
@@ -116,7 +118,7 @@ private void initEvent() {
116118
MenuItem mnuSavePluginConfig = new MenuItem("保存配置");
117119
mnuSavePluginConfig.setOnAction(ev -> {
118120
try {
119-
PluginManager.getInstance().saveLocalPlugins();
121+
PluginManager.getInstance().saveToFile();
120122
TooltipUtil.showToast("保存配置成功");
121123
} catch (Exception ex) {
122124
log.error("保存插件配置失败", ex);

src/main/java/com/xwintop/xJavaFxTool/model/PluginJarInfo.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@
1111

1212
@Data
1313
public class PluginJarInfo {
14-
private String name;//名称
15-
private String synopsis;//简介
16-
private String jarName;//jar包名称
17-
private String version;//版本名称
18-
private Integer versionNumber;//版本号(用来判断更新)
19-
private String downloadUrl;//下载地址
20-
private Boolean isDownload;//是否下载
21-
private Boolean isEnable;//是否启用
14+
15+
////////////////////////////////////////////////////////////// 下面的属性在远程插件列表和本地配置中都存在
16+
17+
private String name; // 名称
18+
19+
private String synopsis; // 简介
20+
21+
private String jarName; // jar包名称
22+
23+
private String version; // 版本名称
24+
25+
private Integer versionNumber; // 版本号(用来判断更新)
26+
27+
private String downloadUrl; // 下载地址
28+
29+
////////////////////////////////////////////////////////////// 下面的属性在远程插件列表中不存在
30+
31+
private Boolean isDownload; // 是否下载
32+
33+
private Boolean isEnable; // 是否启用
34+
35+
private Integer localVersionNumber; // 插件本地版本
2236
}

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

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.Objects;
1717
import java.util.concurrent.CompletableFuture;
18+
import java.util.function.Consumer;
1819
import lombok.extern.slf4j.Slf4j;
1920
import org.apache.commons.lang.StringUtils;
2021

@@ -35,9 +36,7 @@ public static PluginManager getInstance() {
3536

3637
private final String localPluginsPath;
3738

38-
private final List<PluginJarInfo> serverPluginList = new ArrayList<>(); // 服务端插件列表
39-
40-
private final List<PluginJarInfo> localPluginList = new ArrayList<>(); // 本地插件列表
39+
private final List<PluginJarInfo> pluginList = new ArrayList<>(); // 插件列表
4140

4241
public PluginManager(String localPluginsPath) {
4342
this.localPluginsPath = localPluginsPath;
@@ -47,14 +46,21 @@ public PluginManager(String localPluginsPath) {
4746
}
4847
}
4948

50-
public List<PluginJarInfo> getLocalPluginList() {
51-
return Collections.unmodifiableList(this.localPluginList);
49+
////////////////////////////////////////////////////////////// 查询插件
50+
51+
public List<PluginJarInfo> getPluginList() {
52+
return Collections.unmodifiableList(this.pluginList);
5253
}
5354

54-
public List<PluginJarInfo> getServerPluginList() {
55-
return Collections.unmodifiableList(this.serverPluginList);
55+
56+
public PluginJarInfo getPlugin(String jarName) {
57+
return this.pluginList.stream()
58+
.filter(plugin -> Objects.equals(plugin.getJarName(), jarName))
59+
.findFirst().orElse(null);
5660
}
5761

62+
////////////////////////////////////////////////////////////// 更新插件
63+
5864
public void loadLocalPlugins() {
5965
try {
6066
Path path = Paths.get(this.localPluginsPath);
@@ -63,7 +69,13 @@ public void loadLocalPlugins() {
6369
}
6470

6571
String json = new String(Files.readAllBytes(path), DEFAULT_CHARSET);
66-
this.localPluginList.addAll(JSON.parseArray(json, PluginJarInfo.class));
72+
JSON.parseArray(json, PluginJarInfo.class).forEach(plugin -> {
73+
this.addOrUpdatePlugin(plugin, exist -> {
74+
exist.setLocalVersionNumber(plugin.getLocalVersionNumber());
75+
exist.setIsDownload(plugin.getIsDownload());
76+
exist.setIsEnable(plugin.getIsEnable());
77+
});
78+
});
6779
} catch (IOException e) {
6880
log.error("读取插件配置失败", e);
6981
}
@@ -72,7 +84,15 @@ public void loadLocalPlugins() {
7284
public void loadServerPlugins() {
7385
try {
7486
String json = HttpUtil.get(SERVER_PLUGINS_URL);
75-
this.serverPluginList.addAll(JSON.parseArray(json, PluginJarInfo.class));
87+
JSON.parseArray(json, PluginJarInfo.class).forEach(plugin -> {
88+
this.addOrUpdatePlugin(plugin, exist -> {
89+
exist.setName(plugin.getName());
90+
exist.setSynopsis(plugin.getSynopsis());
91+
exist.setVersion(plugin.getVersion());
92+
exist.setVersionNumber(plugin.getVersionNumber());
93+
exist.setDownloadUrl(plugin.getDownloadUrl());
94+
});
95+
});
7696
} catch (Exception e) {
7797
log.error("下载插件列表失败", e);
7898
}
@@ -83,25 +103,38 @@ public CompletableFuture<Void> loadServerPluginsAsync() {
83103
return CompletableFuture.runAsync(this::loadServerPlugins);
84104
}
85105

86-
public PluginJarInfo getLocalPlugin(String jarName) {
87-
return this.localPluginList.stream()
88-
.filter(plugin -> Objects.equals(plugin.getJarName(), jarName))
89-
.findFirst().orElse(null);
106+
private void addOrUpdatePlugin(PluginJarInfo pluginJarInfo, Consumer<PluginJarInfo> ifExists) {
107+
PluginJarInfo exists = getPlugin(pluginJarInfo.getJarName());
108+
if (exists == null) {
109+
this.pluginList.add(pluginJarInfo);
110+
} else {
111+
ifExists.accept(exists);
112+
}
90113
}
91114

115+
////////////////////////////////////////////////////////////// 下载插件
116+
92117
public File downloadPlugin(PluginJarInfo pluginJarInfo) throws IOException {
118+
PluginJarInfo plugin = getPlugin(pluginJarInfo.getJarName());
119+
if (plugin == null) {
120+
throw new IllegalStateException("没有找到插件 " + pluginJarInfo.getJarName());
121+
}
122+
93123
File file = new File("libs/", pluginJarInfo.getJarName() + "-" + pluginJarInfo.getVersion() + ".jar");
94124
HttpUtil.downloadFile(pluginJarInfo.getDownloadUrl(), file);
95125

96-
pluginJarInfo.setIsDownload(true);
97-
this.localPluginList.add(pluginJarInfo);
98-
this.saveLocalPlugins();
126+
plugin.setIsDownload(true);
127+
plugin.setIsEnable(true);
128+
plugin.setLocalVersionNumber(plugin.getVersionNumber());
129+
this.saveToFile();
99130

100131
return file;
101132
}
102133

103-
public void saveLocalPlugins() throws IOException {
104-
String json = JSON.toJSONString(this.localPluginList, true);
134+
////////////////////////////////////////////////////////////// 保存配置
135+
136+
public void saveToFile() throws IOException {
137+
String json = JSON.toJSONString(this.pluginList, true);
105138
Files.write(
106139
Paths.get(this.localPluginsPath),
107140
json.getBytes(DEFAULT_CHARSET)

src/main/java/com/xwintop/xJavaFxTool/services/index/PluginManageService.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,30 @@ public PluginManageService(PluginManageController pluginManageController) {
3939

4040
public void getPluginList() {
4141
pluginManager.loadServerPlugins();
42-
pluginManager.getServerPluginList().forEach(this::addDataRow);
42+
pluginManager.getPluginList().forEach(this::addDataRow);
4343
}
4444

45-
private void addDataRow(PluginJarInfo serverPlugin) {
46-
47-
PluginJarInfo localPlugin = pluginManager.getLocalPlugin(serverPlugin.getJarName());
45+
private void addDataRow(PluginJarInfo plugin) {
4846

4947
Map<String, String> dataRow = new HashMap<>();
50-
dataRow.put("nameTableColumn", serverPlugin.getName());
51-
dataRow.put("synopsisTableColumn", serverPlugin.getSynopsis());
52-
dataRow.put("versionTableColumn", serverPlugin.getVersion());
53-
dataRow.put("jarName", serverPlugin.getJarName());
54-
dataRow.put("downloadUrl", serverPlugin.getDownloadUrl());
55-
dataRow.put("versionNumber", String.valueOf(serverPlugin.getVersionNumber()));
56-
57-
if (localPlugin == null) {
48+
dataRow.put("nameTableColumn", plugin.getName());
49+
dataRow.put("synopsisTableColumn", plugin.getSynopsis());
50+
dataRow.put("versionTableColumn", plugin.getVersion());
51+
dataRow.put("jarName", plugin.getJarName());
52+
dataRow.put("downloadUrl", plugin.getDownloadUrl());
53+
dataRow.put("versionNumber", String.valueOf(plugin.getVersionNumber()));
54+
55+
if (plugin.getIsDownload() == null || !plugin.getIsDownload()) {
5856
dataRow.put("isDownloadTableColumn", "下载");
5957
dataRow.put("isEnableTableColumn", "false");
6058
} else {
61-
if (serverPlugin.getVersionNumber() > localPlugin.getVersionNumber()) {
59+
if (plugin.getLocalVersionNumber() != null &&
60+
plugin.getVersionNumber() > plugin.getLocalVersionNumber()) {
6261
dataRow.put("isDownloadTableColumn", "更新");
6362
} else {
6463
dataRow.put("isDownloadTableColumn", "已下载");
6564
}
66-
dataRow.put("isEnableTableColumn", localPlugin.getIsEnable().toString());
65+
dataRow.put("isEnableTableColumn", plugin.getIsEnable().toString());
6766
}
6867

6968
pluginManageController.getOriginPluginData().add(dataRow);
@@ -88,7 +87,7 @@ public void downloadPluginJar(Map<String, String> dataRow) throws Exception {
8887
public void setIsEnableTableColumn(Integer index) {
8988
Map<String, String> dataRow = pluginManageController.getOriginPluginData().get(index);
9089
String jarName = dataRow.get("jarName");
91-
PluginJarInfo pluginJarInfo = this.pluginManager.getLocalPlugin(jarName);
90+
PluginJarInfo pluginJarInfo = this.pluginManager.getPlugin(jarName);
9291
if (pluginJarInfo != null) {
9392
pluginJarInfo.setIsEnable(Boolean.parseBoolean(dataRow.get("isEnableTableColumn")));
9493
}
@@ -118,7 +117,7 @@ private boolean isPluginDataMatch(Map<String, String> map, String keyword) {
118117
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
119118
public static boolean isPluginEnabled(String fileName) {
120119
String jarName = substringBeforeLast(fileName, "-");
121-
PluginJarInfo pluginJarInfo = PluginManager.getInstance().getLocalPlugin(jarName);
120+
PluginJarInfo pluginJarInfo = PluginManager.getInstance().getPlugin(jarName);
122121
return pluginJarInfo == null || pluginJarInfo.getIsEnable();
123122
}
124123
}

0 commit comments

Comments
 (0)