Skip to content

Commit 9a06fe7

Browse files
committed
newui: 实现点击插件图标打开插件
1 parent 237fe96 commit 9a06fe7

6 files changed

Lines changed: 104 additions & 8 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/newui/NewLauncherController.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.ResourceBundle;
1616
import javafx.fxml.FXMLLoader;
1717
import javafx.scene.Parent;
18+
import javafx.scene.control.TabPane;
1819
import javafx.scene.layout.VBox;
1920
import javafx.scene.web.WebView;
2021
import lombok.extern.slf4j.Slf4j;
@@ -26,6 +27,8 @@ public class NewLauncherController {
2627

2728
public WebView startWebView;
2829

30+
public TabPane tabPane;
31+
2932
public void openConfigDialog() {
3033
SystemSettingService.openSystemSettings("设置");
3134
}
@@ -41,7 +44,12 @@ public void openPluginManager() {
4144
}
4245

4346
public void initialize() {
47+
NewLauncherService.getInstance().setController(this);
48+
loadPlugins(); // 加载插件列表到界面上
49+
startWebView.getEngine().load(IndexController.QQ_URL); // 额外再打开一个反馈页面,可关闭
50+
}
4451

52+
private void loadPlugins() {
4553
List<PluginJarInfo> pluginList = PluginManager.getInstance().getPluginList();
4654
ResourceBundle menuResourceBundle = Main.RESOURCE_BUNDLE;
4755

@@ -64,11 +72,13 @@ public void initialize() {
6472
category.addItem(item);
6573
}
6674
}
67-
68-
startWebView.getEngine().load(IndexController.QQ_URL);
6975
}
7076

7177
private void addCategory(PluginCategoryController category) {
7278
this.pluginCategories.getChildren().add(category.root);
7379
}
80+
81+
public TabPane getTabPane() {
82+
return tabPane;
83+
}
7484
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.xwintop.xJavaFxTool.newui;
2+
3+
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
4+
import com.xwintop.xJavaFxTool.plugin.PluginLoader;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Map.Entry;
8+
import javafx.scene.control.Tab;
9+
import javafx.scene.control.TabPane;
10+
11+
/**
12+
* 新界面中负责与插件操作相关的逻辑
13+
*/
14+
public class NewLauncherService {
15+
16+
private static final NewLauncherService instance = new NewLauncherService();
17+
18+
private NewLauncherController newLauncherController;
19+
20+
private Map<Tab, PluginJarInfo> jarInfoMap = new HashMap<>();
21+
22+
public static NewLauncherService getInstance() {
23+
return instance;
24+
}
25+
26+
private NewLauncherService() {
27+
28+
}
29+
30+
public void setController(NewLauncherController newLauncherController) {
31+
this.newLauncherController = newLauncherController;
32+
}
33+
34+
public void loadPlugin(PluginJarInfo pluginJarInfo) {
35+
TabPane tabPane = this.newLauncherController.getTabPane();
36+
37+
for (Entry<Tab, PluginJarInfo> entry : jarInfoMap.entrySet()) {
38+
if (entry.getValue() == pluginJarInfo) {
39+
tabPane.getSelectionModel().select(entry.getKey());
40+
return;
41+
}
42+
}
43+
44+
Tab tab = PluginLoader.loadPluginAsTab(pluginJarInfo, tabPane);
45+
if (tab != null) {
46+
jarInfoMap.put(tab, pluginJarInfo);
47+
}
48+
}
49+
}

src/main/java/com/xwintop/xJavaFxTool/newui/PluginItemController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import javafx.scene.control.Label;
99
import javafx.scene.image.Image;
1010
import javafx.scene.image.ImageView;
11+
import javafx.scene.input.MouseButton;
12+
import javafx.scene.input.MouseEvent;
1113
import javafx.scene.layout.VBox;
1214
import lombok.extern.slf4j.Slf4j;
1315

@@ -36,6 +38,21 @@ public static PluginItemController newInstance(PluginJarInfo pluginJarInfo) {
3638
public void initialize() {
3739
// 当元素不可见时也从布局流中去掉
3840
this.root.managedProperty().bind(this.root.visibleProperty());
41+
this.root.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
42+
if (event.getButton() == MouseButton.PRIMARY) {
43+
onMouseLeftClicked(event);
44+
} else if (event.getButton() == MouseButton.SECONDARY) {
45+
onMouseRightClicked(event);
46+
}
47+
});
48+
}
49+
50+
private void onMouseRightClicked(MouseEvent event) {
51+
52+
}
53+
54+
private void onMouseLeftClicked(MouseEvent event) {
55+
NewLauncherService.getInstance().loadPlugin(this.pluginJarInfo);
3956
}
4057

4158
private void updateIcon() {

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
44
import com.xwintop.xJavaFxTool.utils.Config;
5+
import com.xwintop.xcore.javafx.dialog.FxAlerts;
56
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
7+
import java.net.URL;
68
import java.util.ResourceBundle;
79
import javafx.fxml.FXMLLoader;
810
import javafx.scene.control.Tab;
@@ -30,10 +32,15 @@ public static void loadPluginAsWindow(PluginJarInfo plugin) {
3032
}
3133
}
3234

33-
34-
public static void loadPluginAsTab(PluginJarInfo plugin, TabPane tabPane) {
35+
public static Tab loadPluginAsTab(PluginJarInfo plugin, TabPane tabPane) {
3536
try {
36-
FXMLLoader generatingCodeFXMLLoader = new FXMLLoader(PluginLoader.class.getResource(plugin.getFxmlPath()));
37+
URL resource = PluginLoader.class.getResource(plugin.getFxmlPath());
38+
if (resource == null) {
39+
FxAlerts.error("加载插件失败", "无法读取资源文件 '" + plugin.getFxmlPath() + "'");
40+
return null;
41+
}
42+
43+
FXMLLoader generatingCodeFXMLLoader = new FXMLLoader(resource);
3744

3845
if (StringUtils.isNotEmpty(plugin.getBundleName())) {
3946
ResourceBundle resourceBundle = ResourceBundle.getBundle(plugin.getBundleName(), Config.defaultLocale);
@@ -49,16 +56,21 @@ public static void loadPluginAsTab(PluginJarInfo plugin, TabPane tabPane) {
4956
tab.setGraphic(imageView);
5057
}
5158

59+
tab.setText(plugin.getName());
5260
tab.setContent(generatingCodeFXMLLoader.load());
5361
tabPane.getTabs().add(tab);
5462
tabPane.getSelectionModel().select(tab);
5563

5664
tab.setOnCloseRequest(
5765
event -> JavaFxViewUtil.setControllerOnCloseRequest(generatingCodeFXMLLoader.getController(), event)
5866
);
67+
68+
return tab;
5969
} catch (Exception e) {
6070
log.error("加载插件失败", e);
6171
}
72+
73+
return null;
6274
}
6375

6476
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,24 @@ public static void parse(File pluginFile, PluginJarInfo pluginJarInfo) throws IO
4242
menuTitles.put(menuElement.attributeValue("menuId"), menuElement.attributeValue("title"));
4343
}
4444

45-
String resourceBundleName = pluginElement.selectSingleNode("child::resourceBundleName").getText();
46-
String menuId = pluginElement.selectSingleNode("child::menuParentId").getText();
45+
String resourceBundleName = getChildNodeText(pluginElement, "resourceBundleName");
46+
String menuId = getChildNodeText(pluginElement, "menuParentId");
47+
String url = getChildNodeText(pluginElement, "url");
48+
String controllerType = getChildNodeText(pluginElement, "controllerType");
4749
String menuTitle = menuTitles.get(menuId);
4850

4951
pluginJarInfo.setMenuParentTitle(menuTitle);
5052
pluginJarInfo.setBundleName(resourceBundleName);
53+
pluginJarInfo.setFxmlPath(url);
54+
pluginJarInfo.setControllerType(controllerType);
5155
}
5256

5357
}
5458

59+
private static String getChildNodeText(Element element, String childNode) {
60+
return element.selectSingleNode("child::" + childNode).getText();
61+
}
62+
5563
private static Element createRootElement(JarFile jarFile, JarEntry entry) throws IOException, DocumentException {
5664
InputStream input = jarFile.getInputStream(entry);
5765
SAXReader saxReader = new SAXReader();

src/main/resources/com/xwintop/xJavaFxTool/fxmlView/newui/main.fxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Insets topRightBottomLeft="5"/>
2121
</padding>
2222

23-
<TabPane VBox.vgrow="ALWAYS" tabMinWidth="45">
23+
<TabPane VBox.vgrow="ALWAYS" tabMinWidth="45" fx:id="tabPane" tabClosingPolicy="ALL_TABS">
2424
<Tab closable="false" text="开始">
2525
<ScrollPane fitToWidth="true" style="-fx-border-width: 0 1 1 1;-fx-border-color: #CCCCCC;-fx-background:#FFFFFF">
2626
<VBox>

0 commit comments

Comments
 (0)