Skip to content
Snippets Groups Projects
Unverified Commit 4c31e8a6 authored by David Bohn's avatar David Bohn
Browse files

Refactored setup dialog into own class

parent 5e0aeffc
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
......@@ -7,26 +7,18 @@ import akka.util.Timeout;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import fucoin.setup.SelectableNetworkInterface;
import fucoin.setup.SetupDialogPanel;
import fucoin.wallet.WalletImpl;
import scala.concurrent.Await;
import scala.concurrent.duration.FiniteDuration;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.io.File;
import java.net.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class MainRemote {
private static JTextField walletNameField = new JTextField(10);
private static JTextField pathField = new JTextField(10);
private static JComboBox<SelectableNetworkInterface> hostnameField = new JComboBox<>();
public static void main(String[] args) throws InterruptedException {
String hostname = "127.0.0.1";
......@@ -50,7 +42,8 @@ public class MainRemote {
//Init System Actor System
ActorSystem system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config));
JPanel dialogPanel = createDialogPanel(hostname);
//JPanel dialogPanel = createDialogPanel(hostname);
SetupDialogPanel dialogPanel = new SetupDialogPanel(hostname);
Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
ActorRef preknownNeighbour = null;
......@@ -70,7 +63,7 @@ public class MainRemote {
return;
}
SelectableNetworkInterface selectedHostname = (SelectableNetworkInterface) hostnameField.getSelectedItem();
SelectableNetworkInterface selectedHostname = dialogPanel.getNetworkInterface();
if (!selectedHostname.getHostName().equals(hostname)) {
if (system != null) {
......@@ -85,8 +78,8 @@ public class MainRemote {
system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config));
}
walletName = walletNameField.getText();
path = pathField.getText();
walletName = dialogPanel.getWalletName();
path = dialogPanel.getAddressOfNeighbour();
// check input
if (path.equals("") || walletName.equals("")) {
......@@ -107,89 +100,4 @@ public class MainRemote {
system.actorOf(WalletImpl.props(preknownNeighbour, walletName), walletName);
}
/**
* Create the setup UI for the remote application
* This provides an input for a wallet name, the initially known neighbour and a combo box to select
* the used network interface for the client.
*
* @param defaultHostname This is the default hostname that should be selected.
*/
private static JPanel createDialogPanel(String defaultHostname) {
JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new GridLayout(3, 1));
JLabel walletLabel = new JLabel("Pick your wallet name: ", SwingConstants.LEFT);
dialogPanel.add(walletLabel);
dialogPanel.add(walletNameField);
dialogPanel.add(new JLabel("Enter a neighbour node address: ", SwingConstants.LEFT));
dialogPanel.add(pathField);
dialogPanel.add(new JLabel("Select your reachable IP address: ", SwingConstants.LEFT));
selectWalletNameField();
List<SelectableNetworkInterface> interfaces = fetchNetworkInterfaces();
for (SelectableNetworkInterface netint : interfaces) {
hostnameField.addItem(netint);
}
hostnameField.setSelectedItem(new SelectableNetworkInterface(defaultHostname, "default"));
dialogPanel.add(hostnameField);
return dialogPanel;
}
/**
* This method makes sure, that the Wallet name field is selected on display of the dialog
*/
private static void selectWalletNameField() {
walletNameField.addAncestorListener(new AncestorListener() {
@Override
public void ancestorAdded(AncestorEvent event) {
final AncestorListener al= this;
SwingUtilities.invokeLater(() -> {
JComponent component = event.getComponent();
component.requestFocusInWindow();
component.removeAncestorListener( al );
});
}
@Override
public void ancestorRemoved(AncestorEvent event) {
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
});
}
/**
* Grab a list of all network interfaces and the associated IP addresses to prevent some strange behaviours of
* the <code>InetAddress.getLocalHost().getHostAddress();</code> call
*
* @return The list contains SelectableNetworkInterfaces, that contain the interface name and the associated IP address
*/
private static List<SelectableNetworkInterface> fetchNetworkInterfaces() {
List<SelectableNetworkInterface> map = new ArrayList<>();
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface networkInterface : Collections.list(nets)) {
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
// To ease the setup, currently only IPv4 addresses are supported
Optional<InetAddress> picked = Collections.list(inetAddresses).stream().filter(inetAddress -> !(inetAddress instanceof Inet6Address)).findFirst();
if (picked.isPresent()) {
String hostAddress = picked.get().getHostAddress();
map.add(new SelectableNetworkInterface(hostAddress, networkInterface.getDisplayName()));
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
}
package fucoin.setup;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
public class NetworkInterfaceReader {
/**
* Grab a list of all network interfaces and the associated IP addresses to prevent some strange behaviours of
* the <code>InetAddress.getLocalHost().getHostAddress();</code> call
*
* @return The list contains SelectableNetworkInterfaces, that contain the interface name and the associated IP address
*/
public static List<SelectableNetworkInterface> fetchNetworkInterfaces() {
List<SelectableNetworkInterface> map = new ArrayList<>();
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface networkInterface : Collections.list(nets)) {
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
// To ease the setup, currently only IPv4 addresses are supported
Optional<InetAddress> picked = Collections.list(inetAddresses).stream().filter(inetAddress -> !(inetAddress instanceof Inet6Address)).findFirst();
if (picked.isPresent()) {
String hostAddress = picked.get().getHostAddress();
map.add(new SelectableNetworkInterface(hostAddress, networkInterface.getDisplayName()));
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
}
package fucoin.setup;
/**
* @author davidbohn
*/
public class SelectableNetworkInterface {
String hostName;
......
package fucoin.setup;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.util.List;
public class SetupDialogPanel extends JPanel {
private JTextField walletNameField = new JTextField(10);
private JTextField pathField = new JTextField(10);
private JComboBox<SelectableNetworkInterface> hostnameField = new JComboBox<>();
public SetupDialogPanel(String defaultHostname) {
super();
createComponents(defaultHostname);
}
private void createComponents(String defaultHostname) {
this.setLayout(new GridLayout(3, 1));
this.add(new JLabel("Pick your wallet name: ", SwingConstants.LEFT));
this.add(walletNameField);
this.add(new JLabel("Enter a neighbour node address: ", SwingConstants.LEFT));
this.add(pathField);
this.add(new JLabel("Select your reachable IP address: ", SwingConstants.LEFT));
selectWalletNameField();
List<SelectableNetworkInterface> interfaces = NetworkInterfaceReader.fetchNetworkInterfaces();
for (SelectableNetworkInterface netint : interfaces) {
hostnameField.addItem(netint);
}
hostnameField.setSelectedItem(new SelectableNetworkInterface(defaultHostname, "default"));
this.add(hostnameField);
}
/**
* This method makes sure, that the Wallet name field is selected on display of the dialog
*/
private void selectWalletNameField() {
walletNameField.addAncestorListener(new AncestorListener() {
@Override
public void ancestorAdded(AncestorEvent event) {
final AncestorListener al= this;
SwingUtilities.invokeLater(() -> {
JComponent component = event.getComponent();
component.requestFocusInWindow();
component.removeAncestorListener( al );
});
}
@Override
public void ancestorRemoved(AncestorEvent event) {
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
});
}
public String getWalletName() {
return walletNameField.getText();
}
public String getAddressOfNeighbour() {
return pathField.getText();
}
public SelectableNetworkInterface getNetworkInterface() {
return (SelectableNetworkInterface) hostnameField.getSelectedItem();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment