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