newui: 实现下载插件后立刻更新列表

This commit is contained in:
Yiding He 2020-04-17 00:00:46 +08:00
parent 418dd1ac11
commit a47712eaa4
11 changed files with 220 additions and 105 deletions

View File

@ -7,3 +7,6 @@ indent_size = 4
[*.{html, xml, yaml, fxml}]
indent_size = 2
[*.java]
ij_any_blank_lines_around_field = 1

View File

@ -59,7 +59,7 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- dom4j 需要的依赖 -->
<dependency>

View File

@ -1,8 +1,5 @@
package com.xwintop.xJavaFxTool.controller;
import static com.xwintop.xJavaFxTool.Main.RESOURCE_BUNDLE;
import static com.xwintop.xJavaFxTool.utils.Config.Keys.NotepadEnabled;
import com.xwintop.xJavaFxTool.controller.index.PluginManageController;
import com.xwintop.xJavaFxTool.model.ToolFxmlLoaderConfiguration;
import com.xwintop.xJavaFxTool.plugin.PluginManager;
@ -10,24 +7,12 @@ import com.xwintop.xJavaFxTool.services.IndexService;
import com.xwintop.xJavaFxTool.services.index.PluginManageService;
import com.xwintop.xJavaFxTool.services.index.SystemSettingService;
import com.xwintop.xJavaFxTool.utils.Config;
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
import com.xwintop.xJavaFxTool.view.IndexView;
import com.xwintop.xcore.util.ConfigureUtil;
import com.xwintop.xcore.util.HttpClientUtil;
import com.xwintop.xcore.util.javafx.AlertUtil;
import com.xwintop.xcore.util.javafx.JavaFxSystemUtil;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
@ -49,6 +34,16 @@ import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import static com.xwintop.xJavaFxTool.Main.RESOURCE_BUNDLE;
import static com.xwintop.xJavaFxTool.utils.Config.Keys.NotepadEnabled;
/**
* @ClassName: IndexController
* @Description: 主页
@ -274,7 +269,6 @@ public class IndexController extends IndexView {
PluginManageController pluginManageController = fXMLLoader.getController();
pluginManageController.setOnPluginDownloaded(jarFile -> {
try {
XJavaFxSystemUtil.addJarClass(jarFile);
this.addToolMenu(jarFile);
PluginManager.getInstance().loadLocalPlugins();
} catch (Exception e) {

View File

@ -1,34 +1,35 @@
package com.xwintop.xJavaFxTool.controller.index;
import com.xwintop.xJavaFxTool.controller.IndexController;
import com.xwintop.xJavaFxTool.event.AppEvents;
import com.xwintop.xJavaFxTool.event.PluginEvent;
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
import com.xwintop.xJavaFxTool.plugin.PluginManager;
import com.xwintop.xJavaFxTool.services.index.PluginManageService;
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
import com.xwintop.xJavaFxTool.view.index.PluginManageView;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Consumer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.*;
import javafx.stage.Window;
import javafx.util.Callback;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Consumer;
/**
* @ClassName: PluginManageController
* @Description: 插件管理
@ -96,21 +97,7 @@ public class PluginManageController extends PluginManageView {
downloadButton.setDisable(true);
}
this.setContentDisplay(ContentDisplay.CENTER);
downloadButton.setOnMouseClicked((me) -> {
try {
pluginManageService.downloadPluginJar(dataRow);
dataRow.put("isEnableTableColumn", "true");
dataRow.put("isDownloadTableColumn", "已下载");
downloadButton.setText("已下载");
downloadButton.setDisable(true);
pluginDataTableView.refresh();
PluginManager.getInstance().saveToFile();
TooltipUtil.showToast("插件 " + dataRow.get("nameTableColumn") + " 下载完成");
} catch (Exception e) {
log.error("下载插件失败:", e);
TooltipUtil.showToast("下载插件失败:" + e.getMessage());
}
});
downloadButton.setOnMouseClicked(event -> downloadPlugin(dataRow, downloadButton));
this.setGraphic(downloadButton);
}
}
@ -121,6 +108,35 @@ public class PluginManageController extends PluginManageView {
pluginDataTableView.setItems(pluginDataTableData);
}
private void downloadPlugin(Map<String, String> dataRow, Button downloadButton) {
try {
PluginJarInfo pluginJarInfo = pluginManageService.downloadPluginJar(dataRow);
afterDownload(dataRow, downloadButton, pluginJarInfo);
} catch (Exception e) {
log.error("下载插件失败:", e);
TooltipUtil.showToast("下载插件失败:" + e.getMessage());
}
}
private void afterDownload(
Map<String, String> dataRow, Button downloadButton, PluginJarInfo pluginJarInfo
) throws IOException {
dataRow.put("isEnableTableColumn", "true");
dataRow.put("isDownloadTableColumn", "已下载");
downloadButton.setText("已下载");
downloadButton.setDisable(true);
XJavaFxSystemUtil.addJarClass(pluginJarInfo.getFile());
pluginDataTableView.refresh();
PluginManager.getInstance().saveToFile();
TooltipUtil.showToast("插件 " + dataRow.get("nameTableColumn") + " 下载完成");
AppEvents.fire(new PluginEvent(PluginEvent.PLUGIN_DOWNLOADED, pluginJarInfo));
}
private void initEvent() {
// 右键菜单

View File

@ -0,0 +1,15 @@
package com.xwintop.xJavaFxTool.event;
import javafx.event.Event;
import javafx.event.EventTarget;
import javafx.event.EventType;
public abstract class AppEvent extends Event {
public AppEvent(EventType<? extends Event> eventType) {
super(eventType);
}
public AppEvent(Object source, EventTarget target, EventType<? extends Event> eventType) {
super(source, target, eventType);
}
}

View File

@ -0,0 +1,43 @@
package com.xwintop.xJavaFxTool.event;
import javafx.event.EventType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
/**
* 应用全局事件注册和触发
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class AppEvents {
private static final AppEvents instance = new AppEvents();
/**
* 触发事件
*
* @param appEvent 事件对象
*/
public static void fire(AppEvent appEvent) {
List<Consumer> handlers = instance.listeners.get(appEvent.getEventType());
for (Consumer handler : handlers) {
handler.accept(appEvent);
}
}
/**
* 注册事件侦听
*
* @param eventType 事件类型
* @param handler 侦听器
* @param <T> 事件对象类型
*/
public static <T extends AppEvent> void addEventHandler(EventType<T> eventType, Consumer<T> handler) {
instance.listeners.computeIfAbsent(eventType, __ -> new ArrayList<>()).add(handler);
}
private final Map<EventType, List<Consumer>> listeners = new ConcurrentHashMap<>();
}

View File

@ -0,0 +1,21 @@
package com.xwintop.xJavaFxTool.event;
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
import javafx.event.Event;
import javafx.event.EventType;
public class PluginEvent extends AppEvent {
public static final EventType<PluginEvent> PLUGIN_DOWNLOADED = new EventType<>(Event.ANY, "PLUGIN_DOWNLOADED");
private final PluginJarInfo pluginJarInfo;
public PluginEvent(EventType<PluginEvent> eventType, PluginJarInfo pluginJarInfo) {
super(eventType);
this.pluginJarInfo = pluginJarInfo;
}
public PluginJarInfo getPluginJarInfo() {
return pluginJarInfo;
}
}

View File

@ -3,16 +3,29 @@ package com.xwintop.xJavaFxTool.newui;
import com.xwintop.xJavaFxTool.Main;
import com.xwintop.xJavaFxTool.controller.IndexController;
import com.xwintop.xJavaFxTool.controller.index.PluginManageController;
import com.xwintop.xJavaFxTool.event.AppEvents;
import com.xwintop.xJavaFxTool.event.PluginEvent;
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
import com.xwintop.xJavaFxTool.newui.creator.CreatePluginProjectService;
import com.xwintop.xJavaFxTool.newui.creator.PluginProjectInfo;
import com.xwintop.xJavaFxTool.plugin.PluginManager;
import com.xwintop.xJavaFxTool.plugin.PluginParser;
import com.xwintop.xJavaFxTool.services.index.SystemSettingService;
import com.xwintop.xcore.javafx.FxApp;
import com.xwintop.xcore.javafx.dialog.FxAlerts;
import com.xwintop.xcore.javafx.dialog.FxDialog;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import java.awt.Desktop;
import javafx.beans.Observable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.TextField;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
@ -20,20 +33,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.beans.Observable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class NewLauncherController {
@ -55,12 +54,22 @@ public class NewLauncherController {
// 实现搜索用
private final List<PluginItemController> pluginItemControllers = new ArrayList<>();
private final Map<String, PluginCategoryController> categoryControllers = new HashMap<>();
public void initialize() {
NewLauncherService.getInstance().setController(this);
txtSearch.textProperty().addListener(this::onSearchKeywordChanged);
initContextMenu();
loadPlugins(); // 加载插件列表到界面上
startWebView.getEngine().load(IndexController.QQ_URL); // 额外再打开一个反馈页面可关闭
AppEvents.addEventHandler(PluginEvent.PLUGIN_DOWNLOADED, pluginEvent -> {
PluginJarInfo pluginJarInfo = pluginEvent.getPluginJarInfo();
PluginParser.parse(pluginJarInfo.getFile(), pluginJarInfo);
loadPlugins();
});
}
private void initContextMenu() {
@ -101,35 +110,34 @@ public class NewLauncherController {
this.pluginCategories.getChildren().clear();
this.pluginItemControllers.clear();
this.categoryControllers.clear();
List<PluginJarInfo> pluginList = PluginManager.getInstance().getPluginList();
ResourceBundle menuResourceBundle = Main.RESOURCE_BUNDLE;
pluginList.forEach(this::loadPlugin);
}
Map<String, PluginCategoryController> categoryControllers = new HashMap<>();
public void loadPlugin(PluginJarInfo jarInfo) {
String menuParentTitle = jarInfo.getMenuParentTitle();
if (menuParentTitle != null) {
for (PluginJarInfo jarInfo : pluginList) {
String menuParentTitle = jarInfo.getMenuParentTitle();
if (menuParentTitle != null) {
String categoryName = jarInfo.getIsFavorite() ?
FAVORITE_CATEGORY_NAME : Main.RESOURCE_BUNDLE.getString(menuParentTitle);
String categoryName = jarInfo.getIsFavorite() ?
FAVORITE_CATEGORY_NAME : menuResourceBundle.getString(menuParentTitle);
PluginCategoryController category = categoryControllers.computeIfAbsent(
categoryName, __ -> {
PluginCategoryController _category =
PluginCategoryController.newInstance(categoryName);
addCategory(_category);
return _category;
}
);
PluginItemController item = PluginItemController.newInstance(jarInfo);
item.setContextMenu(itemContextMenu);
category.addItem(item);
if (!pluginItemControllers.contains(item)) {
pluginItemControllers.add(item);
PluginCategoryController category = categoryControllers.computeIfAbsent(
categoryName, __ -> {
PluginCategoryController _category =
PluginCategoryController.newInstance(categoryName);
addCategory(_category);
return _category;
}
);
PluginItemController item = PluginItemController.newInstance(jarInfo);
item.setContextMenu(itemContextMenu);
category.addItem(item);
if (!pluginItemControllers.contains(item)) {
pluginItemControllers.add(item);
}
}
}

View File

@ -1,9 +1,13 @@
package com.xwintop.xJavaFxTool.plugin;
import static org.apache.commons.lang.StringUtils.defaultString;
import com.xwintop.xJavaFxTool.AppException;
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
import com.xwintop.xJavaFxTool.utils.Config;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -14,10 +18,8 @@ import java.util.ResourceBundle;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import static org.apache.commons.lang.StringUtils.defaultString;
/**
* 用来解析插件文件中的 toolFxmlLoaderConfiguration.xml
@ -29,7 +31,7 @@ public class PluginParser {
/**
* 解析插件文件补完 pluginJarInfo 属性
*/
public static void parse(File pluginFile, PluginJarInfo pluginJarInfo) throws IOException, DocumentException {
public static void parse(File pluginFile, PluginJarInfo pluginJarInfo) {
try (JarFile jarFile = new JarFile(pluginFile)) {
JarEntry entry = jarFile.getJarEntry(ENTRY_NAME);
@ -62,8 +64,9 @@ public class PluginParser {
pluginJarInfo.setFxmlPath(url);
pluginJarInfo.setControllerType(controllerType);
pluginJarInfo.setTitle(title);
} catch (IOException | DocumentException e) {
throw new AppException(e);
}
}
private static String getChildNodeText(Element element, String childNode) {

View File

@ -1,22 +1,23 @@
package com.xwintop.xJavaFxTool.services.index;
import static org.apache.commons.lang3.StringUtils.substringBeforeLast;
import com.xwintop.xJavaFxTool.AppException;
import com.xwintop.xJavaFxTool.controller.index.PluginManageController;
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
import com.xwintop.xJavaFxTool.plugin.PluginManager;
import com.xwintop.xcore.javafx.dialog.FxProgressDialog;
import com.xwintop.xcore.javafx.dialog.ProgressTask;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import static org.apache.commons.lang3.StringUtils.substringBeforeLast;
/**
* 插件管理
*
@ -72,16 +73,10 @@ public class PluginManageService {
pluginManageController.getOriginPluginData().add(dataRow);
}
public void downloadPluginJar(Map<String, String> dataRow) throws Exception {
PluginJarInfo pluginJarInfo = new PluginJarInfo();
pluginJarInfo.setName(dataRow.get("nameTableColumn"));
pluginJarInfo.setSynopsis(dataRow.get("synopsisTableColumn"));
pluginJarInfo.setVersion(dataRow.get("versionTableColumn"));
pluginJarInfo.setVersionNumber(Integer.parseInt(dataRow.get("versionNumber")));
pluginJarInfo.setDownloadUrl(dataRow.get("downloadUrl"));
pluginJarInfo.setJarName(dataRow.get("jarName"));
pluginJarInfo.setIsDownload(true);
pluginJarInfo.setIsEnable(true);
public PluginJarInfo downloadPluginJar(Map<String, String> dataRow) throws Exception {
String jarName = dataRow.get("jarName");
PluginJarInfo pluginJarInfo = pluginManager.getPlugin(jarName);
ProgressTask progressTask = new ProgressTask() {
@Override
@ -103,6 +98,8 @@ public class PluginManageService {
FxProgressDialog
.create(pluginManageController.getWindow(), progressTask, "正在下载插件 " + pluginJarInfo.getName() + "...")
.showAndWait();
return pluginJarInfo;
}
public void setIsEnableTableColumn(Integer index) {

View File

@ -0,0 +1,15 @@
package com.xwintop.xJavaFxTool.event;
import org.junit.Test;
public class AppEventsTest {
@Test
public void testFireEvent() {
AppEvents.addEventHandler(PluginEvent.PLUGIN_DOWNLOADED, event -> {
System.out.println("Plugin downloaded: " + event.getPluginJarInfo());
});
AppEvents.fire(new PluginEvent(PluginEvent.PLUGIN_DOWNLOADED, null));
}
}