1
2
3
4
5 package oshi.demo.gui;
6
7 import java.awt.Component;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import javax.swing.BorderFactory;
15 import javax.swing.BoxLayout;
16 import javax.swing.JLabel;
17 import javax.swing.JScrollPane;
18 import javax.swing.JTable;
19 import javax.swing.JTextArea;
20 import javax.swing.ScrollPaneConstants;
21 import javax.swing.table.DefaultTableModel;
22 import javax.swing.table.TableColumn;
23 import javax.swing.table.TableColumnModel;
24 import javax.swing.table.TableModel;
25
26 import oshi.SystemInfo;
27 import oshi.hardware.NetworkIF;
28 import oshi.software.os.NetworkParams;
29 import oshi.software.os.OperatingSystem;
30 import oshi.util.Constants;
31
32
33
34
35 public class InterfacePanel extends OshiJPanel {
36
37 private static final long serialVersionUID = 1L;
38
39 private static final int INIT_HASH_SIZE = 100;
40 private static final String IP_ADDRESS_SEPARATOR = "; ";
41
42 private static final String PARAMS = "Network Parameters";
43 private static final String INTERFACES = "Network Interfaces";
44 private static final String[] COLUMNS = { "Name", "Index", "Speed", "IPv4 Address", "IPv6 address", "MAC address" };
45 private static final double[] COLUMN_WIDTH_PERCENT = { 0.02, 0.02, 0.1, 0.25, 0.45, 0.15 };
46
47 public InterfacePanel(SystemInfo si) {
48 super();
49 init(si);
50 }
51
52 private void init(SystemInfo si) {
53 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
54 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
55
56 JLabel paramsLabel = new JLabel(PARAMS);
57 paramsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
58 add(paramsLabel);
59
60 JTextArea paramsArea = new JTextArea(0, 0);
61 paramsArea.setText(buildParamsText(si.getOperatingSystem()));
62 add(paramsArea);
63
64 JLabel interfaceLabel = new JLabel(INTERFACES);
65 interfaceLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
66 add(interfaceLabel);
67
68 List<NetworkIF> networkIfList = si.getHardware().getNetworkIFs(true);
69
70 TableModel model = new DefaultTableModel(parseInterfaces(networkIfList), COLUMNS);
71 JTable intfTable = new JTable(model);
72 JScrollPane scrollV = new JScrollPane(intfTable);
73 scrollV.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
74 resizeColumns(intfTable.getColumnModel());
75 add(scrollV);
76 }
77
78 private static String buildParamsText(OperatingSystem os) {
79 NetworkParams params = os.getNetworkParams();
80 StringBuilder sb = new StringBuilder("Host Name: ").append(params.getHostName());
81 if (!params.getDomainName().isEmpty()) {
82 sb.append("\nDomain Name: ").append(params.getDomainName());
83 }
84 sb.append("\nIPv4 Default Gateway: ").append(params.getIpv4DefaultGateway());
85 if (!params.getIpv6DefaultGateway().isEmpty()) {
86 sb.append("\nIPv6 Default Gateway: ").append(params.getIpv6DefaultGateway());
87 }
88 sb.append("\nDNS Servers: ").append(getIPAddressesString(params.getDnsServers()));
89 return sb.toString();
90 }
91
92 private static Object[][] parseInterfaces(List<NetworkIF> list) {
93 Map<NetworkIF, Integer> intfSortValueMap = new HashMap<>(INIT_HASH_SIZE);
94 for (NetworkIF intf : list) {
95 intfSortValueMap.put(intf, intf.getIndex());
96 }
97 List<Entry<NetworkIF, Integer>> intfList = new ArrayList<>(intfSortValueMap.entrySet());
98 intfList.sort(Entry.comparingByValue());
99
100 int i = 0;
101 Object[][] intfArr = new Object[intfList.size()][COLUMNS.length];
102
103 for (Entry<NetworkIF, Integer> e : intfList) {
104 NetworkIF intf = e.getKey();
105
106 intfArr[i][0] = intf.getName();
107 intfArr[i][1] = intf.getIndex();
108 intfArr[i][2] = intf.getSpeed();
109 intfArr[i][3] = getIPAddressesString(intf.getIPv4addr());
110 intfArr[i][4] = getIPAddressesString(intf.getIPv6addr());
111 intfArr[i][5] = Constants.UNKNOWN.equals(intf.getMacaddr()) ? "" : intf.getMacaddr();
112
113 i++;
114 }
115
116 return intfArr;
117 }
118
119 private static void resizeColumns(TableColumnModel tableColumnModel) {
120 TableColumn column;
121 int tW = tableColumnModel.getTotalColumnWidth();
122 int cantCols = tableColumnModel.getColumnCount();
123 for (int i = 0; i < cantCols; i++) {
124 column = tableColumnModel.getColumn(i);
125 int pWidth = (int) Math.round(COLUMN_WIDTH_PERCENT[i] * tW);
126 column.setPreferredWidth(pWidth);
127 }
128 }
129
130 private static String getIPAddressesString(String[] ipAddressArr) {
131 StringBuilder sb = new StringBuilder();
132 boolean first = true;
133
134 for (String ipAddress : ipAddressArr) {
135 if (first) {
136 first = false;
137 } else {
138 sb.append(IP_ADDRESS_SEPARATOR);
139 }
140 sb.append(ipAddress);
141 }
142
143 return sb.toString();
144 }
145 }