changeset 43:fb343e1da209

Remove NONE from ConnectionType
author Omair Majid <omajid@redhat.com>
date Tue, 17 Jan 2012 14:52:54 -0500
parents 89f821df2f1b
children 5d3346b09eb8
files src/com/redhat/thermostat/client/Connection.java src/com/redhat/thermostat/client/Main.java src/com/redhat/thermostat/client/MongoConnection.java src/com/redhat/thermostat/client/ui/ConnectionSelectionDialog.java
diffstat 4 files changed, 44 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/client/Connection.java	Tue Jan 17 13:07:36 2012 -0500
+++ b/src/com/redhat/thermostat/client/Connection.java	Tue Jan 17 14:52:54 2012 -0500
@@ -9,7 +9,7 @@
         LOCAL(false),
         REMOTE(true),
         CLUSTER(true),
-        NONE(false, false), ;
+        ;
 
         boolean isDisplayable = false;
         boolean needsUrl = false;
--- a/src/com/redhat/thermostat/client/Main.java	Tue Jan 17 13:07:36 2012 -0500
+++ b/src/com/redhat/thermostat/client/Main.java	Tue Jan 17 14:52:54 2012 -0500
@@ -11,7 +11,6 @@
 
 import com.redhat.thermostat.client.Connection.ConnectionListener;
 import com.redhat.thermostat.client.Connection.ConnectionStatus;
-import com.redhat.thermostat.client.Connection.ConnectionType;
 import com.redhat.thermostat.client.ui.ConnectionSelectionDialog;
 import com.redhat.thermostat.client.ui.MainWindow;
 import com.redhat.thermostat.common.Constants;
@@ -54,7 +53,7 @@
         dialog.setModal(true);
         dialog.setVisible(true);
 
-        if (connection.getType() == ConnectionType.NONE) {
+        if (dialog.isCancelled()) {
             return;
         }
 
--- a/src/com/redhat/thermostat/client/MongoConnection.java	Tue Jan 17 13:07:36 2012 -0500
+++ b/src/com/redhat/thermostat/client/MongoConnection.java	Tue Jan 17 14:52:54 2012 -0500
@@ -42,8 +42,6 @@
             case CLUSTER:
                 // TODO connect to a cluster
                 break;
-            case NONE:
-                throw new IllegalArgumentException();
         }
         fireChanged(ConnectionStatus.CONNECTED);
         connected = true;
--- a/src/com/redhat/thermostat/client/ui/ConnectionSelectionDialog.java	Tue Jan 17 13:07:36 2012 -0500
+++ b/src/com/redhat/thermostat/client/ui/ConnectionSelectionDialog.java	Tue Jan 17 14:52:54 2012 -0500
@@ -10,6 +10,8 @@
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 
 import javax.swing.BorderFactory;
 import javax.swing.Box;
@@ -28,6 +30,7 @@
 
     private static final int ICON_LABEL_GAP = 5;
 
+    private boolean cancelled = false;
     private final Connection model;
 
     public ConnectionSelectionDialog(JFrame owner, Connection model) {
@@ -35,6 +38,10 @@
         setTitle(_("STARTUP_MODE_SELECTION_DIALOG_TITLE"));
         this.model = model;
         setupUi();
+        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
+        // note: this is only fired when the user tries to close the window
+        // not when we try to close the window
+        addWindowListener(new CancelListener(this));
     }
 
     private void setupUi() {
@@ -53,7 +60,7 @@
 
         JButton cancelButton = new JButton(_("BUTTON_CANCEL"));
         cancelButton.setMargin(new Insets(0, 15, 0, 15));
-        cancelButton.addActionListener(new SetStartupModeListener(this, ConnectionType.NONE));
+        cancelButton.addActionListener(new CancelListener(this));
         buttonsPanel.add(cancelButton);
     }
 
@@ -115,6 +122,14 @@
         return model;
     }
 
+    public void setCancelled(boolean newValue) {
+        cancelled = newValue;
+    }
+
+    public boolean isCancelled() {
+        return cancelled;
+    }
+
     private static class SetStartupModeListener implements ActionListener {
         private final ConnectionType mode;
         private final ConnectionSelectionDialog window;
@@ -131,4 +146,30 @@
             window.dispose();
         }
     }
+
+    private static class CancelListener extends WindowAdapter implements ActionListener {
+
+        private final ConnectionSelectionDialog dialog;
+
+        public CancelListener(ConnectionSelectionDialog dialog) {
+            this.dialog = dialog;
+        }
+
+        @Override
+        public void actionPerformed(ActionEvent e) {
+            cancel();
+        }
+
+        @Override
+        public void windowClosing(WindowEvent e) {
+            cancel();
+        }
+
+        private void cancel() {
+            dialog.setCancelled(true);
+            dialog.setVisible(false);
+            dialog.dispose();
+        }
+
+    }
 }