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 | **********************************************************************/ | |
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.core.commands.IHandler; | |
17 | import org.eclipse.core.commands.IHandlerListener; | |
18 | import org.eclipse.jface.dialogs.MessageDialog; | |
19 | import org.eclipse.jface.window.Window; | |
20 | import org.eclipse.linuxtools.lttng.ui.views.control.ControlView; | |
21 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; | |
22 | import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.INewConnectionDialog; | |
23 | import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.NewConnectionDialog; | |
24 | import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent; | |
25 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TargetNodeComponent; | |
26 | import org.eclipse.rse.core.IRSESystemType; | |
27 | import org.eclipse.rse.core.RSECorePlugin; | |
28 | import org.eclipse.rse.core.model.IHost; | |
29 | import org.eclipse.rse.core.model.ISystemRegistry; | |
30 | import org.eclipse.ui.IWorkbenchPage; | |
31 | import org.eclipse.ui.IWorkbenchPart; | |
32 | import org.eclipse.ui.IWorkbenchWindow; | |
33 | import org.eclipse.ui.PlatformUI; | |
34 | ||
35 | /** | |
36 | * <b><u>NewConnectionHandler</u></b> | |
37 | * <p> | |
38 | * Command handler for creation new connection for trace control. | |
39 | * </p> | |
40 | */ | |
41 | public class NewConnectionHandler implements IHandler { | |
42 | ||
43 | // ------------------------------------------------------------------------ | |
44 | // Constants | |
45 | // ------------------------------------------------------------------------ | |
46 | /** | |
47 | * The trace control system type defined for LTTng version 2.0 and later. | |
48 | */ | |
49 | public final static String TRACE_CONTROL_SYSTEM_TYPE = "org.eclipse.linuxtools.lttng.ui.control.systemType"; //$NON-NLS-1$ | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | /** | |
55 | * The parent trace control component the new node will be added to. | |
56 | */ | |
57 | private ITraceControlComponent fParent = null; | |
58 | ||
59 | /* | |
60 | * (non-Javadoc) | |
61 | * @see org.eclipse.core.commands.IHandler#dispose() | |
62 | */ | |
63 | @Override | |
64 | public void dispose() { | |
65 | } | |
66 | ||
67 | /* | |
68 | * (non-Javadoc) | |
69 | * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
70 | */ | |
71 | @Override | |
72 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
73 | assert (fParent != null); | |
74 | ||
75 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
76 | if (window == null) { | |
77 | return false; | |
78 | } | |
79 | ||
80 | ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry(); | |
81 | ||
82 | // get system type definition for LTTng 2.x connection | |
83 | IRSESystemType sysType = RSECorePlugin.getTheCoreRegistry().getSystemTypeById(TRACE_CONTROL_SYSTEM_TYPE); | |
84 | ||
85 | // get all hosts for this system type | |
86 | IHost[] hosts = registry.getHostsBySystemType(sysType); | |
87 | ||
88 | // Open dialog box for the node name and address | |
89 | INewConnectionDialog dialog = new NewConnectionDialog(window.getShell(), fParent, hosts); | |
90 | ||
91 | if (dialog.open() == Window.OK) { | |
92 | ||
93 | String hostName = dialog.getNodeName(); | |
94 | String hostAddress = dialog.getNodeAddress(); | |
95 | ||
96 | // String hostName = "hallo"; | |
97 | // String hostAddress = "142.133.166.54"; | |
98 | // String hostName = "ha"; | |
99 | // String hostAddress = "192.168.0.196"; | |
100 | // get the singleton RSE registry | |
101 | IHost host = null; | |
102 | ||
103 | for (int i = 0; i < hosts.length; i++) { | |
104 | if (hosts[i].getAliasName().equals(hostName)) { | |
105 | host = hosts[i]; | |
106 | break; | |
107 | } | |
108 | } | |
109 | ||
110 | if (host == null) { | |
111 | // if there's no host then we will create it | |
112 | try { | |
113 | ||
114 | // create the host object as an SSH Only connection | |
115 | host = registry.createHost( | |
116 | sysType, //System Type Name | |
117 | hostName, //Connection name | |
118 | hostAddress, //IP Address | |
119 | "Connection to Host"); //description //$NON-NLS-1$ | |
120 | } | |
121 | catch (Exception e) { | |
122 | MessageDialog.openError(window.getShell(), | |
123 | Messages.TraceControl_EclipseCommandFailure, | |
124 | Messages.TraceControl_NewNodeCreationFailure + " (" + hostName + ", " + hostAddress + ")" + ":\n" + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
125 | return null; | |
126 | } | |
127 | } | |
128 | ||
129 | if (host != null) { | |
130 | // successful creation of host | |
131 | TargetNodeComponent node = null; | |
132 | if (!fParent.containsChild(hostName)) { | |
133 | node = new TargetNodeComponent(hostName, fParent, host); | |
134 | fParent.addChild(node); | |
135 | } | |
136 | else { | |
137 | node = (TargetNodeComponent)fParent.getChild(hostName); | |
138 | } | |
139 | ||
140 | node.connect(); | |
141 | } | |
142 | } | |
143 | return null; | |
144 | } | |
145 | ||
146 | /* | |
147 | * (non-Javadoc) | |
148 | * @see org.eclipse.core.commands.IHandler#isEnabled() | |
149 | */ | |
150 | @Override | |
151 | public boolean isEnabled() { | |
152 | fParent = null; | |
153 | ||
154 | // Check if we are closing down | |
155 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
156 | if (window == null) { | |
157 | return false; | |
158 | } | |
159 | ||
160 | // Check if we are in the Project View | |
161 | IWorkbenchPage page = window.getActivePage(); | |
162 | if (page == null) return false; | |
163 | IWorkbenchPart part = page.getActivePart(); | |
164 | if (!(part instanceof ControlView)) { | |
165 | return false; | |
166 | } | |
167 | ||
168 | fParent = ((ControlView) part).getTraceControlRoot(); | |
169 | ||
170 | return (fParent != null); | |
171 | } | |
172 | ||
173 | /* | |
174 | * (non-Javadoc) | |
175 | * @see org.eclipse.core.commands.IHandler#isHandled() | |
176 | */ | |
177 | @Override | |
178 | public boolean isHandled() { | |
179 | // TODO Auto-generated method stub | |
180 | return true; | |
181 | } | |
182 | ||
183 | // ------------------------------------------------------------------------ | |
184 | // IHandlerListener | |
185 | // ------------------------------------------------------------------------ | |
186 | ||
187 | /* | |
188 | * (non-Javadoc) | |
189 | * @see org.eclipse.core.commands.IHandler#addHandlerListener(org.eclipse.core.commands.IHandlerListener) | |
190 | */ | |
191 | @Override | |
192 | public void addHandlerListener(IHandlerListener handlerListener) { | |
193 | } | |
194 | ||
195 | /* | |
196 | * (non-Javadoc) | |
197 | * @see org.eclipse.core.commands.IHandler#removeHandlerListener(org.eclipse.core.commands.IHandlerListener) | |
198 | */ | |
199 | @Override | |
200 | public void removeHandlerListener(IHandlerListener handlerListener) { | |
201 | } | |
202 | } |