1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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
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
;
15 import static java
.text
.MessageFormat
.format
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.Comparator
;
20 import java
.util
.List
;
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
;
58 * Dialog box for connection information.
61 * @author Bernd Hufmann
63 public class NewConnectionDialog
extends Dialog
implements INewConnectionDialog
{
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 // ------------------------------------------------------------------------
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$
76 private final class ConnectionTreeLabelProvider
extends LabelProvider
{
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
;
86 return Messages
.TraceControl_UnknownNode
;
90 public Image
getImage(Object element
) {
91 if (element
instanceof IRemoteConnection
) {
92 return Activator
.getDefault().loadIcon(CONNECTION_ICON_FILE
);
94 return Activator
.getDefault().loadIcon(PROVIDERS_ICON_FILE
);
98 private static final class ConnectionContentProvider
implements ITreeContentProvider
{
99 private static final Object
[] NO_CHILDREN
= {};
102 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
106 public void dispose() {
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());
116 return children
.toArray();
120 public Object
[] getChildren(Object parentElement
) {
121 if (parentElement
instanceof IRemoteConnectionType
) {
122 return getConnections((IRemoteConnectionType
) parentElement
);
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
>() {
132 public int compare(IRemoteConnection o1
, IRemoteConnection o2
) {
133 return getConnectionLabel(o1
).compareTo(getConnectionLabel(o2
));
140 public Object
getParent(Object element
) {
141 if (element
instanceof IRemoteConnection
) {
142 return ((IRemoteConnection
) element
).getConnectionType();
148 public boolean hasChildren(Object element
) {
149 return getChildren(element
).length
> 0;
154 // ------------------------------------------------------------------------
156 // ------------------------------------------------------------------------
158 * The host combo box.
160 private TreeViewer fConnectionTree
= null;
162 * The push button for creating a new connection.
164 private Button fNewButton
= null;
166 * The push button for editing a connection.
168 private Button fEditButton
= null;
170 private IRemoteConnection fConnection
;
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
181 public NewConnectionDialog(Shell shell
) {
183 setShellStyle(SWT
.RESIZE
| getShellStyle());
186 // ------------------------------------------------------------------------
188 // ------------------------------------------------------------------------
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
));
198 protected Control
createContents(Composite parent
) {
199 Control result
= super.createContents(parent
);
200 fConnectionTree
.setAutoExpandLevel(2);
201 fConnectionTree
.setInput(this);
203 IRemoteServicesManager manager
= Activator
.getService(IRemoteServicesManager
.class);
204 if (manager
== null) {
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]));
214 fConnectionTree
.setSelection(new StructuredSelection(provider
));
217 onSelectionChanged();
223 protected Control
createDialogArea(Composite parent
) {
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
));
231 Label label
= new Label(dialogComposite
, SWT
.NONE
);
232 label
.setText(Messages
.TraceControl_NewNodeExistingConnectionGroupName
);
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() {
246 public void selectionChanged(SelectionChangedEvent event
) {
247 onSelectionChanged();
250 fConnectionTree
.addDoubleClickListener(new IDoubleClickListener() {
252 public void doubleClick(DoubleClickEvent event
) {
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));
264 new Label(buttons
, SWT
.NONE
);
266 fEditButton
= new Button(buttons
, SWT
.PUSH
);
267 fEditButton
.setText(Messages
.TraceControl_NewNodeEditButtonName
);
268 setButtonLayoutData(fEditButton
);
269 fEditButton
.addSelectionListener(new SelectionAdapter() {
271 public void widgetSelected(SelectionEvent e
) {
276 fNewButton
= new Button(buttons
, SWT
.PUSH
);
277 fNewButton
.setText(Messages
.TraceControl_NewNodeCreateButtonText
);
278 setButtonLayoutData(fNewButton
);
279 fNewButton
.addSelectionListener(new SelectionAdapter() {
281 public void widgetSelected(SelectionEvent e
) {
286 return dialogComposite
;
289 private void onSelectionChanged() {
291 getButton(OK
).setEnabled(fConnection
!= null);
292 fEditButton
.setEnabled(canEdit(fConnection
));
293 fNewButton
.setEnabled(getServiceForCreation() != null);
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();
307 if ((result
.getCapabilities() & IRemoteConnectionType
.CAPABILITY_ADD_CONNECTIONS
) == 0) {
314 private static boolean canEdit(IRemoteConnection conn
) {
318 IRemoteConnectionType rs
= conn
.getConnectionType();
319 return (rs
.getCapabilities() & IRemoteConnectionType
.CAPABILITY_EDIT_CONNECTIONS
) != 0;
322 private void onNewConnection() {
323 IRemoteConnectionType rs
= getServiceForCreation();
325 IRemoteUIConnectionService uiService
= rs
.getService(IRemoteUIConnectionService
.class);
326 if (uiService
!= null) {
327 IRemoteUIConnectionWizard wiz
= uiService
.getConnectionWizard(getShell());
329 IRemoteConnectionWorkingCopy wc
= wiz
.open();
331 IRemoteConnection conn
= null;
334 } catch (RemoteConnectionException e
) {
335 Activator
.getDefault().logWarning("Connection configuration could not be saved for " + fConnection
.getName() , e
); //$NON-NLS-1$
338 fConnectionTree
.refresh();
339 fConnectionTree
.setSelection(new StructuredSelection(conn
), true);
347 private void onEditConnection() {
349 if (fConnection
!= null) {
350 IRemoteUIConnectionService ui
= fConnection
.getConnectionType().getService(IRemoteUIConnectionService
.class);
352 IRemoteUIConnectionWizard wiz
= ui
.getConnectionWizard(getShell());
353 wiz
.setConnection(fConnection
.getWorkingCopy());
354 IRemoteConnectionWorkingCopy result
= wiz
.open();
355 if (result
!= null) {
358 } catch (RemoteConnectionException e
) {
359 Activator
.getDefault().logWarning("Connection configuration could not be saved for " + fConnection
.getName() , e
); //$NON-NLS-1$
361 fConnectionTree
.refresh();
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$
374 protected void okPressed() {
376 if (fConnection
!= null) {
381 private void setConnection() {
382 Object o
= ((IStructuredSelection
) fConnectionTree
.getSelection()).getFirstElement();
383 fConnection
= o
instanceof IRemoteConnection ?
(IRemoteConnection
) o
: null;
387 public IRemoteConnection
getConnection() {
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$
397 return label
.toString();