changeset 581:b8a464e454a8

Move actions into separate classes. This addresses a concern mentioned in the Eclipse client prototype review. It moves Actions (i.e. Controllers) into a separate package. PR1128 Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002946.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Fri, 24 Aug 2012 18:57:55 +0200
parents c95cd9ae6db4
children ac8f8bb554d3 d480b69be1d7
files eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/controllers/ConnectDBAction.java eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/controllers/ConnectionJobListener.java eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/jobs/ConnectDbJob.java eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/views/HostsVmsTreeViewPart.java
diffstat 4 files changed, 195 insertions(+), 138 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/controllers/ConnectDBAction.java	Fri Aug 24 18:57:55 2012 +0200
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.eclipse.controllers;
+
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.action.Action;
+
+public class ConnectDBAction extends Action {
+    
+    private Job connectJob;
+    
+    public ConnectDBAction(Job job) {
+        this.connectJob = job;
+    }
+    
+    @Override
+    public void run() {
+        connectJob.schedule();
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/controllers/ConnectionJobListener.java	Fri Aug 24 18:57:55 2012 +0200
@@ -0,0 +1,32 @@
+package com.redhat.thermostat.eclipse.controllers;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+import org.eclipse.jface.action.Action;
+
+import com.redhat.thermostat.eclipse.Activator;
+import com.redhat.thermostat.eclipse.views.HostsVmsTreeViewPart;
+
+public class ConnectionJobListener extends JobChangeAdapter {
+
+    private HostsVmsTreeViewPart view;
+    private Action connectAction;
+    
+    public ConnectionJobListener(Action connectAction, HostsVmsTreeViewPart view) {
+        this.view = view;
+        this.connectAction = connectAction;
+    }
+    
+    @Override
+    public void done(IJobChangeEvent event) {
+        IStatus result = event.getResult();
+        if (result.isOK()) {
+            connectAction.setImageDescriptor(Activator
+                    .getImageDescriptor("icons/online.png"));
+            connectAction.setEnabled(!Activator.getDefault().isConnected());
+            connectAction.setToolTipText("Online");
+            view.showHostVmsPage();
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/jobs/ConnectDbJob.java	Fri Aug 24 18:57:55 2012 +0200
@@ -0,0 +1,91 @@
+package com.redhat.thermostat.eclipse.jobs;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+
+import com.redhat.thermostat.common.ThreadPoolTimerFactory;
+import com.redhat.thermostat.common.TimerFactory;
+import com.redhat.thermostat.common.appctx.ApplicationContext;
+import com.redhat.thermostat.common.config.InvalidConfigurationException;
+import com.redhat.thermostat.common.dao.DAOFactory;
+import com.redhat.thermostat.common.dao.MongoDAOFactory;
+import com.redhat.thermostat.common.storage.Connection;
+import com.redhat.thermostat.common.storage.Connection.ConnectionListener;
+import com.redhat.thermostat.common.storage.Connection.ConnectionStatus;
+import com.redhat.thermostat.common.storage.ConnectionException;
+import com.redhat.thermostat.common.storage.MongoStorageProvider;
+import com.redhat.thermostat.common.storage.StorageProvider;
+import com.redhat.thermostat.eclipse.Activator;
+import com.redhat.thermostat.eclipse.ConnectionConfiguration;
+import com.redhat.thermostat.eclipse.LoggerFacility;
+
+public class ConnectDbJob extends Job {
+
+    private ConnectionConfiguration configuration;
+    
+    public ConnectDbJob(String name, ConnectionConfiguration configuration) {
+        super(name);
+        this.configuration = configuration;
+    }
+
+    @Override
+    protected IStatus run(IProgressMonitor monitor) {
+        monitor.beginTask(
+                "Connecting to " + configuration.getDBConnectionString(),
+                IProgressMonitor.UNKNOWN);
+        try {
+            connectToBackEnd();
+            return Status.OK_STATUS;
+        } catch (InvalidConfigurationException | ConnectionException e) {
+            LoggerFacility.getInstance().log(IStatus.ERROR,
+                    "Could not connect to DB", e);
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not connect to DB", e);
+        }
+    }
+    
+    /*
+     * Establish a Mongo DB connection.
+     */
+    private void connectToBackEnd() throws InvalidConfigurationException, ConnectionException {
+        StorageProvider connProv = new MongoStorageProvider(configuration);
+        DAOFactory daoFactory = new MongoDAOFactory(connProv);
+        ApplicationContext.getInstance().setDAOFactory(daoFactory);
+        TimerFactory timerFactory = new ThreadPoolTimerFactory(1);
+        ApplicationContext.getInstance().setTimerFactory(timerFactory);
+
+        Connection connection = daoFactory.getConnection();
+        ConnectionListener connectionListener = new ConnectionListener() {
+            @Override
+            public void changed(ConnectionStatus newStatus) {
+                switch (newStatus) {
+                case DISCONNECTED:
+                    LoggerFacility.getInstance().log(IStatus.WARNING,
+                            "Unexpected disconnect event.");
+                    break;
+                case CONNECTING:
+                    LoggerFacility.getInstance().log(IStatus.INFO,
+                            "Connecting to storage.");
+                    break;
+                case CONNECTED:
+                    LoggerFacility.getInstance().log(IStatus.INFO,
+                            "Connected to storage.");
+                    Activator.getDefault().setConnected(true);
+                    break;
+                case FAILED_TO_CONNECT:
+                    LoggerFacility.getInstance().log(IStatus.WARNING,
+                            "Could not connect to storage.");
+                default:
+                    LoggerFacility.getInstance().log(IStatus.WARNING,
+                            "Unfamiliar ConnectionStatus value");
+                }
+            }
+        };
+        connection.addListener(connectionListener);
+        LoggerFacility.getInstance().log(IStatus.INFO,
+                "Connecting to storage...");
+        connection.connect();
+    }
+
+}
--- a/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/views/HostsVmsTreeViewPart.java	Thu Aug 30 17:37:39 2012 +0200
+++ b/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/views/HostsVmsTreeViewPart.java	Fri Aug 24 18:57:55 2012 +0200
@@ -36,15 +36,9 @@
 
 package com.redhat.thermostat.eclipse.views;
 
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.jobs.IJobChangeEvent;
 import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.core.runtime.jobs.JobChangeAdapter;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IToolBarManager;
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.RowLayout;
@@ -59,23 +53,14 @@
 
 import com.redhat.thermostat.common.DefaultHostsVMsLoader;
 import com.redhat.thermostat.common.HostsVMsLoader;
-import com.redhat.thermostat.common.ThreadPoolTimerFactory;
-import com.redhat.thermostat.common.TimerFactory;
 import com.redhat.thermostat.common.appctx.ApplicationContext;
-import com.redhat.thermostat.common.config.InvalidConfigurationException;
-import com.redhat.thermostat.common.dao.DAOFactory;
 import com.redhat.thermostat.common.dao.HostInfoDAO;
-import com.redhat.thermostat.common.dao.MongoDAOFactory;
 import com.redhat.thermostat.common.dao.VmInfoDAO;
-import com.redhat.thermostat.common.storage.Connection;
-import com.redhat.thermostat.common.storage.Connection.ConnectionListener;
-import com.redhat.thermostat.common.storage.Connection.ConnectionStatus;
-import com.redhat.thermostat.common.storage.ConnectionException;
-import com.redhat.thermostat.common.storage.MongoStorageProvider;
-import com.redhat.thermostat.common.storage.StorageProvider;
 import com.redhat.thermostat.eclipse.Activator;
 import com.redhat.thermostat.eclipse.ConnectionConfiguration;
-import com.redhat.thermostat.eclipse.LoggerFacility;
+import com.redhat.thermostat.eclipse.controllers.ConnectDBAction;
+import com.redhat.thermostat.eclipse.controllers.ConnectionJobListener;
+import com.redhat.thermostat.eclipse.jobs.ConnectDbJob;
 import com.redhat.thermostat.eclipse.model.HostsVmsLabelProvider;
 import com.redhat.thermostat.eclipse.model.HostsVmsTreeContentProvider;
 import com.redhat.thermostat.eclipse.model.HostsVmsTreeRoot;
@@ -95,9 +80,18 @@
     // Container for tree and connect
     private PageBook pageBook;
 
-    private ConnectionConfiguration configuration;
-
-    private void showConnectionPage() {
+    public HostsVmsTreeViewPart() {
+        ConnectionConfiguration configuration = new ConnectionConfiguration("mongodb://127.0.0.1:27518");
+        Job connectJob = new ConnectDbJob(
+                "Connecting to Thermostat storage...", configuration);
+        connectJob.setSystem(true);
+        connectAction = new ConnectDBAction(connectJob);
+        connectAction.setImageDescriptor(Activator
+                .getImageDescriptor("icons/offline.png"));
+        connectJob.addJobChangeListener(new ConnectionJobListener(connectAction, this));
+    }
+    
+    public void showConnectionPage() {
         PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
             @Override
             public void run() {
@@ -106,7 +100,7 @@
         });
     }
 
-    private void showHostVmsPage() {
+    public void showHostVmsPage() {
         HostInfoDAO hostDAO = ApplicationContext.getInstance().getDAOFactory()
                 .getHostInfoDAO();
         VmInfoDAO vmsDAO = ApplicationContext.getInstance().getDAOFactory()
@@ -131,21 +125,9 @@
 
     @Override
     public void createPartControl(final Composite parent) {
-        connectAction = new Action("Connect to storage...") {
-            public void run() {
-                Job connectJob = new ConnectJob(
-                        "Connecting to Thermostat storage...");
-                connectJob.setSystem(true);
-                connectJob.addJobChangeListener(new ConnectionJobListener());
-                connectJob.schedule();
-            }
-        };
-        connectAction.setImageDescriptor(Activator
-                .getImageDescriptor("icons/offline.png"));
         IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
         mgr.add(connectAction);
 
-        configuration = new ConnectionConfiguration("mongodb://127.0.0.1:27518");
         pageBook = new PageBook(parent, SWT.NONE);
 
         // Prepare Hosts/VMs tree
@@ -166,12 +148,7 @@
         link.addListener(SWT.Selection, new Listener() {
             @Override
             public void handleEvent(Event event) {
-                // implement connect
-                Job connectJob = new ConnectJob(
-                        "Connecting to Thermostat storage...");
-                connectJob.setSystem(true);
-                connectJob.addJobChangeListener(new ConnectionJobListener());
-                connectJob.schedule();
+                connectAction.run();
             }
         });
         // Show appropriate page
@@ -188,102 +165,4 @@
         pageBook.setFocus();
     }
 
-    /*
-     * Mongo connection method
-     */
-    private boolean connectToBackEnd() throws InvalidConfigurationException {
-        StorageProvider connProv = new MongoStorageProvider(configuration);
-        DAOFactory daoFactory = new MongoDAOFactory(connProv);
-        ApplicationContext.getInstance().setDAOFactory(daoFactory);
-        TimerFactory timerFactory = new ThreadPoolTimerFactory(1);
-        ApplicationContext.getInstance().setTimerFactory(timerFactory);
-
-        Connection connection = daoFactory.getConnection();
-        ConnectionListener connectionListener = new ConnectionListener() {
-            @Override
-            public void changed(ConnectionStatus newStatus) {
-                switch (newStatus) {
-                case DISCONNECTED:
-                    LoggerFacility.getInstance().log(IStatus.WARNING,
-                            "Unexpected disconnect event.");
-                    break;
-                case CONNECTING:
-                    LoggerFacility.getInstance().log(IStatus.INFO,
-                            "Connecting to storage.");
-                    break;
-                case CONNECTED:
-                    LoggerFacility.getInstance().log(IStatus.INFO,
-                            "Connected to storage.");
-                    Activator.getDefault().setConnected(true);
-                    break;
-                case FAILED_TO_CONNECT:
-                    LoggerFacility.getInstance().log(IStatus.WARNING,
-                            "Could not connect to storage.");
-                default:
-                    LoggerFacility.getInstance().log(IStatus.WARNING,
-                            "Unfamiliar ConnectionStatus value");
-                }
-            }
-        };
-        connection.addListener(connectionListener);
-        try {
-            LoggerFacility.getInstance().log(IStatus.INFO,
-                    "Connecting to storage...");
-            connection.connect();
-            return true;
-        } catch (final ConnectionException e) {
-            LoggerFacility.getInstance().log(IStatus.ERROR,
-                    e.getCause().getMessage(), e.getCause());
-            PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
-                @Override
-                public void run() {
-                    // FIXME: Show a nicer error message
-                    MessageDialog.openError(null, "Connection Problem", e
-                            .getCause().getMessage());
-                }
-
-            });
-            return false;
-        }
-    }
-
-    private class ConnectJob extends Job {
-
-        public ConnectJob(String name) {
-            super(name);
-        }
-
-        @Override
-        protected IStatus run(IProgressMonitor monitor) {
-            monitor.beginTask(
-                    "Connecting to " + configuration.getDBConnectionString(),
-                    IProgressMonitor.UNKNOWN);
-            try {
-                if (connectToBackEnd()) {
-                    return Status.OK_STATUS;
-                }
-            } catch (InvalidConfigurationException e) {
-                // FIXME: do something more reasonable
-            }
-            return Status.CANCEL_STATUS;
-        }
-
-    }
-
-    private class ConnectionJobListener extends JobChangeAdapter {
-
-        @Override
-        public void done(IJobChangeEvent event) {
-            IStatus result = event.getResult();
-            if (result.isOK() && result.getCode() != IStatus.CANCEL) {
-                showHostVmsPage();
-                connectAction.setImageDescriptor(Activator
-                        .getImageDescriptor("icons/online.png"));
-                connectAction.setEnabled(!Activator.getDefault().isConnected());
-                connectAction.setToolTipText("Online");
-            }
-        }
-
-    }
-
 }