Commits

John Eriksson  committed 966eb93 Merge

Merge

  • Participants
  • Parent commits 600958e, 42290ea

Comments (0)

Files changed (6)

File src/se/kth/csc/sima/SimaConnection.java

         }
     }
 
-    public void joinCourse(String courseName) {
+    public void resetCourse(String courseName) {
         this.joinedCourseName = courseName;
         this.joinedCourseQueue = new ArrayList<SimaQueueObject>();
-        this.sendMessage(new SimaMessage(Utils.REQUEST_MOTD, courseName));
-        this.sendMessage(new SimaMessage(Utils.OBSERVE_COURSE, courseName));
-    }
-
-    public void exitCourse(SimaWindow sw) {
-        this.joinedCourse = false;
-        sw.showCourseList();
-        this.sendMessage(new SimaMessage(Utils.REMOVE_OBSERVER));
-    }
-
-    public void queue(String name, int type, String host, String message, SimaWindow sw) {
-        sw.allowQueueing(false);
-        this.sendMessage(new SimaMessage(Utils.ADD_QUEUE_OBJECT, new SimaQueueObject(name, type, host, message)));
     }
 
     public void exitQueue() {

File src/se/kth/csc/sima/SimaGUICourseDialog.java

     private static final long serialVersionUID = 3356190276831049476L;
 
     private SimaConnection sc;
-    private SimaWindow parent;
+    private SimaUIListener uiListener;
 
     CourseModel queueListModel;
 
     private ActionListener addToQueue;
     private ActionListener removeFromQueue;
 
-    public SimaGUICourseDialog(SimaConnection sc, SimaWindow parent) {
+    public SimaGUICourseDialog(SimaConnection sc, SimaUIListener uiListener) {
         this.sc = sc;
-        this.parent = parent;
+        this.uiListener = uiListener;
 
         this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
         this.setLayout(new BorderLayout(5, 5));
             gridLayout.setConstraints(this.queueDialog.add(new JLabel("Plats")), c);
             ButtonGroup place = new ButtonGroup();
             this.defaultHostButton = new JRadioButton("Använd datornamnet (" + Utils.getHost() + ")", true);
-            this.customHostButton = new JRadioButton();            
+            this.customHostButton = new JRadioButton();
             place.add(this.defaultHostButton);
             place.add(this.customHostButton);
             c.weighty = 0.0;
                     String host = SimaGUICourseDialog.this.defaultHostButton.isSelected() ? Utils.getHost()
                             : SimaGUICourseDialog.this.customHostField.getText();
                     String message = SimaGUICourseDialog.this.messageField.getText();
-                    SimaGUICourseDialog.this.sc.queue(name, type, host, message, SimaGUICourseDialog.this.parent);
+                    SimaGUICourseDialog.this.uiListener.queue(name, type, host, message);
                 }
             };
             this.removeFromQueue = new ActionListener() {
             logout.addActionListener(new ActionListener() {
                 @Override
                 public void actionPerformed(ActionEvent e) {
-                    SimaGUICourseDialog.this.sc.exitCourse(SimaGUICourseDialog.this.parent);
+                    SimaGUICourseDialog.this.uiListener.exitCourse();
                 }
             });
 

File src/se/kth/csc/sima/SimaGUIWindow.java

     private static final long serialVersionUID = -4301547787112940885L;
 
     private SimaConnection sc;
+    private SimaUIListener uiListener;
 
     private JList courseList;
     private SimaGUICourseDialog currentCourseDialog;
         this.addWindowListener(new WindowAdapter() {
             @Override
             public void windowClosing(WindowEvent e) {
-                if (SimaGUIWindow.this.sc.hasJoinedCourse()) {
-                    SimaGUIWindow.this.sc.exitCourse(SimaGUIWindow.this);
-                }
+                uiListener.applicationClosed();
             }
         });
 
     }
 
     @Override
+    public void addUIListener(SimaUIListener uiListener) {
+        this.uiListener = uiListener;
+    }
+
+    @Override
     public void display() {
         this.setVisible(true);
     }
 
     @Override
     public void showCourseQueue() {
-        this.currentCourseDialog = new SimaGUICourseDialog(this.sc, this);
+        this.currentCourseDialog = new SimaGUICourseDialog(this.sc, this.uiListener);
         this.setContentPane(this.currentCourseDialog);
         this.validate();
     }
     private void selectCourse() {
         String courseName = (String)this.courseList.getSelectedValue();
         if (courseName != null) {
-            this.sc.joinCourse(courseName);
+            uiListener.joinCourse(courseName);
         }
     }
 }

File src/se/kth/csc/sima/SimaManager.java

 import javax.swing.UIManager;
 
 public class SimaManager {
+    
+    private static SimaConnection sc;
 
     private static void setupManager(String server) {
         try {
             }
         }
 
-        SimaConnection sc = null;
+        sc = null;
         try {
             sc = new SimaConnection(server, Utils.SERVER_PORT);
         } catch (Exception e) {
         }
 
         final SimaWindow sw = new SimaGUIWindow(sc);
+        
+        SimaUIListener uiListener = new SimaUIListener() {
+            @Override
+            public void joinCourse(String courseName) {
+                sc.resetCourse(courseName);
+                sc.sendMessage(new SimaMessage(Utils.REQUEST_MOTD, courseName));
+                sc.sendMessage(new SimaMessage(Utils.OBSERVE_COURSE, courseName));
+            }
+            
+            @Override
+            public void exitCourse() {
+                sc.setHasJoinedCourse(false);
+                sw.showCourseList();
+                sc.sendMessage(new SimaMessage(Utils.REMOVE_OBSERVER));
+            }
+            
+            @Override
+            public void queue(String name, int type, String host, String message) {
+                sw.allowQueueing(false);
+                sc.sendMessage(new SimaMessage(Utils.ADD_QUEUE_OBJECT, new SimaQueueObject(name, type, host, message)));
+            }
+            
+            @Override
+            public void exitQueue() {
+                sc.sendMessage(new SimaMessage(Utils.REMOVE_QUEUE_OBJECT, sc.getOwnQueueObject().getSqoID()));
+            }
+            
+            @Override
+            public void applicationClosed() {
+                if (sc.hasJoinedCourse()) {
+                    this.exitCourse();
+                }
+            }
+        };
+        
+        sw.addUIListener(uiListener);
 
         sc.addMessageListener(Utils.IDENTIFY_CLIENT, new SimaConnectionListener() {
             @Override

File src/se/kth/csc/sima/SimaUIListener.java

+package se.kth.csc.sima;
+
+public interface SimaUIListener {
+    public void joinCourse(String courseName);
+    public void exitCourse();
+    public void queue(String name, int type, String host, String message);
+    public void exitQueue();
+    public void applicationClosed();
+}

File src/se/kth/csc/sima/SimaWindow.java

 package se.kth.csc.sima;
 
 public interface SimaWindow {
+    public void addUIListener(SimaUIListener uiListener);
     /**
      * Tells the UI to fill its list of courses with this list
      *