Skip to content

Commit b3541bf

Browse files
committed
添加插件下载后动态加载功能
1 parent 8dc4834 commit b3541bf

7 files changed

Lines changed: 157 additions & 189 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/controller/IndexController.java

Lines changed: 100 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import com.xwintop.xcore.util.ConfigureUtil;
1010
import com.xwintop.xcore.util.HttpClientUtil;
1111
import com.xwintop.xcore.util.javafx.AlertUtil;
12+
import com.xwintop.xcore.util.javafx.JavaFxSystemUtil;
1213
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
1314
import javafx.application.Platform;
14-
import javafx.beans.value.ChangeListener;
15-
import javafx.beans.value.ObservableValue;
1615
import javafx.event.ActionEvent;
17-
import javafx.event.EventHandler;
1816
import javafx.fxml.FXML;
1917
import javafx.fxml.FXMLLoader;
18+
import javafx.scene.Parent;
2019
import javafx.scene.control.ContextMenu;
2120
import javafx.scene.control.Menu;
2221
import javafx.scene.control.MenuItem;
@@ -25,11 +24,22 @@
2524
import lombok.Getter;
2625
import lombok.Setter;
2726
import lombok.extern.slf4j.Slf4j;
27+
import org.apache.commons.beanutils.BeanUtils;
2828
import org.apache.commons.lang.StringUtils;
2929
import org.apache.commons.lang3.time.DateFormatUtils;
30+
import org.dom4j.Document;
31+
import org.dom4j.Element;
32+
import org.dom4j.io.SAXReader;
33+
import org.dom4j.tree.DefaultAttribute;
34+
import org.dom4j.tree.DefaultElement;
3035

36+
import java.io.File;
37+
import java.io.FilenameFilter;
38+
import java.io.InputStream;
3139
import java.net.URL;
3240
import java.util.*;
41+
import java.util.jar.JarEntry;
42+
import java.util.jar.JarFile;
3343

3444
/**
3545
* @ClassName: IndexController
@@ -66,21 +76,92 @@ public void initialize(URL location, ResourceBundle resources) {
6676
}
6777

6878
private void initView() {
69-
List<ToolFxmlLoaderConfiguration> toolList = XJavaFxSystemUtil.loaderToolFxmlLoaderConfiguration();
70-
List<ToolFxmlLoaderConfiguration> plugInToolList = XJavaFxSystemUtil.loaderPlugInToolFxmlLoaderConfiguration();
71-
toolList.addAll(plugInToolList);
7279
menuMap.put("toolsMenu", toolsMenu);
7380
menuMap.put("moreToolsMenu", moreToolsMenu);
74-
menuMap.put("netWorkToolsMenu", netWorkToolsMenu);
81+
File libPath = new File("libs/");
82+
// 获取所有的.jar和.zip文件
83+
File[] jarFiles = libPath.listFiles(new FilenameFilter() {
84+
@Override
85+
public boolean accept(File dir, String name) {
86+
return name.endsWith(".jar");
87+
}
88+
});
89+
if (jarFiles != null) {
90+
for (File jarFile : jarFiles) {
91+
try {
92+
this.addToolMenu(jarFile);
93+
} catch (Exception e) {
94+
e.printStackTrace();
95+
}
96+
}
97+
}
98+
}
99+
100+
private void initEvent() {
101+
myTextField.textProperty().addListener((observable, oldValue, newValue) -> selectAction(newValue));
102+
myButton.setOnAction(arg0 -> {
103+
selectAction(myTextField.getText());
104+
// TooltipUtil.showToast(myTextField.getText());
105+
// TooltipUtil.showToast("test",Pos.BOTTOM_RIGHT);
106+
// JOptionPane.showMessageDialog(null, "test");
107+
});
108+
}
109+
110+
private void initService() {
111+
}
112+
113+
public void addToolMenu(File file) throws Exception {
114+
XJavaFxSystemUtil.addJarClass(file);
115+
Map<String, ToolFxmlLoaderConfiguration> toolMap = new HashMap<>();
116+
List<ToolFxmlLoaderConfiguration> toolList = new ArrayList<>();
117+
JarFile jarFile = new JarFile(file);
118+
try {
119+
JarEntry entry = jarFile.getJarEntry("config/toolFxmlLoaderConfiguration.xml");
120+
if (entry == null) {
121+
return;
122+
}
123+
InputStream input = jarFile.getInputStream(entry);
124+
SAXReader saxReader = new SAXReader();
125+
Document document = saxReader.read(input);
126+
Element root = document.getRootElement();
127+
List<Element> elements = root.elements("ToolFxmlLoaderConfiguration");
128+
for (Element configurationNode : elements) {
129+
ToolFxmlLoaderConfiguration toolFxmlLoaderConfiguration = new ToolFxmlLoaderConfiguration();
130+
List<DefaultAttribute> attributes = configurationNode.attributes();
131+
for (DefaultAttribute configuration : attributes) {
132+
BeanUtils.copyProperty(toolFxmlLoaderConfiguration, configuration.getName(), configuration.getValue());
133+
}
134+
List<DefaultElement> childrenList = configurationNode.elements();
135+
for (DefaultElement configuration : childrenList) {
136+
BeanUtils.copyProperty(toolFxmlLoaderConfiguration, configuration.getName(), configuration.getStringValue());
137+
}
138+
if (StringUtils.isEmpty(toolFxmlLoaderConfiguration.getMenuParentId())) {
139+
toolFxmlLoaderConfiguration.setMenuParentId("moreToolsMenu");
140+
}
141+
if (toolFxmlLoaderConfiguration.getIsMenu()) {
142+
if (menuMap.get(toolFxmlLoaderConfiguration.getMenuId()) == null) {
143+
toolMap.putIfAbsent(toolFxmlLoaderConfiguration.getMenuId(), toolFxmlLoaderConfiguration);
144+
}
145+
} else {
146+
toolList.add(toolFxmlLoaderConfiguration);
147+
}
148+
}
149+
} finally {
150+
jarFile.close();
151+
}
152+
toolList.addAll(toolMap.values());
153+
this.addMenu(toolList);
154+
}
155+
156+
private void addMenu(List<ToolFxmlLoaderConfiguration> toolList) {
75157
for (ToolFxmlLoaderConfiguration toolConfig : toolList) {
76158
try {
77159
if (StringUtils.isEmpty(toolConfig.getResourceBundleName())) {
78160
if (StringUtils.isNotEmpty(bundle.getString(toolConfig.getTitle()))) {
79161
toolConfig.setTitle(bundle.getString(toolConfig.getTitle()));
80162
}
81163
} else {
82-
ResourceBundle resourceBundle = ResourceBundle.getBundle(toolConfig.getResourceBundleName(),
83-
Config.defaultLocale);
164+
ResourceBundle resourceBundle = ResourceBundle.getBundle(toolConfig.getResourceBundleName(), Config.defaultLocale);
84165
if (StringUtils.isNotEmpty(resourceBundle.getString(toolConfig.getTitle()))) {
85166
toolConfig.setTitle(resourceBundle.getString(toolConfig.getTitle()));
86167
}
@@ -119,12 +200,10 @@ private void initView() {
119200
}
120201
if ("Node".equals(toolConfig.getControllerType())) {
121202
menuItem.setOnAction((ActionEvent event) -> {
122-
indexService.addContent(menuItem.getText(), toolConfig.getUrl(), toolConfig.getResourceBundleName(),
123-
toolConfig.getIconPath());
203+
indexService.addContent(menuItem.getText(), toolConfig.getUrl(), toolConfig.getResourceBundleName(), toolConfig.getIconPath());
124204
});
125205
if (toolConfig.getIsDefaultShow()) {
126-
indexService.addContent(menuItem.getText(), toolConfig.getUrl(), toolConfig.getResourceBundleName(),
127-
toolConfig.getIconPath());
206+
indexService.addContent(menuItem.getText(), toolConfig.getUrl(), toolConfig.getResourceBundleName(), toolConfig.getIconPath());
128207
}
129208
} else if ("WebView".equals(toolConfig.getControllerType())) {
130209
menuItem.setOnAction((ActionEvent event) -> {
@@ -139,19 +218,6 @@ private void initView() {
139218
}
140219
}
141220

142-
private void initEvent() {
143-
myTextField.textProperty().addListener((observable, oldValue, newValue) -> selectAction(newValue));
144-
myButton.setOnAction(arg0 -> {
145-
selectAction(myTextField.getText());
146-
// TooltipUtil.showToast(myTextField.getText());
147-
// TooltipUtil.showToast("test",Pos.BOTTOM_RIGHT);
148-
// JOptionPane.showMessageDialog(null, "test");
149-
});
150-
}
151-
152-
private void initService() {
153-
}
154-
155221
public void selectAction(String selectText) {
156222
if (contextMenu.isShowing()) {
157223
contextMenu.hide();
@@ -190,7 +256,10 @@ private void addLogConsoleAction(ActionEvent event) {
190256
@FXML
191257
private void pluginManageAction(ActionEvent event) throws Exception {
192258
FXMLLoader fXMLLoader = PluginManageController.getFXMLLoader();
193-
JavaFxViewUtil.openNewWindow(bundle.getString("plugin_manage"), fXMLLoader.load());
259+
Parent root = fXMLLoader.load();
260+
PluginManageController pluginManageController = fXMLLoader.getController();
261+
pluginManageController.setIndexController(this);
262+
JavaFxViewUtil.openNewWindow(bundle.getString("plugin_manage"), root);
194263
}
195264

196265
@FXML
@@ -207,22 +276,22 @@ private void setLanguageAction(ActionEvent event) throws Exception {
207276
@FXML
208277
private void openLogFileAction(ActionEvent event) throws Exception {
209278
String filePath = "logs/logFile." + DateFormatUtils.format(new Date(), "yyyy-MM-dd") + ".log";
210-
XJavaFxSystemUtil.openDirectory(filePath);
279+
JavaFxSystemUtil.openDirectory(filePath);
211280
}
212281

213282
@FXML
214283
private void openLogFolderAction(ActionEvent event) throws Exception {
215-
XJavaFxSystemUtil.openDirectory("logs/");
284+
JavaFxSystemUtil.openDirectory("logs/");
216285
}
217286

218287
@FXML
219288
private void openConfigFolderAction(ActionEvent event) throws Exception {
220-
XJavaFxSystemUtil.openDirectory(ConfigureUtil.getConfigurePath());
289+
JavaFxSystemUtil.openDirectory(ConfigureUtil.getConfigurePath());
221290
}
222291

223292
@FXML
224293
private void openPluginFolderAction(ActionEvent event) throws Exception {
225-
XJavaFxSystemUtil.openDirectory("libs/");
294+
JavaFxSystemUtil.openDirectory("libs/");
226295
}
227296

228297
@FXML

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class PluginManageController extends PluginManageView {
3535
private PluginManageService pluginManageService = new PluginManageService(this);
3636
private ObservableList<Map<String, String>> pluginDataTableData = FXCollections.observableArrayList();
3737

38+
private IndexController indexController;
39+
3840
public static FXMLLoader getFXMLLoader() {
3941
FXMLLoader fXMLLoader = new FXMLLoader(IndexController.class.getResource("/com/xwintop/xJavaFxTool/fxmlView/index/PluginManage.fxml"));
4042
return fXMLLoader;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public void downloadPluginJar(Map<String, String> dataRow) throws Exception {
8989
File file = new File("libs/", pluginJarInfo.getJarName() + "-" + pluginJarInfo.getVersion() + ".jar");
9090
HttpUtil.downloadFile(pluginJarInfo.getDownloadUrl(), file);
9191
PLUGIN_JAR_INFO_MAP.put(pluginJarInfo.getJarName(), pluginJarInfo);
92+
pluginManageController.getIndexController().addToolMenu(file);
9293
PluginManageService.savePluginJarList();
9394
}
9495

@@ -106,7 +107,7 @@ public void selectPluginAction() {
106107
String selectText = pluginManageController.getSelectPluginTextField().getText();
107108
for (Object json : jsonArray) {
108109
JSONObject data = (JSONObject) json;
109-
if (StringUtils.containsIgnoreCase(data.toString(),selectText)) {
110+
if (StringUtils.containsIgnoreCase(data.toString(), selectText)) {
110111
addDataRow(data);
111112
}
112113
}

0 commit comments

Comments
 (0)