Added some more JUnit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / NewConnectionHandler.java
index 5e96e5f5acd91d33989260abea362dcb96c0a04a..6656c03cb35ba7a809de83bee79f859c872c62fd 100644 (file)
@@ -11,7 +11,6 @@
  **********************************************************************/
 package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
 
-import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.jface.dialogs.MessageDialog;
@@ -19,7 +18,7 @@ import org.eclipse.jface.window.Window;
 import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.INewConnectionDialog;
-import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.NewConnectionDialog;
+import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.TraceControlDialogFactory;
 import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent;
 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TargetNodeComponent;
 import org.eclipse.rse.core.IRSESystemType;
@@ -37,7 +36,7 @@ import org.eclipse.ui.PlatformUI;
  * Command handler for creation new connection for trace control.
  * </p>
  */
-public class NewConnectionHandler extends AbstractHandler {
+public class NewConnectionHandler extends BaseControlViewHandler {
 
     // ------------------------------------------------------------------------
     // Constants
@@ -77,55 +76,57 @@ public class NewConnectionHandler extends AbstractHandler {
         IHost[] hosts = registry.getHostsBySystemType(sysType);
 
         // Open dialog box for the node name and address
-        INewConnectionDialog dialog = new NewConnectionDialog(window.getShell(), fRoot, hosts);
+        final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog();
+        dialog.setTraceControlParent(fRoot);
+        dialog.setHosts(hosts);
 
-        if (dialog.open() == Window.OK) {
+        if (dialog.open() != Window.OK) {
+            return null;
+        }
+
+        String hostName = dialog.getConnectionName(); 
+        String hostAddress = dialog.getHostName();
 
-            String hostName = dialog.getNodeName(); 
-            String hostAddress = dialog.getNodeAddress();
+        // get the singleton RSE registry
+        IHost host = null;
 
-            // get the singleton RSE registry
-            IHost host = null;
+        for (int i = 0; i < hosts.length; i++) {
+            if (hosts[i].getAliasName().equals(hostName)) {
+                host = hosts[i];
+                break;
+            }
+        }
 
-            for (int i = 0; i < hosts.length; i++) {
-                if (hosts[i].getAliasName().equals(hostName)) {
-                    host = hosts[i];
-                    break;
-                }
+        if (host == null) {
+            // if there's no host then we will create it
+            try {
+                // create the host object as an SSH Only connection
+                host = registry.createHost(
+                        sysType,       //System Type Name
+                        hostName,      //Connection name
+                        hostAddress,   //IP Address        
+                        "Connection to Host"); //description //$NON-NLS-1$
             }
+            catch (Exception e) {
+                MessageDialog.openError(window.getShell(),
+                        Messages.TraceControl_EclipseCommandFailure,
+                        Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString());  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+                return null;
+            }
+        }
 
-            if (host == null) {
-                // if there's no host then we will create it
-                try {
-                    
-                    // create the host object as an SSH Only connection
-                    host = registry.createHost(
-                            sysType,       //System Type Name
-                            hostName,      //Connection name
-                            hostAddress,   //IP Address        
-                            "Connection to Host"); //description //$NON-NLS-1$
-                }
-                catch (Exception e) {
-                    MessageDialog.openError(window.getShell(),
-                            Messages.TraceControl_EclipseCommandFailure,
-                            Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString());  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
-                    return null;
-                }
+        if (host != null) {
+            // successful creation of host
+            TargetNodeComponent node = null;
+            if (!fRoot.containsChild(hostName)) {
+                node = new TargetNodeComponent(hostName, fRoot, host);
+                fRoot.addChild(node);
             }
-            
-            if (host != null) {
-                // successful creation of host
-                TargetNodeComponent node = null;
-                if (!fRoot.containsChild(hostName)) {
-                    node = new TargetNodeComponent(hostName, fRoot, host);
-                    fRoot.addChild(node);
-                }
-                else {
-                    node = (TargetNodeComponent)fRoot.getChild(hostName);
-                }
-
-                node.connect();
+            else {
+                node = (TargetNodeComponent)fRoot.getChild(hostName);
             }
+
+            node.connect();
         }
         return null;
     }
@@ -137,22 +138,17 @@ public class NewConnectionHandler extends AbstractHandler {
      */
     @Override
     public boolean isEnabled() {
-        fRoot = null;
-
-        // Check if we are closing down
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-        if (window == null) {
+        
+        // Get workbench page for the Control View
+        IWorkbenchPage page = getWorkbenchPage();
+        if (page == null) {
             return false;
         }
 
-        // Check if we are in the Project View
-        IWorkbenchPage page = window.getActivePage();
-        if (page == null) return false;
-        IWorkbenchPart part = page.getActivePart();
-        if (!(part instanceof ControlView)) {
-            return false;
-        }
+        fRoot = null;
 
+        // no need to verify part because it has been already done in getWorkbenchPage()
+        IWorkbenchPart part = page.getActivePart(); 
         fRoot = ((ControlView) part).getTraceControlRoot();
         
         return (fRoot != null);
This page took 0.025427 seconds and 5 git commands to generate.