79dfdef8b70ff9e42338cf340858ff3a1e1bda81
[deliverable/tracecompass.git] / 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.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 // successful creation of host
85 TargetNodeComponent node = null;
86 if (!fRoot.containsChild(connection.getName())) {
87 node = new TargetNodeComponent(connection.getName(), fRoot, connection);
88 fRoot.addChild(node);
89 } else {
90 node = (TargetNodeComponent)fRoot.getChild(connection.getName());
91 }
92
93 node.connect();
94 } finally {
95 fLock.unlock();
96 }
97 }
98 return null;
99 }
100
101 private static IRemoteConnection getConnection(Map<?,?> parameters) {
102 // First check whether arguments have been supplied
103 Object remoteServicesId = parameters.get(PARAMETER_REMOTE_SERVICES_ID);
104 Object connectionName = parameters.get(PARAMETER_CONNECTION_NAME);
105 if ((remoteServicesId != null) && (connectionName != null)) {
106 return TmfRemoteConnectionFactory.getRemoteConnection(
107 checkNotNull(remoteServicesId.toString()),
108 checkNotNull(connectionName.toString()));
109 }
110
111 // Without the arguments, open dialog box for the node name and address
112 final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog();
113 if (dialog.open() == Window.OK) {
114 return dialog.getConnection();
115 }
116
117 return null;
118 }
119
120 @Override
121 public boolean isEnabled() {
122
123 // Get workbench page for the Control View
124 IWorkbenchPage page = getWorkbenchPage();
125 if (page == null) {
126 return false;
127 }
128
129 ITraceControlComponent root = null;
130
131 // no need to verify part because it has been already done in getWorkbenchPage()
132 IWorkbenchPart part = page.getActivePart();
133 root = ((ControlView) part).getTraceControlRoot();
134
135 boolean isEnabled = root != null;
136
137 fLock.lock();
138 try {
139 fRoot = null;
140 if (isEnabled) {
141 fRoot = root;
142 }
143 } finally {
144 fLock.unlock();
145 }
146
147 return isEnabled;
148 }
149 }
This page took 0.055624 seconds and 4 git commands to generate.