0fba7326ba2dcb693f185f65737d8f427bc9ed4d
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / NewConnectionHandler.java
1 /**********************************************************************
2 * Copyright (c) 2012 Ericsson
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 **********************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
13
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
19 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
20 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.INewConnectionDialog;
21 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.NewConnectionDialog;
22 import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent;
23 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TargetNodeComponent;
24 import org.eclipse.rse.core.IRSESystemType;
25 import org.eclipse.rse.core.RSECorePlugin;
26 import org.eclipse.rse.core.model.IHost;
27 import org.eclipse.rse.core.model.ISystemRegistry;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchPart;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32
33 /**
34 * <b><u>NewConnectionHandler</u></b>
35 * <p>
36 * Command handler for creation new connection for trace control.
37 * </p>
38 */
39 public class NewConnectionHandler extends BaseControlViewHandler {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
44 /**
45 * The trace control system type defined for LTTng version 2.0 and later.
46 */
47 public final static String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.lttng.ui.control.systemType"; //$NON-NLS-1$
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52 /**
53 * The parent trace control component the new node will be added to.
54 */
55 private ITraceControlComponent fRoot = null;
56
57 /*
58 * (non-Javadoc)
59 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
60 */
61 @Override
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63 assert (fRoot != null);
64
65 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
66 if (window == null) {
67 return false;
68 }
69
70 ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry();
71
72 // get system type definition for LTTng 2.x connection
73 IRSESystemType sysType = RSECorePlugin.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE);
74
75 // get all hosts for this system type
76 IHost[] hosts = registry.getHostsBySystemType(sysType);
77
78 // Open dialog box for the node name and address
79 INewConnectionDialog dialog = new NewConnectionDialog(window.getShell(), fRoot, hosts);
80
81 if (dialog.open() != Window.OK) {
82 return null;
83 }
84
85 String hostName = dialog.getConnectionName();
86 String hostAddress = dialog.getHostName();
87
88 // get the singleton RSE registry
89 IHost host = null;
90
91 for (int i = 0; i < hosts.length; i++) {
92 if (hosts[i].getAliasName().equals(hostName)) {
93 host = hosts[i];
94 break;
95 }
96 }
97
98 if (host == null) {
99 // if there's no host then we will create it
100 try {
101 // create the host object as an SSH Only connection
102 host = registry.createHost(
103 sysType, //System Type Name
104 hostName, //Connection name
105 hostAddress, //IP Address
106 "Connection to Host"); //description //$NON-NLS-1$
107 }
108 catch (Exception e) {
109 MessageDialog.openError(window.getShell(),
110 Messages.TraceControl_EclipseCommandFailure,
111 Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
112 return null;
113 }
114 }
115
116 if (host != null) {
117 // successful creation of host
118 TargetNodeComponent node = null;
119 if (!fRoot.containsChild(hostName)) {
120 node = new TargetNodeComponent(hostName, fRoot, host);
121 fRoot.addChild(node);
122 }
123 else {
124 node = (TargetNodeComponent)fRoot.getChild(hostName);
125 }
126
127 node.connect();
128 }
129 return null;
130 }
131
132
133 /*
134 * (non-Javadoc)
135 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
136 */
137 @Override
138 public boolean isEnabled() {
139
140 // Get workbench page for the Control View
141 IWorkbenchPage page = getWorkbenchPage();
142 if (page == null) {
143 return false;
144 }
145
146 fRoot = null;
147
148 // no need to verify part because it has been already done in getWorkbenchPage()
149 IWorkbenchPart part = page.getActivePart();
150 fRoot = ((ControlView) part).getTraceControlRoot();
151
152 return (fRoot != null);
153 }
154 }
This page took 0.034002 seconds and 5 git commands to generate.