Skip to content

Commit 9d7bef6

Browse files
committed
1.优化界面菜单功能
1 parent b7114a2 commit 9d7bef6

5 files changed

Lines changed: 43 additions & 40 deletions

File tree

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class IndexController extends IndexView {
7575

7676
// 实现搜索用
7777
private List<PluginItemController> pluginItemControllers = new ArrayList<>();
78+
7879
private Map<String, PluginCategoryController> categoryControllers = new HashMap<>();
7980

8081
public static FXMLLoader getFXMLLoader() {
@@ -103,23 +104,7 @@ private void initNotepad() {
103104
}
104105

105106
private void initView() {
106-
initContextMenu();
107-
menuMap.put("moreToolsMenu", moreToolsMenu);
108-
File libPath = new File("libs/");
109-
// 获取所有的.jar和.zip文件
110-
File[] jarFiles = libPath.listFiles((dir, name) -> name.endsWith(".jar"));
111-
if (jarFiles != null) {
112-
for (File jarFile : jarFiles) {
113-
if (!PluginManageService.isPluginEnabled(jarFile.getName())) {
114-
continue;
115-
}
116-
try {
117-
this.addToolMenu(jarFile);
118-
} catch (Exception e) {
119-
log.error("加载工具出错:", e);
120-
}
121-
}
122-
}
107+
// menuMap.put("moreToolsMenu", moreToolsMenu);
123108
}
124109

125110
private void initEvent() {
@@ -134,16 +119,16 @@ private void initService() {
134119
});
135120
}
136121

137-
private void initContextMenu() {
138-
}
139-
140122
/**
141123
* 加载/刷新插件列表
142124
*/
143125
public void loadPlugins() {
144126
this.pluginCategories.getChildren().clear();
145127
this.pluginItemControllers.clear();
146128
this.categoryControllers.clear();
129+
this.menuMap.clear();
130+
this.menuItemMap.clear();
131+
this.moreToolsMenu.getItems().clear();
147132

148133
PluginManager pluginManager = PluginManager.getInstance();
149134
pluginManager.loadLocalPlugins();
@@ -185,6 +170,8 @@ private void loadPlugin(PluginJarInfo jarInfo) {
185170
if (!pluginItemControllers.contains(item)) {
186171
pluginItemControllers.add(item);
187172
}
173+
174+
addMenu(jarInfo);
188175
}
189176

190177
private void addCategory(PluginCategoryController category) {
@@ -300,6 +287,31 @@ private void addMenu(List<ToolFxmlLoaderConfiguration> toolList) {
300287
}
301288
}
302289

290+
private void addMenu(PluginJarInfo jarInfo) {
291+
// Optional<MenuItem> menu = moreToolsMenu.getItems().stream().filter(menuItem1 -> jarInfo.getMenuParentId().equals(menuItem1.getId())).findAny();
292+
if (!menuMap.containsKey(jarInfo.getMenuParentId())) {
293+
// if (moreToolsMenu.getItems().stream().noneMatch(menuItem -> jarInfo.getMenuParentId().equals(menuItem.getId()))) {
294+
// if (menu == null) {
295+
Menu menu = new Menu(XJavaFxToolApplication.RESOURCE_BUNDLE.getString(jarInfo.getMenuParentTitle()));
296+
menu.setId(jarInfo.getMenuParentId());
297+
menuMap.put(jarInfo.getMenuParentId(),menu);
298+
moreToolsMenu.getItems().add(menu);
299+
}
300+
301+
MenuItem menuItem = new MenuItem(jarInfo.getTitle());
302+
if (StringUtils.isNotEmpty(jarInfo.getIconPath())) {
303+
ImageView imageView = new ImageView(new Image(jarInfo.getIconPath()));
304+
imageView.setFitHeight(18);
305+
imageView.setFitWidth(18);
306+
menuItem.setGraphic(imageView);
307+
}
308+
menuItem.setOnAction((ActionEvent event) -> {
309+
indexService.loadPlugin(jarInfo);
310+
});
311+
menuMap.get(jarInfo.getMenuParentId()).getItems().add(menuItem);
312+
menuItemMap.put(menuItem.getText(), menuItem);
313+
}
314+
303315
public void selectAction(String selectText) {
304316
// if (contextMenu.isShowing()) {
305317
// contextMenu.hide();

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.FileOutputStream;
1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.nio.charset.Charset;
2221
import java.nio.charset.StandardCharsets;
2322
import java.nio.file.Files;
2423
import java.nio.file.Path;
@@ -203,9 +202,7 @@ private void addOrUpdatePlugin(PluginJarInfo pluginJarInfo, Consumer<PluginJarIn
203202

204203
////////////////////////////////////////////////////////////// 下载插件
205204

206-
public File downloadPlugin(
207-
PluginJarInfo pluginJarInfo, BiConsumer<Long, Long> onProgressUpdate
208-
) throws IOException {
205+
public File downloadPlugin(PluginJarInfo pluginJarInfo, BiConsumer<Long, Long> onProgressUpdate) throws IOException {
209206

210207
PluginJarInfo plugin = getPlugin(pluginJarInfo.getJarName());
211208
if (plugin == null) {
@@ -215,8 +212,7 @@ public File downloadPlugin(
215212
File file = pluginJarInfo.getFile();
216213
FileUtils.forceMkdirParent(file);
217214

218-
this.currentProgressListener =
219-
(bytesRead, contentLength, done) -> onProgressUpdate.accept(contentLength, bytesRead);
215+
this.currentProgressListener = (bytesRead, contentLength, done) -> onProgressUpdate.accept(contentLength, bytesRead);
220216

221217
// 使用多个 UA 尝试下载
222218
Throwable downloadFailure = null;
@@ -234,8 +230,7 @@ public File downloadPlugin(
234230
if (downloadFailure instanceof IOException) {
235231
throw (IOException) downloadFailure;
236232
} else {
237-
throw new IOException("插件 '" + plugin.getName() +
238-
"' 下载失败 " + pluginJarInfo.getJarName(), downloadFailure);
233+
throw new IOException("插件 '" + plugin.getName() + "' 下载失败 " + pluginJarInfo.getJarName(), downloadFailure);
239234
}
240235
}
241236

@@ -258,7 +253,6 @@ public File downloadPlugin(
258253
* @param url 下载地址
259254
* @param ua UA 字符串
260255
* @param file 下载到的目标文件
261-
*
262256
* @throws IOException 如果下载失败
263257
*/
264258
private void tryDownload(String pluginName, String url, String ua, File file) throws IOException {
@@ -351,9 +345,7 @@ public BufferedSource source() {
351345

352346
private Source source(Source source) {
353347
return new ForwardingSource(source) {
354-
355348
long totalBytesRead = 0L;
356-
357349
@Override
358350
public long read(Buffer sink, long byteCount) throws IOException {
359351
long bytesRead = super.read(sink, byteCount);
@@ -367,7 +359,6 @@ public long read(Buffer sink, long byteCount) throws IOException {
367359
}
368360

369361
interface ProgressListener {
370-
371362
void update(long bytesRead, long contentLength, boolean done);
372363
}
373364
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static void parse(File pluginFile, PluginJarInfo pluginJarInfo, ClassLoad
7171
String controllerType = getChildNodeText(pluginElement, "controllerType");
7272
String menuTitle = menuTitles.get(menuId);
7373

74+
pluginJarInfo.setMenuParentId(menuId);
7475
pluginJarInfo.setMenuParentTitle(menuTitle);
7576
pluginJarInfo.setBundleName(resourceBundleName);
7677
pluginJarInfo.setControllerType(controllerType);
@@ -89,13 +90,12 @@ public static void parse(File pluginFile, PluginJarInfo pluginJarInfo, ClassLoad
8990
}
9091
}
9192

92-
private static String getTitleFromResourceBundle(
93-
File pluginFile, ClassLoader classLoader, Element pluginElement, String bundleName
94-
) {
93+
private static String getTitleFromResourceBundle(File pluginFile, ClassLoader classLoader, Element pluginElement, String bundleName) {
9594
String titleResourceBundleKey = getChildNodeText(pluginElement, "title");
9695
ClassLoader tmpClassLoader = classLoader == null ? PluginClassLoader.create(pluginFile) : classLoader;
9796
ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleName, Config.defaultLocale, tmpClassLoader);
98-
return resourceBundle.getString(defaultString(titleResourceBundleKey, "Title")); }
97+
return resourceBundle.getString(defaultString(titleResourceBundleKey, "Title"));
98+
}
9999

100100
private static String getChildNodeText(Element element, String childNode) {
101101
return element.selectSingleNode("child::" + childNode).getText();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ public static void openSystemSettings(String title) {
3232

3333
SystemSettingController controller = dialog.show();
3434

35-
dialog
36-
.setButtonHandler(ButtonType.OK, (actionEvent, stage) -> {
35+
dialog.setButtonHandler(ButtonType.OK, (actionEvent, stage) -> {
3736
controller.applySettings();
3837
stage.close();
39-
})
40-
.setButtonHandler(ButtonType.CANCEL, (actionEvent, stage) -> stage.close());
38+
}).setButtonHandler(ButtonType.CANCEL, (actionEvent, stage) -> stage.close());
4139
}
4240
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<?import javafx.scene.layout.VBox?>
1818
<?import javafx.scene.web.WebView?>
1919

20+
<?import javafx.scene.layout.Pane?>
2021
<AnchorPane prefHeight="654.0" prefWidth="1044.0" stylesheets="/com/xwintop/xJavaFxTool/fxmlView/newui/main.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xwintop.xJavaFxTool.controller.IndexController">
2122
<children>
2223
<TabPane fx:id="tabPaneMain" tabClosingPolicy="ALL_TABS" tabMinWidth="45.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
@@ -71,6 +72,7 @@
7172
</padding></Label>
7273
<TextField fx:id="myTextField" promptText="%selectTextField" styleClass="search-text" />
7374
<WebView fx:id="tongjiWebView" prefHeight="1.0" prefWidth="1.0" visible="false" />
75+
<Pane HBox.hgrow="ALWAYS"/>
7476
<Hyperlink onAction="#xwintopLinkOnAction" text="http://gitee.com/xwintop/xJavaFxTool" textFill="#868686" />
7577
</children>
7678
<VBox.margin>

0 commit comments

Comments
 (0)