Commit | Line | Data |
---|---|---|
eb1bab5b BH |
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 | **********************************************************************/ | |
115b4a01 | 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
eb1bab5b BH |
13 | |
14 | import org.eclipse.core.commands.ExecutionEvent; | |
15 | import org.eclipse.core.commands.ExecutionException; | |
eb1bab5b BH |
16 | import org.eclipse.jface.dialogs.MessageDialog; |
17 | import org.eclipse.jface.window.Window; | |
115b4a01 BH |
18 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
19 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages; | |
20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.INewConnectionDialog; | |
21 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; | |
22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent; | |
23 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TargetNodeComponent; | |
eb1bab5b BH |
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 | */ | |
498704b3 | 39 | public class NewConnectionHandler extends BaseControlViewHandler { |
eb1bab5b BH |
40 | |
41 | // ------------------------------------------------------------------------ | |
42 | // Constants | |
43 | // ------------------------------------------------------------------------ | |
44 | /** | |
45 | * The trace control system type defined for LTTng version 2.0 and later. | |
46 | */ | |
115b4a01 | 47 | public final static String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.internal.lttng2.ui.control.systemType"; //$NON-NLS-1$ |
eb1bab5b BH |
48 | |
49 | // ------------------------------------------------------------------------ | |
50 | // Attributes | |
51 | // ------------------------------------------------------------------------ | |
52 | /** | |
53 | * The parent trace control component the new node will be added to. | |
54 | */ | |
4775bcbf | 55 | private ITraceControlComponent fRoot = null; |
eb1bab5b BH |
56 | |
57 | /* | |
58 | * (non-Javadoc) | |
4775bcbf | 59 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
eb1bab5b BH |
60 | */ |
61 | @Override | |
62 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
4775bcbf | 63 | assert (fRoot != null); |
eb1bab5b BH |
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 | |
d132bcc7 BH |
79 | final INewConnectionDialog dialog = TraceControlDialogFactory.getInstance().getNewConnectionDialog(); |
80 | dialog.setTraceControlParent(fRoot); | |
81 | dialog.setHosts(hosts); | |
eb1bab5b | 82 | |
6503ae0f BH |
83 | if (dialog.open() != Window.OK) { |
84 | return null; | |
85 | } | |
eb1bab5b | 86 | |
6503ae0f BH |
87 | String hostName = dialog.getConnectionName(); |
88 | String hostAddress = dialog.getHostName(); | |
eb1bab5b | 89 | |
6503ae0f BH |
90 | // get the singleton RSE registry |
91 | IHost host = null; | |
eb1bab5b | 92 | |
6503ae0f BH |
93 | for (int i = 0; i < hosts.length; i++) { |
94 | if (hosts[i].getAliasName().equals(hostName)) { | |
95 | host = hosts[i]; | |
96 | break; | |
eb1bab5b | 97 | } |
6503ae0f | 98 | } |
eb1bab5b | 99 | |
6503ae0f BH |
100 | if (host == null) { |
101 | // if there's no host then we will create it | |
102 | try { | |
103 | // create the host object as an SSH Only connection | |
104 | host = registry.createHost( | |
105 | sysType, //System Type Name | |
106 | hostName, //Connection name | |
107 | hostAddress, //IP Address | |
108 | "Connection to Host"); //description //$NON-NLS-1$ | |
eb1bab5b | 109 | } |
6503ae0f BH |
110 | catch (Exception e) { |
111 | MessageDialog.openError(window.getShell(), | |
112 | Messages.TraceControl_EclipseCommandFailure, | |
113 | Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
114 | return null; | |
eb1bab5b BH |
115 | } |
116 | } | |
c56972bb | 117 | |
6503ae0f | 118 | if (host != null) { |
c56972bb BH |
119 | fLock.lock(); |
120 | try { | |
121 | // successful creation of host | |
122 | TargetNodeComponent node = null; | |
123 | if (!fRoot.containsChild(hostName)) { | |
124 | node = new TargetNodeComponent(hostName, fRoot, host); | |
125 | fRoot.addChild(node); | |
126 | } | |
127 | else { | |
128 | node = (TargetNodeComponent)fRoot.getChild(hostName); | |
129 | } | |
130 | ||
131 | node.connect(); | |
132 | } finally { | |
133 | fLock.unlock(); | |
6503ae0f | 134 | } |
6503ae0f | 135 | } |
eb1bab5b BH |
136 | return null; |
137 | } | |
138 | ||
4775bcbf | 139 | |
eb1bab5b BH |
140 | /* |
141 | * (non-Javadoc) | |
4775bcbf | 142 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() |
eb1bab5b BH |
143 | */ |
144 | @Override | |
145 | public boolean isEnabled() { | |
498704b3 BH |
146 | |
147 | // Get workbench page for the Control View | |
148 | IWorkbenchPage page = getWorkbenchPage(); | |
149 | if (page == null) { | |
eb1bab5b BH |
150 | return false; |
151 | } | |
152 | ||
c56972bb | 153 | ITraceControlComponent root = null; |
eb1bab5b | 154 | |
498704b3 BH |
155 | // no need to verify part because it has been already done in getWorkbenchPage() |
156 | IWorkbenchPart part = page.getActivePart(); | |
c56972bb BH |
157 | root = ((ControlView) part).getTraceControlRoot(); |
158 | ||
159 | boolean isEnabled = root != null; | |
160 | ||
161 | fLock.lock(); | |
162 | try { | |
163 | fRoot = null; | |
164 | if (isEnabled) { | |
165 | fRoot = root; | |
166 | } | |
167 | } finally { | |
168 | fLock.unlock(); | |
169 | } | |
eb1bab5b | 170 | |
c56972bb | 171 | return isEnabled; |
eb1bab5b BH |
172 | } |
173 | } |