package org.kne.cloud.network.klalb; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.SwingConstants; import javax.swing.border.LineBorder; import org.kne.cloud.network.monitor.MonitorData; import java.awt.Font; import java.util.Objects; public class TPanel extends JPanel { private JLabel tname; private JLabel targ; private KLALBRemoteLine tunnel; @Override public int hashCode() { return Objects.hash(tunnel); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; TPanel other = (TPanel) obj; return Objects.equals(tunnel, other.tunnel); } public JLabel getTname() { return tname; } public JLabel getTarg() { return targ; } /** * @wbp.parser.constructor */ public TPanel(KLALBRemoteLine t) { this.tunnel = t; setOpaque(false); setLayout(new BorderLayout(0, 0)); tname = new JLabel(); tname.setText(t.getMonitor().getName()); tname.setFont(new Font("宋体", Font.BOLD, 16)); add(tname, BorderLayout.WEST); targ = new JLabel(); targ.setFont(new Font("宋体", Font.PLAIN, 13)); targ.setHorizontalAlignment(SwingConstants.TRAILING); targ.setForeground(Color.YELLOW); add(targ, BorderLayout.CENTER); setBorder(new LineBorder(Color.BLACK)); setSize(650, 25); setPreferredSize(getSize()); } private volatile long up, down, ups, downs; private volatile long delay=Long.MAX_VALUE; public long getDelay() { return delay; } private void updateText() { String dl; if (delay == Long.MIN_VALUE) dl = "??"; else dl = Long.toString(delay / 1000000L); targ.setText(bytesUnit(up) + "\u2191 " + bytesUnit(down) + "\u2193 " + bytesUnit(ups) + "/s\u2191 " + bytesUnit(downs) + "/s\u2193 " + dl + "ms"); } public KLALBRemoteLine getTunnel() { return tunnel; } private String bytesUnit(long v) { if (v >= 1024L * 1024 * 1024 * 1024 * 1024) { return String.format("%.1f", v / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0)) + "PB"; } else if (v >= 1024L * 1024 * 1024 * 1024) { return String.format("%.1f", v / (1024.0 * 1024.0 * 1024.0 * 1024.0)) + "TB"; } else if (v >= 1024L * 1024 * 1024) { return String.format("%.1f", v / (1024.0 * 1024.0 * 1024.0)) + "GB"; } else if (v >= 1024L * 1024) { return String.format("%.1f", v / (1024.0 * 1024.0)) + "MB"; } else if (v >= 1024L) { return String.format("%.1f", v / (1024.0)) + "KB"; } else { return v + "B"; } } public void updateTraffic() { switch (tunnel.getMonitor().getState()) { case MonitorData.OFFLINE: tname.setForeground(Color.RED); break; case MonitorData.CONNECTING: tname.setForeground(Color.YELLOW); break; case MonitorData.ONLINE: tname.setForeground(Color.GREEN); break; default: break; } up = tunnel.getMonitor().getOutTraffic(); down = tunnel.getMonitor().getInTraffic(); this.ups = tunnel.getMonitor().getOutSpeed(); this.downs = tunnel.getMonitor().getInSpeed(); this.delay = tunnel.getMonitor().getLatency(); //System.out.println(delay); updateText(); } }