diff --git a/src/main/java/com/luoboduner/moo/info/ui/Style.java b/src/main/java/com/luoboduner/moo/info/ui/Style.java index b7a8bd4..4766b8a 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/Style.java +++ b/src/main/java/com/luoboduner/moo/info/ui/Style.java @@ -18,8 +18,18 @@ public class Style { * * @param component */ - public static void emphaticFont(JComponent component) { + public static void emphaticTitleFont(JComponent component) { Font font = MainWindow.getInstance().getMainPanel().getFont(); component.setFont(new Font(font.getName(), Font.BOLD, font.getSize() + 2)); } + + /** + * emphatic font for indicator + * + * @param component + */ + public static void emphaticIndicatorFont(JComponent component) { + Font font = MainWindow.getInstance().getMainPanel().getFont(); + component.setFont(new Font(font.getName(), Font.BOLD, font.getSize() + 8)); + } } diff --git a/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java b/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java index 542dd88..b1ee035 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java +++ b/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java @@ -67,8 +67,8 @@ public class CpuForm { private static void initUi() { CpuForm cpuForm = getInstance(); - Style.emphaticFont(cpuForm.getScuTitleLabel()); - Style.emphaticFont(cpuForm.getPcuTitleLabel()); + Style.emphaticTitleFont(cpuForm.getScuTitleLabel()); + Style.emphaticTitleFont(cpuForm.getPcuTitleLabel()); Dimension d = new Dimension(-1, 100); cpuForm.getScuProgressBar().setMinimumSize(d); diff --git a/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.form b/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.form index 8c9deeb..4977d76 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.form +++ b/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.form @@ -8,7 +8,7 @@ - + @@ -19,7 +19,7 @@ - + @@ -36,7 +36,7 @@ - + @@ -47,6 +47,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.java b/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.java index 73aabc7..815d722 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.java +++ b/src/main/java/com/luoboduner/moo/info/ui/form/NetworkForm.java @@ -4,23 +4,28 @@ import cn.hutool.log.Log; import cn.hutool.log.LogFactory; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; +import com.intellij.uiDesigner.core.Spacer; import com.luoboduner.moo.info.App; +import com.luoboduner.moo.info.ui.Style; +import com.luoboduner.moo.info.ui.UiConsts; import lombok.Getter; import oshi.hardware.NetworkIF; import oshi.software.os.NetworkParams; import oshi.software.os.OperatingSystem; import oshi.util.Constants; +import javax.swing.Timer; import javax.swing.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; import java.awt.*; -import java.util.ArrayList; -import java.util.HashMap; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.text.DecimalFormat; import java.util.List; -import java.util.Map; +import java.util.*; /** * NetworkForm @@ -34,6 +39,8 @@ public class NetworkForm { private JTable interfacesTable; private JPanel paramatersPanel; private JTextPane parametersTextPane; + private JLabel uploadSpeedLabel; + private JLabel downloadSpeedLabel; private static final int INIT_HASH_SIZE = 100; private static final String IP_ADDRESS_SEPARATOR = "; "; @@ -44,6 +51,10 @@ public class NetworkForm { private static NetworkForm networkForm; + private static Long downloadBefore; + private static Long uploadBefore; + private static Long timestampBefore; + public static NetworkForm getInstance() { if (networkForm == null) { networkForm = new NetworkForm(); @@ -59,11 +70,19 @@ public class NetworkForm { } private static void initUi() { + NetworkForm networkForm = getInstance(); + Style.emphaticIndicatorFont(networkForm.getUploadSpeedLabel()); + Style.emphaticIndicatorFont(networkForm.getDownloadSpeedLabel()); } private static void initInfo() { initParameters(); initInterfaces(); + + Timer timer = new Timer(UiConsts.REFRESH_FAST, e -> { + initNetworkSpeed(); + }); + timer.start(); } private static void initInterfaces() { @@ -85,6 +104,62 @@ public class NetworkForm { parametersTextPane.setText(buildParamsText(App.si.getOperatingSystem())); } + private static void initNetworkSpeed() { + + String genericString; + try { + genericString = getDefaultNetworkInteface(); + } catch (Exception e) { + logger.error("NetworkSpeed not supported"); + return; + } + + long downloadNow = 0; + long uploadNow = 0; + long timestampNow = 0; + + List networkIFs = App.si.getHardware().getNetworkIFs(); + +// int i = 0; +// NetworkIF net = networkIFs.get(0); +// try { +// while (!networkIFs.get(i).getName().equals(genericString)) { +// net = networkIFs.get(i); +// i++; +// } +// } catch (ArrayIndexOutOfBoundsException e) { +// logger.error("NetworkSpeed not supported"); +// return; +// } +// net.updateAttributes(); + + for (int i = 0; i < networkIFs.size(); i++) { + NetworkIF net = networkIFs.get(i); + net.updateAttributes(); + downloadNow += net.getBytesRecv(); + uploadNow += net.getBytesSent(); + timestampNow = net.getTimeStamp(); + } + + if (downloadBefore == null) { + downloadBefore = downloadNow; + } + if (uploadBefore == null) { + uploadBefore = uploadNow; + } + if (timestampBefore == null) { + timestampBefore = timestampNow - 1; + } + + NetworkForm networkForm = getInstance(); + networkForm.getUploadSpeedLabel().setText("↓: " + formatSize((downloadNow - downloadBefore) / (timestampNow - timestampBefore)) + "/s"); + networkForm.getDownloadSpeedLabel().setText("↑: " + formatSize((uploadNow - uploadBefore) / (timestampNow - timestampBefore)) + "/s"); + + downloadBefore = downloadNow; + uploadBefore = uploadNow; + timestampBefore = timestampNow; + } + private static String buildParamsText(OperatingSystem os) { NetworkParams params = os.getNetworkParams(); StringBuilder sb = new StringBuilder("Host Name: ").append(params.getHostName()); @@ -153,6 +228,53 @@ public class NetworkForm { } } + /** + * @return + * @throws Exception + */ + private static String getDefaultNetworkInteface() throws Exception { + Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); + + InetAddress localHost = InetAddress.getLocalHost(); + + while (networkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = networkInterfaces.nextElement(); + Enumeration inetAddresses = networkInterface.getInetAddresses(); + while (inetAddresses.hasMoreElements()) { + InetAddress inetAddress = inetAddresses.nextElement(); + if (inetAddress.equals(localHost)) { + return networkInterface.getName(); + } + } + } + return ""; + } + + /** + * this functions converts byte to KB, MB, GB, TB with 3 decimal places + * + * @param size number to convert + * @return coverted number with unit size (ex. 1,234 KB) + */ + private static String formatSize(long size) { + double m = size / 1024.0; + double g = size / 1048576.0; + double t = size / 1073741824.0; + + DecimalFormat dec = new DecimalFormat("0.000"); + String formattedSize; + if (t > 1) { + formattedSize = dec.format(t).concat(" TB"); + } else if (g > 1) { + formattedSize = dec.format(g).concat(" GB"); + } else if (m > 1) { + formattedSize = dec.format(m).concat(" MB"); + } else { + formattedSize = size + " KB"; + } + return formattedSize; + } + { // GUI initializer generated by IntelliJ IDEA GUI Designer // >>> IMPORTANT!! <<< @@ -171,18 +293,35 @@ public class NetworkForm { mainPanel = new JPanel(); mainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); final JPanel panel1 = new JPanel(); - panel1.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1)); + panel1.setLayout(new GridLayoutManager(3, 1, new Insets(10, 10, 10, 10), -1, -1)); mainPanel.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); paramatersPanel = new JPanel(); paramatersPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); - panel1.add(paramatersPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + panel1.add(paramatersPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); parametersTextPane = new JTextPane(); parametersTextPane.setEditable(false); paramatersPanel.add(parametersTextPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); final JScrollPane scrollPane1 = new JScrollPane(); - panel1.add(scrollPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); + panel1.add(scrollPane1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); interfacesTable = new JTable(); scrollPane1.setViewportView(interfacesTable); + final JPanel panel2 = new JPanel(); + panel2.setLayout(new GridLayoutManager(1, 3, new Insets(10, 10, 10, 10), -1, -1)); + panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + final Spacer spacer1 = new Spacer(); + panel2.add(spacer1, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); + final JPanel panel3 = new JPanel(); + panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); + panel2.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + uploadSpeedLabel = new JLabel(); + uploadSpeedLabel.setText(" ↑: --"); + panel3.add(uploadSpeedLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + final JPanel panel4 = new JPanel(); + panel4.setLayout(new GridLayoutManager(1, 1, new Insets(0, 10, 0, 0), -1, -1)); + panel2.add(panel4, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); + downloadSpeedLabel = new JLabel(); + downloadSpeedLabel.setText(" ↓: --"); + panel4.add(downloadSpeedLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); } /**