Commit | Line | Data |
---|---|---|
eb1bab5b | 1 | /********************************************************************** |
b732adaa | 2 | * Copyright (c) 2012, 2014 Ericsson |
cfdb727a | 3 | * |
eb1bab5b BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
eb1bab5b | 10 | * Bernd Hufmann - Initial API and implementation |
b732adaa | 11 | * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE |
eb1bab5b | 12 | **********************************************************************/ |
9bc60be7 | 13 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs; |
eb1bab5b | 14 | |
b732adaa | 15 | import static java.text.MessageFormat.format; |
eb1bab5b | 16 | |
b732adaa MS |
17 | import java.util.ArrayList; |
18 | import java.util.Arrays; | |
19 | import java.util.Collections; | |
20 | import java.util.HashSet; | |
21 | import java.util.List; | |
22 | import java.util.Set; | |
23 | ||
24 | import org.eclipse.core.runtime.IConfigurationElement; | |
25 | import org.eclipse.core.runtime.IExtensionPoint; | |
26 | import org.eclipse.core.runtime.IExtensionRegistry; | |
27 | import org.eclipse.core.runtime.Platform; | |
eb1bab5b BH |
28 | import org.eclipse.jface.dialogs.Dialog; |
29 | import org.eclipse.jface.dialogs.IDialogConstants; | |
b732adaa MS |
30 | import org.eclipse.jface.viewers.DoubleClickEvent; |
31 | import org.eclipse.jface.viewers.IDoubleClickListener; | |
32 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
33 | import org.eclipse.jface.viewers.IStructuredSelection; | |
34 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
35 | import org.eclipse.jface.viewers.LabelProvider; | |
36 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
37 | import org.eclipse.jface.viewers.StructuredSelection; | |
38 | import org.eclipse.jface.viewers.TreeViewer; | |
39 | import org.eclipse.jface.viewers.Viewer; | |
40 | import org.eclipse.remote.core.IRemoteConnection; | |
41 | import org.eclipse.remote.core.IRemoteConnectionWorkingCopy; | |
42 | import org.eclipse.remote.core.IRemoteServices; | |
43 | import org.eclipse.remote.core.RemoteServices; | |
44 | import org.eclipse.remote.ui.IRemoteUIConnectionManager; | |
45 | import org.eclipse.remote.ui.IRemoteUIConnectionWizard; | |
46 | import org.eclipse.remote.ui.IRemoteUIServices; | |
47 | import org.eclipse.remote.ui.RemoteUIServices; | |
eb1bab5b | 48 | import org.eclipse.swt.SWT; |
b732adaa | 49 | import org.eclipse.swt.events.SelectionAdapter; |
eb1bab5b | 50 | import org.eclipse.swt.events.SelectionEvent; |
b732adaa | 51 | import org.eclipse.swt.graphics.Image; |
eb1bab5b BH |
52 | import org.eclipse.swt.layout.GridData; |
53 | import org.eclipse.swt.layout.GridLayout; | |
54 | import org.eclipse.swt.widgets.Button; | |
55 | import org.eclipse.swt.widgets.Composite; | |
56 | import org.eclipse.swt.widgets.Control; | |
eb1bab5b BH |
57 | import org.eclipse.swt.widgets.Label; |
58 | import org.eclipse.swt.widgets.Shell; | |
9bc60be7 AM |
59 | import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator; |
60 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; | |
eb1bab5b BH |
61 | |
62 | /** | |
eb1bab5b BH |
63 | * <p> |
64 | * Dialog box for connection information. | |
65 | * </p> | |
cfdb727a | 66 | * |
dbd4432d | 67 | * @author Bernd Hufmann |
eb1bab5b BH |
68 | */ |
69 | public class NewConnectionDialog extends Dialog implements INewConnectionDialog { | |
70 | ||
65e28a02 MK |
71 | private static final int BUTTONS_NUMBER_OF_COLUMNS = 3; |
72 | private static final int LABEL_WIDTH_CHARS = 4; | |
73 | private static final int CONNECTIONTREE_HEIGHT_CHARS = 10; | |
74 | private static final int CONNECTIONTREE_WIDTH_CHARS = 40; | |
eb1bab5b BH |
75 | // ------------------------------------------------------------------------ |
76 | // Constants | |
77 | // ------------------------------------------------------------------------ | |
b732adaa MS |
78 | private static final String TARGET_NEW_CONNECTION_ICON_FILE = "icons/elcl16/target_add.gif"; //$NON-NLS-1$ |
79 | private static final String PROVIDERS_ICON_FILE = "icons/obj16/providers.gif"; //$NON-NLS-1$ | |
80 | private static final String CONNECTION_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$ | |
81 | ||
65e28a02 MK |
82 | private final class ConnectionTreeLabelProvider extends LabelProvider { |
83 | @Override | |
84 | public String getText(Object element) { | |
85 | if (element instanceof IRemoteConnection) { | |
86 | IRemoteConnection rc = (IRemoteConnection) element; | |
87 | if (rc.getRemoteServices() == RemoteServices.getLocalServices()) { | |
88 | return rc.getName(); | |
89 | } | |
90 | ||
91 | return format("{0} [{1}]", rc.getName(), rc.getAddress()); //$NON-NLS-1$ | |
92 | } else if (element instanceof IRemoteServices) { | |
93 | IRemoteServices rs = (IRemoteServices) element; | |
94 | return rs.getName(); | |
95 | } | |
96 | return Messages.TraceControl_UnknownNode; | |
97 | } | |
98 | ||
99 | @Override | |
100 | public Image getImage(Object element) { | |
101 | if (element instanceof IRemoteConnection) { | |
102 | return Activator.getDefault().loadIcon(CONNECTION_ICON_FILE); | |
103 | } | |
104 | return Activator.getDefault().loadIcon(PROVIDERS_ICON_FILE); | |
105 | } | |
106 | } | |
107 | ||
b732adaa MS |
108 | private static final class ConnectionContentProvider implements ITreeContentProvider { |
109 | private static final Object[] NO_CHILDREN = {}; | |
110 | private static List<IRemoteServices> fProviders; | |
111 | ||
112 | @Override | |
113 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
114 | } | |
115 | ||
116 | @Override | |
117 | public void dispose() { | |
118 | } | |
119 | ||
120 | @Override | |
121 | public Object[] getElements(Object inputElement) { | |
122 | List<Object> children = new ArrayList<>(); | |
123 | children.addAll(RemoteServices.getLocalServices().getConnectionManager().getConnections()); | |
124 | ||
125 | List<IRemoteServices> result = getProviders(); | |
126 | children.addAll(result); | |
127 | return children.toArray(); | |
128 | } | |
129 | ||
130 | private static List<IRemoteServices> getProviders() { | |
131 | if (fProviders == null) { | |
132 | IExtensionRegistry registry = Platform.getExtensionRegistry(); | |
133 | IExtensionPoint extensionPoint = registry.getExtensionPoint("org.eclipse.remote.core", "remoteServices"); //$NON-NLS-1$ //$NON-NLS-2$ | |
134 | List<IRemoteServices> result = new ArrayList<>(); | |
135 | if (extensionPoint != null) { | |
136 | Set<String> handled = new HashSet<>(); | |
137 | handled.add(RemoteServices.getLocalServices().getId()); | |
138 | for (IConfigurationElement ce : extensionPoint.getConfigurationElements()) { | |
139 | String id = ce.getAttribute("id"); //$NON-NLS-1$ | |
140 | if (handled.add(id)) { | |
141 | IRemoteServices service = RemoteServices.getRemoteServices(id); | |
142 | if (service != null) { | |
143 | result.add(service); | |
144 | } | |
145 | } | |
146 | } | |
147 | Collections.sort(result); | |
148 | } | |
149 | fProviders = Collections.unmodifiableList(result); | |
150 | } | |
151 | return fProviders; | |
152 | } | |
153 | ||
154 | @Override | |
155 | public Object[] getChildren(Object parentElement) { | |
156 | if (parentElement instanceof IRemoteServices) { | |
157 | return getConnections((IRemoteServices) parentElement); | |
158 | } | |
159 | return NO_CHILDREN; | |
160 | } | |
161 | ||
162 | private static IRemoteConnection[] getConnections(IRemoteServices parentElement) { | |
163 | List<IRemoteConnection> connectionList = parentElement.getConnectionManager().getConnections(); | |
164 | IRemoteConnection[] result = connectionList.toArray(new IRemoteConnection[connectionList.size()]); | |
165 | Arrays.sort(result); | |
166 | return result; | |
167 | } | |
168 | ||
169 | @Override | |
170 | public Object getParent(Object element) { | |
171 | if (element instanceof IRemoteConnection) { | |
172 | return ((IRemoteConnection) element).getRemoteServices(); | |
173 | } | |
174 | return null; | |
175 | } | |
176 | ||
177 | @Override | |
178 | public boolean hasChildren(Object element) { | |
179 | return getChildren(element).length > 0; | |
180 | } | |
181 | ||
182 | } | |
eb1bab5b BH |
183 | |
184 | // ------------------------------------------------------------------------ | |
185 | // Attributes | |
186 | // ------------------------------------------------------------------------ | |
eb1bab5b BH |
187 | /** |
188 | * The host combo box. | |
189 | */ | |
b732adaa | 190 | private TreeViewer fConnectionTree = null; |
eb1bab5b | 191 | /** |
b732adaa | 192 | * The push button for creating a new connection. |
eb1bab5b | 193 | */ |
b732adaa | 194 | private Button fNewButton = null; |
eb1bab5b | 195 | /** |
b732adaa | 196 | * The push button for editing a connection. |
eb1bab5b | 197 | */ |
b732adaa MS |
198 | private Button fEditButton = null; |
199 | ||
200 | private IRemoteConnection fConnection; | |
201 | ||
eb1bab5b BH |
202 | // ------------------------------------------------------------------------ |
203 | // Constructors | |
204 | // ------------------------------------------------------------------------ | |
cfdb727a AM |
205 | /** |
206 | * Constructor | |
207 | * | |
208 | * @param shell | |
209 | * The shell | |
210 | */ | |
d132bcc7 | 211 | public NewConnectionDialog(Shell shell) { |
eb1bab5b | 212 | super(shell); |
8a396998 | 213 | setShellStyle(SWT.RESIZE | getShellStyle()); |
eb1bab5b BH |
214 | } |
215 | ||
eb1bab5b BH |
216 | // ------------------------------------------------------------------------ |
217 | // Operations | |
218 | // ------------------------------------------------------------------------ | |
11252342 | 219 | |
eb1bab5b BH |
220 | @Override |
221 | protected void configureShell(Shell newShell) { | |
222 | super.configureShell(newShell); | |
223 | newShell.setText(Messages.TraceControl_NewDialogTitle); | |
31a6a4e4 | 224 | newShell.setImage(Activator.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE)); |
eb1bab5b BH |
225 | } |
226 | ||
eb1bab5b | 227 | @Override |
b732adaa MS |
228 | protected Control createContents(Composite parent) { |
229 | Control result = super.createContents(parent); | |
230 | fConnectionTree.setAutoExpandLevel(2); | |
231 | fConnectionTree.setInput(this); | |
232 | ||
233 | List<IRemoteServices> providers = ConnectionContentProvider.getProviders(); | |
234 | if (!providers.isEmpty()) { | |
235 | IRemoteServices provider = providers.get(0); | |
236 | IRemoteConnection[] connections = ConnectionContentProvider.getConnections(provider); | |
237 | if (connections.length > 0) { | |
238 | fConnectionTree.setSelection(new StructuredSelection(connections[0])); | |
239 | } else { | |
240 | fConnectionTree.setSelection(new StructuredSelection(provider)); | |
241 | } | |
242 | } else { | |
243 | onSelectionChanged(); | |
244 | } | |
245 | return result; | |
246 | } | |
cfdb727a | 247 | |
b732adaa MS |
248 | @Override |
249 | protected Control createDialogArea(Composite parent) { | |
eb1bab5b | 250 | // Main dialog panel |
b732adaa | 251 | GridData gd; |
046b6849 | 252 | Composite dialogComposite = new Composite(parent, SWT.NONE); |
eb1bab5b | 253 | GridLayout layout = new GridLayout(1, true); |
046b6849 BH |
254 | dialogComposite.setLayout(layout); |
255 | dialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
eb1bab5b | 256 | |
b732adaa MS |
257 | Label label = new Label(dialogComposite, SWT.NONE); |
258 | label.setText(Messages.TraceControl_NewNodeExistingConnectionGroupName); | |
65e28a02 MK |
259 | gd = new GridData(); |
260 | label.setLayoutData(gd ); | |
261 | gd.widthHint = label.computeSize(-1, -1).x + convertWidthInCharsToPixels(LABEL_WIDTH_CHARS); | |
eb1bab5b | 262 | // Existing connections group |
b732adaa | 263 | fConnectionTree = new TreeViewer(dialogComposite); |
65e28a02 MK |
264 | gd = new GridData(SWT.FILL, SWT.FILL, true, true); |
265 | fConnectionTree.getTree().setLayoutData(gd); | |
266 | gd.widthHint = convertWidthInCharsToPixels(CONNECTIONTREE_WIDTH_CHARS); | |
267 | gd.heightHint = convertHeightInCharsToPixels(CONNECTIONTREE_HEIGHT_CHARS); | |
268 | fConnectionTree.setLabelProvider(new ConnectionTreeLabelProvider()); | |
b732adaa MS |
269 | fConnectionTree.setContentProvider(new ConnectionContentProvider()); |
270 | fConnectionTree.addSelectionChangedListener(new ISelectionChangedListener() { | |
eb1bab5b | 271 | @Override |
b732adaa MS |
272 | public void selectionChanged(SelectionChangedEvent event) { |
273 | onSelectionChanged(); | |
274 | } | |
275 | }); | |
276 | fConnectionTree.addDoubleClickListener(new IDoubleClickListener() { | |
277 | @Override | |
278 | public void doubleClick(DoubleClickEvent event) { | |
279 | okPressed(); | |
eb1bab5b BH |
280 | } |
281 | }); | |
282 | ||
b732adaa | 283 | Composite buttons = new Composite(dialogComposite, SWT.NONE); |
65e28a02 MK |
284 | layout = new GridLayout(BUTTONS_NUMBER_OF_COLUMNS, true); |
285 | layout.marginHeight = 0; | |
286 | layout.marginWidth = 0; | |
b732adaa MS |
287 | buttons.setLayout(layout); |
288 | buttons.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false)); | |
289 | ||
290 | new Label(buttons, SWT.NONE); | |
291 | ||
292 | fEditButton = new Button(buttons, SWT.PUSH); | |
293 | fEditButton.setText(Messages.TraceControl_NewNodeEditButtonName); | |
294 | setButtonLayoutData(fEditButton); | |
295 | fEditButton.addSelectionListener(new SelectionAdapter() { | |
eb1bab5b BH |
296 | @Override |
297 | public void widgetSelected(SelectionEvent e) { | |
b732adaa | 298 | onEditConnection(); |
eb1bab5b | 299 | } |
b732adaa | 300 | }); |
eb1bab5b | 301 | |
b732adaa MS |
302 | fNewButton = new Button(buttons, SWT.PUSH); |
303 | fNewButton.setText(Messages.TraceControl_NewNodeCreateButtonText); | |
304 | setButtonLayoutData(fNewButton); | |
305 | fNewButton.addSelectionListener(new SelectionAdapter() { | |
eb1bab5b | 306 | @Override |
b732adaa MS |
307 | public void widgetSelected(SelectionEvent e) { |
308 | onNewConnection(); | |
eb1bab5b BH |
309 | } |
310 | }); | |
cfdb727a | 311 | |
b732adaa MS |
312 | return dialogComposite; |
313 | } | |
00de7b32 | 314 | |
b732adaa MS |
315 | private void onSelectionChanged() { |
316 | setConnection(); | |
317 | getButton(OK).setEnabled(fConnection != null); | |
318 | fEditButton.setEnabled(canEdit(fConnection)); | |
319 | fNewButton.setEnabled(getServiceForCreation() != null); | |
320 | } | |
cfdb727a | 321 | |
b732adaa MS |
322 | private IRemoteServices getServiceForCreation() { |
323 | Object o = ((IStructuredSelection) fConnectionTree.getSelection()).getFirstElement(); | |
324 | IRemoteServices result = null; | |
325 | if (o instanceof IRemoteServices) { | |
326 | result = (IRemoteServices) o; | |
327 | } else if (o instanceof IRemoteConnection) { | |
328 | result = ((IRemoteConnection) o).getRemoteServices(); | |
329 | } else { | |
330 | return null; | |
331 | } | |
00de7b32 | 332 | |
b732adaa MS |
333 | if ((result.getCapabilities() & IRemoteServices.CAPABILITY_ADD_CONNECTIONS) == 0) { |
334 | return null; | |
335 | } | |
cfdb727a | 336 | |
b732adaa MS |
337 | return result; |
338 | } | |
339 | ||
340 | private static boolean canEdit(IRemoteConnection conn) { | |
341 | if (conn == null) { | |
342 | return false; | |
343 | } | |
344 | IRemoteServices rs = conn.getRemoteServices(); | |
345 | return (rs.getCapabilities() & IRemoteServices.CAPABILITY_EDIT_CONNECTIONS) != 0; | |
346 | } | |
347 | ||
b732adaa MS |
348 | private void onNewConnection() { |
349 | IRemoteServices rs = getServiceForCreation(); | |
350 | if (rs != null) { | |
351 | IRemoteUIServices uiService = RemoteUIServices.getRemoteUIServices(rs); | |
352 | if (uiService != null) { | |
353 | IRemoteUIConnectionWizard wiz = uiService.getUIConnectionManager().getConnectionWizard(getShell()); | |
354 | if (wiz != null) { | |
355 | IRemoteConnectionWorkingCopy wc = wiz.open(); | |
356 | if (wc != null) { | |
357 | IRemoteConnection conn = wc.save(); | |
358 | if (conn != null) { | |
359 | fConnectionTree.refresh(); | |
360 | fConnectionTree.setSelection(new StructuredSelection(conn), true); | |
361 | } | |
362 | } | |
363 | } | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | private void onEditConnection() { | |
369 | setConnection(); | |
370 | if (fConnection != null) { | |
371 | IRemoteUIServices ui = RemoteUIServices.getRemoteUIServices(fConnection.getRemoteServices()); | |
372 | if (ui != null) { | |
373 | IRemoteUIConnectionManager connManager = ui.getUIConnectionManager(); | |
374 | if (connManager != null) { | |
375 | IRemoteUIConnectionWizard wiz = connManager.getConnectionWizard(getShell()); | |
376 | wiz.setConnection(fConnection.getWorkingCopy()); | |
377 | IRemoteConnectionWorkingCopy result = wiz.open(); | |
378 | if (result != null) { | |
379 | result.save(); | |
380 | fConnectionTree.refresh(); | |
381 | } | |
382 | } | |
383 | } | |
384 | } | |
eb1bab5b BH |
385 | } |
386 | ||
eb1bab5b BH |
387 | @Override |
388 | protected void createButtonsForButtonBar(Composite parent) { | |
79c3db85 | 389 | createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$ |
eb1bab5b BH |
390 | createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ |
391 | } | |
392 | ||
eb1bab5b BH |
393 | @Override |
394 | protected void okPressed() { | |
b732adaa MS |
395 | setConnection(); |
396 | if (fConnection != null) { | |
397 | super.okPressed(); | |
eb1bab5b | 398 | } |
b732adaa MS |
399 | } |
400 | ||
401 | private void setConnection() { | |
402 | Object o = ((IStructuredSelection) fConnectionTree.getSelection()).getFirstElement(); | |
65e28a02 | 403 | fConnection = o instanceof IRemoteConnection ? (IRemoteConnection) o : null; |
b732adaa MS |
404 | } |
405 | ||
406 | @Override | |
407 | public IRemoteConnection getConnection() { | |
408 | return fConnection; | |
eb1bab5b BH |
409 | } |
410 | } |