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