Bug 453499: Allow for programmatically adding a connection to the
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / NewConnectionHandler.java
CommitLineData
eb1bab5b 1/**********************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 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
cf9fcae0
MS
16import java.util.Map;
17
eb1bab5b
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
eb1bab5b 20import org.eclipse.jface.window.Window;
b732adaa 21import org.eclipse.remote.core.IRemoteConnection;
cf9fcae0
MS
22import org.eclipse.remote.core.IRemoteServices;
23import org.eclipse.remote.core.RemoteServices;
9bc60be7 24import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
cf9fcae0 25import org.eclipse.tracecompass.internal.lttng2.control.ui.views.Workaround_Bug449362;
9bc60be7
AM
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;
eb1bab5b
BH
30import org.eclipse.ui.IWorkbenchPage;
31import org.eclipse.ui.IWorkbenchPart;
32import org.eclipse.ui.IWorkbenchWindow;
33import org.eclipse.ui.PlatformUI;
34
35/**
eb1bab5b 36 * <p>
cf9fcae0
MS
37 * Command handler for creation of a new connection for trace control.
38 * <br> By supplying arguments for the parameters with id {@link #PARAMETER_REMOTE_SERVICES_ID} and
39 * {@link #PARAMETER_CONNECTION_NAME}, the caller can specify the remote connection that will
40 * be added to the trace control. In case one of the optional arguments is not supplied, the handler
41 * opens a dialog for selecting a remote connection.
eb1bab5b 42 * </p>
ea21cd65 43 *
dbd4432d 44 * @author Bernd Hufmann
eb1bab5b 45 */
498704b3 46public class NewConnectionHandler extends BaseControlViewHandler {
eb1bab5b 47
cf9fcae0
MS
48 /**
49 * Id of the parameter for the remote services id.
50 * @see NewConnectionHandler
51 * @see IRemoteServices#getId()
52 */
53 public static final String PARAMETER_REMOTE_SERVICES_ID = "org.eclipse.linuxtools.lttng2.control.ui.remoteServicesIdParameter"; //$NON-NLS-1$
54 /**
55 * Id of the parameter for the name of the remote connection.
56 * @see NewConnectionHandler
57 * @see IRemoteServices#getName()
58 */
59 public static final String PARAMETER_CONNECTION_NAME = "org.eclipse.linuxtools.lttng2.control.ui.connectionNameParameter"; //$NON-NLS-1$
60
eb1bab5b
BH
61 // ------------------------------------------------------------------------
62 // Attributes
63 // ------------------------------------------------------------------------
11252342 64
eb1bab5b 65 /**
ea21cd65 66 * The parent trace control component the new node will be added to.
eb1bab5b 67 */
4775bcbf 68 private ITraceControlComponent fRoot = null;
eb1bab5b 69
eb1bab5b
BH
70 @Override
71 public Object execute(ExecutionEvent event) throws ExecutionException {
4775bcbf 72 assert (fRoot != null);
eb1bab5b
BH
73
74 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
75 if (window == null) {
76 return false;
77 }
78
cf9fcae0
MS
79 IRemoteConnection connection = getConnection(event.getParameters());
80 if (connection != null) {
c56972bb
BH
81 fLock.lock();
82 try {
83 // successful creation of host
84 TargetNodeComponent node = null;
cf9fcae0
MS
85 if (!fRoot.containsChild(connection.getName())) {
86 node = new TargetNodeComponent(connection.getName(), fRoot, connection);
c56972bb 87 fRoot.addChild(node);
b732adaa 88 } else {
cf9fcae0 89 node = (TargetNodeComponent)fRoot.getChild(connection.getName());
c56972bb
BH
90 }
91
92 node.connect();
93 } finally {
94 fLock.unlock();
6503ae0f 95 }
6503ae0f 96 }
eb1bab5b
BH
97 return null;
98 }
99
cf9fcae0
MS
100 private static IRemoteConnection getConnection(Map<?,?> parameters) {
101 // First check whether arguments have been supplied
102 Object remoteServicesId = parameters.get(PARAMETER_REMOTE_SERVICES_ID);
103 Object connectionName = parameters.get(PARAMETER_CONNECTION_NAME);
104 if (remoteServicesId != null && connectionName != null) {
105 if (!Workaround_Bug449362.triggerRSEStartup(remoteServicesId.toString())) {
106 // Skip the connection in order to avoid an infinite loop
107 } else {
108 IRemoteServices rs = RemoteServices.getRemoteServices(remoteServicesId.toString());
109 if (rs != null) {
110 return rs.getConnectionManager().getConnection(connectionName.toString());
111 }
112 }
113 return null;
114 }
115
116 // Without the arguments, open dialog box for the node name and address
117 final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog();
118 if (dialog.open() == Window.OK) {
119 return dialog.getConnection();
120 }
121
122 return null;
123 }
124
eb1bab5b
BH
125 @Override
126 public boolean isEnabled() {
ea21cd65 127
498704b3
BH
128 // Get workbench page for the Control View
129 IWorkbenchPage page = getWorkbenchPage();
130 if (page == null) {
eb1bab5b
BH
131 return false;
132 }
133
c56972bb 134 ITraceControlComponent root = null;
eb1bab5b 135
498704b3 136 // no need to verify part because it has been already done in getWorkbenchPage()
ea21cd65 137 IWorkbenchPart part = page.getActivePart();
c56972bb 138 root = ((ControlView) part).getTraceControlRoot();
ea21cd65 139
c56972bb 140 boolean isEnabled = root != null;
ea21cd65 141
c56972bb
BH
142 fLock.lock();
143 try {
144 fRoot = null;
145 if (isEnabled) {
146 fRoot = root;
147 }
148 } finally {
149 fLock.unlock();
150 }
ea21cd65 151
c56972bb 152 return isEnabled;
eb1bab5b
BH
153 }
154}
This page took 0.121731 seconds and 5 git commands to generate.