changeset 56:335a8653d206

exit program when main window is closed
author Omair Majid <omajid@redhat.com>
date Thu, 26 Jan 2012 11:55:52 -0500
parents fc0ff5ca8e76
children cad48fbe4e5a
files src/com/redhat/thermostat/client/ui/MainWindow.java
diffstat 1 files changed, 20 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/client/ui/MainWindow.java	Thu Jan 26 11:12:48 2012 -0500
+++ b/src/com/redhat/thermostat/client/ui/MainWindow.java	Thu Jan 26 11:55:52 2012 -0500
@@ -47,6 +47,8 @@
 import java.awt.event.ActionListener;
 import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 
 import javax.swing.BorderFactory;
 import javax.swing.JFrame;
@@ -92,12 +94,17 @@
     private JTextField searchField = null;
     private JTree agentVmTree = null;
 
+    private final ShutdownClient shutdownAction;
+
     public MainWindow(UiFacadeFactory facadeFactory) {
         super();
         setTitle(_("MAIN_WINDOW_TITLE"));
 
         this.facadeFactory = facadeFactory;
         this.facade = facadeFactory.getMainWindow();
+
+        shutdownAction = new ShutdownClient(facade, this);
+
         searchField = new JTextField();
         TreeModel model = facade.getHostVmTree();
         agentVmTree = new JTree(model);
@@ -111,7 +118,8 @@
 
         agentVmTree.setSelectionPath(new TreePath(((DefaultMutableTreeNode) model.getRoot()).getPath()));
 
-        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
+        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
+        addWindowListener(shutdownAction);
 
         this.facade.start();
     }
@@ -144,7 +152,7 @@
 
         JMenuItem fileExitMenu = new JMenuItem(_("MENU_FILE_EXIT"));
         fileExitMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
-        fileExitMenu.addActionListener(new ShutdownClient(this.facade, this));
+        fileExitMenu.addActionListener(shutdownAction);
         fileMenu.add(fileExitMenu);
 
         JMenu helpMenu = new JMenu(_("MENU_HELP"));
@@ -252,7 +260,7 @@
         return result;
     }
 
-    public static class ShutdownClient implements ActionListener {
+    public static class ShutdownClient extends WindowAdapter implements ActionListener {
 
         private JFrame toDispose;
         private MainWindowFacade facade;
@@ -263,7 +271,16 @@
         }
 
         @Override
+        public void windowClosing(WindowEvent e) {
+            shutdown();
+        }
+
+        @Override
         public void actionPerformed(ActionEvent e) {
+            shutdown();
+        }
+
+        private void shutdown() {
             toDispose.dispose();
             facade.stop();
         }