1 package org
.argeo
.minidesktop
;
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
;
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
;
21 /** A start page displaying network information and resources. */
22 public class MiniHomePart
{
24 public Control
createUiPart(Composite parent
, Object context
) {
25 parent
.setLayout(new GridLayout(2, false));
26 Display display
= parent
.getDisplay();
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
);
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: ");
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());
51 Enumeration
<NetworkInterface
> netInterfaces
= null;
53 netInterfaces
= NetworkInterface
.getNetworkInterfaces();
54 } catch (SocketException e
) {
55 label(hostGroup
, "Interfaces: ");
56 label(hostGroup
, e
.getMessage());
58 if (netInterfaces
!= null)
59 while (netInterfaces
.hasMoreElements()) {
60 NetworkInterface netInterface
= netInterfaces
.nextElement();
61 byte[] hardwareAddress
= null;
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()));
72 } catch (SocketException e
) {
73 label(hostGroup
, e
.getMessage());
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));
83 Runtime runtime
= Runtime
.getRuntime();
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());
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);
101 while (!totalOnMax
.isDisposed()) {
102 display
.asyncExec(() -> {
103 if (totalOnMax
.isDisposed())
105 totalOnMax
.setSelection(javaTotalOnMaxPerct(runtime
));
106 usedOnTotal
.setSelection(javaUsedOnTotalPerct(runtime
));
107 totalMemory
.setText(Long
.toString(runtime
.totalMemory() / (1024 * 1024)) + " MB");
109 Long
.toString((runtime
.totalMemory() - runtime
.freeMemory()) / (1024 * 1024)) + " MB");
113 } catch (InterruptedException e
) {
122 protected void fillAppsToolBar(ToolBar toolBar
) {
126 protected int javaUsedOnTotalPerct(Runtime runtime
) {
127 return Math
.toIntExact((runtime
.totalMemory() - runtime
.freeMemory()) * 100 / runtime
.totalMemory());
130 protected int javaTotalOnMaxPerct(Runtime runtime
) {
131 return Math
.toIntExact((runtime
.totalMemory()) * 100 / runtime
.maxMemory());
134 protected Label
label(Composite parent
, String text
) {
135 Label label
= new Label(parent
, SWT
.WRAP
);
140 protected String
cleanHostAddress(String hostAddress
) {
141 // remove % from Ipv6 addresses
142 int index
= hostAddress
.indexOf('%');
144 return hostAddress
.substring(0, index
);
149 protected String
convertHardwareAddress(byte[] hardwareAddress
) {
150 if (hardwareAddress
== null)
152 // from https://stackoverflow.com/a/2797498/7878010
153 StringBuilder sb
= new StringBuilder(18);
154 for (byte b
: hardwareAddress
) {
157 sb
.append(String
.format("%02x", b
).toUpperCase());
159 return sb
.toString();