| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2015 Ericsson and others |
| 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 | * Anna Dushistova(Montavista) - [382684] Allow reusing already defined connections that have Files and Shells subsystems |
| 12 | * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE |
| 13 | **********************************************************************/ |
| 14 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers; |
| 15 | |
| 16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.eclipse.core.commands.ExecutionEvent; |
| 21 | import org.eclipse.core.commands.ExecutionException; |
| 22 | import org.eclipse.jface.window.Window; |
| 23 | import org.eclipse.remote.core.IRemoteConnection; |
| 24 | import org.eclipse.remote.core.IRemoteConnectionType; |
| 25 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView; |
| 26 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.INewConnectionDialog; |
| 27 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory; |
| 28 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent; |
| 29 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent; |
| 30 | import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory; |
| 31 | import org.eclipse.ui.IWorkbenchPage; |
| 32 | import org.eclipse.ui.IWorkbenchPart; |
| 33 | import org.eclipse.ui.IWorkbenchWindow; |
| 34 | import org.eclipse.ui.PlatformUI; |
| 35 | |
| 36 | /** |
| 37 | * <p> |
| 38 | * Command handler for creation of a new connection for trace control. |
| 39 | * <br> By supplying arguments for the parameters with id {@link #PARAMETER_REMOTE_SERVICES_ID} and |
| 40 | * {@link #PARAMETER_CONNECTION_NAME}, the caller can specify the remote connection that will |
| 41 | * be added to the trace control. In case one of the optional arguments is not supplied, the handler |
| 42 | * opens a dialog for selecting a remote connection. |
| 43 | * </p> |
| 44 | * |
| 45 | * @author Bernd Hufmann |
| 46 | */ |
| 47 | public class NewConnectionHandler extends BaseControlViewHandler { |
| 48 | |
| 49 | /** |
| 50 | * Id of the parameter for the remote services id. |
| 51 | * @see NewConnectionHandler |
| 52 | * @see IRemoteConnectionType#getId() |
| 53 | */ |
| 54 | public static final String PARAMETER_REMOTE_SERVICES_ID = "org.eclipse.linuxtools.lttng2.control.ui.remoteServicesIdParameter"; //$NON-NLS-1$ |
| 55 | /** |
| 56 | * Id of the parameter for the name of the remote connection. |
| 57 | * @see NewConnectionHandler |
| 58 | * @see IRemoteConnection#getName() |
| 59 | */ |
| 60 | public static final String PARAMETER_CONNECTION_NAME = "org.eclipse.linuxtools.lttng2.control.ui.connectionNameParameter"; //$NON-NLS-1$ |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Attributes |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | /** |
| 67 | * The parent trace control component the new node will be added to. |
| 68 | */ |
| 69 | private ITraceControlComponent fRoot = null; |
| 70 | |
| 71 | @Override |
| 72 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 73 | assert (fRoot != null); |
| 74 | |
| 75 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 76 | if (window == null) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | IRemoteConnection connection = getConnection(event.getParameters()); |
| 81 | if (connection != null) { |
| 82 | fLock.lock(); |
| 83 | try { |
| 84 | if (fRoot == null) { |
| 85 | return null; |
| 86 | } |
| 87 | // successful creation of host |
| 88 | TargetNodeComponent node = null; |
| 89 | if (!fRoot.containsChild(connection.getName())) { |
| 90 | node = new TargetNodeComponent(connection.getName(), fRoot, connection); |
| 91 | fRoot.addChild(node); |
| 92 | } else { |
| 93 | node = (TargetNodeComponent)fRoot.getChild(connection.getName()); |
| 94 | } |
| 95 | |
| 96 | node.connect(); |
| 97 | } finally { |
| 98 | fLock.unlock(); |
| 99 | } |
| 100 | } |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | private static IRemoteConnection getConnection(Map<?,?> parameters) { |
| 105 | // First check whether arguments have been supplied |
| 106 | Object remoteServicesId = parameters.get(PARAMETER_REMOTE_SERVICES_ID); |
| 107 | Object connectionName = parameters.get(PARAMETER_CONNECTION_NAME); |
| 108 | if ((remoteServicesId != null) && (connectionName != null)) { |
| 109 | return TmfRemoteConnectionFactory.getRemoteConnection( |
| 110 | checkNotNull(remoteServicesId.toString()), |
| 111 | checkNotNull(connectionName.toString())); |
| 112 | } |
| 113 | |
| 114 | // Without the arguments, open dialog box for the node name and address |
| 115 | final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog(); |
| 116 | if (dialog.open() == Window.OK) { |
| 117 | return dialog.getConnection(); |
| 118 | } |
| 119 | |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean isEnabled() { |
| 125 | |
| 126 | // Get workbench page for the Control View |
| 127 | IWorkbenchPage page = getWorkbenchPage(); |
| 128 | if (page == null) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | ITraceControlComponent root = null; |
| 133 | |
| 134 | // no need to verify part because it has been already done in getWorkbenchPage() |
| 135 | IWorkbenchPart part = page.getActivePart(); |
| 136 | root = ((ControlView) part).getTraceControlRoot(); |
| 137 | |
| 138 | boolean isEnabled = root != null; |
| 139 | |
| 140 | fLock.lock(); |
| 141 | try { |
| 142 | fRoot = null; |
| 143 | if (isEnabled) { |
| 144 | fRoot = root; |
| 145 | } |
| 146 | } finally { |
| 147 | fLock.unlock(); |
| 148 | } |
| 149 | |
| 150 | return isEnabled; |
| 151 | } |
| 152 | } |