1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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
.Collections
;
20 import java
.util
.HashSet
;
21 import java
.util
.List
;
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
;
28 import org
.eclipse
.jface
.dialogs
.Dialog
;
29 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
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
;
48 import org
.eclipse
.swt
.SWT
;
49 import org
.eclipse
.swt
.events
.SelectionAdapter
;
50 import org
.eclipse
.swt
.events
.SelectionEvent
;
51 import org
.eclipse
.swt
.graphics
.Image
;
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
;
57 import org
.eclipse
.swt
.widgets
.Label
;
58 import org
.eclipse
.swt
.widgets
.Shell
;
59 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
60 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
64 * Dialog box for connection information.
67 * @author Bernd Hufmann
69 public class NewConnectionDialog
extends Dialog
implements INewConnectionDialog
{
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;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
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$
82 private final class ConnectionTreeLabelProvider
extends LabelProvider
{
84 public String
getText(Object element
) {
85 if (element
instanceof IRemoteConnection
) {
86 IRemoteConnection rc
= (IRemoteConnection
) element
;
87 if (rc
.getRemoteServices() == RemoteServices
.getLocalServices()) {
91 return format("{0} [{1}]", rc
.getName(), rc
.getAddress()); //$NON-NLS-1$
92 } else if (element
instanceof IRemoteServices
) {
93 IRemoteServices rs
= (IRemoteServices
) element
;
96 return Messages
.TraceControl_UnknownNode
;
100 public Image
getImage(Object element
) {
101 if (element
instanceof IRemoteConnection
) {
102 return Activator
.getDefault().loadIcon(CONNECTION_ICON_FILE
);
104 return Activator
.getDefault().loadIcon(PROVIDERS_ICON_FILE
);
108 private static final class ConnectionContentProvider
implements ITreeContentProvider
{
109 private static final Object
[] NO_CHILDREN
= {};
110 private static List
<IRemoteServices
> fProviders
;
113 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
117 public void dispose() {
121 public Object
[] getElements(Object inputElement
) {
122 List
<Object
> children
= new ArrayList
<>();
123 children
.addAll(RemoteServices
.getLocalServices().getConnectionManager().getConnections());
125 List
<IRemoteServices
> result
= getProviders();
126 children
.addAll(result
);
127 return children
.toArray();
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) {
147 Collections
.sort(result
);
149 fProviders
= Collections
.unmodifiableList(result
);
155 public Object
[] getChildren(Object parentElement
) {
156 if (parentElement
instanceof IRemoteServices
) {
157 return getConnections((IRemoteServices
) parentElement
);
162 private static IRemoteConnection
[] getConnections(IRemoteServices parentElement
) {
163 List
<IRemoteConnection
> connectionList
= parentElement
.getConnectionManager().getConnections();
164 IRemoteConnection
[] result
= connectionList
.toArray(new IRemoteConnection
[connectionList
.size()]);
170 public Object
getParent(Object element
) {
171 if (element
instanceof IRemoteConnection
) {
172 return ((IRemoteConnection
) element
).getRemoteServices();
178 public boolean hasChildren(Object element
) {
179 return getChildren(element
).length
> 0;
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
188 * The host combo box.
190 private TreeViewer fConnectionTree
= null;
192 * The push button for creating a new connection.
194 private Button fNewButton
= null;
196 * The push button for editing a connection.
198 private Button fEditButton
= null;
200 private IRemoteConnection fConnection
;
202 // ------------------------------------------------------------------------
204 // ------------------------------------------------------------------------
211 public NewConnectionDialog(Shell shell
) {
213 setShellStyle(SWT
.RESIZE
| getShellStyle());
216 // ------------------------------------------------------------------------
218 // ------------------------------------------------------------------------
221 protected void configureShell(Shell newShell
) {
222 super.configureShell(newShell
);
223 newShell
.setText(Messages
.TraceControl_NewDialogTitle
);
224 newShell
.setImage(Activator
.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE
));
228 protected Control
createContents(Composite parent
) {
229 Control result
= super.createContents(parent
);
230 fConnectionTree
.setAutoExpandLevel(2);
231 fConnectionTree
.setInput(this);
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]));
240 fConnectionTree
.setSelection(new StructuredSelection(provider
));
243 onSelectionChanged();
249 protected Control
createDialogArea(Composite parent
) {
252 Composite dialogComposite
= new Composite(parent
, SWT
.NONE
);
253 GridLayout layout
= new GridLayout(1, true);
254 dialogComposite
.setLayout(layout
);
255 dialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
257 Label label
= new Label(dialogComposite
, SWT
.NONE
);
258 label
.setText(Messages
.TraceControl_NewNodeExistingConnectionGroupName
);
260 label
.setLayoutData(gd
);
261 gd
.widthHint
= label
.computeSize(-1, -1).x
+ convertWidthInCharsToPixels(LABEL_WIDTH_CHARS
);
262 // Existing connections group
263 fConnectionTree
= new TreeViewer(dialogComposite
);
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());
269 fConnectionTree
.setContentProvider(new ConnectionContentProvider());
270 fConnectionTree
.addSelectionChangedListener(new ISelectionChangedListener() {
272 public void selectionChanged(SelectionChangedEvent event
) {
273 onSelectionChanged();
276 fConnectionTree
.addDoubleClickListener(new IDoubleClickListener() {
278 public void doubleClick(DoubleClickEvent event
) {
283 Composite buttons
= new Composite(dialogComposite
, SWT
.NONE
);
284 layout
= new GridLayout(BUTTONS_NUMBER_OF_COLUMNS
, true);
285 layout
.marginHeight
= 0;
286 layout
.marginWidth
= 0;
287 buttons
.setLayout(layout
);
288 buttons
.setLayoutData(new GridData(SWT
.END
, SWT
.CENTER
, false, false));
290 new Label(buttons
, SWT
.NONE
);
292 fEditButton
= new Button(buttons
, SWT
.PUSH
);
293 fEditButton
.setText(Messages
.TraceControl_NewNodeEditButtonName
);
294 setButtonLayoutData(fEditButton
);
295 fEditButton
.addSelectionListener(new SelectionAdapter() {
297 public void widgetSelected(SelectionEvent e
) {
302 fNewButton
= new Button(buttons
, SWT
.PUSH
);
303 fNewButton
.setText(Messages
.TraceControl_NewNodeCreateButtonText
);
304 setButtonLayoutData(fNewButton
);
305 fNewButton
.addSelectionListener(new SelectionAdapter() {
307 public void widgetSelected(SelectionEvent e
) {
312 return dialogComposite
;
315 private void onSelectionChanged() {
317 getButton(OK
).setEnabled(fConnection
!= null);
318 fEditButton
.setEnabled(canEdit(fConnection
));
319 fNewButton
.setEnabled(getServiceForCreation() != null);
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();
333 if ((result
.getCapabilities() & IRemoteServices
.CAPABILITY_ADD_CONNECTIONS
) == 0) {
340 private static boolean canEdit(IRemoteConnection conn
) {
344 IRemoteServices rs
= conn
.getRemoteServices();
345 return (rs
.getCapabilities() & IRemoteServices
.CAPABILITY_EDIT_CONNECTIONS
) != 0;
348 private void onNewConnection() {
349 IRemoteServices rs
= getServiceForCreation();
351 IRemoteUIServices uiService
= RemoteUIServices
.getRemoteUIServices(rs
);
352 if (uiService
!= null) {
353 IRemoteUIConnectionWizard wiz
= uiService
.getUIConnectionManager().getConnectionWizard(getShell());
355 IRemoteConnectionWorkingCopy wc
= wiz
.open();
357 IRemoteConnection conn
= wc
.save();
359 fConnectionTree
.refresh();
360 fConnectionTree
.setSelection(new StructuredSelection(conn
), true);
368 private void onEditConnection() {
370 if (fConnection
!= null) {
371 IRemoteUIServices ui
= RemoteUIServices
.getRemoteUIServices(fConnection
.getRemoteServices());
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) {
380 fConnectionTree
.refresh();
388 protected void createButtonsForButtonBar(Composite parent
) {
389 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
390 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
394 protected void okPressed() {
396 if (fConnection
!= null) {
401 private void setConnection() {
402 Object o
= ((IStructuredSelection
) fConnectionTree
.getSelection()).getFirstElement();
403 fConnection
= o
instanceof IRemoteConnection ?
(IRemoteConnection
) o
: null;
407 public IRemoteConnection
getConnection() {