diff --git a/src/main/java/fucoin/MainRemote.java b/src/main/java/fucoin/MainRemote.java index e148dc8bed2c26f584a1ed41f05d9013c41150ce..9eea194e5b35dda55d3c35d2bb24db54218b09d3 100644 --- a/src/main/java/fucoin/MainRemote.java +++ b/src/main/java/fucoin/MainRemote.java @@ -3,29 +3,22 @@ package fucoin; import akka.actor.ActorRef; import akka.actor.ActorSelection; import akka.actor.ActorSystem; -import akka.japi.Pair; 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 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"; @@ -49,13 +42,14 @@ 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; String walletName = null; - String path = null; + String path; while (preknownNeighbour == null) { @@ -69,7 +63,7 @@ public class MainRemote { return; } - SelectableNetworkInterface selectedHostname = (SelectableNetworkInterface) hostnameField.getSelectedItem(); + SelectableNetworkInterface selectedHostname = dialogPanel.getNetworkInterface(); if (!selectedHostname.getHostName().equals(hostname)) { if (system != null) { @@ -84,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("")) { @@ -106,47 +100,4 @@ public class MainRemote { system.actorOf(WalletImpl.props(preknownNeighbour, walletName), walletName); } - - 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)); - - List<SelectableNetworkInterface> interfaces = fetchNetworkInterfaces(); - - for (SelectableNetworkInterface netint : interfaces) { - hostnameField.addItem(netint); - } - - hostnameField.setSelectedItem(new SelectableNetworkInterface(defaultHostname, "default")); - dialogPanel.add(hostnameField); - return dialogPanel; - } - - 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; - } } diff --git a/src/main/java/fucoin/gui/WalletGuiControl.java b/src/main/java/fucoin/gui/WalletGuiControl.java index 480ba08436f412e6e4c73f4016c49fb1d4896fcc..90bdbdb4e43b6d8fe346d0ebce46996304200696 100644 --- a/src/main/java/fucoin/gui/WalletGuiControl.java +++ b/src/main/java/fucoin/gui/WalletGuiControl.java @@ -13,4 +13,10 @@ public interface WalletGuiControl { void addTransactionLogMessageSuccess(String message); void addTransactionLogMessageFail(String message); + + /** + * Tell the GUI, that the wallet is a remote wallet. + */ + void setRemote(); + } diff --git a/src/main/java/fucoin/gui/WalletGuiControlImpl.java b/src/main/java/fucoin/gui/WalletGuiControlImpl.java index 2e93631ea53ef95e5069ec59a1236695b7e6b88c..2a2e008f3ba57d8e55cf49c6597acba091220366 100644 --- a/src/main/java/fucoin/gui/WalletGuiControlImpl.java +++ b/src/main/java/fucoin/gui/WalletGuiControlImpl.java @@ -40,20 +40,22 @@ public class WalletGuiControlImpl implements WalletGuiControl { // Row 1 JPanel row1 = new JPanel(); row1.setLayout(new BoxLayout(row1, BoxLayout.PAGE_AXIS)); - + txtMyName.setHorizontalAlignment(JTextField.CENTER); txtMyName.setBorder(BorderFactory.createEmptyBorder()); txtMyName.setFont(txtMyName.getFont().deriveFont(18f).deriveFont(Font.BOLD)); txtMyName.setEditable(false); txtMyName.setForeground(Color.WHITE); - txtMyName.setBackground(new Color(123,94,167)); + txtMyName.setText(wallet.getName()); txtMyAddress.setHorizontalAlignment(JTextField.CENTER); txtMyAddress.setBorder(BorderFactory.createEmptyBorder()); txtMyAddress.setEditable(false); txtMyAddress.setForeground(Color.WHITE); - txtMyAddress.setBackground(new Color(137,112,176)); + + //setTint(new Color(123,94,167)); + setTint(new Color(54, 135, 56)); row1.add(txtMyName); row1.add(txtMyAddress); @@ -132,8 +134,15 @@ public class WalletGuiControlImpl implements WalletGuiControl { }); + txtLog.setCellRenderer(new LogCellRenderer()); + // Disable currently unused controls that might be useful in the future + addNewButton.setEnabled(false); + btnStore.setEnabled(false); + btnSearch.setEnabled(false); + sendToNewEdt.setEditable(false); + btnExit.addActionListener(e -> window.dispose()); window.addWindowListener(new WindowAdapter() { @@ -155,6 +164,24 @@ public class WalletGuiControlImpl implements WalletGuiControl { }); } + /** + * Set the color of the top section + * + * @param color The color used for the upper row. The color for the bottom row is calculated by lightening up the provided color + */ + private void setTint(Color color) { + txtMyName.setBackground(color); + + // Make the secondary color roughly 15% lighter + float amount = 0.15f; + + int red = (int) ((color.getRed() * (1 - amount) / 255 + amount) * 255); + int green = (int) ((color.getGreen() * (1 - amount) / 255 + amount) * 255); + int blue = (int) ((color.getBlue() * (1 - amount) / 255 + amount) * 255); + + txtMyAddress.setBackground(new Color(red, green, blue)); + } + @Override public void setAddress(String address) { txtMyAddress.setText(address); @@ -186,6 +213,11 @@ public class WalletGuiControlImpl implements WalletGuiControl { log(new LogMessage(message, LogMessage.Context.TRANSACTION_FAIL)); } + @Override + public void setRemote() { + setTint(new Color(45, 94, 167)); + } + private void log(LogMessage logMessage) { SwingUtilities.invokeLater(() -> { log.addElement(logMessage); diff --git a/src/main/java/fucoin/setup/NetworkInterfaceReader.java b/src/main/java/fucoin/setup/NetworkInterfaceReader.java new file mode 100644 index 0000000000000000000000000000000000000000..2aa5db414cfdd547d2b722a1f7e6520b5b83a75b --- /dev/null +++ b/src/main/java/fucoin/setup/NetworkInterfaceReader.java @@ -0,0 +1,37 @@ +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; + } +} diff --git a/src/main/java/fucoin/setup/SelectableNetworkInterface.java b/src/main/java/fucoin/setup/SelectableNetworkInterface.java index 0e023c0ebb1f23261244e03c54c84521678e294e..8d21975277fb2df1665727f784c39946814627ac 100644 --- a/src/main/java/fucoin/setup/SelectableNetworkInterface.java +++ b/src/main/java/fucoin/setup/SelectableNetworkInterface.java @@ -1,8 +1,5 @@ package fucoin.setup; -/** - * @author davidbohn - */ public class SelectableNetworkInterface { String hostName; diff --git a/src/main/java/fucoin/setup/SetupDialogPanel.java b/src/main/java/fucoin/setup/SetupDialogPanel.java new file mode 100644 index 0000000000000000000000000000000000000000..96e2cc7bcc9c9de65e4bf97fb1c7edb14f305024 --- /dev/null +++ b/src/main/java/fucoin/setup/SetupDialogPanel.java @@ -0,0 +1,78 @@ +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(); + } +} diff --git a/src/main/java/fucoin/wallet/WalletImpl.java b/src/main/java/fucoin/wallet/WalletImpl.java index 60ae569dd84a217195c247ce7471b313a381d771..b81619a3de26f7b54be739685a5576a80be9c861 100644 --- a/src/main/java/fucoin/wallet/WalletImpl.java +++ b/src/main/java/fucoin/wallet/WalletImpl.java @@ -72,7 +72,11 @@ public class WalletImpl extends AbstractWallet { isActive = true; if (gui != null) { - gui.setAddress(getAddress()); + String address = getAddress(); + gui.setAddress(address); + if (address.contains("Remote@")) { + gui.setRemote(); + } } if (preKnownNeighbour != null) {