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