]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.swt.minidesktop/src/org/argeo/minidesktop/MiniHomePart.java
Disable bind
[lgpl/argeo-commons.git] / swt / org.argeo.swt.minidesktop / src / org / argeo / minidesktop / MiniHomePart.java
1 package org.argeo.minidesktop;
2
3 import java.net.InetAddress;
4 import java.net.InterfaceAddress;
5 import java.net.NetworkInterface;
6 import java.net.SocketException;
7 import java.net.UnknownHostException;
8 import java.util.Enumeration;
9
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.swt.widgets.Group;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.ProgressBar;
19 import org.eclipse.swt.widgets.ToolBar;
20
21 /** A start page displaying network information and resources. */
22 public class MiniHomePart {
23
24 public Control createUiPart(Composite parent, Object context) {
25 parent.setLayout(new GridLayout(2, false));
26 Display display = parent.getDisplay();
27
28 // Apps
29 Group appsGroup = new Group(parent, SWT.NONE);
30 appsGroup.setText("Apps");
31 appsGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 2, 1));
32 ToolBar appsToolBar = new ToolBar(appsGroup, SWT.HORIZONTAL | SWT.FLAT);
33 fillAppsToolBar(appsToolBar);
34
35 // Host
36 Group hostGroup = new Group(parent, SWT.NONE);
37 hostGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
38 hostGroup.setText("Host");
39 hostGroup.setLayout(new GridLayout(2, false));
40 label(hostGroup, "Hostname: ");
41 try {
42 InetAddress defaultAddr = InetAddress.getLocalHost();
43 String hostname = defaultAddr.getHostName();
44 label(hostGroup, hostname);
45 label(hostGroup, "Address: ");
46 label(hostGroup, defaultAddr.getHostAddress());
47 } catch (UnknownHostException e) {
48 label(hostGroup, e.getMessage());
49 }
50
51 Enumeration<NetworkInterface> netInterfaces = null;
52 try {
53 netInterfaces = NetworkInterface.getNetworkInterfaces();
54 } catch (SocketException e) {
55 label(hostGroup, "Interfaces: ");
56 label(hostGroup, e.getMessage());
57 }
58 if (netInterfaces != null)
59 while (netInterfaces.hasMoreElements()) {
60 NetworkInterface netInterface = netInterfaces.nextElement();
61 byte[] hardwareAddress = null;
62 try {
63 hardwareAddress = netInterface.getHardwareAddress();
64 if (hardwareAddress != null) {
65 label(hostGroup, convertHardwareAddress(hardwareAddress));
66 label(hostGroup, netInterface.getName());
67 for (InterfaceAddress addr : netInterface.getInterfaceAddresses()) {
68 label(hostGroup, cleanHostAddress(addr.getAddress().getHostAddress()));
69 label(hostGroup, Short.toString(addr.getNetworkPrefixLength()));
70 }
71 }
72 } catch (SocketException e) {
73 label(hostGroup, e.getMessage());
74 }
75 }
76
77 // Resources
78 Group resGroup = new Group(parent, SWT.NONE);
79 resGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
80 resGroup.setText("Resources");
81 resGroup.setLayout(new GridLayout(3, false));
82
83 Runtime runtime = Runtime.getRuntime();
84
85 String maxMemoryStr = Long.toString(runtime.maxMemory() / (1024 * 1024)) + " MB";
86 label(resGroup, "Max Java memory: ");
87 label(resGroup, maxMemoryStr);
88 label(resGroup, "Java version: " + Runtime.version().toString());
89
90 label(resGroup, "Usable Java memory: ");
91 Label totalMemory = label(resGroup, maxMemoryStr);
92 ProgressBar totalOnMax = new ProgressBar(resGroup, SWT.SMOOTH);
93 totalOnMax.setMaximum(100);
94 label(resGroup, "Used Java memory: ");
95 Label usedMemory = label(resGroup, maxMemoryStr);
96 ProgressBar usedOnTotal = new ProgressBar(resGroup, SWT.SMOOTH);
97 totalOnMax.setMaximum(100);
98 new Thread() {
99 @Override
100 public void run() {
101 while (!totalOnMax.isDisposed()) {
102 display.asyncExec(() -> {
103 if (totalOnMax.isDisposed())
104 return;
105 totalOnMax.setSelection(javaTotalOnMaxPerct(runtime));
106 usedOnTotal.setSelection(javaUsedOnTotalPerct(runtime));
107 totalMemory.setText(Long.toString(runtime.totalMemory() / (1024 * 1024)) + " MB");
108 usedMemory.setText(
109 Long.toString((runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024)) + " MB");
110 });
111 try {
112 Thread.sleep(1000);
113 } catch (InterruptedException e) {
114 return;
115 }
116 }
117 }
118 }.start();
119 return parent;
120 }
121
122 protected void fillAppsToolBar(ToolBar toolBar) {
123
124 }
125
126 protected int javaUsedOnTotalPerct(Runtime runtime) {
127 return Math.toIntExact((runtime.totalMemory() - runtime.freeMemory()) * 100 / runtime.totalMemory());
128 }
129
130 protected int javaTotalOnMaxPerct(Runtime runtime) {
131 return Math.toIntExact((runtime.totalMemory()) * 100 / runtime.maxMemory());
132 }
133
134 protected Label label(Composite parent, String text) {
135 Label label = new Label(parent, SWT.WRAP);
136 label.setText(text);
137 return label;
138 }
139
140 protected String cleanHostAddress(String hostAddress) {
141 // remove % from Ipv6 addresses
142 int index = hostAddress.indexOf('%');
143 if (index > 0)
144 return hostAddress.substring(0, index);
145 else
146 return hostAddress;
147 }
148
149 protected String convertHardwareAddress(byte[] hardwareAddress) {
150 if (hardwareAddress == null)
151 return "";
152 // from https://stackoverflow.com/a/2797498/7878010
153 StringBuilder sb = new StringBuilder(18);
154 for (byte b : hardwareAddress) {
155 if (sb.length() > 0)
156 sb.append(':');
157 sb.append(String.format("%02x", b).toUpperCase());
158 }
159 return sb.toString();
160 }
161 }