|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 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 | +package com.tlcsdm.core.javafx.dialog; |
| 28 | + |
| 29 | +import com.tlcsdm.core.javafx.FxApp; |
| 30 | +import com.tlcsdm.core.javafx.richtext.InformationArea; |
| 31 | +import com.tlcsdm.core.javafx.richtext.PropertiesArea; |
| 32 | +import com.tlcsdm.core.util.I18nUtils; |
| 33 | +import javafx.scene.control.Tab; |
| 34 | +import javafx.scene.control.TabPane; |
| 35 | +import javafx.scene.layout.Priority; |
| 36 | +import javafx.scene.layout.VBox; |
| 37 | +import org.fxmisc.flowless.VirtualizedScrollPane; |
| 38 | +import org.fxmisc.richtext.InlineCssTextArea; |
| 39 | +import org.fxmisc.richtext.LineNumberFactory; |
| 40 | + |
| 41 | +import java.lang.management.ManagementFactory; |
| 42 | +import java.lang.management.RuntimeMXBean; |
| 43 | +import java.nio.charset.Charset; |
| 44 | +import java.time.Duration; |
| 45 | +import java.time.Instant; |
| 46 | +import java.time.LocalDateTime; |
| 47 | +import java.time.ZoneId; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Map; |
| 50 | + |
| 51 | +/** |
| 52 | + * Property弹窗 |
| 53 | + * |
| 54 | + * @author unknowIfGuestInDream |
| 55 | + */ |
| 56 | +public class PropertiesDialog { |
| 57 | + |
| 58 | + private PropertiesDialog() { |
| 59 | + } |
| 60 | + |
| 61 | + public static void openPropertiesDialog() { |
| 62 | + VBox vbox = new VBox(); |
| 63 | + TabPane tabPane = new TabPane(); |
| 64 | + tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); |
| 65 | + tabPane.getStyleClass().add(TabPane.STYLE_CLASS_FLOATING); |
| 66 | + |
| 67 | + InformationArea overviewArea = new InformationArea(); |
| 68 | + overviewArea.setEditable(false); |
| 69 | + overviewArea.setStyle("-fx-font-size: 14;-fx-padding: 5 0 0 5;"); |
| 70 | + buildOverviewArea(overviewArea); |
| 71 | + VirtualizedScrollPane<InformationArea> overviewPane = new VirtualizedScrollPane<>(overviewArea); |
| 72 | + |
| 73 | + InlineCssTextArea jvmArea = new InlineCssTextArea(); |
| 74 | + jvmArea.setEditable(false); |
| 75 | + jvmArea.setStyle("-fx-font-size: 14;-fx-padding: 5 0 0 5;"); |
| 76 | + jvmArea.setParagraphGraphicFactory(LineNumberFactory.get(jvmArea)); |
| 77 | + buildJvmArea(jvmArea); |
| 78 | + VirtualizedScrollPane<InlineCssTextArea> jvmPane = new VirtualizedScrollPane<>(jvmArea); |
| 79 | + |
| 80 | + PropertiesArea sysArea = new PropertiesArea(); |
| 81 | + sysArea.setEditable(false); |
| 82 | + buildSysArea(sysArea); |
| 83 | + VirtualizedScrollPane<PropertiesArea> sysPane = new VirtualizedScrollPane<>(sysArea); |
| 84 | + |
| 85 | + Tab overviewTab = new Tab("Overview"); |
| 86 | + overviewTab.setContent(overviewPane); |
| 87 | + |
| 88 | + Tab jvmTab = new Tab("JVM"); |
| 89 | + jvmTab.setContent(jvmPane); |
| 90 | + |
| 91 | + Tab sysTab = new Tab("System"); |
| 92 | + sysTab.setContent(sysPane); |
| 93 | + |
| 94 | + tabPane.getTabs().setAll(overviewTab, jvmTab, sysTab); |
| 95 | + |
| 96 | + vbox.getChildren().addAll(tabPane); |
| 97 | + VBox.setVgrow(tabPane, Priority.ALWAYS); |
| 98 | + |
| 99 | + FxDialog<VBox> dialog = new FxDialog<VBox>().setTitle( |
| 100 | + I18nUtils.get("core.menubar.setting.systemProperties")).setOwner(FxApp.primaryStage) |
| 101 | + .setPrefSize(680, 540).setResizable(true).setBody(vbox) |
| 102 | + .setButtonTypes(FxButtonType.CLOSE); |
| 103 | + dialog.setButtonHandler(FxButtonType.CLOSE, (e, s) -> s.close()); |
| 104 | + dialog.show(); |
| 105 | + } |
| 106 | + |
| 107 | + private static void buildOverviewArea(InformationArea area) { |
| 108 | + // get name representing the running Java virtual machine. |
| 109 | + String name = ManagementFactory.getRuntimeMXBean().getName(); |
| 110 | + // get pid |
| 111 | + String pid = name.split("@")[0]; |
| 112 | + // 基本JVM信息 |
| 113 | + String jvmName = ManagementFactory.getRuntimeMXBean().getVmName(); |
| 114 | + String jvmVersion = System.getProperty("java.vm.version"); |
| 115 | + String jvmVendor = System.getProperty("java.vm.vendor"); |
| 116 | + String javaHome = System.getProperty("java.home"); |
| 117 | + String fileEncoding = Charset.defaultCharset().displayName(); |
| 118 | + String workingDir = System.getProperty("user.dir"); |
| 119 | + // 运行时信息 |
| 120 | + RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); |
| 121 | + // 获取JVM启动时间(毫秒) |
| 122 | + long startTime = runtimeMXBean.getStartTime(); |
| 123 | + LocalDateTime startDateTime = LocalDateTime.ofInstant( |
| 124 | + Instant.ofEpochMilli(startTime), ZoneId.systemDefault()); |
| 125 | + // 获取JVM运行时间(毫秒) |
| 126 | + long uptime = runtimeMXBean.getUptime(); |
| 127 | + Duration duration = Duration.ofMillis(uptime); |
| 128 | + String runTime = "%d Day %d Hour %d Min %d s".formatted(duration.toDays(), duration.toHours() % 24, |
| 129 | + duration.toMinutes() % 60, duration.getSeconds() % 60); |
| 130 | + |
| 131 | + area.appendText("PID: " + pid); |
| 132 | + area.appendText(System.lineSeparator()); |
| 133 | + area.appendText("JVM: " + jvmName + " (" + System.getProperty("java.vm.info") + ")"); |
| 134 | + area.appendText(System.lineSeparator()); |
| 135 | + area.appendText("Java: " + jvmVendor + " " + jvmVersion); |
| 136 | + area.appendText(System.lineSeparator()); |
| 137 | + area.appendText("Java Home: " + javaHome); |
| 138 | + area.appendText(System.lineSeparator()); |
| 139 | + area.appendText("JVM StartUpTime: " + startDateTime); |
| 140 | + area.appendText(System.lineSeparator()); |
| 141 | + area.appendText("JVM Run time: " + runTime); |
| 142 | + area.appendText(System.lineSeparator()); |
| 143 | + area.appendText("Program Path: " + workingDir); |
| 144 | + area.appendText(System.lineSeparator()); |
| 145 | + area.appendText("Encoding: " + fileEncoding); |
| 146 | + } |
| 147 | + |
| 148 | + private static void buildJvmArea(InlineCssTextArea area) { |
| 149 | + RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); |
| 150 | + List<String> arguments = runtimeMxBean.getInputArguments(); |
| 151 | + for (String arg : arguments) { |
| 152 | + area.appendText(arg); |
| 153 | + area.appendText(System.lineSeparator()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static void buildSysArea(PropertiesArea area) { |
| 158 | + Map<String, String> env = System.getenv(); |
| 159 | + env.forEach((key, value) -> { |
| 160 | + area.appendText(key + " = " + value); |
| 161 | + area.appendText(System.lineSeparator()); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments