3786c172bdafbab1cf3a3f77061a87841fbe65e5
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / NewConnectionHandler.java
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.Command;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.remote.core.IRemoteConnection;
25 import org.eclipse.remote.core.IRemoteConnectionType;
26 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
27 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.INewConnectionDialog;
28 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
29 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
30 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
31 import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.commands.ICommandService;
37 import org.eclipse.ui.services.IServiceLocator;
38
39 /**
40 * <p>
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.
46 * </p>
47 *
48 * @author Bernd Hufmann
49 */
50 public class NewConnectionHandler extends BaseControlViewHandler {
51
52 /**
53 * Id of the parameter for the remote services id.
54 * @see NewConnectionHandler
55 * @see IRemoteConnectionType#getId()
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
61 * @see IRemoteConnection#getName()
62 */
63 public static final String PARAMETER_CONNECTION_NAME = "org.eclipse.linuxtools.lttng2.control.ui.connectionNameParameter"; //$NON-NLS-1$
64
65 // ------------------------------------------------------------------------
66 // Attributes
67 // ------------------------------------------------------------------------
68
69 /**
70 * The parent trace control component the new node will be added to.
71 */
72 private ITraceControlComponent fRoot = null;
73
74 @Override
75 public Object execute(ExecutionEvent event) throws ExecutionException {
76 assert (fRoot != null);
77
78 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
79 if (window == null) {
80 return false;
81 }
82
83 IRemoteConnection connection = getConnection(event.getParameters());
84 if (connection != null) {
85 fLock.lock();
86 try {
87 if (fRoot == null) {
88 return null;
89 }
90 // successful creation of host
91 TargetNodeComponent node = null;
92 if (!fRoot.containsChild(connection.getName())) {
93 node = new TargetNodeComponent(connection.getName(), fRoot, connection);
94 fRoot.addChild(node);
95 } else {
96 node = (TargetNodeComponent)fRoot.getChild(connection.getName());
97 }
98
99 node.connect();
100 } finally {
101 fLock.unlock();
102 }
103 }
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
126 return null;
127 }
128
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);
133 if ((remoteServicesId != null) && (connectionName != null)) {
134 return TmfRemoteConnectionFactory.getRemoteConnection(
135 checkNotNull(remoteServicesId.toString()),
136 checkNotNull(connectionName.toString()));
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
148 @Override
149 public boolean isEnabled() {
150
151 // Get workbench page for the Control View
152 IWorkbenchPage page = getWorkbenchPage();
153 if (page == null) {
154 return false;
155 }
156
157 ITraceControlComponent root = null;
158
159 // no need to verify part because it has been already done in getWorkbenchPage()
160 IWorkbenchPart part = page.getActivePart();
161 root = ((ControlView) part).getTraceControlRoot();
162
163 boolean isEnabled = root != null;
164
165 fLock.lock();
166 try {
167 fRoot = null;
168 if (isEnabled) {
169 fRoot = root;
170 }
171 } finally {
172 fLock.unlock();
173 }
174
175 return isEnabled;
176 }
177 }
This page took 0.033818 seconds and 5 git commands to generate.