| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.lttng.ui.views.control.handlers; |
| 13 | |
| 14 | import org.eclipse.core.commands.AbstractHandler; |
| 15 | import org.eclipse.core.commands.ExecutionEvent; |
| 16 | import org.eclipse.core.commands.ExecutionException; |
| 17 | import org.eclipse.jface.dialogs.MessageDialog; |
| 18 | import org.eclipse.jface.window.Window; |
| 19 | import org.eclipse.linuxtools.lttng.ui.views.control.ControlView; |
| 20 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; |
| 21 | import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.INewConnectionDialog; |
| 22 | import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.NewConnectionDialog; |
| 23 | import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent; |
| 24 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TargetNodeComponent; |
| 25 | import org.eclipse.rse.core.IRSESystemType; |
| 26 | import org.eclipse.rse.core.RSECorePlugin; |
| 27 | import org.eclipse.rse.core.model.IHost; |
| 28 | import org.eclipse.rse.core.model.ISystemRegistry; |
| 29 | import org.eclipse.ui.IWorkbenchPage; |
| 30 | import org.eclipse.ui.IWorkbenchPart; |
| 31 | import org.eclipse.ui.IWorkbenchWindow; |
| 32 | import org.eclipse.ui.PlatformUI; |
| 33 | |
| 34 | /** |
| 35 | * <b><u>NewConnectionHandler</u></b> |
| 36 | * <p> |
| 37 | * Command handler for creation new connection for trace control. |
| 38 | * </p> |
| 39 | */ |
| 40 | public class NewConnectionHandler extends AbstractHandler { |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Constants |
| 44 | // ------------------------------------------------------------------------ |
| 45 | /** |
| 46 | * The trace control system type defined for LTTng version 2.0 and later. |
| 47 | */ |
| 48 | public final static String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.lttng.ui.control.systemType"; //$NON-NLS-1$ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Attributes |
| 52 | // ------------------------------------------------------------------------ |
| 53 | /** |
| 54 | * The parent trace control component the new node will be added to. |
| 55 | */ |
| 56 | private ITraceControlComponent fRoot = null; |
| 57 | |
| 58 | /* |
| 59 | * (non-Javadoc) |
| 60 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 61 | */ |
| 62 | @Override |
| 63 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 64 | assert (fRoot != null); |
| 65 | |
| 66 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 67 | if (window == null) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry(); |
| 72 | |
| 73 | // get system type definition for LTTng 2.x connection |
| 74 | IRSESystemType sysType = RSECorePlugin.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE); |
| 75 | |
| 76 | // get all hosts for this system type |
| 77 | IHost[] hosts = registry.getHostsBySystemType(sysType); |
| 78 | |
| 79 | // Open dialog box for the node name and address |
| 80 | INewConnectionDialog dialog = new NewConnectionDialog(window.getShell(), fRoot, hosts); |
| 81 | |
| 82 | if (dialog.open() == Window.OK) { |
| 83 | |
| 84 | String hostName = dialog.getConnectionName(); |
| 85 | String hostAddress = dialog.getHostName(); |
| 86 | |
| 87 | // get the singleton RSE registry |
| 88 | IHost host = null; |
| 89 | |
| 90 | for (int i = 0; i < hosts.length; i++) { |
| 91 | if (hosts[i].getAliasName().equals(hostName)) { |
| 92 | host = hosts[i]; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (host == null) { |
| 98 | // if there's no host then we will create it |
| 99 | try { |
| 100 | // create the host object as an SSH Only connection |
| 101 | host = registry.createHost( |
| 102 | sysType, //System Type Name |
| 103 | hostName, //Connection name |
| 104 | hostAddress, //IP Address |
| 105 | "Connection to Host"); //description //$NON-NLS-1$ |
| 106 | } |
| 107 | catch (Exception e) { |
| 108 | MessageDialog.openError(window.getShell(), |
| 109 | Messages.TraceControl_EclipseCommandFailure, |
| 110 | Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 111 | return null; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (host != null) { |
| 116 | // successful creation of host |
| 117 | TargetNodeComponent node = null; |
| 118 | if (!fRoot.containsChild(hostName)) { |
| 119 | node = new TargetNodeComponent(hostName, fRoot, host); |
| 120 | fRoot.addChild(node); |
| 121 | } |
| 122 | else { |
| 123 | node = (TargetNodeComponent)fRoot.getChild(hostName); |
| 124 | } |
| 125 | |
| 126 | node.connect(); |
| 127 | } |
| 128 | } |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* |
| 134 | * (non-Javadoc) |
| 135 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() |
| 136 | */ |
| 137 | @Override |
| 138 | public boolean isEnabled() { |
| 139 | fRoot = null; |
| 140 | |
| 141 | // Check if we are closing down |
| 142 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 143 | if (window == null) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // Check if we are in the Control View |
| 148 | IWorkbenchPage page = window.getActivePage(); |
| 149 | if (page == null) return false; |
| 150 | IWorkbenchPart part = page.getActivePart(); |
| 151 | if (!(part instanceof ControlView)) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | fRoot = ((ControlView) part).getTraceControlRoot(); |
| 156 | |
| 157 | return (fRoot != null); |
| 158 | } |
| 159 | } |