Skip to content

Commit 753d6d1

Browse files
committed
重构插件管理界面(未完成)
1 parent 94504cb commit 753d6d1

6 files changed

Lines changed: 721 additions & 156 deletions

File tree

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.xwintop.xJavaFxTool.controller.index;
22

33
import com.xwintop.xJavaFxTool.controller.IndexController;
4+
import com.xwintop.xJavaFxTool.plugin.PluginManager;
45
import com.xwintop.xJavaFxTool.services.index.PluginManageService;
56
import com.xwintop.xJavaFxTool.view.index.PluginManageView;
67
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
@@ -10,6 +11,7 @@
1011
import java.util.ResourceBundle;
1112
import javafx.collections.FXCollections;
1213
import javafx.collections.ObservableList;
14+
import javafx.collections.transformation.FilteredList;
1315
import javafx.event.ActionEvent;
1416
import javafx.fxml.FXML;
1517
import javafx.fxml.FXMLLoader;
@@ -19,7 +21,6 @@
1921
import javafx.scene.control.MenuItem;
2022
import javafx.scene.control.TableCell;
2123
import javafx.scene.control.TableColumn;
22-
import javafx.scene.input.MouseButton;
2324
import javafx.util.Callback;
2425
import lombok.Getter;
2526
import lombok.Setter;
@@ -36,14 +37,19 @@
3637
@Setter
3738
@Slf4j
3839
public class PluginManageController extends PluginManageView {
40+
41+
public static final String FXML = "/com/xwintop/xJavaFxTool/fxmlView/index/PluginManage.fxml";
42+
3943
private PluginManageService pluginManageService = new PluginManageService(this);
40-
private ObservableList<Map<String, String>> pluginDataTableData = FXCollections.observableArrayList();
44+
45+
private ObservableList<Map<String, String>> originPluginData = FXCollections.observableArrayList();
46+
47+
private FilteredList<Map<String, String>> pluginDataTableData = new FilteredList<>(originPluginData, m -> true);
4148

4249
private IndexController indexController;
4350

4451
public static FXMLLoader getFXMLLoader() {
45-
FXMLLoader fXMLLoader = new FXMLLoader(IndexController.class.getResource("/com/xwintop/xJavaFxTool/fxmlView/index/PluginManage.fxml"));
46-
return fXMLLoader;
52+
return new FXMLLoader(IndexController.class.getResource(FXML));
4753
}
4854

4955
@Override
@@ -58,68 +64,71 @@ private void initView() {
5864
JavaFxViewUtil.setTableColumnMapValueFactory(synopsisTableColumn, "synopsisTableColumn");
5965
JavaFxViewUtil.setTableColumnMapValueFactory(versionTableColumn, "versionTableColumn");
6066
JavaFxViewUtil.setTableColumnMapValueFactory(isDownloadTableColumn, "isDownloadTableColumn");
61-
JavaFxViewUtil.setTableColumnMapAsCheckBoxValueFactory(isEnableTableColumn, "isEnableTableColumn", (mouseEvent, index) -> {
62-
pluginManageService.setIsEnableTableColumn(index);
63-
});
67+
JavaFxViewUtil.setTableColumnMapAsCheckBoxValueFactory(isEnableTableColumn, "isEnableTableColumn",
68+
(mouseEvent, index) -> {
69+
pluginManageService.setIsEnableTableColumn(index);
70+
});
6471

65-
downloadTableColumn.setCellFactory(new Callback<TableColumn<Map<String, String>, String>, TableCell<Map<String, String>, String>>() {
66-
@Override
67-
public TableCell<Map<String, String>, String> call(TableColumn<Map<String, String>, String> param) {
68-
TableCell<Map<String, String>, String> cell = new TableCell<Map<String, String>, String>() {
69-
@Override
70-
protected void updateItem(String item, boolean empty) {
71-
super.updateItem(item, empty);
72-
this.setText(null);
73-
this.setGraphic(null);
74-
if (!empty) {
75-
Map<String, String> dataRow = pluginDataTableData.get(this.getIndex());
76-
Button downloadButton = new Button(dataRow.get("isDownloadTableColumn"));
77-
if ("已下载".equals(dataRow.get("isDownloadTableColumn"))) {
78-
downloadButton.setDisable(true);
79-
}
80-
this.setContentDisplay(ContentDisplay.CENTER);
81-
downloadButton.setOnMouseClicked((me) -> {
82-
try {
83-
pluginManageService.downloadPluginJar(dataRow);
84-
dataRow.put("isEnableTableColumn","true");
85-
dataRow.put("isDownloadTableColumn", "已下载");
86-
downloadButton.setText("已下载");
72+
downloadTableColumn.setCellFactory(
73+
new Callback<TableColumn<Map<String, String>, String>, TableCell<Map<String, String>, String>>() {
74+
@Override
75+
public TableCell<Map<String, String>, String> call(TableColumn<Map<String, String>, String> param) {
76+
return new TableCell<Map<String, String>, String>() {
77+
@Override
78+
protected void updateItem(String item, boolean empty) {
79+
super.updateItem(item, empty);
80+
this.setText(null);
81+
this.setGraphic(null);
82+
if (!empty) {
83+
Map<String, String> dataRow = pluginDataTableData.get(this.getIndex());
84+
Button downloadButton = new Button(dataRow.get("isDownloadTableColumn"));
85+
if ("已下载".equals(dataRow.get("isDownloadTableColumn"))) {
8786
downloadButton.setDisable(true);
88-
pluginDataTableView.refresh();
89-
TooltipUtil.showToast("插件 " + dataRow.get("nameTableColumn") + " 下载完成");
90-
} catch (Exception e) {
91-
log.error("下载插件失败:", e);
92-
TooltipUtil.showToast("下载插件失败:" + e.getMessage());
9387
}
94-
});
95-
this.setGraphic(downloadButton);
88+
this.setContentDisplay(ContentDisplay.CENTER);
89+
downloadButton.setOnMouseClicked((me) -> {
90+
try {
91+
pluginManageService.downloadPluginJar(dataRow);
92+
dataRow.put("isEnableTableColumn", "true");
93+
dataRow.put("isDownloadTableColumn", "已下载");
94+
downloadButton.setText("已下载");
95+
downloadButton.setDisable(true);
96+
pluginDataTableView.refresh();
97+
TooltipUtil.showToast("插件 " + dataRow.get("nameTableColumn") + " 下载完成");
98+
} catch (Exception e) {
99+
log.error("下载插件失败:", e);
100+
TooltipUtil.showToast("下载插件失败:" + e.getMessage());
101+
}
102+
});
103+
this.setGraphic(downloadButton);
104+
}
96105
}
97-
}
98-
};
99-
return cell;
100-
}
101-
});
106+
};
107+
}
108+
});
102109

103110
pluginDataTableView.setItems(pluginDataTableData);
104111
}
105112

106113
private void initEvent() {
107-
pluginDataTableView.setOnMouseClicked(event -> {
108-
if (event.getButton() == MouseButton.SECONDARY) {
109-
MenuItem menuSave = new MenuItem("保存配置");
110-
menuSave.setOnAction(event1 -> {
111-
try {
112-
pluginManageService.savePluginJarList();
113-
TooltipUtil.showToast("保存配置成功");
114-
} catch (Exception e) {
115-
log.error("保存配置失败", e);
116-
}
117-
});
118-
pluginDataTableView.setContextMenu(new ContextMenu(menuSave));
114+
115+
// 右键菜单
116+
MenuItem mnuSavePluginConfig = new MenuItem("保存配置");
117+
mnuSavePluginConfig.setOnAction(ev -> {
118+
try {
119+
PluginManager.getInstance().saveLocalPlugins();
120+
TooltipUtil.showToast("保存配置成功");
121+
} catch (Exception ex) {
122+
log.error("保存插件配置失败", ex);
119123
}
120124
});
125+
126+
ContextMenu contextMenu = new ContextMenu(mnuSavePluginConfig);
127+
pluginDataTableView.setContextMenu(contextMenu);
128+
129+
// 搜索
121130
selectPluginTextField.textProperty().addListener((_ob, _old, _new) -> {
122-
pluginManageService.selectPluginAction();
131+
pluginManageService.searchPlugin(_new);
123132
});
124133
}
125134

@@ -129,6 +138,6 @@ private void initService() {
129138

130139
@FXML
131140
private void selectPluginAction(ActionEvent event) {
132-
pluginManageService.selectPluginAction();
141+
pluginManageService.searchPlugin(selectPluginTextField.getText());
133142
}
134143
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.xwintop.xJavaFxTool.plugin;
2+
3+
import cn.hutool.http.HttpUtil;
4+
import com.alibaba.fastjson.JSON;
5+
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.charset.Charset;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.List;
16+
import java.util.Objects;
17+
import java.util.concurrent.CompletableFuture;
18+
import lombok.extern.slf4j.Slf4j;
19+
import org.apache.commons.lang.StringUtils;
20+
21+
@Slf4j
22+
public class PluginManager {
23+
24+
public static final String SERVER_PLUGINS_URL = "https://xwintop.gitee.io/maven/plugin-libs/plugin-list.json";
25+
26+
public static final String LOCAL_PLUGINS_PATH = "./system_plugin_list.json";
27+
28+
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
29+
30+
public static PluginManager getInstance() {
31+
return new PluginManager(LOCAL_PLUGINS_PATH);
32+
}
33+
34+
//////////////////////////////////////////////////////////////
35+
36+
private final String localPluginsPath;
37+
38+
private final List<PluginJarInfo> serverPluginList = new ArrayList<>(); // 服务端插件列表
39+
40+
private final List<PluginJarInfo> localPluginList = new ArrayList<>(); // 本地插件列表
41+
42+
public PluginManager(String localPluginsPath) {
43+
this.localPluginsPath = localPluginsPath;
44+
45+
if (StringUtils.isNotEmpty(localPluginsPath)) {
46+
loadLocalPlugins();
47+
}
48+
}
49+
50+
public List<PluginJarInfo> getLocalPluginList() {
51+
return Collections.unmodifiableList(this.localPluginList);
52+
}
53+
54+
public List<PluginJarInfo> getServerPluginList() {
55+
return Collections.unmodifiableList(this.serverPluginList);
56+
}
57+
58+
public void loadLocalPlugins() {
59+
try {
60+
Path path = Paths.get(this.localPluginsPath);
61+
if (!Files.exists(path)) {
62+
return;
63+
}
64+
65+
String json = new String(Files.readAllBytes(path), DEFAULT_CHARSET);
66+
this.localPluginList.addAll(JSON.parseArray(json, PluginJarInfo.class));
67+
} catch (IOException e) {
68+
log.error("读取插件配置失败", e);
69+
}
70+
}
71+
72+
public void loadServerPlugins() {
73+
try {
74+
String json = HttpUtil.get(SERVER_PLUGINS_URL);
75+
this.serverPluginList.addAll(JSON.parseArray(json, PluginJarInfo.class));
76+
} catch (Exception e) {
77+
log.error("下载插件列表失败", e);
78+
}
79+
}
80+
81+
// 异步下载服务器插件列表,便于界面上展示 loading 动画
82+
public CompletableFuture<Void> loadServerPluginsAsync() {
83+
return CompletableFuture.runAsync(this::loadServerPlugins);
84+
}
85+
86+
public PluginJarInfo getLocalPlugin(String jarName) {
87+
return this.localPluginList.stream()
88+
.filter(plugin -> Objects.equals(plugin.getJarName(), jarName))
89+
.findFirst().orElse(null);
90+
}
91+
92+
public File downloadPlugin(PluginJarInfo pluginJarInfo) throws IOException {
93+
File file = new File("libs/", pluginJarInfo.getJarName() + "-" + pluginJarInfo.getVersion() + ".jar");
94+
HttpUtil.downloadFile(pluginJarInfo.getDownloadUrl(), file);
95+
96+
pluginJarInfo.setIsDownload(true);
97+
this.localPluginList.add(pluginJarInfo);
98+
this.saveLocalPlugins();
99+
100+
return file;
101+
}
102+
103+
public void saveLocalPlugins() throws IOException {
104+
String json = JSON.toJSONString(this.localPluginList, true);
105+
Files.write(
106+
Paths.get(this.localPluginsPath),
107+
json.getBytes(DEFAULT_CHARSET)
108+
);
109+
}
110+
}

0 commit comments

Comments
 (0)