control: Base code for profile dialog window
[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
ad9972cc 20import org.eclipse.core.commands.Command;
eb1bab5b
BH
21import org.eclipse.core.commands.ExecutionEvent;
22import org.eclipse.core.commands.ExecutionException;
eb1bab5b 23import org.eclipse.jface.window.Window;
b732adaa 24import org.eclipse.remote.core.IRemoteConnection;
533d0bc3 25import org.eclipse.remote.core.IRemoteConnectionType;
9bc60be7
AM
26import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
27import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.INewConnectionDialog;
28import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
9bc60be7
AM
29import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
30import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
b6b4e8b4 31import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory;
eb1bab5b
BH
32import org.eclipse.ui.IWorkbenchPage;
33import org.eclipse.ui.IWorkbenchPart;
34import org.eclipse.ui.IWorkbenchWindow;
35import org.eclipse.ui.PlatformUI;
ad9972cc
PJPG
36import org.eclipse.ui.commands.ICommandService;
37import org.eclipse.ui.services.IServiceLocator;
eb1bab5b
BH
38
39/**
eb1bab5b 40 * <p>
cf9fcae0
MS
41 * Command handler for creation of a new connection for trace control.
42 * <br> By supplying arguments for the parameters with id {@link #PARAMETER_REMOTE_SERVICES_ID} and
43 * {@link #PARAMETER_CONNECTION_NAME}, the caller can specify the remote connection that will
44 * be added to the trace control. In case one of the optional arguments is not supplied, the handler
45 * opens a dialog for selecting a remote connection.
eb1bab5b 46 * </p>
ea21cd65 47 *
dbd4432d 48 * @author Bernd Hufmann
eb1bab5b 49 */
498704b3 50public class NewConnectionHandler extends BaseControlViewHandler {
eb1bab5b 51
cf9fcae0
MS
52 /**
53 * Id of the parameter for the remote services id.
54 * @see NewConnectionHandler
533d0bc3 55 * @see IRemoteConnectionType#getId()
cf9fcae0
MS
56 */
57 public static final String PARAMETER_REMOTE_SERVICES_ID = "org.eclipse.linuxtools.lttng2.control.ui.remoteServicesIdParameter"; //$NON-NLS-1$
58 /**
59 * Id of the parameter for the name of the remote connection.
60 * @see NewConnectionHandler
533d0bc3 61 * @see IRemoteConnection#getName()
cf9fcae0
MS
62 */
63 public static final String PARAMETER_CONNECTION_NAME = "org.eclipse.linuxtools.lttng2.control.ui.connectionNameParameter"; //$NON-NLS-1$
64
eb1bab5b
BH
65 // ------------------------------------------------------------------------
66 // Attributes
67 // ------------------------------------------------------------------------
11252342 68
eb1bab5b 69 /**
ea21cd65 70 * The parent trace control component the new node will be added to.
eb1bab5b 71 */
4775bcbf 72 private ITraceControlComponent fRoot = null;
eb1bab5b 73
eb1bab5b
BH
74 @Override
75 public Object execute(ExecutionEvent event) throws ExecutionException {
4775bcbf 76 assert (fRoot != null);
eb1bab5b
BH
77
78 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
79 if (window == null) {
80 return false;
81 }
82
cf9fcae0
MS
83 IRemoteConnection connection = getConnection(event.getParameters());
84 if (connection != null) {
c56972bb
BH
85 fLock.lock();
86 try {
48a7e2a5
BH
87 if (fRoot == null) {
88 return null;
89 }
c56972bb
BH
90 // successful creation of host
91 TargetNodeComponent node = null;
cf9fcae0
MS
92 if (!fRoot.containsChild(connection.getName())) {
93 node = new TargetNodeComponent(connection.getName(), fRoot, connection);
c56972bb 94 fRoot.addChild(node);
b732adaa 95 } else {
cf9fcae0 96 node = (TargetNodeComponent)fRoot.getChild(connection.getName());
c56972bb
BH
97 }
98
99 node.connect();
100 } finally {
101 fLock.unlock();
6503ae0f 102 }
6503ae0f 103 }
ad9972cc
PJPG
104
105 // Obtain IServiceLocator implementer, e.g. from PlatformUI.getWorkbench():
106 IServiceLocator serviceLocator = PlatformUI.getWorkbench();
107 // or a site from within a editor or view:
108 // IServiceLocator serviceLocator = getSite();
109
110 ICommandService commandService = serviceLocator.getService(ICommandService.class);
111
112
113 // Lookup commmand with its ID
114 Command command = commandService.getCommand("org.eclipse.linuxtools.internal.lttng2.ui.commands.control.createSession"); //$NON-NLS-1$
115
116 // Optionally pass a ExecutionEvent instance, default no-param arg creates blank event
117 try {
118 // execute new connection command directly
119 command.executeWithChecks(new ExecutionEvent());
120 // FIX THIS OH GOD THE INHUMANITY
121 } catch (Exception e) {
122
123 e.printStackTrace();
124 }
125
eb1bab5b
BH
126 return null;
127 }
128
cf9fcae0
MS
129 private static IRemoteConnection getConnection(Map<?,?> parameters) {
130 // First check whether arguments have been supplied
131 Object remoteServicesId = parameters.get(PARAMETER_REMOTE_SERVICES_ID);
132 Object connectionName = parameters.get(PARAMETER_CONNECTION_NAME);
533d0bc3 133 if ((remoteServicesId != null) && (connectionName != null)) {
b6b4e8b4 134 return TmfRemoteConnectionFactory.getRemoteConnection(
533d0bc3
BH
135 checkNotNull(remoteServicesId.toString()),
136 checkNotNull(connectionName.toString()));
cf9fcae0
MS
137 }
138
139 // Without the arguments, open dialog box for the node name and address
140 final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog();
141 if (dialog.open() == Window.OK) {
142 return dialog.getConnection();
143 }
144
145 return null;
146 }
147
eb1bab5b
BH
148 @Override
149 public boolean isEnabled() {
ea21cd65 150
498704b3
BH
151 // Get workbench page for the Control View
152 IWorkbenchPage page = getWorkbenchPage();
153 if (page == null) {
eb1bab5b
BH
154 return false;
155 }
156
c56972bb 157 ITraceControlComponent root = null;
eb1bab5b 158
498704b3 159 // no need to verify part because it has been already done in getWorkbenchPage()
ea21cd65 160 IWorkbenchPart part = page.getActivePart();
c56972bb 161 root = ((ControlView) part).getTraceControlRoot();
ea21cd65 162
c56972bb 163 boolean isEnabled = root != null;
ea21cd65 164
c56972bb
BH
165 fLock.lock();
166 try {
167 fRoot = null;
168 if (isEnabled) {
169 fRoot = root;
170 }
171 } finally {
172 fLock.unlock();
173 }
ea21cd65 174
c56972bb 175 return isEnabled;
eb1bab5b
BH
176 }
177}
This page took 0.142493 seconds and 5 git commands to generate.