control: Add enhanced support for loading of sessions
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / NewConnectionHandler.java
CommitLineData
eb1bab5b 1/**********************************************************************
533d0bc3 2 * Copyright (c) 2012, 2015 Ericsson and others
ea21cd65 3 *
eb1bab5b
BH
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
ea21cd65
AM
8 *
9 * Contributors:
ef5356e1
BH
10 * Bernd Hufmann - Initial API and implementation
11 * Anna Dushistova(Montavista) - [382684] Allow reusing already defined connections that have Files and Shells subsystems
b732adaa 12 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
eb1bab5b 13 **********************************************************************/
9bc60be7 14package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers;
eb1bab5b 15
ec619615
BH
16import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
cf9fcae0
MS
18import java.util.Map;
19
eb1bab5b
BH
20import org.eclipse.core.commands.ExecutionEvent;
21import org.eclipse.core.commands.ExecutionException;
eb1bab5b 22import org.eclipse.jface.window.Window;
b732adaa 23import org.eclipse.remote.core.IRemoteConnection;
533d0bc3 24import org.eclipse.remote.core.IRemoteConnectionType;
9bc60be7
AM
25import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
26import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.INewConnectionDialog;
27import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
9bc60be7
AM
28import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
29import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
b6b4e8b4 30import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory;
eb1bab5b
BH
31import org.eclipse.ui.IWorkbenchPage;
32import org.eclipse.ui.IWorkbenchPart;
33import org.eclipse.ui.IWorkbenchWindow;
34import org.eclipse.ui.PlatformUI;
35
36/**
eb1bab5b 37 * <p>
cf9fcae0
MS
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.
eb1bab5b 43 * </p>
ea21cd65 44 *
dbd4432d 45 * @author Bernd Hufmann
eb1bab5b 46 */
498704b3 47public class NewConnectionHandler extends BaseControlViewHandler {
eb1bab5b 48
cf9fcae0
MS
49 /**
50 * Id of the parameter for the remote services id.
51 * @see NewConnectionHandler
533d0bc3 52 * @see IRemoteConnectionType#getId()
cf9fcae0
MS
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
533d0bc3 58 * @see IRemoteConnection#getName()
cf9fcae0
MS
59 */
60 public static final String PARAMETER_CONNECTION_NAME = "org.eclipse.linuxtools.lttng2.control.ui.connectionNameParameter"; //$NON-NLS-1$
61
eb1bab5b
BH
62 // ------------------------------------------------------------------------
63 // Attributes
64 // ------------------------------------------------------------------------
11252342 65
eb1bab5b 66 /**
ea21cd65 67 * The parent trace control component the new node will be added to.
eb1bab5b 68 */
4775bcbf 69 private ITraceControlComponent fRoot = null;
eb1bab5b 70
eb1bab5b
BH
71 @Override
72 public Object execute(ExecutionEvent event) throws ExecutionException {
4775bcbf 73 assert (fRoot != null);
eb1bab5b
BH
74
75 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
76 if (window == null) {
77 return false;
78 }
79
cf9fcae0
MS
80 IRemoteConnection connection = getConnection(event.getParameters());
81 if (connection != null) {
c56972bb
BH
82 fLock.lock();
83 try {
48a7e2a5
BH
84 if (fRoot == null) {
85 return null;
86 }
c56972bb
BH
87 // successful creation of host
88 TargetNodeComponent node = null;
cf9fcae0
MS
89 if (!fRoot.containsChild(connection.getName())) {
90 node = new TargetNodeComponent(connection.getName(), fRoot, connection);
c56972bb 91 fRoot.addChild(node);
b732adaa 92 } else {
cf9fcae0 93 node = (TargetNodeComponent)fRoot.getChild(connection.getName());
c56972bb
BH
94 }
95
96 node.connect();
97 } finally {
98 fLock.unlock();
6503ae0f 99 }
6503ae0f 100 }
eb1bab5b
BH
101 return null;
102 }
103
cf9fcae0
MS
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);
533d0bc3 108 if ((remoteServicesId != null) && (connectionName != null)) {
b6b4e8b4 109 return TmfRemoteConnectionFactory.getRemoteConnection(
533d0bc3
BH
110 checkNotNull(remoteServicesId.toString()),
111 checkNotNull(connectionName.toString()));
cf9fcae0
MS
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
eb1bab5b
BH
123 @Override
124 public boolean isEnabled() {
ea21cd65 125
498704b3
BH
126 // Get workbench page for the Control View
127 IWorkbenchPage page = getWorkbenchPage();
128 if (page == null) {
eb1bab5b
BH
129 return false;
130 }
131
c56972bb 132 ITraceControlComponent root = null;
eb1bab5b 133
498704b3 134 // no need to verify part because it has been already done in getWorkbenchPage()
ea21cd65 135 IWorkbenchPart part = page.getActivePart();
c56972bb 136 root = ((ControlView) part).getTraceControlRoot();
ea21cd65 137
c56972bb 138 boolean isEnabled = root != null;
ea21cd65 139
c56972bb
BH
140 fLock.lock();
141 try {
142 fRoot = null;
143 if (isEnabled) {
144 fRoot = root;
145 }
146 } finally {
147 fLock.unlock();
148 }
ea21cd65 149
c56972bb 150 return isEnabled;
eb1bab5b
BH
151 }
152}
This page took 0.077235 seconds and 5 git commands to generate.