From 1e9566f95fc5d791b31333d8ab8188a4bf713aa2 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Fri, 24 Apr 2020 10:54:28 +0200 Subject: [PATCH] Add home part to mini desktop. --- .../argeo/minidesktop/MiniDesktopManager.java | 8 + .../org/argeo/minidesktop/MiniHomePart.java | 157 ++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniHomePart.java diff --git a/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniDesktopManager.java b/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniDesktopManager.java index fa66a8e76..6f7b7426c 100644 --- a/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniDesktopManager.java +++ b/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniDesktopManager.java @@ -250,6 +250,14 @@ public class MiniDesktopManager { } protected void initBackground(Composite backgroundArea) { + MiniHomePart homePart = new MiniHomePart() { + + @Override + protected void fillAppsToolBar(ToolBar toolBar) { + createDock(toolBar); + } + }; + homePart.createUiPart(backgroundArea, null); } public void run() { diff --git a/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniHomePart.java b/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniHomePart.java new file mode 100644 index 000000000..b2ee6c1bc --- /dev/null +++ b/rcp/org.argeo.minidesktop/src/org/argeo/minidesktop/MiniHomePart.java @@ -0,0 +1,157 @@ +package org.argeo.minidesktop; + +import java.net.InetAddress; +import java.net.InterfaceAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Enumeration; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.ProgressBar; +import org.eclipse.swt.widgets.ToolBar; + +/** A start page displaying network information and resources. */ +public class MiniHomePart { + + public Control createUiPart(Composite parent, Object context) { + parent.setLayout(new GridLayout(2, false)); + Display display = parent.getDisplay(); + + // Apps + Group appsGroup = new Group(parent, SWT.NONE); + appsGroup.setText("Apps"); + appsGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 2, 1)); + ToolBar appsToolBar = new ToolBar(appsGroup, SWT.HORIZONTAL | SWT.FLAT); + fillAppsToolBar(appsToolBar); + + // Host + Group hostGroup = new Group(parent, SWT.NONE); + hostGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false)); + hostGroup.setText("Host"); + hostGroup.setLayout(new GridLayout(2, false)); + label(hostGroup, "Hostname: "); + try { + InetAddress defaultAddr = InetAddress.getLocalHost(); + String hostname = defaultAddr.getHostName(); + label(hostGroup, hostname); + label(hostGroup, "Address: "); + label(hostGroup, defaultAddr.getHostAddress()); + } catch (UnknownHostException e) { + label(hostGroup, e.getMessage()); + } + + Enumeration netInterfaces = null; + try { + netInterfaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + label(hostGroup, "Interfaces: "); + label(hostGroup, e.getMessage()); + } + if (netInterfaces != null) + while (netInterfaces.hasMoreElements()) { + NetworkInterface netInterface = netInterfaces.nextElement(); + try { + label(hostGroup, convertHardwareAddress(netInterface.getHardwareAddress())); + } catch (SocketException e) { + label(hostGroup, e.getMessage()); + } + label(hostGroup, netInterface.getName()); + for (InterfaceAddress addr : netInterface.getInterfaceAddresses()) { + label(hostGroup, cleanHostAddress(addr.getAddress().getHostAddress())); + label(hostGroup, Short.toString(addr.getNetworkPrefixLength())); + } + } + + // Resources + Group resGroup = new Group(parent, SWT.NONE); + resGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); + resGroup.setText("Resources"); + resGroup.setLayout(new GridLayout(3, false)); + + Runtime runtime = Runtime.getRuntime(); + + String maxMemoryStr = Long.toString(runtime.maxMemory() / (1024 * 1024)) + " MB"; + label(resGroup, "Max Java memory: "); + label(resGroup, maxMemoryStr); + label(resGroup, "Java version: " + Runtime.version().toString()); + + label(resGroup, "Usable Java memory: "); + Label totalMemory = label(resGroup, maxMemoryStr); + ProgressBar totalOnMax = new ProgressBar(resGroup, SWT.SMOOTH); + totalOnMax.setMaximum(100); + label(resGroup, "Used Java memory: "); + Label usedMemory = label(resGroup, maxMemoryStr); + ProgressBar usedOnTotal = new ProgressBar(resGroup, SWT.SMOOTH); + totalOnMax.setMaximum(100); + new Thread() { + @Override + public void run() { + while (!totalOnMax.isDisposed()) { + display.asyncExec(() -> { + if (totalOnMax.isDisposed()) + return; + totalOnMax.setSelection(javaTotalOnMaxPerct(runtime)); + usedOnTotal.setSelection(javaUsedOnTotalPerct(runtime)); + totalMemory.setText(Long.toString(runtime.totalMemory() / (1024 * 1024)) + " MB"); + usedMemory.setText( + Long.toString((runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024)) + " MB"); + }); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + return; + } + } + } + }.start(); + return parent; + } + + protected void fillAppsToolBar(ToolBar toolBar) { + + } + + protected int javaUsedOnTotalPerct(Runtime runtime) { + return Math.toIntExact((runtime.totalMemory() - runtime.freeMemory()) * 100 / runtime.totalMemory()); + } + + protected int javaTotalOnMaxPerct(Runtime runtime) { + return Math.toIntExact((runtime.totalMemory()) * 100 / runtime.maxMemory()); + } + + protected Label label(Composite parent, String text) { + Label label = new Label(parent, SWT.WRAP); + label.setText(text); + return label; + } + + protected String cleanHostAddress(String hostAddress) { + // remove % from Ipv6 addresses + int index = hostAddress.indexOf('%'); + if (index > 0) + return hostAddress.substring(0, index); + else + return hostAddress; + } + + protected String convertHardwareAddress(byte[] hardwareAddress) { + if (hardwareAddress == null) + return ""; + // from https://stackoverflow.com/a/2797498/7878010 + StringBuilder sb = new StringBuilder(18); + for (byte b : hardwareAddress) { + if (sb.length() > 0) + sb.append(':'); + sb.append(String.format("%02x", b).toUpperCase()); + } + return sb.toString(); + } +} -- 2.39.2