|
| 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 | +} |
0 commit comments