From 345c69b543d4dd7eebf87f158c630ca25ebc3aa5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=B6nnecke?= <simonkoennecke@gmail.com>
Date: Tue, 14 Jun 2016 01:25:21 +0200
Subject: [PATCH] #13 moved supervisor form to gui package, + typos

---
 src/fucoin/gui/SuperVisorGuiControl.java      |  8 +++
 src/fucoin/gui/SuperVisorGuiControlImpl.java  | 62 +++++++++++++++++++
 ...lletController.java => WalletControl.java} |  2 +-
 ...rollerImpl.java => WalletControlImpl.java} |  4 +-
 ...iController.java => WalletGuiControl.java} |  2 +-
 ...lerImpl.java => WalletGuiControlImpl.java} | 10 +--
 src/fucoin/supervisor/SuperVisorCreator.java  | 35 ++---------
 src/fucoin/supervisor/SuperVisorImpl.java     | 32 ++++++++--
 src/fucoin/wallet/Wallet.java                 |  4 +-
 src/fucoin/wallet/WalletCreator.java          |  5 +-
 src/fucoin/wallet/WalletImpl.java             | 12 ++--
 11 files changed, 121 insertions(+), 55 deletions(-)
 create mode 100644 src/fucoin/gui/SuperVisorGuiControl.java
 create mode 100644 src/fucoin/gui/SuperVisorGuiControlImpl.java
 rename src/fucoin/gui/{WalletController.java => WalletControl.java} (70%)
 rename src/fucoin/gui/{WalletControllerImpl.java => WalletControlImpl.java} (72%)
 rename src/fucoin/gui/{WalletGuiController.java => WalletGuiControl.java} (85%)
 rename src/fucoin/gui/{WalletGuiControllerImpl.java => WalletGuiControlImpl.java} (95%)

diff --git a/src/fucoin/gui/SuperVisorGuiControl.java b/src/fucoin/gui/SuperVisorGuiControl.java
new file mode 100644
index 0000000..d27cf2c
--- /dev/null
+++ b/src/fucoin/gui/SuperVisorGuiControl.java
@@ -0,0 +1,8 @@
+package fucoin.gui;
+
+public interface SuperVisorGuiControl {
+    /**
+     * Call from SuperVisorImpl after poison pill or kill
+     */
+    void onLeave();
+}
diff --git a/src/fucoin/gui/SuperVisorGuiControlImpl.java b/src/fucoin/gui/SuperVisorGuiControlImpl.java
new file mode 100644
index 0000000..1ca6922
--- /dev/null
+++ b/src/fucoin/gui/SuperVisorGuiControlImpl.java
@@ -0,0 +1,62 @@
+package fucoin.gui;
+
+import fucoin.supervisor.SuperVisorImpl;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+
+public class SuperVisorGuiControlImpl implements SuperVisorGuiControl {
+    private SuperVisorImpl superVisor;
+    private JFrame frame;
+
+    public SuperVisorGuiControlImpl(SuperVisorImpl sv) {
+        superVisor = sv;
+        init();
+    }
+
+    private void init() {
+        //Show AWT window for runtime information
+        frame = new JFrame("Server");
+        frame.setLayout(new GridLayout(3, 2));
+        frame.add(new Label("All Amounts:"));
+
+        //Init Amount Table and SuperVisorImpl
+
+        JTable amountListView = new JTable(superVisor.getAmountTableModel());
+        Label averageAmountLbl = new Label("Average Amounts:");
+        frame.add(new JScrollPane(amountListView));
+        frame.add(new Label("Average Amounts:"));
+        frame.add(averageAmountLbl);
+
+        //Call update on supervisor
+        JButton updateBtn = new JButton("Update");
+        updateBtn.addActionListener(e -> superVisor.updateValues());
+        frame.add(updateBtn);
+
+        //Exit Button and shutdown supervisor
+        JButton exitBtn = new JButton("exit");
+        exitBtn.addActionListener(e -> {
+            superVisor.exit();
+            frame.setVisible(false);
+            frame.dispose();
+        });
+        frame.add(exitBtn);
+        frame.setSize(200, 400);
+        frame.setVisible(true);
+
+        frame.addWindowListener(new WindowAdapter() {
+            @Override
+            public void windowClosing(WindowEvent e) {
+                super.windowClosing(e);
+                superVisor.exit();
+            }
+        });
+    }
+
+    @Override
+    public void onLeave() {
+        frame.dispose();
+    }
+}
diff --git a/src/fucoin/gui/WalletController.java b/src/fucoin/gui/WalletControl.java
similarity index 70%
rename from src/fucoin/gui/WalletController.java
rename to src/fucoin/gui/WalletControl.java
index c32fd53..96e378c 100644
--- a/src/fucoin/gui/WalletController.java
+++ b/src/fucoin/gui/WalletControl.java
@@ -1,6 +1,6 @@
 package fucoin.gui;
 
-public interface WalletController {
+public interface WalletControl {
     void leave();
 
     void send(String address, int amount);
diff --git a/src/fucoin/gui/WalletControllerImpl.java b/src/fucoin/gui/WalletControlImpl.java
similarity index 72%
rename from src/fucoin/gui/WalletControllerImpl.java
rename to src/fucoin/gui/WalletControlImpl.java
index 954bf86..be00024 100644
--- a/src/fucoin/gui/WalletControllerImpl.java
+++ b/src/fucoin/gui/WalletControlImpl.java
@@ -3,11 +3,11 @@ package fucoin.gui;
 import fucoin.wallet.WalletImpl;
 
 
-public class WalletControllerImpl implements WalletController {
+public class WalletControlImpl implements WalletControl {
 
     private WalletImpl wallet;
 
-    public WalletControllerImpl(WalletImpl wallet) {
+    public WalletControlImpl(WalletImpl wallet) {
         this.wallet = wallet;
     }
 
diff --git a/src/fucoin/gui/WalletGuiController.java b/src/fucoin/gui/WalletGuiControl.java
similarity index 85%
rename from src/fucoin/gui/WalletGuiController.java
rename to src/fucoin/gui/WalletGuiControl.java
index 3e1be4c..d19b454 100644
--- a/src/fucoin/gui/WalletGuiController.java
+++ b/src/fucoin/gui/WalletGuiControl.java
@@ -1,7 +1,7 @@
 package fucoin.gui;
 
 
-public interface WalletGuiController {
+public interface WalletGuiControl {
     void setAddress(String address);
 
     void setAmount(int amount);
diff --git a/src/fucoin/gui/WalletGuiControllerImpl.java b/src/fucoin/gui/WalletGuiControlImpl.java
similarity index 95%
rename from src/fucoin/gui/WalletGuiControllerImpl.java
rename to src/fucoin/gui/WalletGuiControlImpl.java
index 59f87b1..b3f8904 100644
--- a/src/fucoin/gui/WalletGuiControllerImpl.java
+++ b/src/fucoin/gui/WalletGuiControlImpl.java
@@ -5,7 +5,7 @@ import java.awt.*;
 import java.awt.event.*;
 import java.util.Enumeration;
 
-public class WalletGuiControllerImpl implements WalletGuiController {
+public class WalletGuiControlImpl implements WalletGuiControl {
 
     private DefaultListModel<LogMessage> log = new DefaultListModel<>();
 
@@ -30,7 +30,7 @@ public class WalletGuiControllerImpl implements WalletGuiController {
     private JScrollPane logPane = new JScrollPane(txtLog);
     private JCheckBox showDebug;
 
-    public WalletGuiControllerImpl(WalletController walletControle) {
+    public WalletGuiControlImpl(WalletControl walletControl) {
 
         window.setSize(400, 600);
         window.setLayout(new GridLayout(3, 1));
@@ -102,7 +102,7 @@ public class WalletGuiControllerImpl implements WalletGuiController {
         window.setVisible(true);
 
         btnSend.addActionListener(e -> {
-            walletControle.send(txtSendTo.getSelectedItem().toString(),
+            walletControl.send(txtSendTo.getSelectedItem().toString(),
                     Integer.parseInt(txtSendAmount.getText()));
         });
 
@@ -117,7 +117,7 @@ public class WalletGuiControllerImpl implements WalletGuiController {
             @Override
             public void windowClosing(WindowEvent e) {
                 System.out.println("window closing");
-                walletControle.leave();
+                walletControl.leave();
                 super.windowClosing(e);
 
             }
@@ -125,7 +125,7 @@ public class WalletGuiControllerImpl implements WalletGuiController {
             @Override
             public void windowClosed(WindowEvent e) {
                 System.out.println("window closing");
-                walletControle.leave();
+                walletControl.leave();
                 super.windowClosing(e);
             }
         });
diff --git a/src/fucoin/supervisor/SuperVisorCreator.java b/src/fucoin/supervisor/SuperVisorCreator.java
index 70ea296..088ea78 100644
--- a/src/fucoin/supervisor/SuperVisorCreator.java
+++ b/src/fucoin/supervisor/SuperVisorCreator.java
@@ -1,6 +1,8 @@
 package fucoin.supervisor;
 
 import akka.japi.Creator;
+import fucoin.gui.SuperVisorGuiControl;
+import fucoin.gui.SuperVisorGuiControlImpl;
 
 import javax.swing.*;
 import java.awt.*;
@@ -13,36 +15,9 @@ public class SuperVisorCreator implements Creator<SuperVisorImpl> {
 
     @Override
     public SuperVisorImpl create() throws Exception {
-        //Show AWT window for runtime information
-        JFrame frame = new JFrame("Server");
-        frame.setLayout(new GridLayout(3, 2));
-        frame.add(new Label("All Amounts:"));
-
-        //Init Amount Table and SuperVisorImpl
-        AmountTableModel amountTableModel = new AmountTableModel();
-        SuperVisorImpl sv = new SuperVisorImpl(amountTableModel);
-
-        JTable amountListView = new JTable(amountTableModel);
-        Label averageAmountLbl = new Label("Average Amounts:");
-        frame.add(new JScrollPane(amountListView));
-        frame.add(new Label("Average Amounts:"));
-        frame.add(averageAmountLbl);
-
-        //Call update on supervisor
-        JButton updateBtn = new JButton("Update");
-        updateBtn.addActionListener(e -> sv.updateValues());
-        frame.add(updateBtn);
-
-        //Exit Button and shutdown supervisor
-        JButton exitBtn = new JButton("exit");
-        exitBtn.addActionListener(e -> {
-            sv.exit();
-            frame.setVisible(false);
-        });
-        frame.add(exitBtn);
-        frame.setSize(200, 400);
-        frame.setVisible(true);
-
+        SuperVisorImpl sv = new SuperVisorImpl();
+        SuperVisorGuiControl superVisorGuiControl = new SuperVisorGuiControlImpl(sv);
+        sv.setGuiControl(superVisorGuiControl);
         return sv;
     }
 
diff --git a/src/fucoin/supervisor/SuperVisorImpl.java b/src/fucoin/supervisor/SuperVisorImpl.java
index 224efaf..e7c7c2d 100644
--- a/src/fucoin/supervisor/SuperVisorImpl.java
+++ b/src/fucoin/supervisor/SuperVisorImpl.java
@@ -1,11 +1,12 @@
 package fucoin.supervisor;
 
 import akka.actor.Props;
-import fucoin.wallet.AbstractNode;
 import fucoin.actions.Action;
 import fucoin.actions.persist.ActionInvokeUpdate;
 import fucoin.actions.transaction.ActionGetAmountAnswer;
 import fucoin.actions.transaction.SuperVisorAction;
+import fucoin.gui.SuperVisorGuiControl;
+import fucoin.wallet.AbstractNode;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -14,8 +15,21 @@ import java.util.Map;
 import java.util.Map.Entry;
 
 public class SuperVisorImpl extends AbstractNode {
+
     private AmountTableModel amountTableModel;
 
+    private Map<Long, DistributedCommittedTransferRequest> requestQueue;
+
+    private SuperVisorGuiControl gui;
+
+    public SuperVisorImpl() {
+        this.amountTableModel = new AmountTableModel();
+    }
+
+    public void setGuiControl(SuperVisorGuiControl gui) {
+        this.gui = gui;
+    }
+
     public SuperVisorImpl(AmountTableModel amountTableModel) {
         this.amountTableModel = amountTableModel;
     }
@@ -28,14 +42,11 @@ public class SuperVisorImpl extends AbstractNode {
         if (msg instanceof ActionGetAmountAnswer) {
             ActionGetAmountAnswer answer = (ActionGetAmountAnswer) msg;
             amountTableModel.updateTable(answer.address, answer.name, answer.amount);
-        } /* TODO: Whats happened here?? Why we can invoke doAction of abstract class? */
-         else if (msg instanceof SuperVisorAction) {
+        } /* TODO: Whats happened here?? Why we can invoke doAction of abstract class? */ else if (msg instanceof SuperVisorAction) {
             ((Action) msg).doAction(this);
         }
     }
 
-    private Map<Long, DistributedCommittedTransferRequest> requestQueue;
-
     public static Props props() {
         return Props.create(new SuperVisorCreator());
     }
@@ -46,6 +57,9 @@ public class SuperVisorImpl extends AbstractNode {
 
     public void exit() {
         getContext().stop(getSelf());
+        if (gui != null) {
+            gui.onLeave();
+        }
     }
 
     @Override
@@ -94,4 +108,12 @@ public class SuperVisorImpl extends AbstractNode {
     public void deleteRequest(DistributedCommittedTransferRequest request) {
         requestQueue.remove(request.getId());
     }
+
+    public AmountTableModel getAmountTableModel() {
+        return amountTableModel;
+    }
+
+    public void setAmountTableModel(AmountTableModel amountTableModel) {
+        this.amountTableModel = amountTableModel;
+    }
 }
diff --git a/src/fucoin/wallet/Wallet.java b/src/fucoin/wallet/Wallet.java
index 78d1870..1c8b56a 100644
--- a/src/fucoin/wallet/Wallet.java
+++ b/src/fucoin/wallet/Wallet.java
@@ -1,9 +1,9 @@
 package fucoin.wallet;
 
-import fucoin.gui.WalletController;
+import fucoin.gui.WalletControl;
 
 
-public interface Wallet extends WalletController {
+public interface Wallet extends WalletControl {
     //Vector<WalletPointer> join();
     void storeOrUpdate(WalletImpl w);
 
diff --git a/src/fucoin/wallet/WalletCreator.java b/src/fucoin/wallet/WalletCreator.java
index d35360d..6779914 100644
--- a/src/fucoin/wallet/WalletCreator.java
+++ b/src/fucoin/wallet/WalletCreator.java
@@ -2,8 +2,7 @@ package fucoin.wallet;
 
 import akka.actor.ActorRef;
 import akka.japi.Creator;
-import fucoin.gui.WalletGuiController;
-import fucoin.gui.WalletGuiControllerImpl;
+import fucoin.gui.WalletGuiControlImpl;
 
 public class WalletCreator implements Creator<AbstractWallet> {
     private ActorRef preKnownNeighbour;
@@ -25,7 +24,7 @@ public class WalletCreator implements Creator<AbstractWallet> {
         WalletImpl wallet = new WalletImpl(preKnownNeighbour, preKnownNeighbourName,
                 walletName, remoteSuperVisorActor);
 
-        WalletGuiControllerImpl gui = new WalletGuiControllerImpl(wallet);
+        WalletGuiControlImpl gui = new WalletGuiControlImpl(wallet);
         wallet.setGui(gui);
         return wallet;
     }
diff --git a/src/fucoin/wallet/WalletImpl.java b/src/fucoin/wallet/WalletImpl.java
index 83ee800..b737fef 100644
--- a/src/fucoin/wallet/WalletImpl.java
+++ b/src/fucoin/wallet/WalletImpl.java
@@ -9,14 +9,14 @@ import fucoin.actions.persist.ActionInvokeLeave;
 import fucoin.actions.persist.ActionInvokeRevive;
 import fucoin.actions.transaction.ActionGetAmountAnswer;
 import fucoin.actions.transaction.ActionInvokeSentMoney;
-import fucoin.gui.WalletController;
-import fucoin.gui.WalletGuiController;
+import fucoin.gui.WalletControl;
+import fucoin.gui.WalletGuiControl;
 
-public class WalletImpl extends AbstractWallet implements WalletController {
+public class WalletImpl extends AbstractWallet implements WalletControl {
 
     private ActorRef preKnownNeighbour;
     private ActorRef remoteSuperVisorActor;
-    private WalletGuiController gui;
+    private WalletGuiControl gui;
     private String preKnownNeighbourName;
     private boolean isActive;
 
@@ -134,11 +134,11 @@ public class WalletImpl extends AbstractWallet implements WalletController {
         this.remoteSuperVisorActor = remoteSuperVisorActor;
     }
 
-    public WalletGuiController getGui() {
+    public WalletGuiControl getGui() {
         return gui;
     }
 
-    public void setGui(WalletGuiController gui) {
+    public void setGui(WalletGuiControl gui) {
         this.gui = gui;
     }
 
-- 
GitLab