Skip to content

Commit de11091

Browse files
feat(core): 支持JDK Tool ActionGroup (#2130)
* feat(core): 支持JDK Tool ActionGroup Close #2078 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * feat(core): 支持JDK Tool ActionGroup Close #2078 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * feat(core): 支持JDK Tool ActionGroup Close #2078 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> --------- Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com>
1 parent ae0f037 commit de11091

File tree

13 files changed

+134
-2
lines changed

13 files changed

+134
-2
lines changed

core/src/main/java/com/tlcsdm/core/javafx/controlsfx/FxActionGroup.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public static ActionGroup view(Action... actions) {
8686
return create(I18nUtils.get("core.menubar.view"), "/com/tlcsdm/core/static/menubar/view.png", actions);
8787
}
8888

89+
/**
90+
* menubar jdkTool
91+
*/
92+
public static ActionGroup jdkTool(Action... actions) {
93+
return create(I18nUtils.get("core.menubar.setting.jdkTool"), "/com/tlcsdm/core/static/icon/java.png",
94+
actions);
95+
}
96+
8997
/**
9098
* menubar language
9199
*/
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2023 unknowIfGuestInDream.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.tlcsdm.core.javafx.controlsfx;
29+
30+
import cn.hutool.log.StaticLog;
31+
import com.tlcsdm.core.javafx.dialog.ExceptionDialog;
32+
import com.tlcsdm.core.javafx.helper.LayoutHelper;
33+
import com.tlcsdm.core.javafx.util.OSUtil;
34+
import org.controlsfx.control.action.Action;
35+
import org.controlsfx.control.action.ActionGroup;
36+
37+
import java.io.File;
38+
import java.io.IOException;
39+
40+
/**
41+
* JDK Tool ActionGroup.
42+
*
43+
* @author unknowIfGuestInDream
44+
*/
45+
public class FxJDKToolActionGroup {
46+
Action jmc;
47+
Action jconsole;
48+
Action jvisualvm;
49+
Action jshell;
50+
private static final String javaHome = System.getProperty("java.home");
51+
private static final String javaHomeEnv = System.getenv("JAVA_HOME");
52+
private final String jmcRelativePath;
53+
private final String jconsoleRelativePath;
54+
private final String jvisualvmRelativePath;
55+
private final String jshellRelativePath;
56+
57+
public FxJDKToolActionGroup() {
58+
boolean isWin = OSUtil.getOS().equals(OSUtil.OS.WINDOWS);
59+
jmcRelativePath = "bin" + File.separator + (isWin ? "jmc.exe" : "jmc");
60+
jconsoleRelativePath = "bin" + File.separator + (isWin ? "jconsole.exe" : "jconsole");
61+
jvisualvmRelativePath = "bin" + File.separator + (isWin ? "jvisualvm.exe" : "jvisualvm");
62+
jshellRelativePath = "bin" + File.separator + (isWin ? "jshell.exe" : "jshell");
63+
64+
jmc = createToolAction("jmc", jmcRelativePath, "/com/tlcsdm/core/static/icon/jmc.png");
65+
jconsole = createToolAction("jConsole", jconsoleRelativePath, "/com/tlcsdm/core/static/icon/java.png");
66+
jvisualvm = createToolAction("jvisualvm", jvisualvmRelativePath, "/com/tlcsdm/core/static/icon/jvisualvm.png");
67+
jshell = createToolAction("jshell", jshellRelativePath, "/com/tlcsdm/core/static/icon/jshell.png");
68+
}
69+
70+
private Action createToolAction(String name, String relativePath, String iconPath) {
71+
Action action = new Action(name, e -> {
72+
File file = findToolExecutable(name, relativePath);
73+
if (file != null) {
74+
runProgram(file.getAbsolutePath());
75+
}
76+
});
77+
action.setGraphic(LayoutHelper.iconView(FxJDKToolActionGroup.class.getResource(iconPath)));
78+
79+
// 检查工具是否存在
80+
boolean toolExists = findToolExecutable(name, relativePath) != null;
81+
if (!toolExists) {
82+
action.setDisabled(true);
83+
}
84+
85+
return action;
86+
}
87+
88+
private File findToolExecutable(String toolName, String relativePath) {
89+
File file = new File(javaHome, relativePath);
90+
if (file.exists()) {
91+
return file;
92+
}
93+
94+
file = new File(javaHomeEnv, relativePath);
95+
if (file.exists()) {
96+
return file;
97+
}
98+
99+
StaticLog.warn("Not found " + toolName + ".");
100+
return null;
101+
}
102+
103+
private void runProgram(String filePath) {
104+
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "start", filePath);
105+
try {
106+
builder.start();
107+
} catch (IOException ex) {
108+
new ExceptionDialog(ex).show();
109+
}
110+
}
111+
112+
public ActionGroup create() {
113+
return FxActionGroup.jdkTool(jmc, jconsole, jvisualvm, jshell);
114+
}
115+
}

core/src/main/resources/com/tlcsdm/core/i18n/messages_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ core.menubar.file.restart=Restart
6565
core.menubar.file.exit=Exit
6666
core.menubar.setting=Setting
6767
core.menubar.setting.language=Language
68+
core.menubar.setting.jdkTool=JDK Tool
6869
core.menubar.setting.systemSetting=System Setting
6970
core.menubar.setting.systemProperties=System Properties
7071
core.menubar.setting.colorPicker=Color Extractor

core/src/main/resources/com/tlcsdm/core/i18n/messages_ja.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ core.menubar.file.restart=\u518D\u8D77\u52D5
6565
core.menubar.file.exit=\u7D42\u4E86\u3059\u308B
6666
core.menubar.setting=\u8A2D\u5B9A
6767
core.menubar.setting.language=\u8A00\u8A9E
68+
core.menubar.setting.jdkTool=JDK\u30C4\u30FC\u30EB
6869
core.menubar.setting.systemSetting=\u30B7\u30B9\u30C6\u30E0\u8A2D\u5B9A
6970
core.menubar.setting.systemProperties=\u30B7\u30B9\u30C6\u30E0\u30D7\u30ED\u30D1\u30C6\u30A3
7071
core.menubar.setting.colorPicker=\u30AB\u30E9\u30FC\u30A8\u30AF\u30B9\u30C8\u30E9\u30AF\u30BF

core/src/main/resources/com/tlcsdm/core/i18n/messages_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ core.menubar.file.restart=\u91CD\u542F
6565
core.menubar.file.exit=\u9000\u51FA
6666
core.menubar.setting=\u8BBE\u7F6E
6767
core.menubar.setting.language=\u8BED\u8A00
68+
core.menubar.setting.jdkTool=JDK \u5DE5\u5177
6869
core.menubar.setting.systemSetting=\u7CFB\u7EDF\u8BBE\u7F6E
6970
core.menubar.setting.systemProperties=\u7CFB\u7EDF\u5C5E\u6027
7071
core.menubar.setting.colorPicker=\u989C\u8272\u63D0\u53D6\u5668
1.04 KB
Loading
3.21 KB
Loading
199 Bytes
Loading
280 Bytes
Loading
526 Bytes
Loading

0 commit comments

Comments
 (0)